Java Reference
In-Depth Information
Themembers of the
class
CashRegister
are listedbelowand shown in Figure 8-19.
Instance
Variables
private int
cashOnHand;
Constructors
and Methods
public
CashRegister()
//Default constructor
//To set the cash in the register 500 cents
//Postcondition: cashOnHand = 500;
public
CashRegister(
int
cashIn)
//Constructor with parameters
//Postcondition: cashOnHand = cashIn;
public int
currentBalance()
//Method to show the current amount in the cash register
//Postcondition: The value of the instance variable
//
cashOnHand is returned
public void
acceptAmount(
int
amountIn)
//Method to receive the amount deposited by
//the customer and update the amount in the register
//Postcondition: cashOnHand = cashOnHand + amountIn
CashRegister
-cashOnHand:
int
+CashRegister()
+CashRegister(
int
)
+currentBalance():
int
+acceptAmount(
int
):
void
FIGURE 8-19
UML class diagram of the
class
CashRegister
Next, we give the definitions of the methods to implement the operations of the
class
CashRegister
. The definitions of these methods are simple and easy to
follow.
The method
currentBalance
shows the current amount in the cash register. The
amount stored in the cash register is in cents. Its definition is:
public int
currentBalance()
{
return
cashOnHand;
}
Search WWH ::
Custom Search