2013-10-10 73 views
0
public static void main(String[] args){ 

      Employee[] empList = new Employee[2]; 

     for(int i=0; i < empList.length; i++){ 
      String empType = JOptionPane.showInputDialog("Enter S for a salaried employee or H for an hourly employee."); 
      if (empType.charAt(0) == 's' || empType.charAt(0) == 'S'){ 
       empList[i] = new Salaried(); 
       collectEmpInfo(empList[i]); 
       displayEmpInfo(empList[i]); 
       System.out.println("Number of Employees: " + Salaried.getNumEmployees()); 
      } 
      else if (empType.charAt(0) == 'h' || empType.charAt(0) == 'H'){ 
       empList[i] = new Hourly(); 
       collectEmpInfo(empList[i]); 
       displayEmpInfo(empList[i]); 
       System.out.println("Number of Employees: " + Hourly.getNumEmployees()); 
      } 
      else{ 
       JOptionPane.showMessageDialog(null, "Invalid Employee Type"); 
       //reset i for this iteration if input is invalid 
       i--; 
      } 
     } 

    } 


import java.text.NumberFormat; 
public abstract class Employee { 

protected String firstName; 
protected String lastName; 
protected char gender; 
protected int dependents; 
protected double annualSalary; 
protected static int numEmployees = 0; 
public Benefit benefit; 

public Employee(){ 
    firstName = "not given"; 
    lastName = "not given"; 
    gender = 'U'; 
    dependents = 0; 
    annualSalary = 20000; 
    benefit = new Benefit(); 

} 

public Employee(String first, String last, char gen, int dep, double salary, Benefit ben){ 
    firstName = first; 
    lastName = last; 
    gender = gen; 
    dependents = dep; 
    annualSalary = salary; 
    numEmployees += 1; 
    benefit = ben; 
} 



public String toString(){ 
    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    return "First Name:" + firstName + 
      "\nLast Name: " + lastName + 
      "\nGender: " + gender + 
      "\nDependents: " + dependents + 
      "\nAnnual Salary: " + nf.format(annualSalary) + 
      "\nEmployee weekly pay: " + nf.format(calculatePay()) + 
      benefit.toString(); 


} 

public String getFirstName(){ 
    return firstName; 
} 

public void setFirstName(String first){ 
    firstName = first; 
} 

public String getLastName(){ 
    return lastName; 
} 

public void setLastName(String last){ 
    lastName = last; 
} 

public char getGender(){ 
    return gender; 
} 

public void setGender(char gen){ 
    gender = gen; 
} 

public int getDependents(){ 
    return dependents; 
} 

public void setDependents(int dep){ 
    dependents = dep; 
} 

public double getAnnualSalary(){ 
    return annualSalary; 
} 

public void setAnnualSalary(double salary){ 
    annualSalary = salary; 
} 

public static int getNumEmployees(){ 
    return numEmployees; 
} 

public void setDependents(String dep){ 
    dependents = Integer.parseInt(dep); 
} 

public void setAnnualSalary(String sal){ 
    annualSalary = Double.parseDouble(sal); 
} 
public abstract double calculatePay(); 
} 

public class Benefit { 

private String healthInsurance; 
private double lifeInsurance; 
private int vacation; 

public Benefit(){ 
    healthInsurance = "Full"; 
    lifeInsurance = 1000; 
    vacation = 5; 
} 

public Benefit(String health, double life, int vacation){ 
    healthInsurance = health; 
    lifeInsurance = life; 
    this.vacation = vacation; 
} 

public String toString(){ 
    return "\nHealth Insurance: " + healthInsurance +  
      "\nLife Insurance: " + lifeInsurance + 
      "\nVacation: " + vacation; 
} 

public String getHealthInsurance(){ 
    return healthInsurance; 
} 

public void setHealthInsurance(String hins){ 
    healthInsurance = hins; 
} 

public double getLifeInsurance(){ 
    return lifeInsurance; 
} 

public void setLifeInsurance(double lifeins){ 
    lifeInsurance = lifeins; 
} 

public int getVacation(){ 
    return vacation; 
} 

public void setVacation(int vaca){ 
    vacation = vaca; 
} 
} 

