A solution for Project 5
// *** FILE Scheduler.java
import java.util.*;
import javax.swing.*;
import java.io.*;
public class Scheduler
{
public static ArrayList students = new ArrayList();
public static ArrayList courses = new ArrayList();
public static void showMenu() {
System.out.println("0) Exit
program");
System.out.println("1) Add a
new student");
System.out.println("2) Add a
new class");
System.out.println("3)
Assign a student to a class");
System.out.println("4) Print
a course roster");
}
public static int readInt(String prompt) {
int value = 0;
while (true) {
try {
value = Integer.parseInt(JOptionPane.showInputDialog(prompt));
return value;
}
catch (Exception e) {
System.out.println("Invalid number, try again");
}
}
}
public static void addStudent() {
Student s = new Student();
s.studentName =
JOptionPane.showInputDialog("Enter in the student name");
s.studentAge =
readInt("Enter in the student age");
s.studentID = readInt("Enter
in the student ID");
students.add(s);
}
public static void addClass() {
Course c = new Course();
c.name =
JOptionPane.showInputDialog("Enter in the course name");
c.size = readInt("Enter in
the maximum size of the class");
c.period = readInt("Enter in
the period of the class");
c.teacher =
JOptionPane.showInputDialog("Enter in the teacher for this class");
courses.add(c);
}
public static void assignStudent() {
int SID = readInt("Enter in
the ID of the student you want to assign");
Student s = null;
for (int a = 0; a <
students.size(); a++)
if
(((Student) students.get(a)).studentID == SID) s = (Student)
students.get(a);
for (int a = 0; a <
courses.size(); a++)
System.out.println(((Course) courses.get(a)).name);
String CN =
JOptionPane.showInputDialog("Enter in the class name you want to assign
the student to");
Course c = null;
for (int a = 0; a <
courses.size(); a++)
if
(((Course) courses.get(a)).name.equals(CN))
c = (Course) courses.get(a);
c.students.add(s);
}
public static void printRoster() {
String CN =
JOptionPane.showInputDialog("Enter in the class name you want to print
a roster for");
Course c = null;
for (int a = 0; a <
courses.size(); a++)
if
(((Course) courses.get(a)).name.equals(CN))
c = (Course) courses.get(a);
for (int a = 0; a <
c.students.size(); a++)
System.out.println(((Student) c.students.get(a)).studentName);
}
public static void main(String[] args) {
int input = -1;
while (input != 0) {
showMenu();
input = readInt("Which menu option do you want?");
switch
(input) {
case 1:
addStudent();
break;
case 2:
addClass();
break;
case 3:
assignStudent();
break;
case 4:
printRoster();
break;
default:
break;
}
}
}
}
// *** FILE Course.java
import java.util.*;
public class Course
{
public ArrayList students = new ArrayList();
public String name;
public int size;
public int period;
public String teacher;
}
// *** FILE Student.java
public class Student
{
public String studentName;
public int studentAge;
public int studentID;
}