binibiningtinamoran

MusicDatabase

Nov 6th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.util.stream.Stream;
  4.  
  5. public class MusicDatabase {
  6.  
  7.  
  8.     // TODO: Copy findExact and findContains methods from Ch 9, Part 2: ArrayList Searching here.
  9.  
  10.     public static void listBandSongs(Scanner sc, ArrayList <String> bands,
  11.             ArrayList <ArrayList <String>> songs) {
  12.  
  13.         System.out.print("Enter band: ");
  14.         sc.nextLine();
  15.         String bandName = sc.nextLine();
  16.  
  17.         if (!(bands.contains(bandName))) {
  18.             System.out.println(bandName + " not found in database!\n");
  19.         } else {
  20.             int indexOfBand = bands.indexOf(bandName);
  21.             System.out.printf("%s song list:\n%s\n", bandName, songs.get(indexOfBand).toString());
  22.             System.out.println();
  23.         }
  24.     }
  25.  
  26.     public static void searchBandSongs(Scanner sc, ArrayList <String> bands,
  27.             ArrayList <ArrayList <String>> songs) {
  28.  
  29.         System.out.print("Search for: ");
  30.         sc.nextLine();
  31.         String input = sc.nextLine();
  32.  
  33.         ArrayList <Integer> returnedBands = new ArrayList <>(findContains(input, bands));
  34.         ArrayList <Integer> returnedSongs;
  35.         if (returnedBands.size() > 0) {
  36.             for (Integer b : returnedBands) {
  37.                 String bandMatched = bands.get(b);
  38.                 System.out.println("Band Matched: " + bandMatched);
  39.                 System.out.printf("Songs Matched by %s:\n", bandMatched);
  40.  
  41.  
  42.                 returnedSongs = new ArrayList <>(findContains(input,
  43.                         Config.SONGS.get(b)));
  44.                 for (Integer s : returnedSongs) {
  45.                     System.out.println("\t" + songs.get(b).get(s));
  46.                 }
  47.                 System.out.println();
  48.             }
  49.         } else {
  50.             System.out.println("Band and/or song is not found.\n");
  51.         }
  52.     }
  53.  
  54.     public static void modifyBandSong(Scanner sc, boolean toAdd, ArrayList <String> bands,
  55.             ArrayList <ArrayList <String>> songs) {
  56.         System.out.print("Enter band: ");
  57.         sc.nextLine();
  58.         String band = sc.nextLine();
  59.         ArrayList <Integer> bandMatch = findExact(band, bands);
  60.         if (bandMatch.size() > 0) {
  61.             System.out.println(band + " found in the database!");
  62.             if (!toAdd) {
  63.                 System.out.print("Delete? (y/n) ");
  64.                 if (readChar(sc) == 'y') {
  65.                     bands.remove((int) bandMatch.get(0));
  66.                     songs.remove((int) bandMatch.get(0));
  67.                     System.out.println(band + " deleted.");
  68.                     return;
  69.                 }
  70.             }
  71.             System.out.print("Enter song: ");
  72.             String song = sc.nextLine();
  73.             ArrayList <Integer> songMatch = findExact(song, songs.get(bandMatch.get(0)));
  74.             if (songMatch.size() > 0) {
  75.                 System.out.println("Modifying band: " + band);
  76.                 if (toAdd) {
  77.                     System.out.println("Song, " + song + ", already exists in database!");
  78.                 } else {
  79.                     songs.get(bandMatch.get(0)).remove((int) songMatch.get(0));
  80.                     System.out.println("Song, " + song + ", deleted.");
  81.                 }
  82.             } else {
  83.                 if (toAdd && song.length() > 0) {
  84.                     songs.get(bandMatch.get(0)).add(song);
  85.                     System.out.println("Song, " + song + ", added.");
  86.                 } else {
  87.                     System.out.println("Song, " + song + ", does not exist in database!");
  88.                 }
  89.             }
  90.         } else {
  91.             System.out.println(band + " does not exist in the database!");
  92.             if (toAdd) {
  93.                 System.out.print("Add? (y/n) ");
  94.                 if (readChar(sc) == 'y') {
  95.                     bands.add(band);
  96.                     songs.add(new ArrayList <String>());
  97.                     System.out.println(band + " added.");
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     public static char readChar(Scanner sc) {
  104.         return sc.next().toLowerCase().charAt(0);
  105.     }
  106.  
  107.     public static ArrayList <Integer> findExact(String s, ArrayList <String> arr) {
  108.  
  109.         // returning null if arr is null
  110.         if (arr == null) {
  111.             return null;
  112.         }
  113.  
  114.         // creating an integer array list
  115.         ArrayList <Integer> indices = new ArrayList <Integer>();
  116.  
  117.         // looping through each index in arr
  118.         for (int i = 0; i < arr.size(); i++) {
  119.             // if both s and arr.get(i) are null, adding index to list
  120.             if (s == null && arr.get(i) == null) {
  121.                 indices.add(i);
  122.             }
  123.             // otherwise if both are not null and are equal, adding to list
  124.             else if (s != null && arr.get(i) != null && s.equals(arr.get(i))) {
  125.                 indices.add(i);
  126.             }
  127.         }
  128.  
  129.         return indices;
  130.     }
  131.  
  132.     public static ArrayList <Integer> findContains(String s, ArrayList <String> arr) {
  133.  
  134.         // returning null if arr is null
  135.         if (arr == null) {
  136.             return null;
  137.         }
  138.  
  139.         // ceating an integer array list
  140.         ArrayList <Integer> indices = new ArrayList <Integer>();
  141.  
  142.         // looping through each index in arr
  143.         for (int i = 0; i < arr.size(); i++) {
  144.             // if both s and arr.get(i) are null, adding index to list
  145.             if (s == null && arr.get(i) == null) {
  146.                 indices.add(i);
  147.             }
  148.  
  149.             // otherwise if both are not null and arr.get(i) contains s, adding
  150.             // to list
  151.             else if (s != null && arr.get(i) != null && arr.get(i).contains(s)) {
  152.                 indices.add(i);
  153.             }
  154.         }
  155.         return indices;
  156.     }
  157.  
  158.     public static void displayMenu() {
  159.         System.out.print("(A)dd a band and/or a song.\n" +
  160.                 "(D)elete a band and/or a song.\n" +
  161.                 "(B)ands list.\n" +
  162.                 "(L)ist a band's song list.\n" +
  163.                 "(S)earch for a band and/or a song.\n" +
  164.                 "(?) Display this full menu.\n" +
  165.                 "(Q)uit.\n" +
  166.                 "Action (? for full menu): ");
  167.     }
  168.  
  169.     public static void main(String[] args) {
  170.  
  171.         Scanner scan = new Scanner(System.in);
  172.  
  173.         StringBuilder asterisks = new StringBuilder(80);
  174.         Stream.generate(() -> '*').limit(80).forEach(asterisks::append);
  175.  
  176.         System.out.println(asterisks);
  177.         System.out.println("Welcome to the music database!");
  178.         System.out.println(asterisks);
  179.         System.out.println();
  180.  
  181.         char userChoice;
  182.  
  183.         do {
  184.             displayMenu();
  185.             //userChoice = scan.next().toLowerCase().charAt(0);
  186.             userChoice = readChar(scan);
  187.  
  188.             if (userChoice == 'a') {
  189.                 modifyBandSong(scan, true, Config.BANDS, Config.SONGS);
  190.             } else if (userChoice == 'd') {
  191.                 modifyBandSong(scan, false, Config.BANDS, Config.SONGS);
  192.             } else if (userChoice == 'b') {
  193.                 System.out.println("Bands: " + Config.BANDS.toString());
  194.             } else if (userChoice == 'l') {
  195.                 listBandSongs(scan, Config.BANDS, Config.SONGS);
  196.             } else if (userChoice == 's') {
  197.                 searchBandSongs(scan, Config.BANDS, Config.SONGS);
  198.             } else if (userChoice == '?') {
  199.                 displayMenu();
  200.             } else {
  201.                 System.out.println("Invalid choice made.\n");
  202.             }
  203.         } while (userChoice != 'q');
  204.     } // end main
  205.  
  206. } // end class
Advertisement
Add Comment
Please, Sign In to add comment