Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. public class CDDriver
  2. {
  3.  
  4. public static void main(String[] args) throws IOException
  5. {
  6. final int MAX_ARRAY_SIZE = 50;
  7. final String FILENAME = "Collection.txt";
  8.  
  9. int count = 0; // Counter to keep track of number of elements in the array
  10. int choice = 0; // Menu choice
  11.  
  12. // Create array to hold song collection
  13. Song [] mySongArray = new Song [MAX_ARRAY_SIZE];
  14. String [] myArray = {"Add song","Display songs in file", "Quit"};
  15. // Create TextMenu object for menu (this may involve writing multiple lines of code)
  16. TextMenu myTextMenu = new TextMenu(myArray);
  17.  
  18.  
  19. // Read the data from the input file into the array
  20. // Count the elements currently in the array
  21. try
  22. {
  23. Scanner input = new Scanner(new File(FILENAME));
  24.  
  25. while (input.hasNextLine())
  26. {
  27.  
  28. String song1 = input.nextLine();
  29.  
  30. String artist1 = input.nextLine();
  31.  
  32. Song mySong = new Song(song1, artist1);
  33. mySongArray[count] = mySong;
  34. count++;
  35. }
  36. input.close();
  37. }
  38. catch (FileNotFoundException e)
  39. {
  40. System.out.println("Could not find data.txt file");
  41. System.exit(1);
  42. }
  43.  
  44. // Get the menu choice
  45. Scanner scan = new Scanner(System.in);
  46. choice = myTextMenu.getChoice(scan);
  47.  
  48.  
  49. while (choice != 3)
  50. {
  51. switch (choice)
  52. {
  53. case 1:
  54. // Read data for new song to add to the collection from the keyboard
  55. String mySongName;
  56. String artist2;
  57.  
  58. System.out.println("Enter a song of your choice ");
  59. scan.nextLine();
  60. mySongName = scan.nextLine();
  61.  
  62. System.out.println("Enter the artist ");
  63. artist2 = scan.nextLine();
  64.  
  65. Song mySong2 = new Song(mySongName, artist2);
  66. mySongArray[count] = mySong2;
  67. count++;
  68.  
  69.  
  70. break;
  71. case 2:
  72. // Print the list
  73. for (int i =0; i<count; i++)
  74. System.out.println(mySongArray[i].toString());
  75. break;
  76. default:
  77. System.out.println("Invalid menu choice. Please try again.");
  78. }
  79. // Get the menu choice
  80. choice = myTextMenu.getChoice(scan);
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement