strydez

Untitled

Nov 30th, 2020
546
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package src.main;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.*;
  8.  
  9. public class menuInterface {
  10.     public static void main(String[] args) {
  11.  
  12.         HashTable hashTable = new HashTable(2000);
  13.         hashTable.initializeSecondHashMap();
  14.  
  15.         File file = new File("songsTextFile.txt");
  16.         String name, composer, lyrics;
  17.         int songLength, releaseYear;
  18.  
  19.         Scanner input;
  20.         try {
  21.             input = new Scanner(file);
  22.             while (input.hasNextLine()) {
  23.                 name = input.nextLine();
  24.                 composer = input.nextLine();
  25.                 releaseYear = input.nextInt();
  26.                 songLength = input.nextInt();
  27.                 String leftover = input.nextLine();
  28.                 lyrics = input.nextLine();
  29.                 Song newSong = new Song(name, composer, releaseYear, songLength, lyrics);
  30.                 hashTable.put(newSong);
  31.             }
  32.             input.close();
  33.         } catch (FileNotFoundException e) {
  34.             e.printStackTrace();
  35.         }
  36.         Scanner in = new Scanner(System.in);
  37.  
  38.         System.out.println("Welcome to the EDM records file!\n");
  39.         printMenu();
  40.         char option = Character.toUpperCase(in.next().charAt(0));
  41.         while (option != 'A' && option != 'B' && option != 'C' && option != 'D' && option != 'E' && option != 'X') {
  42.             System.out.println("\nInvalid menu option. Please enter A-E or X to exit.\n");
  43.             printMenu();
  44.             option = Character.toUpperCase(in.next().charAt(0));
  45.         }
  46.         while (option != 'X') {
  47.             if (option == 'A') {
  48.                 String leftover = in.nextLine();
  49.                 System.out.print("\nPlease input the record's name: ");
  50.                 name = in.nextLine();
  51.                 System.out.print("\nPlease input the record's composer: ");
  52.                 composer = in.nextLine();
  53.                 if (hashTable.contains(new Song(name, composer)))
  54.                     System.out.println("\nSorry, this record has already been filed!\n");
  55.                 else {
  56.                     System.out.print("Please input the record's release year: ");
  57.                     releaseYear = in.nextInt();
  58.                     System.out.print("Please input the record's song length (in seconds): ");
  59.                     songLength = in.nextInt();
  60.                     leftover = in.nextLine();
  61.                     System.out.print("Please input the record's lyrics (Only alphabets saved): ");
  62.                     lyrics = in.nextLine().replaceAll("[^A-Za-z]+", "");
  63.                     Song newSong = new Song(name, composer, releaseYear, songLength, lyrics);
  64.                     hashTable.put(newSong);
  65.                     System.out.println("\nYour record " + name + " has been added!\n");
  66.                 }
  67.             } else if (option == 'B') {
  68.                 String leftover = in.nextLine();
  69.  
  70.                 System.out.println("\nPlease choose a record from below\n");
  71.                 for (Song song : hashTable.getUniqueSongs())
  72.                     System.out.println(song.toString() + "\n");
  73.  
  74.                 System.out.println(hashTable.getUniqueValues());
  75.                 System.out.print("Please input the record's name: ");
  76.                 name = in.nextLine();
  77.                 System.out.print("Please input the record's composer: ");
  78.                 composer = in.nextLine();
  79.                 Song songToDelete = new Song(name, composer);
  80.  
  81.                 if (hashTable.remove(songToDelete))
  82.                     System.out.println("\nYour record " + name + " has been deleted!\n");
  83.  
  84.             } else if (option == 'C') {
  85.                 System.out.println("\nPlease selection an option:");
  86.                 System.out.println("1. Find and display one record using the primary key");
  87.                 System.out.println("2. Find and display records using keywords");
  88.                 System.out.print("\nPlease input 1 or 2: ");
  89.                 int displayOption = in.nextInt();
  90.                 while (displayOption != 1 && displayOption != 2) {
  91.                     System.out.println("Please input 1 or 2: ");
  92.                     displayOption = in.nextInt();
  93.                 }
  94.                 String leftover = in.nextLine();
  95.                 if (displayOption == 2) {
  96.                     System.out.print("\nPlease input your keyword for search: ");
  97.                     String keyword = in.nextLine().toLowerCase();
  98.                     hashTable.searchByKeyword(keyword, 2);
  99.                     System.out.println();
  100.                 } else { // display option 1
  101.                             // TODO idk what primary key is
  102.                     System.out.print("\nPlease input a single keyword: ");
  103.                     name = in.next();
  104.                     hashTable.searchByKeyword(name, 1);
  105.                     System.out.println();
  106.                 }
  107.             } else if (option == 'D') {
  108.                 String leftover = in.nextLine();
  109.  
  110.                 for (Song song : hashTable.getUniqueSongs())
  111.                 System.out.println(song.toString() + "\n");
  112. //
  113. //              System.out.println(hashTable.getUniqueValues());
  114.  
  115.                 System.out.print("\nPlease input the record's name: ");
  116.                 name = in.nextLine();
  117.                 System.out.print("Please input the record's composer: ");
  118.                 composer = in.nextLine();
  119.                 Song songTemp = new Song(name, composer);
  120.  
  121.                 if (!hashTable.contains(songTemp))
  122.                     System.out.println("Database does not contain the song name and composer");
  123.  
  124.                 else {
  125.                    
  126.                     Song songToUpdate = hashTable.returnSong(songTemp);
  127.  
  128.                     hashTable.remove(songToUpdate);
  129.                     char update;
  130.                     System.out.print("\nWould you like to update the record's name (Y/N):  ");
  131.                     update = updateRecord();
  132.                     if (update == 'Y') {
  133.                         System.out.print("Input the record's updated name: ");
  134.                         name = in.nextLine();
  135.                         songToUpdate.setComposer(name);
  136.                     }
  137.                     System.out.print("\nWould you like to update the record's composer: ");
  138.                     update = updateRecord();
  139.                     if (update == 'Y') {
  140.                         System.out.print("Input the record's updated composer: ");
  141.                         composer = in.nextLine();
  142.                         songToUpdate.setComposer(composer);
  143.                     }
  144.                     System.out.print("\nWould you like to update the record's release year: ");
  145.                     update = updateRecord();
  146.                     if (update == 'Y') {
  147.                         System.out.print("Input the record's updated release year: ");
  148.                         releaseYear = in.nextInt();
  149.                         songToUpdate.setReleaseYear(releaseYear);
  150.                     }
  151.                     System.out.print("\nWould you like to update the record's song length: ");
  152.                     update = updateRecord();
  153.                     if (update == 'Y') {
  154.                         System.out.print("Input the record's updated song length(in seconds): ");
  155.                         songLength = in.nextInt();
  156.                         songToUpdate.setSongLength(songLength);
  157.                     }
  158.                     System.out.print("\nWould you like to update the record's lyrics: ");
  159.                     update = updateRecord();
  160.                     if (update == 'Y') {
  161.                         System.out.print("Input the record's updated lyrics: ");
  162.                         lyrics = in.nextLine();
  163.                         songToUpdate.setComposer(composer);
  164.                     }
  165.                     hashTable.put(songToUpdate);
  166.                     System.out.println("\nYour record has been updated!\n");
  167.                     System.out.println(songToUpdate.toString() + "\n");
  168.                 }
  169.             }
  170.  
  171.             else if (option == 'E') {
  172.                 // option == E
  173.                 try {
  174.                     hashTable.getStatistics();
  175.                 } catch (FileNotFoundException e) {
  176.                     e.printStackTrace();
  177.                 }
  178.             }
  179.            
  180.             printMenu();
  181.             // NO SUCH ELEMENTEXCEPTION B->2->A MOMENT APART->1/2/3
  182.             option = Character.toUpperCase(in.next().charAt(0));
  183.             while (option != 'A' && option != 'B' && option != 'C' && option != 'D' && option != 'E' && option != 'X') {
  184.                 System.out.println("\nInvalid menu option. Please enter A-E or X to exit.\n");
  185.                 printMenu();
  186.                 option = in.next().charAt(0);
  187.             }
  188.         }
  189.         // Records are no longer being added
  190.         String outputFile;
  191.         System.out.println("\nTotal Number of Records: " + hashTable.populateNumElements() + "\n");
  192.         String entireInput = hashTable.toString();
  193.  
  194.         System.out.println("Enter the name of the output file: ");
  195.         outputFile = in.next();
  196.         try {
  197.             FileWriter f = new FileWriter(outputFile, true);
  198.             f.write(entireInput);
  199.             f.close();
  200.         } catch (FileNotFoundException e) {
  201.             System.out.println("An error has occured.");
  202.             e.printStackTrace();
  203.         } catch (IOException e) {
  204.             e.printStackTrace();
  205.         }
  206.         System.out.println("\nGoodbye!");
  207.         in.close();
  208.     }
  209.  
  210.     public static void printMenu() {
  211.         System.out.println("A. Upload a new record");
  212.         System.out.println("B. Delete a record");
  213.         System.out.println("C. Search for a record");
  214.         System.out.println("D. Update a record");
  215.         System.out.println("E. Statistics");
  216.         System.out.println("X. Exit\n");
  217.         System.out.print("Enter your choice: ");
  218.     }
  219.  
  220.     public static char updateRecord() {
  221.         Scanner in = new Scanner(System.in);
  222.         char updateOption = Character.toUpperCase(in.next().charAt(0));
  223.         while ((updateOption != 'Y') && updateOption != 'N') {
  224.             System.out.print("Please input Y or N: ");
  225.             updateOption = Character.toUpperCase(in.next().charAt(0));
  226.         }
  227.         return updateOption;
  228.        
  229.     }
  230.  
  231. }
RAW Paste Data