Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
2,783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.91 KB | None | 0 0
  1. package tools;
  2.  
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5.  
  6.  
  7. public class Driverclass {
  8.  
  9.     @SuppressWarnings("resource")
  10.     public static void main(String[] args) {
  11.         //Total of 4 classes, driver class and 3 others
  12.         //one constructor at each class, 1 class with multiple constructors
  13.         //all classes have getters and setters, and at least one method with a parameter
  14.         //at least 1 method who returns something
  15.         //at least 1 array with objects
  16.         //at least 1 loop , and use of if statements
  17.         //input by the user
  18.         //error processing exception at least once
  19.         //program should enable the user to choose things
  20.         //your classes use private properties - getters and setters
  21.        
  22.         String tempName;
  23.         String tempPassword;
  24.         String tempEmail;
  25.         Playlist tempPlaylist;
  26.         Musicplayer player = new Musicplayer();
  27.         ArrayList<Playlist> playlists = new ArrayList<Playlist>();
  28.        
  29.        
  30.         System.out.println("Welcome to Spotify");
  31.         System.out.println("What would you like to do today?");
  32.        
  33.         while(true) {
  34.  
  35.             //Main menu//
  36.             Scanner numberInput = new Scanner(System.in);
  37.             int number;
  38.             System.out.println("\nCreate an account = \t \t 1 \nCheck playlists = \t \t 2 \nAccess the music player =  \t 3 \nQuit program = \t \t \t 0 ");
  39.             number = numberInput.nextInt();
  40.            
  41.             //---------------------------------------------------------------------//
  42.                
  43.                 //This exits the program.
  44.                 if (number == 0) {
  45.                     System.out.println("You've chosen to exit the program");
  46.                     System.out.println("Thanks for using Spotify, hope to see you soon");
  47.                     return;
  48.                 }
  49.                 //------------------------------------------------------------------//
  50.            
  51.                 //This menu lets you create accounts or see premade accounts//
  52.                 if (number == 1){
  53.                     System.out.println("There are no created accounts yet, would you like to create one?");
  54.                    
  55.                     Scanner scanner = new Scanner(System.in);
  56.                     String input = scanner.next();
  57.                     switch(input) {
  58.                         case "yes":
  59.                                 System.out.println("Creating your account...");
  60.                                 Scanner in = new Scanner(System.in);
  61.                                 System.out.println("Create a username");
  62.                                 tempName = in.next();
  63.                                 System.out.println("Create a password");
  64.                                 tempPassword = in.next();
  65.                                 System.out.println("Enter your Email");
  66.                                 tempEmail = in.next();
  67.                                 Account a1 = new Account(tempName, tempPassword, tempEmail);
  68.                                 System.out.println("Thank you for registering, here are your account details:" + "\n" + a1);
  69.                             break;
  70.                         case "no":
  71.                             //return to main menu?
  72.                             break;
  73.                     }
  74.                    
  75.                 }
  76.                 //------------------------------------------------------------------//
  77.                
  78.                 //This menu brings you to the playlists.
  79.                 if (number == 2){
  80.                    
  81.                     if (playlists.size() == 0) {
  82.                         System.out.println("There are no playlists available");
  83.                         System.out.println("Would you like to create a playlist?");
  84.                     }else {
  85.                         for(int i = 0; i < playlists.size(); i++) {
  86.                             System.out.println(playlists.get(i));
  87.                         }
  88.                         System.out.println("Would you like to create a new playlist?");
  89.                     }
  90.                     Scanner scanner = new Scanner(System.in);
  91.                     String input = scanner.next();
  92.                     switch(input) {
  93.                         case "yes":
  94.                                 System.out.println("Creating a playlist...");
  95.                                 Scanner in = new Scanner(System.in);
  96.                                 System.out.println("Enter a name for your playlist");
  97.                                 tempPlaylist = new Playlist(in.next());
  98.                                
  99.                                 for (int i = 0; i < 2; i++) {
  100.                                     System.out.println("Enter a song name");
  101.                                     tempPlaylist.addSong(in.next());
  102.                                 }
  103.                                 playlists.add(tempPlaylist);
  104.                                 System.out.println("Playlist has been added");
  105.                                 System.out.println(tempPlaylist);
  106.                             break;
  107.                         case "no":
  108.  
  109.                             break;
  110.                     }
  111.                 }
  112.                 //------------------------------------------------------------------//
  113.                
  114.                 //This menu brings you to the music player.
  115.                 if (number == 3) {
  116.                     System.out.println("Welcome to the music player \n type 'play' to start playing \n "
  117.                             + "type 'insert to add a playlist to play \n type 0 to return to main menu");
  118.        
  119.                     Scanner scanner = new Scanner(System.in);
  120.                     String input = null;
  121.                     boolean inProgress = true;
  122.                     while(inProgress) {
  123.                         input = scanner.next();
  124.                         switch(input) {
  125.                             case "insert":
  126.                                 System.out.println("Please select the index of the playlist you want to insert");
  127.                                 for(int i = 0; i < playlists.size(); i++) {
  128.                                     System.out.println("index " + i + ": " + playlists.get(i));
  129.                                 }
  130.                                 int selectedIndex = Integer.valueOf(scanner.next());
  131.                                 try {
  132.                                     player.setPlaylist(playlists.get(selectedIndex));
  133.                                     System.out.println("Inserted playlist: " + playlists.get(selectedIndex));
  134.                                 }catch (java.lang.IndexOutOfBoundsException e) {
  135.                                     System.out.println("There is no playlist, please create one first");
  136.                                     break;
  137.                                 }
  138.                                
  139.                                 break;
  140.                             case "play":
  141.                                 try {
  142.                                     player.playMusic();
  143.                                     System.out.println("Now playing..." + player.getPlaylist());
  144.                                 }catch (NoPlaylistException e) {
  145.                                     System.out.println("Please insert a playlist first");
  146.                                 }
  147.                                 break;
  148.                             case "0":
  149.                                 inProgress = false;
  150.                                 break;
  151.                         }
  152.                     }
  153.                 }
  154.                
  155.                 //------------------------------------------------------------------//
  156.                
  157.             }
  158.         }
  159.     }
  160.  
  161. --------------------------------------------------------------------------------------------------------------------------------
  162. package tools;
  163.  
  164. public class Account {
  165.  
  166.     //lets the user create an account, multiple times and check already made accounts
  167.    
  168.     private String name;
  169.     private String password;
  170.     private String email;
  171.    
  172.     public Account (String name, String password, String email) {
  173.         this.name = name;
  174.         this.password = password;
  175.         this.email = email;
  176.     }
  177.    
  178.     public Account () {
  179.        
  180.     }
  181.    
  182.     public String getName() {
  183.         return name;
  184.     }
  185.    
  186.     public void setName(String name) {
  187.         this.name = name;
  188.     }
  189.  
  190.     public String getPassword() {
  191.         return password;
  192.     }
  193.    
  194.     public void setPassword(String password) {
  195.         this.password = password;
  196.     }
  197.    
  198.     public String getEmail() {
  199.         return email;
  200.     }
  201.  
  202.     public void setEmail(String email) {
  203.         this.email = email;
  204.     }
  205.    
  206.     public String toString() {
  207.         return  "Your account name" + "\t" + "\t: " + name + "\n" +
  208.                 "Your account password" + "\t" + "\t: "  + password +  "\n" +
  209.                 "Your account email" + "\t" + "\t: "  + email +  "\n";
  210.     }
  211. }
  212.  
  213. ------------------------------------------------------------------------------------------------------------------------
  214. package tools;
  215.  
  216. import java.util.ArrayList;
  217.  
  218.  
  219. public class Playlist {
  220.    
  221.     //has an array with pre-made playlists and an option to create your own array with playlists
  222.  
  223.     private String name;
  224.     private ArrayList<String> songs;
  225.    
  226.     public Playlist (String name) {
  227.         this.name = name;
  228.         this.songs = new ArrayList<String>();
  229.     }
  230.    
  231.     public String getName() {
  232.         return name;
  233.     }
  234.    
  235.     public void setName(String name) {
  236.         this.name = name;
  237.     }
  238.    
  239.     public void addSong(String name) {
  240.         this.songs.add(name);
  241.     }
  242.    
  243.     public void removeSong(String name) {
  244.         this.songs.remove(name);
  245.     }
  246.    
  247.     public String toString() {
  248.         return this.name + ": " + songs.toString();
  249.     }
  250. }
  251.  
  252. ---------------------------------------------------------------------------------------------------------------
  253. package tools;
  254.  
  255. public class Musicplayer {
  256.  
  257.     //this lets you see a random number that is playing, and you can pause it or play again.
  258.     Playlist playlist;
  259.     boolean playing;
  260.    
  261.     public Musicplayer() {
  262.        
  263.     }
  264.    
  265.     public void playMusic() throws NoPlaylistException {
  266.         if(playlist == null) {
  267.             throw new NoPlaylistException("Insert playlist first");
  268.         }
  269.         playing = true;
  270.     }
  271.    
  272.     public void stopMusic() {
  273.         playing = false;
  274.     }
  275.    
  276.     public Playlist getPlaylist() {
  277.         return playlist;
  278.     }
  279.    
  280.     public void setPlaylist(Playlist playlist) {
  281.         this.playlist = playlist;
  282.     }
  283.    
  284.     public boolean getPlayMusic() {
  285.         return playing;
  286.     }
  287.    
  288. }
  289.  
  290. class NoPlaylistException extends Exception {
  291.  
  292.     NoPlaylistException(String s) {
  293.         super(s);
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement