1 package first; 2 3 public class Employee { 4 private String fName, lName; 5 private long _ID; 6 private int workdays; 7 private float BASIC, HRA, TA, PF, NET; 8 9 public String getfName() {10 return fName;11 }12 public void setfName(String fName) {13 this.fName = fName;14 }15 public String getlName() {16 return lName;17 }18 public void setlName(String lName) {19 this.lName = lName;20 }21 public long get_ID() {22 return _ID;23 }24 public void set_ID(long _ID) {25 this._ID = _ID;26 }27 public float getBASIC() {28 return BASIC;29 }30 public void setBASIC(float bASIC) {31 BASIC = bASIC;32 }33 public int getWorkdays() {34 return workdays;35 }36 public void setWorkdays(int workdays) {37 this.workdays = workdays;38 }39 public float calculateSalary(){40 BASIC = BASIC* workdays;41 HRA = 0.04f * BASIC;42 TA = 0.01f * (BASIC + HRA);43 PF = 0.0125f * (BASIC + HRA + TA);44 NET = (BASIC + HRA + TA) - PF;45 return NET;46 47 }48 49 50 }
1 package first; 2 3 import java.util.Scanner; 4 5 public class Company { 6 7 public static void main(String[] args) { 8 Employee []emp=new Employee[5]; 9 Scanner reader = new Scanner(System.in);10 for(int i=0;i<5;i++){11 System.out.println("Enter the 0"+(i+1)+" Employee");12 emp[i]=new Employee(); //否则会提示空指针异常 Otherwise you will be prompted to null pointer exception13 System.out.println("Enter First Name:"); 14 emp[i].setfName(reader.next());15 System.out.println("Enter Last Name:");16 emp[i].setlName(reader.next());17 System.out.println("Enter Daily Wages:");18 emp[i].setBASIC(reader.nextFloat());19 System.out.println("Enter Number of Working Days:");20 21 emp[i].setWorkdays(reader.nextInt());22 emp[i].set_ID(100+(long)( Math.random()*(200-100)));//create ID between 100--20023 24 }25 System.out.println("ID FirstName LastName WorkingDays Salary ");26 for(int j=0;j<5;j++){27 System.out.printf("%d \t %s \t %s\t\t%d \t %.2f\n",emp[j].get_ID(),emp[j].getfName(),emp[j].getlName(),emp[j].getWorkdays(),emp[j].calculateSalary());28 }29 30 }31 32 }