import java.awt.*;
import java.awt.event.*;
import java.io.*;
IOOperation ioo;
Student stu;
Student student[] = new Student[100];
public StudentInformation() {
/**
* set mainframe
*/
mainFrame
= new Frame("Student Information");
}
});
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu1.addSeparator();
menu1.add(item4);
menu2.add(item5);
mb.add(menu1);
mb.add(menu2);
mainFrame.setMenuBar(mb);
stuInfo.
setFont(new Font("serif",
Font.
PLAIN,
18));
mainFrame.add(stuInfo);
mainFrame.setSize(400, 250);
mainFrame.setLocation(200, 100);
mainFrame.setVisible(true);
/**
* set inputFrame which is used to record student information
*/
inputFrame
= new Frame();
inputFrame.setVisible(false);
}
});
String lbname
[] = {"Code:",
"Name:",
"Sex:",
"BirthPlace:",
"Class:", "Chinese:", "Math:", "English:", "TotalScore:"};
String btnname
[] = {"Save",
"Delete",
" Exit "};
for(int i=0; i<9; i++) {
lb
[i
] = new Label(lbname
[i
]);
p1.add(lb[i]);
p1.add(tf[i]);
}
for(int i=0; i<3; i++) {
btn
[i
] = new Button(btnname
[i
]);
btn[i].addActionListener(this);
p2.add(btn[i]);
}
btn[2].setActionCommand("input");
inputFrame.pack();
inputFrame.setLocationRelativeTo(mainFrame);
/**
* set searchFrame which is used to search student information
*/
searchFrame
= new Frame("Search student");
searchFrame.setVisible(false);
}
});
String lbname2
[] = {"Code:",
"Name:"};
String btnname2
[] = {"Search",
" Exit "};
for(int i=0; i<2; i++) {
lb2
[i
] = new Label(lbname2
[i
]);
p3.add(lb2[i]);
p3.add(tf2[i]);
}
for(int i=0; i<2; i++) {
btn2
[i
] = new Button(btnname2
[i
]);
btn2[i].addActionListener(this);
p4.add(btn2[i]);
}
btn2[1].setActionCommand("search");
searchFrame.pack();
searchFrame.setLocationRelativeTo(mainFrame);
/**
* IO operation object
*/
ioo = new IOOperation();
student = ioo.getAllStudent();
}
/**
* MenuItem action
*/
if (e.
getSource() instanceof MenuItem) {
if (mi.getLabel().equals("Record")) {
inputFrame.setTitle("Record");
for(int i=0; i<9; i++)
tf[i].setText("");
p2.remove(btn[1]);
btn[0].setActionCommand("input");
inputFrame.setVisible(true);
}
else if (mi.getLabel().equals("Search")) {
searchFrame.setVisible(true);
}
else if (mi.getLabel().equals("Modify")) {
inputFrame.setTitle("Modify");
if(stu != null) {
tf[0].setText(stu.getCode());
tf[1].setText(stu.getName());
tf[2].setText(stu.getSex());
tf[3].setText(stu.getBirthPlace());
tf[4].setText(stu.getStuClass());
tf[5].setText(stu.getChinese() + "");
tf[6].setText(stu.getMath() + "");
tf[7].setText(stu.getEnglish() + "");
tf[8].setText(stu.getTotalScore() + "");
}
p2.remove(btn[2]);
p2.add(btn[1]);
p2.add(btn[2]);
btn[0].setActionCommand("modify");
inputFrame.setVisible(true);
}
else if (mi.getLabel().equals("Exit"))
else if (mi.getLabel().equals("About")) {
final Dialog progInfo
= new Dialog(mainFrame,
"ProgInfo",
true);
progInfo.dispose();
}
});
Label l
= new Label("Student Information System");
progInfo.add(l);
progInfo.setSize(200, 80);
progInfo.setLocationRelativeTo(mainFrame);
progInfo.setVisible(true);
}
}
/**
* Button action
*/
else {
if(btn.getLabel().equals("Save")) {
if(!tf[0].getText().equals("") &&
!tf[1].getText().equals("") &&
!tf[2].getText().equals("") &&
!tf[3].getText().equals("") &&
!tf[4].getText().equals("") &&
!tf[5].getText().equals("") &&
!tf[6].getText().equals("") &&
!tf[7].getText().equals("") &&
!tf[8].getText().equals("")) {
Student s = new Student(tf[0].getText(), tf[1].getText(),
tf[2].getText(), tf[3].getText(), tf[4].getText(),
Integer.
parseInt(tf
[8].
getText()));
if(btn.getActionCommand().equals("input")) {
for(int i=0; i<student.length; i++) {
if(student[i] == null) {
student[i] = s;
break;
}
}
ioo.write(student);
}
else {
for(int i=0; i<student.length; i++) {
if(student[i].equals(stu)) {
student[i] = s;
break;
}
}
ioo.write(student);
}
}
inputFrame.setVisible(false);
stuInfo.setText("");
}
else if(btn.getLabel().equals("Delete")) {
int index = 200;
if(stu != null) {
for(int i=0; i<student.length; i++) {
if(student[i]!=null && student[i].equals(stu)) {
index = i;
if(i != student.length-1)
student[i] = student[i+1];
else
student[i] = null;
}
if(i==index && student[i+1]==null)
break;
else if(i>index && i<student.length-1) {
student[i] = student[i+1];
if(i == student.length - 1)
student[i] = null;
}
}
ioo.write(student);
}
stu = null;
inputFrame.setVisible(false);
stuInfo.setText("");
/*for(int i=0; i<student.length; i++) {
if(student[i]!=null)
System.out.println(i + " " + student[i].getCode());
}*/
}
else if(btn.getLabel().equals("Search")) {
stu = null;
if(!tf2[0].getText().equals("") ||
!tf2[1].getText().equals("")) {
if(!tf2[0].getText().equals("")) {
condition = tf2[0].getText();
}
else
condition = tf2[1].getText();
for(int i=0; i<student.length; i++) {
if (student[i] != null) {
if (student[i].getCode().equals(condition) ||
student[i].getName().equals(condition)) {
stu = student[i];
break;
}
}
}
}
if(stu != null) {
stuInfo.setText("Code: " + stu.getCode() + "\n" +
"Name: " + stu.getName() + "\n" +
"Sex: " + stu.getSex() + "\n" +
"BirthPlace: " + stu.getBirthPlace() + "\n" +
"Class: " + stu.getStuClass() + "\n" +
"Chinese: " + stu.getChinese() + "\n" +
"Math: " + stu.getMath() + "\n" +
"English: " + stu.getEnglish() + "\n" +
"TotalScore: " + stu.getTotalScore());
}
searchFrame.setVisible(false);
}
else if(btn.getLabel().equals(" Exit "))
if(btn.getActionCommand().equals("input"))
inputFrame.setVisible(false);
else
searchFrame.setVisible(false);
}
}
public static void main
(String[] args
) {
new StudentInformation();
}
}
/**
* class which is used to store student information
*/
private int Chinese;
private int English;
private int totalScore;
String stuClass,
int chinese,
int math,
int english,
int totalScore
) {
super();
this.code = code;
this.name = name;
this.sex = sex;
this.birthPlace = birthPlace;
this.stuClass = stuClass;
Chinese = chinese;
English = english;
this.totalScore = totalScore;
}
public String getBirthPlace
() {
return birthPlace;
}
public int getChinese() {
return Chinese;
}
return code;
}
public int getEnglish() {
return English;
}
public int getMath() {
}
return name;
}
return sex;
}
return stuClass;
}
public int getTotalScore() {
return totalScore;
}
public boolean equals
(Object obj
) {
if(obj != null && (obj instanceof Student))
if(this.getCode().equals(((Student)obj).getCode()) &&
this.getName().equals(((Student)obj).getName()) &&
this.getSex().equals(((Student)obj).getSex()) &&
this.getBirthPlace().equals(((Student)obj).getBirthPlace()) &&
this.getStuClass().equals(((Student)obj).getStuClass()) &&
this.getChinese() == ((Student)obj).getChinese() &&
this.getMath() == ((Student)obj).getMath() &&
this.getEnglish() == ((Student)obj).getEnglish() &&
this.getTotalScore() == ((Student)obj).getTotalScore())
return true;
return false;
}
}
/**
* class which is used to do I/O operation
*/
class IOOperation {
private File file
= new File("C:\\stuinfo.txt");
public IOOperation() {
try {
if(!file.exists())
file.createNewFile();
e.printStackTrace();
}
}
/**
* write to file
*/
public void write(Student[] s) {
try {
objOut.writeObject(s);
objOut.close();
fos.close();
e.printStackTrace();
}
}
/**
* read all student information from file
*/
public Student[] getAllStudent() {
Student ss[] = new Student[100];
try {
if (file.length() > 0) {
ss = (Student[]) ois.readObject();
ois.close();
fis.close();
}
e.printStackTrace();
}
return ss;
}
}