/* Employee.java CIS 160 David Klick 2004-10-27 Employee class implementation modified 2013-12-01 - changed DecimalFormat to String.format - added a few comments - added error checking to setName(String) */ public class Employee { // private instance variables private String name; private double hours; private double rate; // constructors // default constructor - no arguments public Employee() { // calls the constructor with three arguments this("", 0.0, 0.0); } public Employee(String n) { // calls the constructor with three arguments this(n, 0.0, 0.0); } public Employee(String n, double rate) { // calls the constructor with three arguments this(n, 0.0, rate); } public Employee(String n, double rate, double hours) { // calls the constructor with three arguments setName(n); setHours(hours); setRate(rate); } // accessor methods public String getName() { return name; } public double getHours() { return hours; } public double getRate() { return rate; } // mutator methods public void setName(String s) { if (s == null) name = new String(""); else name = new String(s); } public void setHours(double hours) { if (hours < 0.0) hours = 0.0; this.hours = hours; } public void setRate(double rate) { if (rate < 0.0) rate = 0.0; this.rate = rate; } // various other methods public double getGrossPay() { double gp = 0.0; if (hours <= 40.0) gp = hours * rate; else gp = 40 * rate + (hours - 40.0) * (rate * 1.5); return gp; } public String toString() { return String.format("%s, rate: %.2f, hours: %.2f", name, rate, hours); } public boolean equals(Employee e) { return (name.equals(e.name) && rate==e.rate && hours==e.hours); } // copy constructor public Employee(Employee e) { // creating a new object isn't really needed in the special // case of a String, but would be needed for most reference // data members name = new String(e.name); rate = e.rate; hours = e.hours; } }