Advertisement
maha_kaal

JTUNES - ALL

May 13th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. /** JTUNE MAIN **/
  2. import MyTools.*;
  3.  
  4. public class JTunes {
  5.     public static void benvenuto(){
  6.         MyTools.printerF("Benvenuto in JTunes. \nQuesto programma gestirà tutto il tuo archivio di CD.");
  7.     }
  8.    
  9.     public static void main(String [] arg){
  10.         benvenuto();
  11.         Archivio archivio = new Archivio();
  12.         String [] voci = {"1- Inserire nuovo CD", "2- Visualizza CD", "3- Eliminna CD", "4- Brano a Caso"};
  13.         boolean bool = true;
  14.         MyMenu menu = new MyMenu("Menu Principale", voci);
  15.         int risp;
  16.         while(bool){
  17.             risp = menu.scegli();
  18.             switch (risp){
  19.             case 0:
  20.                 bool = false;
  21.                 break;
  22.             case 1:
  23.                 archivio.inserisciNuovoCD();
  24.                 break;
  25.             case 2:
  26.                 archivio.visualizza();
  27.                 break;
  28.             case 3:
  29.                 archivio.rimuovi();
  30.                 break;
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. /** MyMenu **/
  37. package MyTools;
  38. /*
  39. Questa classe rappresenta un menu testuale generico a piu' voci
  40. Si suppone che la voce per uscire sia sempre associata alla scelta 0
  41. e sia presentata in fondo al menu
  42.  
  43. */
  44. public class MyMenu
  45. {
  46.   final private static String CORNICE = "--------------------------------";
  47.   final private static String VOCE_USCITA = "0\tEsci";
  48.   final private static String RICHIESTA_INSERIMENTO = "Digita il numero dell'opzione desiderata > ";
  49.  
  50.   private String titolo;
  51.   private String [] voci;
  52.  
  53.    
  54.   public MyMenu (String titolo, String [] voci)
  55.   {
  56.     this.titolo = titolo;
  57.     this.voci = voci;
  58.   }
  59.  
  60.   public int scegli ()
  61.   {
  62.     stampaMenu();
  63.     return MyTools.getIntegerMM(RICHIESTA_INSERIMENTO, 0, voci.length);  
  64.   }
  65.        
  66.   public void stampaMenu ()
  67.   {
  68.     System.out.println(CORNICE);
  69.     System.out.println(titolo);
  70.     System.out.println(CORNICE);
  71.     for (int i=0; i<voci.length; i++)
  72.      {
  73.       System.out.println( (i+1) + "\t" + voci[i]);
  74.      }
  75.     System.out.println();
  76.     System.out.println(VOCE_USCITA);
  77.     System.out.println();
  78.   }
  79.        
  80. }
  81.  
  82. /** MyTools **/
  83. package MyTools;
  84. import java.util.*;
  85.  
  86. public class MyTools {
  87.    
  88.     public static void printerF(String msg){
  89.         System.out.println(msg);
  90.     }
  91.    
  92.     public static int getInteger(String msg){
  93.         printerF(msg);
  94.         Scanner reader = new Scanner(System.in);
  95.         int result = reader.nextInt();
  96.         return result;
  97.     }
  98.    
  99.     public static double getDouble(String msg){
  100.         printerF(msg);
  101.         Scanner reader = new Scanner(System.in);
  102.         double result = reader.nextDouble();
  103.        
  104.         return result;
  105.     }
  106.    
  107.     public static String getString(String msg){
  108.         printerF(msg);
  109.         Scanner reader = new Scanner(System.in);
  110.         String result = reader.nextLine();
  111.        
  112.         return result;
  113.     }
  114.    
  115.     public static char getChar(String msg){
  116.         printerF(msg);
  117.         Scanner reader = new Scanner(System.in);
  118.         char result = reader.nextLine().charAt(0);
  119.        
  120.         return result;
  121.     }
  122.    
  123.     public static int getRandom(){
  124.         Random rand = new Random();
  125.         int num = rand.nextInt();
  126.        
  127.         return num;
  128.     }
  129.    
  130.     public static int getRandom(int range){
  131.         Random rand = new Random();
  132.         int num = rand.nextInt(range) +1;
  133.        
  134.         return num;
  135.     }
  136.    
  137.     public static boolean confirm(String msg){
  138.         printerF(msg);
  139.         Scanner reader = new Scanner(System.in);
  140.         String answ = reader.nextLine().toUpperCase();
  141.        
  142.         return(answ.equalsIgnoreCase("SI") || answ.charAt(0) == 'S');
  143.     }
  144.    
  145.       public static int readMinInteger(String messaggio, int minimo)
  146.       {
  147.        boolean finito = false;
  148.        int valoreLetto = 0;
  149.        do
  150.         {
  151.          valoreLetto = getInteger(messaggio);
  152.          if (valoreLetto >= minimo)
  153.           finito = true;
  154.          else
  155.           System.out.println("Il valore deve essere maggiore di " + minimo);
  156.         } while (!finito);
  157.          
  158.        return valoreLetto;
  159.       }
  160.      
  161.       public static int getIntegerMM(String msg, int min, int max){
  162.           boolean done = false;
  163.           int readValue = 0;
  164.           do {
  165.               readValue = getInteger(msg);
  166.               if(readValue >= min && readValue <= max){
  167.                   done = true;
  168.               } else if(readValue < min){
  169.                   printerF("Il valore è minore del minimo");
  170.               } else {
  171.                   printerF("Il valore è maggiore del massimo");
  172.               }
  173.           } while(done);
  174.          
  175.           return readValue;  
  176.       }
  177.      
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement