[Java] Bài toán quản lý tiền lương cho Nhân viên [Kế thừa trong Lập trình hướng đối tượng]

Ví dụ về kế thừa trong lập trình hướng đối tượng

Bài toán quản lý tiền lương: Viết project Java theo mô hình kế thừa sau




Trong đó:

+ Nhân viên Văn phòng (NVVP)
    lương = hệ số lương * LCB + phụ cấp
    (phụ cấp = 1*LCB nếu số năm công tác <10; phụ cấp = 1.2*LCB nếu số năm công tác >=10)

+ Nhân viên Kinh doanh (NVKD)
   lương = LCB + thưởng
  (thưởng = doanh thu * phần trăm hoa hồng)

+ Công nhân (CN)
   lương = hệ số lương * LCB + thưởng
   (thưởng = 1*LCB nếu sản lường <1000; thưởng = 1.5*LCB nếu sản lượng >=1000)

[Code Java]

 /* ********** Class NV ****************/
  * http://lap-trinh-may-tinh.blogspot.com/
 */
package quanlyluongnv181113;

public class NV {
    String maNV,ht,dvct;
    int ns,sonct;
    final float LCB=1050000;
 
    // khoi tao
    public NV(String maNV,String ht, String dvct, int ns, int sonct){
        this.maNV=maNV;
        this.ht=ht;
        this.dvct=dvct;
        this.ns=ns;
        this.sonct=sonct;
    }
    // In thong tin
    void InTT(){
        System.out.print(" ******************** ");
        System.out.print("\n IN THONG TIN NHAN VIEN  ");
        System.out.print("\n - Ma nhan vien: "+maNV);
        System.out.print("\n - Ho ten: "+ht);
        System.out.print("\n - Don vi cong tac: "+dvct);
        System.out.print("\n - Nam sinh: "+ns);
        System.out.print("\n - So nam cong tac: "+sonct);
    }  
}

/*********  Class NVVP*****************/
/*
 * http://lap-trinh-may-tinh.blogspot.com/
 */
package quanlyluongnv181113;


public class NVVP extends NV{
    float hsl; // he so luong  
    // khoi tao
    public NVVP(String maNV,String ht, String dvct, int ns, int sonct,float hsl){
        super(maNV,ht,dvct,ns,sonct);
        this.hsl=hsl;
    }
    // tinh luong
    float TinhLuong(){
        float luong,pc;
        if (sonct>=10)
            pc=1.2f*LCB;
        else
            pc=LCB;
        luong= hsl*LCB+pc;
        return luong;
    }
    // in thong tin
    void InTT(){
        super.InTT();
        System.out.print("\n - Luong: "+TinhLuong());
    }
}

/********  Class NVKD *****************/
/*
 * http://lap-trinh-may-tinh.blogspot.com/
 */
package quanlyluongnv181113;


public class NVKD extends NV {
    float dthu; // doanh thu
    float pthh; // phan tram hoa hong
    // khoi tao
    public NVKD(String maNV,String ht, String dvct, int ns, int sonct,float dthu,float pthh){
        super(maNV,ht,dvct,ns,sonct);
        this.dthu=dthu;
        this.pthh=pthh;
    }
    // tinh luong
    float TinhLuong(){
        float luong;
        luong= LCB+dthu*pthh;
        return luong;
    }
    // in thong tin
    void InTT(){
        super.InTT();
        System.out.print("\n - Luong: "+TinhLuong());
    }
}

/*********  Class CN *******************/
/*
 * http://lap-trinh-may-tinh.blogspot.com/
 */
package quanlyluongnv181113;

public class CN extends NV{
        float hsl; // he so luong
        int sanLuong;// san luong
    // khoi tao
    public CN(String maNV,String ht, String dvct, int ns, int sonct,float hsl,int sanLuong){
        super(maNV,ht,dvct,ns,sonct);
        this.hsl=hsl;
        this.sanLuong=sanLuong;
    }
    // tinh luong
    float TinhLuong(){
        float luong,thuong;
        if (sanLuong>=1000)
            thuong=1.5f*LCB;
        else
            thuong=LCB;
        luong= hsl*LCB+thuong;
        return luong;
    }
    // in thong tin
    void InTT(){
        super.InTT();
        System.out.print("\n - Luong: "+TinhLuong());
    }
}