import java.text.NumberFormat; 


public class Salaried extends Employee{ 

private final static int MIN_MANAGEMENT_LEVEL = 0; 
private final static int MAX_MANAGEMENT_LEVEL = 3; 
private final static double BONUS_PERCENT = 0.10; 
private int managementLevel; 

public Salaried(){ 
    firstName = "not given"; 
    lastName = "not given"; 
    gender = 'U'; 
    dependents = 0; 
    annualSalary = 20000; 
    benefit = new Benefit(); 
    managementLevel = 0; 
} 

public Salaried(int managementLevel){ 
    firstName = "not given"; 
    lastName = "not given"; 
    gender = 'U'; 
    dependents = 0; 
    annualSalary = 20000; 
    benefit = new Benefit(); 
    numEmployees +=1; 
    this.managementLevel = (managementLevel >= MIN_MANAGEMENT_LEVEL && managementLevel <= MAX_MANAGEMENT_LEVEL)? managementLevel : 0; 
} 

public Salaried(String first, String last, char gen, int dep, double salary, Benefit bene, int managementLevel){ 
    firstName = first; 
    lastName = last; 
    gender = gen; 
    dependents = dep; 
    annualSalary = salary; 
    numEmployees += 1; 
    benefit = bene; 
    this.managementLevel = (managementLevel >= MIN_MANAGEMENT_LEVEL && managementLevel <= MAX_MANAGEMENT_LEVEL)? managementLevel : 0; 
} 

public double calculatePay(){ 
    double bonusPercentage = managementLevel * BONUS_PERCENT; 
    double weeklyBonus = bonusPercentage * annualSalary; 
    double weeklyPay = annualSalary/52 + weeklyBonus/52; 
    return weeklyPay; 
} 

public String toString(){ 
    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    return "First Name:" + firstName + 
      "\nLast Name: " + lastName + 
      "\nGender: " + gender + 
      "\nDependents: " + dependents + 
      "\nAnnual Salary: " + nf.format(annualSalary) + 
      "\nEmployee weekly pay: " + nf.format(calculatePay()) + 
      benefit.toString() + 
      "\nManagement Level: " + managementLevel; 
} 

public void setManagementLevel(int manLevel){ 
    managementLevel = (manLevel >= MIN_MANAGEMENT_LEVEL && manLevel <= MAX_MANAGEMENT_LEVEL)? manLevel : 0; 
} 

public int getManagementLevel(){ 
    return managementLevel; 
} 

} 

import java.text.NumberFormat; 


