Run the program and you'll see I am not filling the arrayList but am invoking the toString method. Obviously this is not giving me the desired output. What's wrong? (I did not post the other classes because I don't think my error is there but if anyone needs them let me know.

In this lab you are going to write the classes Test, Student, Teacher, and Person. The classes Student and Teacher will inherit from the class Person. The class Person will have the single variable name, while the class Teacher will have in addition the variable salary of type double. The class Student will have in addition the variable grade of type integer. The classes Student, Teacher, and Person should have constructors and toString methods.

The class Test will contain a main method and the methods:
public static String getName() - gets a name from the screen
public static double getSalary() - gets a salary from the screen
public static int getGrade() - gets a grade from the screen
public static int printMenu() - prints a menu and returns the choice of the user. The menu will be:

1: Add new student
2: Add new teacher
3: Print all students
4: Print all teachers
5: Quit
The main method in the class Test will have a loop that prints the menu, gets a response from the user and performs the requested operation. The current list of students and teachers should be stored in a single ArrayList of Person. The Boolean expression:

(a instance of Student)

will return true when a is Student object.

import java.util.ArrayList;

import javax.swing.JOptionPane;


public class Test 
{
	private static String name;
	private static int userResponse;
	
	public static void main(String[] args) 
	{
		ArrayList<Person> people = new ArrayList<Person>();

		{
			do
				{userResponse = JOptionPane.showConfirmDialog(null, "Do You Want To Make" +
						"Changes To The Student/Teacher Table", "Change Table",
						JOptionPane.YES_NO_OPTION);
				while (userResponse == JOptionPane.YES_OPTION)
					
					{int choice = printMenu();
					
						switch (choice)
						{
						case 1:
				
						String passStudentName = getName();
						int passGrade = getGrade();
						Student stu = new Student(passStudentName, passGrade);
						people.add(stu);
						break;
						
						case 2:
						String passTeacherName = getName();
						double passSalary = getSalary();
						Teacher teach = new Teacher(passTeacherName, passSalary);
						people.add(teach);
						break;
						
						case 3:
							for (int i = 0; i < people.size(); i++)
							{
								if (people.get(i)instanceof Student)
								{
									System.out.println(people.get(i));
								}
							}
							break;

						case 4:
							for (int i = 0; i < people.size(); i++)
							{
								if (people.get(i)instanceof Teacher)
								{
									System.out.println(people.get(i));
								}
							}
							break;
							
						case 5:

							JOptionPane.showMessageDialog(null, "The Program Terminates",
									"End of Program", JOptionPane.OK_OPTION);
							
							System.exit(0);
						}
				}
			}while (userResponse == JOptionPane.YES_OPTION);
		}System.exit(0);
	}
	
	public static String getName()
	{
		String userInputName = JOptionPane.showInputDialog(null, "Enter A Name ",
				"Name", JOptionPane.INFORMATION_MESSAGE);
		return userInputName;
		//gets a name from the screen
	}
	
	  public static double getSalary()
	  {
			String userInputSalary = JOptionPane.showInputDialog(null, "Enter A Salary ",
					"Salary", JOptionPane.INFORMATION_MESSAGE);
			Double salary = Double.parseDouble(userInputSalary);
		return salary;
		  //gets a salary from the screen
	  }
	  
	  public static int getGrade()
	  {
			String userInputGrade = JOptionPane.showInputDialog(null, "Enter A Grade ",
					"Grade", JOptionPane.INFORMATION_MESSAGE);
			Integer grade = Integer.parseInt(userInputGrade);
		return grade;
		  //gets a grade from the screen
	  }
	  
	  public static int printMenu()
	  {
		  String userChoice = JOptionPane.showInputDialog(null, "Please Choose an Option:\n" +
				  "1. Add New Student\n2. Add New Teacher\n3. Print All Students\n" +
				  "4. Print All Teachers\n5. Quit",
				  "Menu Choice", JOptionPane.INFORMATION_MESSAGE);
		  Integer choice = Integer.parseInt(userChoice);
		return choice;
	  }
}

Recommended Answers

All 2 Replies

For debugging try whenever you add anything to the list to print it as well as the element entered:

Student stu = new Student(passStudentName, passGrade);
people.add(stu);
System.out.println("Student:"+ stu);
System.out.println("People:"+ people);

Do that for all cases and try to add a person and see what happens. Also I trust that you have overridden the toString method in those classes (Student and Person).

Also if you want us to test that code, we will need all of your classes, but try first those suggestions and see what happens

I created the 3 other classes and run the code. It works, it prints the values of the array at the console.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.