Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.stream.Stream;
- public class MusicDatabase {
- // TODO: Copy findExact and findContains methods from Ch 9, Part 2: ArrayList Searching here.
- public static void listBandSongs(Scanner sc, ArrayList <String> bands,
- ArrayList <ArrayList <String>> songs) {
- System.out.print("Enter band: ");
- sc.nextLine();
- String bandName = sc.nextLine();
- if (!(bands.contains(bandName))) {
- System.out.println(bandName + " not found in database!\n");
- } else {
- int indexOfBand = bands.indexOf(bandName);
- System.out.printf("%s song list:\n%s\n", bandName, songs.get(indexOfBand).toString());
- System.out.println();
- }
- }
- public static void searchBandSongs(Scanner sc, ArrayList <String> bands,
- ArrayList <ArrayList <String>> songs) {
- System.out.print("Search for: ");
- sc.nextLine();
- String input = sc.nextLine();
- ArrayList <Integer> returnedBands = new ArrayList <>(findContains(input, bands));
- ArrayList <Integer> returnedSongs;
- if (returnedBands.size() > 0) {
- for (Integer b : returnedBands) {
- String bandMatched = bands.get(b);
- System.out.println("Band Matched: " + bandMatched);
- System.out.printf("Songs Matched by %s:\n", bandMatched);
- returnedSongs = new ArrayList <>(findContains(input,
- Config.SONGS.get(b)));
- for (Integer s : returnedSongs) {
- System.out.println("\t" + songs.get(b).get(s));
- }
- System.out.println();
- }
- } else {
- System.out.println("Band and/or song is not found.\n");
- }
- }
- public static void modifyBandSong(Scanner sc, boolean toAdd, ArrayList <String> bands,
- ArrayList <ArrayList <String>> songs) {
- System.out.print("Enter band: ");
- sc.nextLine();
- String band = sc.nextLine();
- ArrayList <Integer> bandMatch = findExact(band, bands);
- if (bandMatch.size() > 0) {
- System.out.println(band + " found in the database!");
- if (!toAdd) {
- System.out.print("Delete? (y/n) ");
- if (readChar(sc) == 'y') {
- bands.remove((int) bandMatch.get(0));
- songs.remove((int) bandMatch.get(0));
- System.out.println(band + " deleted.");
- return;
- }
- }
- System.out.print("Enter song: ");
- String song = sc.nextLine();
- ArrayList <Integer> songMatch = findExact(song, songs.get(bandMatch.get(0)));
- if (songMatch.size() > 0) {
- System.out.println("Modifying band: " + band);
- if (toAdd) {
- System.out.println("Song, " + song + ", already exists in database!");
- } else {
- songs.get(bandMatch.get(0)).remove((int) songMatch.get(0));
- System.out.println("Song, " + song + ", deleted.");
- }
- } else {
- if (toAdd && song.length() > 0) {
- songs.get(bandMatch.get(0)).add(song);
- System.out.println("Song, " + song + ", added.");
- } else {
- System.out.println("Song, " + song + ", does not exist in database!");
- }
- }
- } else {
- System.out.println(band + " does not exist in the database!");
- if (toAdd) {
- System.out.print("Add? (y/n) ");
- if (readChar(sc) == 'y') {
- bands.add(band);
- songs.add(new ArrayList <String>());
- System.out.println(band + " added.");
- }
- }
- }
- }
- public static char readChar(Scanner sc) {
- return sc.next().toLowerCase().charAt(0);
- }
- public static ArrayList <Integer> findExact(String s, ArrayList <String> arr) {
- // returning null if arr is null
- if (arr == null) {
- return null;
- }
- // creating an integer array list
- ArrayList <Integer> indices = new ArrayList <Integer>();
- // looping through each index in arr
- for (int i = 0; i < arr.size(); i++) {
- // if both s and arr.get(i) are null, adding index to list
- if (s == null && arr.get(i) == null) {
- indices.add(i);
- }
- // otherwise if both are not null and are equal, adding to list
- else if (s != null && arr.get(i) != null && s.equals(arr.get(i))) {
- indices.add(i);
- }
- }
- return indices;
- }
- public static ArrayList <Integer> findContains(String s, ArrayList <String> arr) {
- // returning null if arr is null
- if (arr == null) {
- return null;
- }
- // ceating an integer array list
- ArrayList <Integer> indices = new ArrayList <Integer>();
- // looping through each index in arr
- for (int i = 0; i < arr.size(); i++) {
- // if both s and arr.get(i) are null, adding index to list
- if (s == null && arr.get(i) == null) {
- indices.add(i);
- }
- // otherwise if both are not null and arr.get(i) contains s, adding
- // to list
- else if (s != null && arr.get(i) != null && arr.get(i).contains(s)) {
- indices.add(i);
- }
- }
- return indices;
- }
- public static void displayMenu() {
- System.out.print("(A)dd a band and/or a song.\n" +
- "(D)elete a band and/or a song.\n" +
- "(B)ands list.\n" +
- "(L)ist a band's song list.\n" +
- "(S)earch for a band and/or a song.\n" +
- "(?) Display this full menu.\n" +
- "(Q)uit.\n" +
- "Action (? for full menu): ");
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- StringBuilder asterisks = new StringBuilder(80);
- Stream.generate(() -> '*').limit(80).forEach(asterisks::append);
- System.out.println(asterisks);
- System.out.println("Welcome to the music database!");
- System.out.println(asterisks);
- System.out.println();
- char userChoice;
- do {
- displayMenu();
- //userChoice = scan.next().toLowerCase().charAt(0);
- userChoice = readChar(scan);
- if (userChoice == 'a') {
- modifyBandSong(scan, true, Config.BANDS, Config.SONGS);
- } else if (userChoice == 'd') {
- modifyBandSong(scan, false, Config.BANDS, Config.SONGS);
- } else if (userChoice == 'b') {
- System.out.println("Bands: " + Config.BANDS.toString());
- } else if (userChoice == 'l') {
- listBandSongs(scan, Config.BANDS, Config.SONGS);
- } else if (userChoice == 's') {
- searchBandSongs(scan, Config.BANDS, Config.SONGS);
- } else if (userChoice == '?') {
- displayMenu();
- } else {
- System.out.println("Invalid choice made.\n");
- }
- } while (userChoice != 'q');
- } // end main
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment