1. 團隊課程設計博客鏈接
http://www.cnblogs.com/Min21/p/7064093.html
2.個人負責模塊或任務說明
負責person類的編寫,建立person對象,
完成Menu中增刪改查的功能。
3.自己的代碼提交記錄截圖
4..自己負責模塊或任務詳細說明
1、建立對象建立可序列化對象person,並構建函數。
主要代碼:
import java.io.Serializable;
public class Person implements Serializable{
private String num;
private String name;
private String dor;
private String address;
private String sex;
private String date;
private String pol;
private String phone;
public Person(){}
public Person(String num,String name,String dor,String address,String sex,String date,String pol,String phone ){
this.num=num;
this.name=name;
this.dor=dor;
this.address=address;
this.sex=sex;
this.date=date;
this.pol=pol;
this.phone=phone;
}
public void setNum(String num){
this.num=num;
}
public String getNum(){
return num;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setDor(String dor){
this.dor=dor;
}
public String getDor(){
return dor;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return address;
}
public void setSex(String sex){
this.sex=sex;
}
public String getSex(){
return sex;
}
public void setDate(String date){
this.date=date;
}
public String getDate(){
return date;
}
public void setPol(String pol){
this.pol=pol;
}
public String getPol(){
return pol;
}
public void setPhone(String phone){
this.phone=phone;
}
public String getPhone(){
return phone;
}
}
2.增加學生信息功能
but1.addActionListener(new ActionListener() { // 增加,內部類//進行某項操作時觸發功能
public void actionPerformed(ActionEvent e) {//用於接收操作事件的偵聽器接口
if (e.getSource() == but1) {
but3.setEnabled(false);//使but3這個按鈕變灰不可點擊了
String number1 = number.getText();
if (number1.length() == 12) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));//讀回對象
has = (Hashtable) in.readObject(); in.close();
} catch (Exception e1) {
}
} else {
JOptionPane.showMessageDialog(null, "請輸入12位數字的學號");//提示框顯示
}
//
if (number1.length() == 12) {
if (has.containsKey(number1)) { JOptionPane.showMessageDialog(null, "該生信息已存在,請到修改頁面修改!");
} else {
String name1 = name.getText();
String dor1 = dor.getText();
String address1 = address.getText();
String sex1 = sex.getText();
String date1 = date.getText();
String pol1 = pol.getText();
String phone1 = phone.getText();
Person per = null;
per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
has.put(number1, per);// ???
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(has);
out.close();
JOptionPane.showMessageDialog(null, "添加成功!");
} catch (Exception e1) {}
}
}
}
}
3.)修改功能:為了防止誤修改,首先要查看,才能修改,查看后直接改輸入欄中的數據,點擊修改,既修改成功。
but3.addActionListener(new ActionListener() { // 修改
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but3) {
but3.setEnabled(false);
String number1 = number.getText();
String name1 = name.getText();
String dor1 = dor.getText();
String address1 = address.getText();
String sex1 = sex.getText();
String date1 = date.getText();
String pol1 = pol.getText();
String phone1 = phone.getText();
Person per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
has.put(number1, per);
JOptionPane.showMessageDialog(null, "修改成功");
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(has);
out.close();
} catch (Exception e1) {
}
}
}
});
4.查看功能:首先輸入要查看的學號,點查看,信息將會顯示在輸入欄中。如果學號不存在,下面會有提示。
but4.addActionListener(new ActionListener() { // 查看
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but4) {
but3.setEnabled(false);
String number1 = number.getText();
if (number1.length() == 12) {
if (has.containsKey(number1)) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
has = (Hashtable) in.readObject();
in.close();
} catch (Exception e1) {
}
Person per = (Person) has.get(number1);
name.setText(per.getName());
dor.setText(per.getDor());
address.setText(per.getAddress());
sex.setText(per.getSex());
date.setText(per.getDate());
pol.setText(per.getPol());
phone.setText(per.getPhone());
but3.setEnabled(true);
} else {
JOptionPane.showMessageDialog(null, "學號不存在");
}
} else {
JOptionPane.showMessageDialog(null, "請輸入12位數字的學號");
}
}
}
});
5.(5)刪除功能:先輸入要刪除的學號,點刪除,該學生的信息將被移除,在查看該學號,將不存在。
but2.addActionListener(new ActionListener() { // 刪除
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but2) {
but3.setEnabled(false);
String number1 = number.getText();
if (number1.length() == 12) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
has = (Hashtable) in.readObject();
in.close();
} catch (Exception e1) {
}
} else {
JOptionPane.showMessageDialog(null, "請輸入12位數字的學號");
}
if (has.containsKey(number1)) {
has.remove(number1);
ObjectOutputStream out = null;
JOptionPane.showMessageDialog(null, "刪除成功");
try {
out = new ObjectOutputStream(new FileOutputStream(file));
//out.writeObject(has);
out.close();
} catch (IOException ex) {
Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);//記錄當前類可能發生的異常
}
} else {
JOptionPane.showMessageDialog(null, "學號不存在");
}
}
}
});
5.五.課程設計感想
我們打算做文本io時想實現內存與文本的交互一直無法成功,后來百度查了一下是需要將數據可序列化才能行,而且還要用ObjectOutputStream和ObjectInputStream來傳遞數據,而它們對象序列話是在它們中實現的,這些我們都沒學過。還好書上有,網上也有它們的解析,於是我們就一點一點的嘗試,最終實現內存與文本的交互並把數據存到文本里。