1

我的教授给了我以下代码。

public static void main(String[] args) {

    Customer customer;
    Transaction transaction;
    double withdrawalAmount = 0;

    boolean finished = false;

    while (finished == false)
    {
        // Menu Display and Get user input
        int inputInt = 0;
        while (inputInt == 0)
        {
            inputInt = displayMenuAndGetInput();

                    // if the input is out of range
                    if ((inputInt < 1) || (inputInt > 8))
                    {
                            System.out.println("\nThe input is out of range!");
                            System.out.println();
                            inputInt = 0;
                    }
            } //end while

            // switch to correspondence function
            switch (inputInt)

            {
                case 1:
                    customer = createPersonalCustomer();
                    System.out.println("\nThe Personal customer has been created: \n" + newPC.toString());
                    customers.add(customer);
                    break;
                case 2:
                    customer = createCommercialCustomer();
                    System.out.println("\nThe Commercial customer has been created: \n" + newCC.toString());
                    customers.add(customer);
                    break;
                case 3:
                    transaction = recordTransaction();
                    if(transaction != null)
                        System.out.println("\nThe Transaction has been created: \n" + trans.toString());
                    else
                        System.out.println("\nThe ID could not be found.");
                    break;
                case 4:
                    withdrawalAmount = makeWithdrawal();
                    if(withdrawalAmount > 0)
                        System.out.println("\nAmount withdrawn from this account: " + moneyFormat.format(acct.getMakeWithdrawal()) + "\n");
                    else
                        System.out.println("\nThe ID could not be found.");
                    break;
                case 5:
                    displayCustomer();
                    break;
                case 6:
                    displayCustomerSummary();
                    break;
                case 7:
                    displayGrandSummary();
                    break;
                case 8:
                    // exit
                    finished = true;
                    break;
                default:
                    System.out.println("Invalid Input!");

                    break;
            } // end switch
    } // end while

}

我应该采用以下代码

// Create a new Transaction
public static Transaction recordTransaction(){}

并制作一个适用于以下场景的循环:

输入客户id,如果数组中没有匹配到客户id,则产生case 3中读出的错误,并显示主菜单。如果客户 ID 有效,则用户在下面输入输入信息。

下面是我的代码

public static Transaction recordTransaction(){

System.out.println("Enter the customer ID to create the transaction > ");
    long customerID = scan.nextLong();

    for (Customer c : customers) {
        if (c.getCustomerID() == customerID) {
            if (trans != null) {

            System.out.println("\nEnter the weight of gold > ");
                Transaction.goldWt = scan.nextDouble();

                System.out.println("\nEnter the weight of platinum > ");
                Transaction.platinumWt = scan.nextDouble();

                System.out.println("\nEnter the weight of silver > ");
                Transaction.silverWt = scan.nextDouble();
                }
        }
            return null; 
}

任何人,我已经以多种方式运行此程序,并且我的代码将接受无效和有效的客户 ID,或者它不会接受无效或有效的客户 ID。我知道我可能忽略了一些东西,这就是为什么我拼命请求论坛的帮助。在编程方面,我有强迫症倾向,这是我的第一个 Java 入门课程,所以我不太精通这门语言。在过去的两天里,我一直被困在这个问题上。请帮忙。

4

3 回答 3

1

您需要new Transaction()在方法中实例化 arecordTransaction()并在适当时返回它。

public static Transaction recordTransaction(){

    System.out.println("Enter the customer ID to create the transaction > "); long customerID = scan.nextLong();

    for (Customer c : customers) {
        if (c.getCustomerID() == customerID) {
            Transaction trans = new Transaction();

            System.out.println("\nEnter the weight of gold > ");
            trans.goldWt = scan.nextDouble();

            System.out.println("\nEnter the weight of platinum > ");
            trans.platinumWt = scan.nextDouble();

            System.out.println("\nEnter the weight of silver > ");
            trans.silverWt = scan.nextDouble();

            return trans;
        }
    }
    return null; 
}
于 2013-03-07T15:43:44.123 回答
0

关于您的代码的一些说明:

if (trans != null) {

什么是“反式”?我想它必须引用一个 Transaction 实例。

Transaction.goldWt = ...

那不应该是“trans.goldWt”吗?(当然,植物和银也是如此)。如果确实Transaction.goldWt如此,那么您总是在更改该单个值。

return null;

你永远不会返回任何东西,所以调用代码总是“无 ID”。你不应该“ return trans”吗?

于 2013-03-07T15:44:08.887 回答
0

该问题是由正在创建的每个帐户的随机 customerID 的 getter 和 setter 方法生成的。

谢谢谢谢!

于 2013-03-08T03:28:58.250 回答