haithienht

JAVA_CD_Main

Nov 13th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. import java.util.Scanner;
  8.  
  9. /**
  10.  *
  11.  * @author Thien
  12.  */
  13. public class Main {
  14.     // Khai báo  các biến để chút nữa dùng
  15.     int maxCD,  nextCD=0; // biến khai báo để chạy vòng lặp cho mảng CD
  16.     CDData[] CD; // tạo mảng chứa dữ liệu CD
  17.     CDData objCD; // tạo obj để gọi các class bên CD.java
  18.     Scanner input; // biến để nhập dữ liệu
  19.    
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         // Khai báo các biến cần thiết
  26.         int choice; // biến lựa chọn
  27.         Scanner input = new Scanner(System.in); // biến để nhập dữ liệu
  28.         // khởi tạo obj Main
  29.         Main objM = new Main();
  30.        
  31.         // cho vào vòng lặp để nó tự chạy lại khi xong lệnh
  32.         while(true){
  33.             // Gọi Menu
  34.             System.out.println("");
  35.             System.out.println("++++++++++++++++++++++++++++++");
  36.             System.out.println("+     CD Catalog Manager     +");
  37.             System.out.println("++++++++++++++++++++++++++++++");
  38.             System.out.println(" 1. Add CD to the catalog");
  39.             System.out.println(" 2. Search CD by CD title");
  40.             System.out.println(" 3. Display the catalog");
  41.             System.out.println(" 4. Exit");
  42.             System.out.println("");
  43.  
  44.             // Nhập choice
  45.             System.out.print("Please enter your choice(1-4): ");
  46.             choice = input.nextInt();
  47.            
  48.             switch(choice){
  49.                 case 1:
  50.                     objM.addCD();
  51.                     break;
  52.                 case 2:
  53.                     objM.searchCD();
  54.                     break;
  55.                 case 3:
  56.                     objM.showCD();
  57.                     break;
  58.                 case 4:
  59.                     System.out.println("Program exit...");
  60.                     System.exit(0);
  61.                 default:
  62.                     System.out.println("Wrong choice!");
  63.                     break;
  64.             }
  65.            
  66.         }
  67.        
  68.     }
  69.    
  70.     public Main(){
  71.         // ở đây khai báo maxCD để tạo mảng, cho người dùng nhập hoặc tự mình cho đều được
  72.         input = new Scanner(System.in);
  73.         System.out.print("Enter maximum number of CDs: ");
  74.         maxCD = input.nextInt();
  75.         CD = new CDData[maxCD]; // tạo mảng chứa dữ liệu của CD với max size = biến maxCD
  76.     }
  77.    
  78.    
  79.     public void addCD(){
  80.         // check coi list có bị full chưa
  81.         if(nextCD==maxCD){
  82.             System.out.println("List is full");
  83.             return; // nếu đầy thì thoát khỏi function này
  84.         }
  85.        
  86.         // nếu chưa thì bắt đầu tạo mới
  87.         objCD = new CDData(); // gọi CDData đã tạo bên kia
  88.         CD[nextCD++] = objCD; // sao chép dữ liệu của bên kia thành dữ liệu của Main bên đây
  89.         //nextCD++;   //Nếu kg thích ++ ở trên thì cho xuống đây cũng đc, nhớ xóa ++ ở trên
  90.         System.out.println("Sucessfully added!");
  91.     }
  92.    
  93.    
  94.     public void searchCD(){
  95.         input = new Scanner(System.in);
  96.         String in;
  97.         boolean check=false;  // tạo biến để check nếu kiếm đc dữ liệu hay kg
  98.         //check coi list có dữ liệu hay chưa, nếu chưa thì thoát khỏi function này
  99.         if(nextCD==0){
  100.             System.out.println("List is empty");
  101.             return;
  102.         }
  103.        
  104.         //Cho nhập tựa cần tìm
  105.         System.out.print("Enter title you want to search: ");
  106.         in = input.nextLine();
  107.        
  108.         for (int i = 0; i < nextCD; i++) {
  109.             if (CD[i].title.equals(in)) {
  110.                 System.out.println(CD[i].showDetail());
  111.                 check = true; // đã thấy dữ liệu nên cho check = true
  112.             }
  113.         }
  114.        
  115.         // check = false nghĩa là kg kiếm đc dữ liệu nên đưa ra thông báo
  116.         if (!check) {
  117.             System.out.println("Cannot find CD: "+in);
  118.         }
  119.     }
  120.    
  121.    
  122.     public void showCD(){
  123.         //check coi list có dữ liệu hay chưa, nếu chưa thì thoát khỏi function này, giống searchCD
  124.         if(nextCD==0){
  125.             System.out.println("Nothing to show!");
  126.             return;
  127.         }
  128.         // nếu có dữ liệu thì in ra tất cả
  129.         for (int i = 0; i < nextCD; i++) {
  130.             System.out.println(CD[i].showDetail());
  131.         }
  132.     }
  133.    
  134. }
Add Comment
Please, Sign In to add comment