Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CDDriver
- {
- public static void main(String[] args) throws IOException
- {
- final int MAX_ARRAY_SIZE = 50;
- final String FILENAME = "Collection.txt";
- int count = 0; // Counter to keep track of number of elements in the array
- int choice = 0; // Menu choice
- // Create array to hold song collection
- Song [] mySongArray = new Song [MAX_ARRAY_SIZE];
- String [] myArray = {"Add song","Display songs in file", "Quit"};
- // Create TextMenu object for menu (this may involve writing multiple lines of code)
- TextMenu myTextMenu = new TextMenu(myArray);
- // Read the data from the input file into the array
- // Count the elements currently in the array
- try
- {
- Scanner input = new Scanner(new File(FILENAME));
- while (input.hasNextLine())
- {
- String song1 = input.nextLine();
- String artist1 = input.nextLine();
- Song mySong = new Song(song1, artist1);
- mySongArray[count] = mySong;
- count++;
- }
- input.close();
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Could not find data.txt file");
- System.exit(1);
- }
- // Get the menu choice
- Scanner scan = new Scanner(System.in);
- choice = myTextMenu.getChoice(scan);
- while (choice != 3)
- {
- switch (choice)
- {
- case 1:
- // Read data for new song to add to the collection from the keyboard
- String mySongName;
- String artist2;
- System.out.println("Enter a song of your choice ");
- scan.nextLine();
- mySongName = scan.nextLine();
- System.out.println("Enter the artist ");
- artist2 = scan.nextLine();
- Song mySong2 = new Song(mySongName, artist2);
- mySongArray[count] = mySong2;
- count++;
- break;
- case 2:
- // Print the list
- for (int i =0; i<count; i++)
- System.out.println(mySongArray[i].toString());
- break;
- default:
- System.out.println("Invalid menu choice. Please try again.");
- }
- // Get the menu choice
- choice = myTextMenu.getChoice(scan);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement