Java Reference
In-Depth Information
166
Here is an example of a class that takes user input. This class uses the
CashRegister
class and simulates a transaction in which a user purchases an item,
pays for it, and receives change.
We call this class
CashRegisterSimulator
, not
CashRegisterTester
. We
reserve the
Tester
suffix for classes whose sole purpose is to test other classes.
ch04/cashregister/CashRegisterSimulator.java
1
import
java.util.Scanner;
2
3 /**
4
This program simulates a transaction in
which a user pays for an item
5
and receives change.
6 */
7
public class
CashRegisterSimulator
8 {
9
public static void
main(String[]
args)
10 {
11 Scanner in =
new
Scanner(System.in);
12
13 CashRegister register =
new
CashRegister();
14
15 System.out.print(
ÐEnter
price: Ñ
);
16
double
price =
in.nextDouble();
17 register.recordPurchase(price);
18
19 System.out.print(
ÐEnter
dollars: Ñ
);
20
int
dollars = in.nextInt();
21 System.out.print(
ÐEnter
quarters: Ñ
);
22
int
quarters = in.nextInt();
23 System.out.print(
ÐEnter
dimes: Ñ
);
24
int
dimes = in.nextInt();