import java.io.IOException; import java.text.*; import java.util.Scanner; /* Class BinarySearchTree */ public class Lab7Q2 { 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; 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 Item "); System.out.println("2. Find an item by its code "); System.out.println("3. List valid items in alphabetical order of their names "); System.out.println("4. List expired items in alphabetical order of their names "); System.out.println("5. List all items "); System.out.println("6. Delete an item given by its code "); System.out.println("7. Delete all expired items. "); System.out.println("8. Create a separate search tree for expired items"); System.out.println("9. Save stock in file stock.data"); int choice = scan.nextInt(); switch (choice) { case 1: System.out.println("Enter Item Code"); element.code = scan.nextInt(); System.out.println("Enter Item Name"); element.name = scan.next(); System.out.println("Enter Item Price"); element.price = scan.nextDouble(); System.out.println("Enter Item Amount"); element.amount = scan.nextInt(); System.out.println("Enter Item received date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.receivedDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Enter Item expiration date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.expirationDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } bst.insert(element); break; case 2: System.out.println("Enter code item to search"); BSTNode temp = bst.search(scan.nextInt()); if (temp == null) System.out.println("Not found"); else { System.out.println("Item Information = " + temp.getData()); System.out.println("Do you want to modify the information(Y/N)"); if (scan.next().charAt(0) == 'Y') { element.code = temp.data.code; System.out.println("Enter Item Name"); element.name = scan.next(); System.out.println("Enter Item Price"); element.price = scan.nextDouble(); System.out.println("Enter Item Amount"); element.amount = scan.nextInt(); System.out.println("Enter Item received date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.receivedDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Enter Item expiration date"); next = scan.next("[0-9]{2}/[0-9]{2}/[0-9]{4}"); try { element.expirationDate = new SimpleDateFormat("dd/MM/yyyy").parse(next); } catch (ParseException e) { e.printStackTrace(); } temp.data = element; } } break; case 3: bstName = new BST(); bst.createValidSortedByNameTree(bstName); bstName.inorder(); break; case 4: bstName = new BST(); bst.createInValidSortedByNameTree(bstName); bstName.inorder(); break; case 5: bst.inorder(); break; case 6: System.out.println("Enter code item to delete"); bst.delete(scan.nextInt()); break; case 7: bst.deleteExpiredDate(); bst.inorder(); break; case 8: bstExpired = new BST(); bst.createExpiredDateTree(bstExpired); bstExpired.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'); } }