 |
Defining classes, part II
|
Using ints in a class/program (contd.)
Determining the behavior of the class Shop
-
The main behavior of the Shop is to
-
contain three employees,
-
pay the employees,
-
perform the information display, and
-
keep track of how much has been paid in total.
-
This leads to the following behaviors
-
Shop
-
hireEmployees
-
payEmployees
-
displayTotals
Defining the interface for class Shop
-
How do we construct a shop?
Shop cornerShop = new Shop ();
-
No information is needed to create the shop
Each week, we can calculate the pay for each employee in the shop
cornerShop.payEmployees ()
We can look at the totals:
cornerShop.displayTotals ()
How do we hire employees?
cornerShop.hireEmployees (e1, e2, e3)
None of these methods return any information. They just make the Shop object
do things.
class Shop {
// Methods
public Shop () {...}
public void payEmployees () {...}
public void displayTotals () {...}
public void hireEmployess (Employee e1, Employee e2, Employee e3) {...}
// Instance variables
...
}
In order to calculate totals of the amounts paid, an instance variable
is required, as are the employees
private int totalPaid;
private Employee emp1, emp2, emp3;
Implementing methods
-
When we first construct a shop, there are no totals and no employees. The
constructor needs to initialize these:
public Shop () {
totalPaid = 0;
Employee noEmp = new Employee ("Noone", 0, 0);
emp1 = noEmp;
emp2 = noEmp;
emp3 = noEmp;
}
First we need to hire some employees
public void hireEmployees (Employee e1, Employee e2, Employee e3) {
emp1 = e1;
emp2 = e2;
emp3 = e3;
}
To calculate the pay, the shopkeeper needs to be asked how long each employee
has worked
public void payEmployees () {
BasicDataReader input = new BasicDataReader ();
BasicDataWriter output = new BasicDataWriter ();
int hours;
output.write ("How long did " + emp1.getName () + " work? ");
hours = input.readInt (); // BasicDataReader has a method to read ints
emp1.setHoursWorked (hours);
totalPaid = totalPaid + emp1.calculatePay ();
output.write ("How long did " + emp2.getName () + " work? ");
hours = input.readInt (); // BasicDataReader has a method to read ints
emp2.setHoursWorked (hours);
totalPaid = totalPaid + emp2.calculatePay ();
output.write ("How long did " + emp3.getName () + " work? ");
hours = input.readInt (); // BasicDataReader has a method to read ints
emp3.setHoursWorked (hours);
totalPaid = totalPaid + emp3.calculatePay ();
}
To display the totals, we need to output all the information
public void displayTotals () {
// Display each employee
System.out.println ("Pay for each each employee: ");
System.out.print (emp1.getName () + " - Hours this week: ");
System.out.print (emp1.getHoursWorked ());
System.out.print (", Pay rate: $");
System.out.print (emp1.getPayRate ());
System.out.print (", Pay due: $");
System.out.println (emp1.calculatePay ());
// Repeat last 6 lines for emp2, emp3
// Display the totals
System.out.print ("Total amount paid this week: $");
System.out.print (totalPaid);
System.out.println (" to 3 employees.");
}
Source code:
Last modified: 2/14/99