can somebody edit my code to do this for me?
/**Driving School Tester*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
public class DrivingSchoolTest extends JFrame implements ActionListener{
JMenu fileMenu,lessonMenu,studentMenu;
Students [] student;
Lessons [] lesson;
int count;
int count1;
public static void main( String[] args ) {
DrivingSchoolTest frame = new DrivingSchoolTest();
frame.setVisible(true);
}
public DrivingSchoolTest( ) {
setTitle ( "Driving School" );
setSize ( 400,200 );
setLocation ( 350,300 );
Container pane = getContentPane();
pane.setBackground(Color.WHITE);
setDefaultCloseOperation( EXIT_ON_CLOSE );
createFileMenu();
createStudentMenu();
createLessonMenu();
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(studentMenu);
menuBar.add(lessonMenu);
}
public void newSystemStudent() {
student = new Students[10];
count = 0;
}
/** writes the array of students to the file "student.dat"
*/ // NEW
public void saveStudent() throws IOException {
ObjectOutputStream os;
os = new ObjectOutputStream(new FileOutputStream ("student.dat"));
os.writeObject(student);
os.close();
}
/** loads an array of students from the file "student.dat"
*/ // NEW
public void openStudent() {
count = 0;
try{
ObjectInputStream is;
is = new ObjectInputStream(new FileInputStream ("student.dat"));
student = (Students []) is.readObject();
is.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Open File Failed");
e.printStackTrace();
}
// how many valid student records?
while (student[count] !=null)
count++;
} // end open()
public void addStudent(){
Students s1 = new Students();
s1.setName(JOptionPane.showInputDialog("Enter Student Name:"));
s1.setAddress(JOptionPane.showInputDialog("Enter address for Student 2: "));
s1.setNumber(JOptionPane.showInputDialog("Enter phone number for Student 2: "));
s1.setNoLessons(Integer.parseInt(JOptionPane.showInputDialog("Enter number of lessons for Student 2: ")));
student[count] = s1; // 'default student
// set the attributes: ask the user for name etc
count++; // now there is one more student in the system
}
public void displayStudent(){
JTextArea area = new JTextArea();
if (count>0) {
area.setText("Student List:\n\n");
for (int i = 0; i<count; i++) // loop over existing bikes, rather than array size
area.append("Students: " + i + " " +student[i].toString()+"\n");
showMessage(area);
}
else
showMessage("No students in the system");
} // end display
public void newSystemLesson() {
lesson = new Lessons[10];
count1 = 0;
}
/** writes the array of lessons to the file "lesson.dat"
*/ // NEW
public void saveLesson() throws IOException {
ObjectOutputStream os;
os = new ObjectOutputStream(new FileOutputStream ("lesson.dat"));
os.writeObject(lesson);
os.close();
}
/** loads an array of lessons from the file "lesson.dat"
*/ // NEW
public void openLesson() {
count1 = 0;
try{
ObjectInputStream is;
is = new ObjectInputStream(new FileInputStream ("lesson.dat"));
lesson = (Lessons []) is.readObject();
is.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Open File Failed");
e.printStackTrace();
}
// how many valid lesson records?
while (lesson[count1] !=null)
count1++;
} // end open()
public void addLesson(){
Lessons l2 = new Lessons();
l2.setStName(JOptionPane.showInputDialog("Enter Student Name:"));
l2.setLocation(JOptionPane.showInputDialog("Enter location for Lesson 2: "));
l2.setTime(JOptionPane.showInputDialog("Enter time for Lesson 2: "));
l2.setPrice(Double.parseDouble(JOptionPane.showInputDialog("Please enter a price for lesson 2: ")));
lesson[count1] = l2; // 'default lesson
// set the attributes: ask the user for location etc
count1++; // now there is one more lesson in the system
}
public void displayLesson(){
JTextArea area = new JTextArea();
if (count1>0) {
area.setText("Lesson List:\n\n");
for (int i = 0; i<count1; i++) // loop over existing lessons, rather than array size
area.append("Lessons: " + i + " " +lesson[i].toString()+"\n");
showMessage(area);
}
else
showMessage("No lessons in the system");
} // end display
public void actionPerformed (ActionEvent e) {
if (e.getActionCommand() .equals ("Quit")){
showMessage("Shutting down the system");
System.exit(0);
}
else if (e.getActionCommand() .equals ("Register")){
addStudent(); // branch to a separate method
}
else if (e.getActionCommand() .equals ("Display")){
displayStudent();
}
else if (e.getActionCommand() .equals ("Create")){
addLesson(); // branch to a separate method
}
else if (e.getActionCommand() .equals ("View")){
displayLesson();
}
else if (e.getActionCommand() .equals ("New File")){
newSystemStudent();
newSystemLesson();
}
else if (e.getActionCommand() .equals ("Save")){
// Error Handling
try{
saveStudent();
saveLesson();
showMessage("Unable to Save Data");
} // try
catch (IOException f){
showMessage("Unable to Save Data: ");
f.printStackTrace();
}// catch
}// else if
else if (e.getActionCommand() .equals ("Open")){
openStudent();
}
else
showMessage("Error");
} // actionPerformed
private void createFileMenu(){
// create the menu
fileMenu = new JMenu("File");
// declare a menu item (re-usable)
JMenuItem item;
item = new JMenuItem("New File");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Save");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Open");
item.addActionListener(this);
fileMenu.add(item);
// create the 'quit' option
fileMenu.addSeparator();
item = new JMenuItem("Quit");
item.addActionListener(this);
fileMenu.add(item);
}
private void createStudentMenu(){
// create the menu
studentMenu = new JMenu("Student");
// declare a menu item (re-usable)
JMenuItem item;
item = new JMenuItem("Register");
item.addActionListener(this);
studentMenu.add(item);
item = new JMenuItem("Display");
item.addActionListener(this);
studentMenu.add(item);
}
private void createLessonMenu(){
// create the menu
lessonMenu = new JMenu("Lesson");
// declare a menu item (re-usable)
JMenuItem item;
item = new JMenuItem("Create");
item.addActionListener(this);
lessonMenu.add(item);
item = new JMenuItem("View");
item.addActionListener(this);
lessonMenu.add(item);
}
/** utility methods to make the code simpler */
public void showMessage (String s){
JOptionPane.showMessageDialog(null,s);
}
public void showMessage (JTextArea s){
JOptionPane.showMessageDialog(null,s);
}
}


Sign In
Create Account

Back to top









