import java.util.Scanner; import java.io.IOException; import java.text.*; /* Class BinarySearchTree */ public class Lab7Q3 { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); /* Creating object of BST */ BST bst = new BST(); Item element = new Item(); String next; BST bstExpired; BST bstName; int classId; System.out.println("Binary Search Tree Test\n"); char ch; /* Perform tree operations */ do { System.out.println("\nBinary Search Tree Operations\n"); System.out.println("1. insert student "); System.out.println("2. Find a student by its code "); System.out.println("3. List students in alphabetical order of their names for a specific class "); System.out.println("4. List students in alphabetical order of their names "); System.out.println("5. List all graduated students "); System.out.println("6. List all undergrads for a specific class in alphabetical order of their names "); System.out.println("7. Delete a student given by its code "); System.out.println("8. Delete all graduates"); System.out.println("9. Save stock in file stock.data"); int choice = scan.nextInt(); switch (choice) { case 1: System.out.println("Enter student Code"); element.code = scan.nextInt(); System.out.println("Enter student Name"); element.name = scan.nextLine(); System.out.println("Enter student birth date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.birthDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Enter student address"); element.address = scan.nextLine(); System.out.println("Enter class Id"); element.classId = scan.nextInt(); System.out.println("Enter student enrollment date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.enrollmentDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Enter Item status (undergrad = 1, graduate = 2)"); element.classId = scan.nextByte(); bst.insert(element); break; case 2: System.out.println("Enter student code to search"); BSTNode temp = bst.search(scan.nextInt()); if (temp == null) System.out.println("Not found"); else { System.out.println("Student Information = " + temp.getData()); System.out.println("Do you want to modify the student information(Y/N)"); if (scan.next().charAt(0) == 'Y') { System.out.println("Enter student Code"); element.code = scan.nextInt(); System.out.println("Enter student Name"); element.name = scan.next(); System.out.println("Enter student birth date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.birthDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Enter student address"); element.address = scan.next(); System.out.println("Enter class Id"); element.classId = scan.nextInt(); System.out.println("Enter student enrollment date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.enrollmentDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Enter Item status (undergrad = 1, graduate = 2)"); element.status = scan.nextByte(); temp.data = element; } } break; case 3: System.out.println("Enter class Id"); classId = scan.nextInt(); bstName = new BST(); bst.insertByClassAndName(bstName, classId); bstName.inorder(); break; case 4: bstName = new BST(); bst.createTreeByName(bstName); bstName.inorder(); break; case 5: bst.listGraduate(); break; case 6: System.out.println("Enter class Id"); classId = scan.nextInt(); bstName = new BST(); bst.insertByClassAndName(bstName, classId); bstName.listUndergradsByClassAndName(); break; case 7: System.out.println("Enter student code to delete"); bst.delete(scan.nextInt()); break; case 8: bst.deleteGraduates(); bst.inorder(); break; case 9: bst.writeToFile("output.txt"); break; default: System.out.println("Wrong Entry \n "); break; } System.out.println("\nDo you want to continue (Type y or n) \n"); ch = scan.next().charAt(0); } while (ch == 'Y' || ch == 'y'); } }