[Java] Viết chương trình theo hướng đối tượng để quản lý Sách!

1-Xây dung Class Sach gồm các thuộc tính:Masach,Tensach,Loaisach,Soluong,NXB,DGia
2-Yêu cầu :
-Nhập dữ lieu từ bàn phím
-Hiển thị DS vừa nhập
-Đếm số sách 'Văn Học'
-Đưa ra danh sách sách của nhà xuất bản ' Lao Động'
-Tính tổng tiền phải trả của các loại sách
-Sắp xếp Sách theo đơn giá tăn dần

Code:


package de2;
//Code Design by Tạ Văn Anh
import java.util.Scanner;

public class Sach {
    String Masach,Tensach,Loaisach, NXB;
    int Soluong,n;
    float Dongia;
  
public String imput(){
             String x="";
        try
            {  
                Scanner nhap= new Scanner(System.in);
                x=nhap.nextLine();
             
            }
        catch( Exception e){System.out.print("loi do:\n"+e.getMessage());}
        return x;
     
    }
public void view(){
try{
   
System.out.println("Ma sach: "+Masach+" |Ten Sach: "+Tensach+" | Loai sach: "+Loaisach+" | Nha Xuat Ban: "+NXB+" |So Luong "+Soluong+" |Don gia: "+Dongia+"");

}catch(Exception e) {System.out.print("loi do: "+e.getMessage());
        }
}
   public void nhapdanhsach(){
    try{
        System.out.print("Ma Sach:");
        Masach=imput();
        System.out.print("Ten Sach:");
        Tensach=imput();
            System.out.print("Loai Sach:");
            Loaisach=imput();
            System.out.print("Nha Xuat Ban:");
            NXB=imput();
                System.out.print("So Luong:");
                Soluong= Integer.parseInt(imput());
                System.out.print("Don Gia:");
                Dongia= Float.parseFloat(imput());



       }
   catch(Exception e){System.out.print("loi do:\n"+e.getMessage());}
}
 
    public Sach[] Sach = new Sach[100];

    public void danhsachsach() {
  try{
      System.out.print("\n Nhap vao so luong sach:");
      n= Integer.parseInt(imput());
      for(int i=0;i<n;i++){
          Sach[i]=new Sach();
      System.out.println("Nhap thong tin cho cuon sach thu:"+" "+(i+1));
      Sach[i].nhapdanhsach();
      }
 
    }
  catch(Exception e){System.out.print("loi do:\n"+e.getMessage());}
}
public void hienthiDSSACH(){
   try {
       System.out.print(" DANH SACH SACH LA:\n");
            for(int i=0;i<n;i++){
                                Sach[i].view();
                            }
       }
  catch(Exception e ){System.out.print("loi do:"+e.getMessage());}
 }
//dem xem co bao nhieu sach van hoc
public void demsach(){
    try{
        int dem=0;
        for( int i=0;i<n;i++){
                if(Sach[i].Loaisach.trim().equalsIgnoreCase("Van hoc")){
                    dem+=1;
                }
            }
        System.out.print(" So luong sach van hoc la:"+dem);
       } catch( Exception e){System.out.print("loi do:\n"+e.getMessage());
    }
}
//dua ra danh sach Sach cua NXB Lao Động
public void DStheoNXB(){
    try{
        for( int i=0;i<n;i++){
            if(Sach[i].NXB.trim().equalsIgnoreCase("Lao dong")){
                Sach[i].view();
            }
                 
        }
       
    }catch(Exception e){System.out.print("loi do:"+e.getMessage());
    }
}
public void Tongtien(){
     float tong=0;
    try{
      
        for(int i=0;i<n;i++){
            tong+=Sach[i].Soluong*Sach[i].Dongia;
        }
       
    }catch(Exception e){System.out.print("loi do:"+e.getMessage());
    } 
    System.out.print(" Tong so tien phai tra cua cac loai sach la:"+tong);
   
}
public void sapxepSach(){
   
    try {
           
          Sach tg= new Sach();
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(Sach[i].Dongia >= Sach[j].Dongia) 
                    {                          
                        tg=Sach[i];
                        Sach[i]=Sach[j];
                        Sach[j]=tg;
                    }
                System.out.println("DANH SACH SAP XEP TANG DAN THEO DON GIA LA:\n");
                hienthiDSSACH();
                      
        }
    }
       
    }catch(Exception e){System.out.print("loi do:"+e.getMessage());
    }
           
}
public static void main(String[] args) {
   
    try{
        Sach Sach = new Sach();
        while(true)
        {

            System.out.println("\n\t+-------------- Code Design by Tạ Văn Anh-------------- ");
            System.out.print(" \n Nhap danh sach sach, Chon: 1");
            System.out.print(" \n Hien Thi danh sach sach vua nhap,Chon: 2");
            System.out.print(" \n Dem xem co bao nhieu loai sach 'Van Hoc', Chon: 3");
            System.out.print(" \n Dua ra danh sach sach cua NXB Lao Dong,Chon: 4");
            System.out.print(" \n Tinh tong tien cua cac loai sach,Chon:5");
            System.out.print(" \n Sap xep theo don gia tang dan,Chon: 6");
            System.out.print(" \n Thoat(Exit),Chon: 7");
            System.out.print("\n Vui long chon :1,2,3,4,5,6 or 7:  ");
            Scanner nhapvao = new Scanner(System.in);
            int chucnang=nhapvao.nextInt();
            switch(chucnang)
            {
                case 1:
 Sach.danhsachsach();
break;
                case 2:
 Sach.hienthiDSSACH();
break;
                case 3:
 Sach.demsach();
break;
                case 4:
Sach.DStheoNXB();
break;
                case 5:
 Sach.Tongtien();
break;
                case 6:
 Sach.sapxepSach();
break;
                case 7:
 System.exit(0);
break;
            }
}
        }catch(Exception e){System.out.print("loi do:"+e.getMessage());}
    }
}



Nhãn bài viết:
Blog, Updated at: 23:09