import java.io.*; import java.util.ArrayList; public class StudentNodeTest { private static BufferedReader console; private static boolean validCommand; private static Iterator iter; public static void main(String[] args) throws IOException, FileNotFoundException, ClassNotFoundException { /* Run this code to test w/o a menu. StudentNode jon = new StudentNode("Jon", "Pezzino", 4.0); Iterator iter = new Iterator(jon); iter.add(new StudentNode("Herbert", "Bunkel, Q.", 3.0)); iter.add(new StudentNode("Mayhew", "Lieberstein", 3.5)); iter.add(new StudentNode("Spongebob", "Squarepants", 1.0)); iter.add(new StudentNode("Bonnie &", "Clyde", 2.0)); iter.add(new StudentNode("Albert", "Einstein", 3.9)); iter.add(new StudentNode("Maddox", "Nolastname", 2.1)); iter.add(new StudentNode("1337", "H4X0R", 1.2)); iter.add(new StudentNode("Jimmy", "Jimmy!", 2.9)); iter.add(new StudentNode("Bob", "Johnson", 2.7)); iter.add(new StudentNode("Alan", "Shmidov", 2.8)); iter.printAlphaOrder(); */ iter = new Iterator(null); InputStreamReader reader = new InputStreamReader(System.in); console = new BufferedReader(reader); String input = ""; while(true) { printMenu(); input = console.readLine(); validCommand = false; if(input.equals("A")) { add(); } if(input.equals("R")) { remove(); } if(input.equals("P")) { print(); } if(input.equals("Q")) { reversePrint(); } if(input.equals("D")) { deansList(); } if(input.equals("F")) { search(); } if(input.equals("E")) { export(); } if(input.equals("I")) { importFile(); } if(input.equals("X")) { exit(); } if(!validCommand) { System.out.println("Invalid command. Please try again."); } } } public static void printMenu() { System.out.println("\n---------------" + "\nA - Add a student" + "\nR - Remove a student" + "\nP - Print all students" + "\nQ - Print all students in reverse alphabetical order" + "\nD - Dean's list" + "\nF - Find name" + "\nE - Export to File" + "\nI - Import from File" + "\nX - Quit"); } private static void add() throws IOException { System.out.println("Add a student. \nFirst Name: "); String firstName = console.readLine(); System.out.println("Last name: "); String lastName = console.readLine(); System.out.println("GPA: "); double gpa = Double.parseDouble(console.readLine()); iter.add(new StudentNode(firstName, lastName, gpa)); System.out.println("Student added."); validCommand = true; } private static void remove() throws IOException { System.out.println("Remove a student. \nStudent Name (First or Last):"); String name = console.readLine(); iter.removeStudent(name); System.out.println("Student removed."); validCommand = true; } private static void print() { System.out.println("Print all students. \n----------"); iter.printAlphaOrder(); validCommand = true; } private static void reversePrint() { System.out.println("Print all students in reverse alphabetical order. \n----------"); iter.printReverseAlphaOrder(); validCommand = true; } private static void deansList() throws IOException { System.out.println("Dean's List. \nMinimun GPA to make Dean's List:"); iter.printDeansList(Double.parseDouble(console.readLine())); validCommand = true; } private static void search() throws IOException { System.out.println("Search Students. \nStudent Name (First or Last): "); iter.search(console.readLine()); validCommand = true; } private static void exit() throws IOException { System.out.println("Exiting."); System.exit(0); } private static void export() throws FileNotFoundException, IOException { System.out.println("Exporting to File students.dat."); ArrayList sendToFile = iter.packageNodes(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("students.dat")); out.writeObject(sendToFile); validCommand = true; } private static void importFile() throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream(new FileInputStream("students.dat")); ArrayList imp = (ArrayList)in.readObject(); iter = (Iterator)imp.get(0); validCommand = true; } }