public class Employee {
private String name; // the employee's name
private String jobTitle; // the employee's job title
private double wage; // the hourly wage for the employee
// the amount of money the company currently owes the employee
protected double payOwed;
// Default starting wage
public static final double MINIMUM_WAGE = 10.0;
// Class variable: the highest wage any employee receives
private static double maxWage = 0;
/**********************************************************************
* Constructor: Creates an employee given a name, job title, and wage.
*
* Parameters:
* theName: the name for the employee
* title: the job title for the employee
* wage: the employee's wage
*
* Errors: If wage is negative, writes error message and uses zero instead.
**********************************************************************/
public Employee(String name, String title, double wage) {
// Possible improvement: disallow null or empty names & titles
this.name = name;
jobTitle = title;
// start out with no pay accumulated
payOwed = 0;
if (wage < 0) {
System.out.println("Error: negative wages not allowed!");
wage = 0;
} // end if
this.wage = wage;
if (wage > maxWage)
maxWage = wage;
} // end constructor
/**********************************************************************
* Returns the employee's name.
*********************************************************************/
public String getName() {
return name;
} // end getName
/**********************************************************************
* Returns the employee's job title.
*********************************************************************/
public String getTitle() {
return jobTitle;
} // end getTitle
/**********************************************************************
* Returns the employee's wage
*********************************************************************/
public double getWage() {
return wage;
} // end getWage
/**********************************************************************
* Changes the employee's job title
* Parameter: new job title
*********************************************************************/
public void setTitle(String newTitle) {
jobTitle = newTitle;
} // end setTitle
/**********************************************************************
* Gives the employee a raise.
*
* Parameter: the amount by which to raise this employee's hourly wage.
*********************************************************************/
public void raise(double amount) throws EmployeeException{
if (amount < 0){
throw new EmployeeException();
}
else{
wage += amount;
}
if (wage > maxWage)
maxWage = wage;
} // end raise
/**********************************************************************
* Pays the employee for a number of hours worked. This increases the
* amount of pay owed to the employee.
*
* Parameter: the number of hours worked
*********************************************************************/
public void pay(double hours) {
payOwed += hours * wage;
} // end pay
/**********************************************************************
* Returns the amount of pay owed to this employee.
*********************************************************************/
public double amountToPay() {
return payOwed;
} // end amountToPay
/**********************************************************************
* Zeros the amount of pay owed to this employee. Call this after you
* write the employee a cheque for a pay period.
*********************************************************************/
public void zero() {
payOwed = 0;
} // end zero
/**********************************************************************
* Creates a String representation of the Employee, including name,
* job title, wage and pay owed.
*
* Return value: the string representation
*********************************************************************/
public String toString() {
return name + ": " + jobTitle + ", wage=$" + wage
+ ", pay owed = $" + payOwed;
} // end toString
/**********************************************************************
* Constructor: Creates an employee with the default starting wage,
* given a name and job title
*
* Parameters:
* name: the name for the employee
* title: the job title for the employee
* wage: the employee's wage
*
* Errors: If wage is negative, writes error message and uses zero instead.
**********************************************************************/
public Employee(String theName, String title) {
this(theName, title, MINIMUM_WAGE);
} // end constructor
/**********************************************************************
* Returns the highest wage any employee currently receives
*********************************************************************/
public static double getMaxWage() {
return maxWage;
} // end getMaxWage
/**********************************************************************
* Compares this employee with another object for equality.
* We define equality to mean that the other object must be some kind
* of employee and have the same name, job title and wage.
* Parameter: another object to compare with this employee
*********************************************************************/
public boolean equals(Object other) {
if (other instanceof Employee) {
Employee otherEmp = (Employee) other;
return (otherEmp.name.equals(name) &&
otherEmp.jobTitle.equals(jobTitle) &&
otherEmp.wage == wage);
}
else {
return false;
} // end if
} // end equals
public class EmployeeException extends Exception{
public EmployeeException(){
super();
}
}
} // end class Employee
public class Test{
public static void main(String[] args){
Employee Alex = new Employee("Alex", "Mr", 20);
try{
Alex.raise(-5);
}
catch(EmployeeException e){
System.out.println("You cannot do that");
}
}
}
出于某种原因,我不断收到一条消息,说Java无法找到员工异常类。我想知道为什么会这样。它与员工异常是一个内部类有什么关系吗?感谢。
答案 0 :(得分:0)
EmployeeException
在Employee
内定义。
您可以Employee.EmployeeException
:
try{
Alex.raise(-5);
}
catch(Employee.EmployeeException e){
System.out.println("You cannot do that");
}