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

1-Xây dung Class Canbo gồm các thuộc tính:MaCB,Hoten,GTinh,QQuan,Tuoi,Luong
2-Yêu cầu :
-Nhập dữ lieu từ bàn phím
-Hiển thị DSCB vừa nhập
-In ra DSCB có QQuán là Hà Nội
-Sắp xếp DSCB giảm giần theo Luong
-In ra thong tin về CB có lương thấp nhất
-Đếm CB có tuổi <30

Code:


package de3;
//Code Design by Tạ Văn Anh
import java.util.Scanner;
public class CanBo {
   
         String MaCB,Hoten,Qquan, gioitinh;
         int n,tuoi;
         int luong;
        
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("MaCB: "+MaCB+" |Ho TenCB "+Hoten+" |Que Quan:  "+Qquan+" |Gioi Tinh: "+gioitinh+" |Tuoi: "+tuoi+" |Luong: "+luong+"");
        }catch(Exception e) {System.out.print("Loi do: "+e.getMessage());
        }
    }
   public void nhapdanhsach(){
    try{
        System.out.print("Ma Can Bo:");
        MaCB=imput();
        System.out.print("Ho ten Can Bo:");
        Hoten=imput();
            System.out.print("Que Quan Can Bo:");
            Qquan=imput();
            System.out.print("Gioi tinh can bo:");
            gioitinh=imput();
                System.out.print("Tuoi:");
                tuoi= Integer.parseInt(imput());
                System.out.print("Luong:");
                luong= Integer.parseInt(imput());
       }
   catch(Exception e){System.out.print("Loi do:\n"+e.getMessage());}

    public CanBo[] CB = new CanBo[100];
    public void danhsachcanbo() {
  try{
      System.out.print("\n Nhap vao so luong can bo:");
      n= Integer.parseInt(imput());
      for(int i=0;i<n;i++){
         CB[i]=new CanBo();
      System.out.println("\n Nhap thong tin cho can bo thu:"+" "+(i+1)+" ");
      CB[i].nhapdanhsach();
      }
 
    }
  catch(Exception e){System.out.print("Loi do:\n"+e.getMessage());}
}
public void hienthiDSCB(){
   try {
       System.out.print(" DANH SACH CAN BO LA:\n");
            for(int i=0;i<n;i++){
                               CB[i].view();
                            }
       }
  catch(Exception e ){System.out.print("Loi do:"+e.getMessage());}
 }
   
    public void DSCBtheoqqHN(){
    try{
        for( int i=0;i<n;i++){
            if(CB[i].Qquan.trim().equalsIgnoreCase("Ha Noi")){
                CB[i].view();
            }
                 
        }
       }catch(Exception e){System.out.print("Loi do:"+e.getMessage());
    }
}
public void sapxepCBtheoluong(){
   
    try {
           
          CanBo tg= new CanBo();
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(CB[i].luong <= CB[j].luong)  //giam dan
                    {                          
                        tg=CB[i];
                        CB[i]=CB[j];
                        CB[j]=tg;
                    }
                System.out.println("DANH SACH SAP XEP GiAM DAN THEO LUONG LA:\n");
                hienthiDSCB();
                      
        }
    }
       
    }catch(Exception e){System.out.print("Loi do:"+e.getMessage());
    }
           
}
public void CBLUONGMIN(){
  
    try{
       int  min=0;
        for( int i=0;i<n;i++){
            if(CB[min].luong > CB[i].luong){
                min=i;
            }
        }
        System.out.print(" Thong tin can bo co luong thap nhat la: ");
        CB[min].view();
       
    }catch( Exception e){System.out.print("Loi do"+e.getMessage());
    }
}
    public void demCBTuoi30(){
    try{
        int dem=0;     
        for( int i=0;i<n;i++){
                if(CB[i].tuoi < 30){
                    dem+=1;
                }
            }
        System.out.print(" So luong can bo co tuoi < 30 la:"+dem);
       } catch( Exception e){System.out.print("Loi do:\n"+e.getMessage());
    }
}

 
    public static void main(String[] args) {
        try{
            CanBo CB = new CanBo();
        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 In ra DSCB có QQuan là “Hà Nội”,Chon : 2");
            System.out.print(" \n Danh sach Can Bo giam dan theo luong, Chon: 3");
            System.out.print(" \n Dua ra thong tin về CB co luong thap nhat,Chon: 4");
            System.out.print(" \n Dem so luong can bo co tuoi <30 ,Chon: 5");
            System.out.print(" \n Thoat,Chon: 6");
            System.out.print("\n Vui Long chon:1,2,3,4,5 or 6: ");
            Scanner nhapvao = new Scanner(System.in);
            int chucnang=nhapvao.nextInt();
            switch(chucnang)
            {
                case 1:
 CB.danhsachcanbo();
break;
                case 2:
 CB.DSCBtheoqqHN();
break;
                case 3:
 CB.sapxepCBtheoluong();
break;
                case 4:
 CB.CBLUONGMIN();
break;
                case 5:
CB.demCBTuoi30();
break;
                case 6:
System.exit(0);
break;
            }
}
           
        }catch( Exception e){System.out.print("Loi do"+ e.getMessage());
        }
      
}
}



Nhãn bài viết:
Blog, Updated at: 16:54