/*********  Class (main) ****************/
/*
 * http://lap-trinh-may-tinh.blogspot.com/
 */
package quanlyluongnv181113;
import java.util.Scanner;

public class QuanLyLuongNV181113 {
    public static void main(String[] args) {
     
           String maNV,ht,dvct;
           int ns,sonct,sanLuong;
           float hsl,pthh,dthu;
         
           Scanner in1=new Scanner(System.in);
           Scanner in2=new Scanner(System.in);
         
         try{
           // Nhap thong tin cho Nhan vien VP
           System.out.print("\n ********************* ");
           System.out.print("\n NHAP THONG TIN NHAN VIEN VAN PHONG");
           System.out.print("\n + Ma Nhan vien: ");
           maNV=in1.nextLine();
           System.out.print("\n + Ho ten: ");
           ht=in1.nextLine();
           System.out.print("\n + Don vi cong tac: ");
           dvct=in1.nextLine();
           System.out.print("\n + Nam sinh: ");
           ns=in2.nextInt();
           System.out.print("\n + So nam congt tac: ");
           sonct=in2.nextInt();
           System.out.print("\n + He so luong: ");
           hsl=in2.nextFloat();
         
           NVVP t=new NVVP(maNV,ht,dvct,ns,sonct,hsl);
           t.InTT();
           }
           catch(Exception e){
               System.out.print("\n Xay ra loi khi nhap du lieu! Loi "+e.toString());
           }
         
            // Nhap thong tin cho Nhan vien kinh doanh
        try{
           System.out.print("\n ********************* ");
           System.out.print("\n NHAP THONG TIN NHAN VIEN KINH DOANH");
           System.out.print("\n + Ma Nhan vien: ");
           maNV=in1.nextLine();
           System.out.print("\n + Ho ten: ");
           ht=in1.nextLine();
           System.out.print("\n + Don vi cong tac: ");
           dvct=in1.nextLine();
           System.out.print("\n + Nam sinh: ");
           ns=in2.nextInt();
           System.out.print("\n + So nam congt tac: ");
           sonct=in2.nextInt();
           System.out.print("\n + Doanh thu: ");
           dthu=in2.nextInt();
           System.out.print("\n + Phan tram (%) hoa hong: ");
           pthh=in2.nextInt();
         
           NVKD t1=new NVKD(maNV,ht,dvct,ns,sonct,dthu,pthh);
           t1.InTT();
           }
           catch(Exception e){
               System.out.print("\n Xay ra loi khi nhap du lieu! Loi "+e.toString());
           }
         
             // Nhap thong tin cho Cong Nhan
         try{
           System.out.print("\n ********************* ");
           System.out.print("\n NHAP THONG TIN CONG NHAN");
           System.out.print("\n + Ma Nhan vien: ");
           maNV=in1.nextLine();
           System.out.print("\n + Ho ten: ");
           ht=in1.nextLine();
           System.out.print("\n + Don vi cong tac: ");
           dvct=in1.nextLine();
           System.out.print("\n + Nam sinh: ");
           ns=in2.nextInt();
           System.out.print("\n + So nam congt tac: ");
           sonct=in2.nextInt();
           System.out.print("\n + He so luong: ");
           hsl=in2.nextFloat();
           System.out.print("\n + San luong: ");
           sanLuong=in2.nextInt();
         
           CN t3=new CN(maNV,ht,dvct,ns,sonct,hsl,sanLuong);
           t3.InTT();
         }
           catch(Exception e){
               System.out.print("\n Xay ra loi khi nhap du lieu! Loi "+e.toString());
           }
         
     
    }
}

https://shorten.asia/3CzUBfaX
 Lập trình Java - ĐH FPT

----------

Tìm kiếm nội dung khác: