Results 1 to 6 of 6
  1. #1
    Aaron140 is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default Instantiable Classes - Driver Program Help

    Hello All,

    ive got a problem :(

    In my driver program i have a Menu GUI, one of my menu headings is
    "Lessons", When i click add under this heading, It asks me to Add new Student. It shouldnt.

    Can anybody please tell me where im going wrong in my code?


    STUDENTS:

    Java Code:
    /** A class that models a Student of a driving school
    */
    
    
    public class Students extends Lessons{
    
    	private String name;
    	private String address;
    	private int number;
    	private int noLessons;
    
    
    	public Students() {
    		name = "No Name Specified";
    		address = "No Address Specified";
    		number = 0;
    		noLessons = 0;
    	}
    
    
    	public Students (String name, String addr, int num, int noLes) {
    		setName(name);
    		setAddress(addr);
    		setNumber(num);
    		setNoLessons(noLes);
    
    	}
    
    
    	public String getName() {
    		return name;
    	}
    
    
    
        public String getAddress() {
    		return address;
    	}
    
    
        public int getNumber() {
        	return number;
        }
    
    
        public int getNoLessons() {
        	return noLessons;
        }
    
    
    
        public void setName(String name) {
        	name = name;
        }
    
    
    
        public void setAddress(String addr) {
        	name = addr;
        }
    
    
        public void setNumber(int num) {
        	number = num;
        }
    
    
        public void setNoLessons(int noLes) {
        	noLessons = noLes;
        }
    
        public String toString(){
        	return "Name: " + name + "\nAddress: " + address + "\nPhone Number: " + number + "\nNumber of Lessons: " + noLessons;
        }
    
    }


    LESSONS:

    Java Code:
    /**A class that models the Lessons in a driving school
     */
    
    
    public class Lessons{
    	private String location;
    	private String time;
    	private double price;
    
        // 'accessor' methods to return a copy of an attribute
    	public String getLocation() { return location;}
    	public String getTime() { return time;}
    	public double getPrice() { return price;}
    
    	// 'mutator' methods to change the value of an attribute
    	public void setLocation (String location) {
    				this.location = location;
    	}
    
    	public void setTime( String time) {
    				this.time = time;
    	}
    
    	public void setPrice(double price) {
    				this.price = price;
    	}
    
        // 'constructor' methods to give lessons a sensible initial state
    
    	/** full-args constructor, to create a lesson, which everything is known
    	 */
    	public Lessons(String location, String time, double price) {
    				setLocation(location);
    				setTime(time);
    				setPrice(price);
    	}
    
    	/** no-args constructor
    	 */
    	public Lessons() {
    				this("Not Given","00:00",00.00);
    	}
    
    	/* String summary of the values of all the object's attributes
    	 */
    	public String toString() {
    				return "Location: " + getLocation() + "\nPrice: " + getPrice() + "\nTime: " + getTime();
    	}
    }
    DRIVER PROGRAM:

    Java Code:
    /**Driving School Tester*/
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    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 didn't work");
          		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:");
          	  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 didn't work");
          		e.printStackTrace();
          	}
          	
          	// how many valid lesson records?
          	while (lesson[count1] !=null)
          	   count1++;
          } // end open()   
          
          
          public void addLesson(){
          	Lessons l2 = new Lessons();
          	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
          	count++; // now there is one more lesson in the system
          }
          
          public void displayLesson(){
          	JTextArea area = new JTextArea();
          	if (count1>0) {
          	  area.setText("Lesson List:");
          	  for (int i = 0; i<count1; i++) // loop over existing bikes, 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 ("Add")){
          	   addStudent(); // branch to a separate method
          	}
          	else if (e.getActionCommand() .equals ("Display")){
               displayStudent();
          	}
          	else if (e.getActionCommand() .equals ("Add")){
          	   addLesson(); // branch to a separate method
          	}
          	else if (e.getActionCommand() .equals ("Display")){
               displayLesson();
          	}
          	else if (e.getActionCommand() .equals ("New File")){
          		newSystemStudent();
          		newSystemStudent();
            }
          	else if (e.getActionCommand() .equals ("Save")){
          	// Error Handling
          	 try{
          	 	saveStudent();
          	 	saveLesson();
          	 	showMessage("Data saved successfully");
          	 } // try
          	 catch (IOException f){
          	 	showMessage("Not able to save the file:");
          	 	f.printStackTrace();
          	 }// catch
          	}// else if
          	
          	else if (e.getActionCommand() .equals ("Open")){
          	 openStudent();
          	 openLesson();
             displayStudent();
             displayLesson();
          	}
          	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("Save");
          	item.addActionListener(this);
          	fileMenu.add(item);
          	item = new JMenuItem("Open");
          	item.addActionListener(this);
          	fileMenu.add(item);
          	item = new JMenuItem("New File");
          	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("Add");
          	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("Add");
          	item.addActionListener(this);
          	lessonMenu.add(item);
          	
          	item = new JMenuItem("Display");
          	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);
          }
    }
    Thanks

    Aaron.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    one of my menu headings is
    "Lessons", When i click add under this heading, It asks me to Add new Student.
    Do you mean that when you select "Add" under "Lesson" that it seems to want to add a new student?

    Java Code:
    public void actionPerformed (ActionEvent e) {
        if (e.getActionCommand() .equals ("Quit")){
            showMessage("Shutting down the system");
            System.exit(0);
        }
        else if (e.getActionCommand() .equals ("Add")){
            addStudent(); // branch to a separate method
        }
        else if (e.getActionCommand() .equals ("Display")){
            displayStudent();
        }
        else if (e.getActionCommand() .equals ("Add")){
            addLesson(); // branch to a separate method
        }
        else if // etc...

    The add lesson menu item has an action command "Add". You can work through that list of if-else-if-... and see for yourself what is going to be executed: ie which else-if is the first to match.

    JMenuItem inherits a method - setActionCommand() - that may help resolve this. This (or rather the approach of having the class implement ActionListener) becomes a bit unwieldly as the user interface grows.

    Or notice that you can add an action listener to a menu item. (Another AbstractButton method). This would result in code that is more "targeted" for the specific menu item.

    Or use construct the menu item from an Action. Described in Oracle's Tutorial in the section How to Use Actions. (This is a very flexible approach as described there.)
    Last edited by pbrockway2; 11-19-2010 at 01:32 AM.

  3. #3
    Aaron140 is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    Im Sorry, but this is all japanese to me.
    Can you not just show me what i need to change and what i need to change it to?

    or is that not allowed or something?

    Im really struggling with Java this year.

    And Yes, i do mean that when you select "Add" under "Lesson" that it seems to want to add a new student. Very Annoying.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Can you not just show me what i need to change and what i need to change it to?
    or is that not allowed or something?

    I won't do that. There's no ill will or meanness involved: it's just that I think posting teh codez is pointless. (That's just me though, there are others here.)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with pbrockway. If you don't understand what he posts, then by all means ask for clarification, but don't ask anyone to do your work for you.
    Last edited by Fubarable; 11-19-2010 at 10:40 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    If you don't understand what he posts, then by all means ask for clarification

    Certainly.

    A short-textual-message-mediated forum like this makes it hard sometimes to tell the difference between a person who (a) wants their homework done for them and (b) is using "... implements ActionListener {" and "item.addActionListener(this)" as a sort of pattern without really understanding what these things do in practice.

Similar Threads

  1. how to make to classes in the bank account program
    By buzzing in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 05:02 AM
  2. which classes to write..how to program this?
    By sony1 in forum New To Java
    Replies: 7
    Last Post: 09-04-2010, 07:12 PM
  3. Driver for Java-SQL
    By Pr0n in forum New To Java
    Replies: 7
    Last Post: 07-24-2009, 12:30 AM
  4. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 05:50 AM
  5. PostgreSQL driver
    By bugger in forum JDBC
    Replies: 3
    Last Post: 01-11-2008, 10:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •