| mrynit |
Apr 24th, 2007 1:00 AM |
review my code
This is an school asignment I finished. The program reads a file of student info if it exists. The user enters student info. A dialog box apreas if the Student's name already exists in the file/array list. User can then save the new student info or not. There is no need for error checking in this assignment. On existing the ArrayList of stduents is writen to file.
I am looking from some suggestions, commetns, complaints on how to make it better, more effient, more human readable, easier to manage, etc.
I have yet to add comments to the enteir program.
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class StudentDatabase extends JFrame
{
// declare the name of the text file that stores the student datatbase
private final File STUDIO_FILE = new File("studio.txt");
// create an IO object for the database file
private StudentIO studio = new StudentIO(STUDIO_FILE);
// declare the GUI text fields
private JTextField nameField = new JTextField(20);
private JTextField addressField = new JTextField(20);
private JTextField cityField = new JTextField(20);
private JTextField stateField = new JTextField(20);
private JTextField majorField = new JTextField(20);
// default Student Database constructor
public StudentDatabase()
{
setLayout(new FlowLayout());
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(6, 1, 0, 5));
p1.add(new JLabel("Name"));
p1.add(new JLabel("Address"));
p1.add(new JLabel("City"));
p1.add(new JLabel("State"));
p1.add(new JLabel("Major"));
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
clearAllFields();
}
});
p1.add(clearButton);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(6, 1, 0, 5));
p2.add(nameField);
p2.add(addressField);
p2.add(cityField);
p2.add(stateField);
p2.add(majorField);
JButton enterButton = new JButton("Enter");
enterButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Student tempStudent = new Student(
nameField.getText(),
addressField.getText(),
cityField.getText(),
stateField.getText(),
majorField.getText());
/* search list for a matching record
for
tempStudent.equals(i);
*/
int index = studio.compareStudent(tempStudent);
if(index < 0)
studio.addStudent(tempStudent);
else
{
new UpdateDialog(index, tempStudent);
}
clearAllFields();
}
});
p2.add(enterButton);
add(p1);
add(p2);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
studio.save();
System.exit(0);
}
});
setSize(333,225);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
/*
dialog box with actionlitsteners for buttons
Lables for new and old studetns info
*/
class UpdateDialog extends JDialog
{
//where and why should these variables be??
private Student s;
private int index;
JLabel nameNew,addressNew,cityNew,stateNew,majorNew;
JLabel nameOld,addressOld,cityOld,stateOld,majorOld;
UpdateDialog(int i, Student tempStudent)
{
//some variables used in this class
this.s = tempStudent;
this.index = i;
//nested layouts:: flow has two gridlayouts with init
setLayout(new GridLayout(1,2));
//first panel is on top
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(7, 1, 0, 5));
//second pannel is on the bottom of the top pannel
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new GridLayout(7, 1, 0, 5));
//set the labels for NEW student info
nameNew = new JLabel(s.getName());
addressNew = new JLabel(s.getAddress());
cityNew = new JLabel(s.getCity());
stateNew = new JLabel(s.getState());
majorNew = new JLabel(s.getMajor());
//set the labels for OLD student info
nameOld = new JLabel(studio.getStudent(index).getName());
addressOld = new JLabel(studio.getStudent(index).getAddress());
cityOld = new JLabel(studio.getStudent(index).getCity());
stateOld = new JLabel(studio.getStudent(index).getState());
majorOld = new JLabel(studio.getStudent(index).getMajor());
//buttons and their listeners
//ok button
JButton okButton = new JButton( "OK" );
okButton.setBackground(Color.GREEN);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
/* delete and replace student record */
studio.removeStudent(index);
studio.addStudent(s);
dispose();
}
});
//cancel button
JButton cancelButton = new JButton( "Cancel" );
cancelButton.setBackground(Color.RED);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
});
//addlables and buttons to Left pannles
leftPanel.add(new Label("New Student"));
leftPanel.add(nameNew);
leftPanel.add(addressNew);
leftPanel.add(cityNew);
leftPanel.add(stateNew);
leftPanel.add(majorNew);
leftPanel.add(okButton);
//addlables and buttons to Right pannles
rightPanel.add(new Label("Old Student"));
rightPanel.add(nameOld);
rightPanel.add(addressOld);
rightPanel.add(cityOld);
rightPanel.add(stateOld);
rightPanel.add(majorOld);
rightPanel.add(cancelButton);
//add panels to frame
add(leftPanel);
add(rightPanel);
//window attributes
setModal(true);
setSize(250,190);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
}
void clearAllFields()
{
nameField.setText("");
addressField.setText("");
cityField.setText("");
stateField.setText("");
majorField.setText("");
}
public static void main(String[] args)
{
StudentDatabase frame = new StudentDatabase();
}
}
class Student implements Comparable
{
private String name, address, city, state, major;
public Student(String name, String address, String city, String state,
String major)
{
this.name = name;
this.address = address;
this.city = city;
this.state = state;
this.major = major;
}
public String toString()
{
return (
name + "\n" +
address + "\n" +
city + "\n" +
state + "\n" +
major + "\n");
}
public int compareTo(Object o)
{
return name.compareTo(((Student)o).name);
}
public boolean equals(Object s)
{
return name.equalsIgnoreCase(((Student)s).name);
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public String getMajor()
{
return major;
}
}
class StudentIO
{
//private File file;
private ArrayList<Student> list = new ArrayList();
private Scanner in;
private PrintWriter pw;
//private String name, address, city, state, major;
/*load student object into arralist
-accept file
-create scanner using file
-load sutdents from file if there are any students
*/
public StudentIO(File file)
{
if(file.exists())
{
try
{
this.in = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
do
{
list.add(getStudentFromFile(file));
in.nextLine();
} while(in.hasNext());
System.out.println(list.size() + " entries read.");
}
try
{
this.pw = new PrintWriter(file);
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
private Student getStudentFromFile(File file)
{
return
//calls constructor from Student Class
new Student
(
in.nextLine(),
in.nextLine(),
in.nextLine(),
in.nextLine(),
in.nextLine()
);
}
public void save()
{
for(int i = 0; i < list.size(); i++)
{
pw.println(list.get(i));
}
System.out.println("File saved.");
pw.close();
}
public int compareStudent(Student s)
{
for(int i = 0; i < list.size(); i++)
{
if(s.equals(list.get(i)))
return i;
}
return -1;
}
public void removeStudent(int i)
{
list.remove(i);
}
public void addStudent(Student s)
{
list.add(s);
}
public Student getStudent(int index)
{
return list.get(index);
}
}
|