本文最後由 編輯部女孩 於 2016-7-28 14:00 編輯
這是父類別
package javahomework;
public abstract class Emp {
private int y;
private int z;
private String id;
public void setId(String i) {
y = 0;
if (i.trim().length() == 0 || Integer.parseInt(i) < 0) {
System.out.println("ID號碼輸入錯誤,請重新輸入");
} else {
id = i;
y = 1;
}
}
public String getId() {
return id;
}
private String name;
public void setName(String j) {
z = 0;
if (j.trim().length() == 0) {
System.out.println("員工姓名輸入錯誤,請重新輸入");
} else {
name = j;
z = 1;
}
}
public String getName() {
return name;
}
public abstract int getSalary();
}
子類別一
package javahomework;
public class Secretary extends Emp {
private int x ;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getSalary() {
return x;
}
}
子類別二
package javahomework;
public class Business extends Emp {
private int y ;
public int getY() {
return y;
}
public void setY(int y) {
this.y = 20000+(y/10);
}
public int getSalary(){
return y;
}
}
子類別三
package javahomework;
public class PartTimeWorkers extends Emp {
private int z ;
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z*100;
}
public int getSalary(){
return z ;
}
}
主程式
package javahomework;
import java.util.*;
public class JavaHomework {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
Secretary sec = new Secretary();
Business bu = new Business();
PartTimeWorkers pt = new PartTimeWorkers();
System.out.println("請輸入業務的總業績");
int sal = a.nextInt();
System.out.println("請輸入工讀生的工作時數");
int sala = a.nextInt();
int sum = 0;
String[][][] emp = {
{{sec.getId(), sec.getName(), String.valueOf(sec.getSalary())}},
{{bu.getId(), bu.getName(), String.valueOf(bu.getSalary())}},
{{pt.getId(), pt.getName(), String.valueOf(pt.getSalary())}}
};
System.out.println("本月薪資報表" + "n" + "編號" + "t" + "姓名" + "t" + "薪水");
for (String[][] i : emp) {
for (String[] j : i) {
for (String k : j) {
System.out.print(k + "t");
}
System.out.println("");
}
}
for(int i=0 ; i<emp.length ; i++){
for(int j=0 ; i<emp.length ; j++){
for(int k=2 ; i<=emp[j].length ; i++){
sum+=Integer.parseInt(emp[j][k]);
}
}
}
System.out.println("============================"+"n"+"本月給銀行之薪水共計"+sum+"元");
}
}
以上是這次的作業
花了一些時間才寫出程式來
用到的東西有:封裝.繼承.迴圈.多維陣列.String.valueOf Integer.parseInt...等等
若有可以改進或是優化甚至哪邊寫錯的
望請大家不吝指教
|