【发布时间】:2015-10-29 07:23:31
【问题描述】:
import java.util.*;
public class RegisterUI
{
public static void main(String[] args)
{ `
//creating objects of class CashRegister
CashRegister runningTotal = new CashRegister();
CashRegister amountTendered = new CashRegister();
CashRegister changeDue = new CashRegister();
Scanner keyboard_input = new Scanner(System.in);
int selection = keyboard_input.nextInt();
System.out.println("Cash Register");
System.out.println("Running total: R" + runningTotal.total(keyboard_input.nextDouble()));
System.out.println("1. Ring up item.");
System.out.println("2. Enter amount tendered.");
System.out.println("3. New transaction.");
System.out.println("4. Quit");
while (selection != 4)
{
if (selection == 4)
{
break;
}
if (selection == 1)
{
//从 CashRegister 程序中获取输入
使用'keyboard_input.nextDouble()'获取runningTotal的输入是否正确?
System.out.println("Enter amount:");
runningTotal.total(keyboard_input.nextDouble());
System.out.println("Running total: R" + runningTotal.total(keyboard_input.nextDouble()));
// code to get amount from CashRegister
}
if (selection == 2)
{
System.out.println("Enter amount:");
amountTendered.tender(keyboard_input.nextDouble());
System.out.println("Running total: R" + runningTotal.total(keyboard_input.nextDouble()));
System.out.println("Amount tendered: R" + amountTendered.tender(keyboard_input.nextDouble()));
System.out.println("Change due: R" + changeDue.change());
}
if (selection == 3)
{
System.out.println("Running total: R" + runningTotal.total(keyboard_input.nextDouble()));
}
}
}
}
到目前为止,我已经编写了一个程序,但我需要 CashRegister 程序来测试它。
我可以创建哪些方法来输入值并进行计算?
整个问题:
The Acme cash register is a simple device that is used in commerce to calculate sales and change due. (Actually, strictly speaking, it's not a register at all since it does not record sales. It just assists with the calculations.)
The device supports customer transactions in the following way:
Ready to ring up a customer's purchases, the running total is initially zero.
As the operative enters the price of each item the running total is updated.
Once the final item has been added, requesting payment, the operator may enter the amount tendered. This causes the cash drawer to open along with the calculation of the change that is due.
Having received payment and obtained change as required, closing the cash drawer serves to complete the transaction, setting the running total, amount tendered and change due back to zero.
The running total may be examined at any point.
A transaction can be cancelled at any point. All values stored by the register revert to zero.
An Acme cash register simulator is required. Here is sample I/O for the program:
Cash Register
Running total: R0.00
1. Ring up item.
2. Enter amount tendered.
3. New transaction.
4. Quit
1
Enter amount:
34.50
Running total: R34.50
1. Ring up item.
2. Enter amount tendered.
3. New transaction.
4. Quit
2
CONTINUED
Enter amount:
50
Running total: R34.50
Amount tendered: R50.00
Change due: R15.50
1. Ring up item.
2. Enter amount tendered.
3. New transaction.
4. Quit
3
Running total: R0.00
1. Ring up item.
2. Enter amount tendered.
3. New transaction.
4. Quit
4
Design and implement a CashRegister class that models Acme cash register function, and then write a program called RegisterUI that uses a CashRegister object, providing I/O.
Use the design of the meterology program of question 3 to guide you:
The CashRegister class should contain methods for each register function.
The RegisterUI class should be responsible for creating a CashRegister object,
obtaining user input, calling the CashRegister object’s methods, and printing results.
The CashRegister class should NOT contain any print statements.
【问题讨论】:
-
我想没人明白你在问什么......
-
我编辑了问题并发布了整个问题。
标签: java object methods constructor