public class Hourly extends Employee{ 
private static final double MIN_WAGE = 10; 
private static final double MAX_WAGE = 75; 
private static final double MIN_HOURS = 0; 
private static final double MAX_HOURS = 50; 

private double wage; 
private double hours; 
private String category; 

public Hourly(){ 
    wage = 0; 
    hours = 0; 
    category = ""; 
    firstName = "not given"; 
    lastName = "not given"; 
    gender = 'U'; 
    dependents = 0; 
    annualSalary = 20000; 
    numEmployees +=1; 
    benefit = new Benefit(); 
} 

public Hourly(double wage, double hours, String category){ 
    this.wage = (wage >= MIN_WAGE && wage <= MAX_WAGE)? wage : 10; 
    this.hours = (hours >= MIN_HOURS && hours <= MAX_HOURS)? hours : 0; 
    this.category = (category.equalsIgnoreCase("temporary") || category.equalsIgnoreCase("full time") || category.equalsIgnoreCase("part time"))? 
      category: "Invalid category"; 
    firstName = "not given"; 
    lastName = "not given"; 
    gender = 'U'; 
    dependents = 0; 
    annualSalary = 20000; 
    benefit = new Benefit(); 
} 

public Hourly(String first, String last, char gen, int dep, Benefit bene, double wage, double hours, String category){ 
    this.wage = (wage >= MIN_WAGE && wage <= MAX_WAGE)? wage : 10; 
    this.hours = (hours >= MIN_HOURS && hours <= MAX_HOURS)? hours : 0; 
    this.category = (category.equalsIgnoreCase("temporary") || category.equalsIgnoreCase("full time") || category.equalsIgnoreCase("part time"))? 
      category: "Invalid category"; 
    double salary = wage * hours * 52; 
    firstName = first; 
    lastName = last; 
    gender = gen; 
    dependents = dep; 
    annualSalary = salary; 
    numEmployees += 1; 
    benefit = bene; 
} 

public String toString(){ 
    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    return "First Name:" + firstName + 
      "\nLast Name: " + lastName + 
      "\nGender: " + gender + 
      "\nDependents: " + dependents + 
      "\nAnnual Salary: " + nf.format(annualSalary) + 
      "\nEmployee weekly pay: " + nf.format(calculatePay()) + 
      benefit.toString() + 
      "\nCategory: " + category; 
} 

public double getWage() { 
    return wage; 
} 

public void setWage(double wage) { 
    this.wage = (wage >= MIN_WAGE && wage <= MAX_WAGE)? wage : 10; 
    annualSalary = wage * hours *52; 
} 

public double getHours() { 
    return hours; 
} 

public void setHours(double hours) { 
    this.hours = (hours >= MIN_HOURS && hours <= MAX_HOURS)? hours : 0; 
    annualSalary = wage * hours * 52; 
} 

public String getCategory() { 
    return category; 
} 

public void setCategory(String category) { 
    this.category = (category.equalsIgnoreCase("temporary") || category.equalsIgnoreCase("full time") || category.equalsIgnoreCase("part time"))? 
      category: "Invalid category"; 
} 

public double calculatePay(){ 
    return wage * hours; 
} 
} 

員工是一個包含static int numEmployees的抽象類。工資和小時是員工的子類。 Salaried和Hourly中的構造函數增加numEmployees + = 1,那麼爲什麼當我調用Salaried/Hourly.getNumEmployees()時,它返回0?如何通過繼承所述抽象類的類從main調用抽象類的靜態變量訪問器?

+2

請發佈這些類。 –

+1

如果您僅發佈不相關的代碼,我們如何能夠猜測代碼中出現了什麼問題? – nhgrif

+2

我的猜測是這個子類有自己的靜態變量,它會影響超級的 – Bohemian

回答

2

沒有參數的構造函數(主要調用的構造函數)不會增加計數器。 編輯:好的,至少不是那些在薪水中的人...

此外,計數器可能不會按照您的意圖工作,因爲對於所有子類只有一個靜態計數器。

Java中的靜態字段不是學習繼承的好方法。他們的行爲大多像全局變量。如果你想學習繼承和多態,你應該關注非靜態方法。如果需要,您可以按照評論中的建議創建「公司」類。

+0

不幸的是,這個程序是針對一個專注於Java的OOP課程。部分要求是使用上面定義的類。另外,感謝您注意到Salaried中的默認構造函數沒有增加numEmployees。我將該行添加到默認構造函數中,並且程序運行良好。我不確定我明白爲什麼靜態方法/字段不是學習繼承的好方法。在我看來,這並沒有什麼不同。無論如何,你確實給我解決了我的問題,所以謝謝。 –

+0

嗨喬希。我沒有意識到你只是在學習編程,對於我的評論感到抱歉。 Java中的靜態變量通常是非常糟糕的做法。 [見此](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-vil)。作爲一般事物,完全避免任何靜態狀態!如果你需要將員工數量保持在某個地方,也許你應該有一個課程,其主要目的就是要做到這一點。例如,公司班的人告訴你。但爲什麼不只是查詢數據庫(你可能會有一個在任何真正的應用程序)的員工數量? – Renato

+0

在這種情況下,您不必擔心必須跟蹤員工人數,以防有人在某個時候提問。在設計課程時,您必須遵守「單一責任原則」。這會讓你的代碼變得更好。祝你好運。 – Renato