【问题标题】:I need to write two Java programs, one which uses the methods of the other. How do I proceed according to the first program I wrote?我需要编写两个 Java 程序,一个使用另一个的方法。我如何按照我写的第一个程序进行?
【发布时间】: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


【解决方案1】:

要回答标题问题,您可以将您的第一个项目导出为 .jar,然后将其导入您的第二个项目中。

Right click on first project -> Export -> jar
Right click on second project ->import

你可以使用类似的东西

private String getInput(String description) {
System.out.print(description + ": ");
String input = null;

InputStreamReader stream = null;
BufferedReader reader = null;
try {
    // Open a stream to stdin
    stream = new InputStreamReader(System.in);

    // Create a buffered reader to stdin
    reader = new BufferedReader(stream);

    // Try to read the string
    input = reader.readLine();           
} catch (IOException e) {
    e.printStackTrace();
} 

return input;}

从键盘读取输入,然后将您输入的字符串转换为 Double 或您需要的任何其他类型

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 2018-12-08
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2013-12-26
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多