2013-03-07 54 views
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不匹配在数组中,生成情况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; 
} 

Anywho,我这运行多种方式,要么我的代码将接受无效和有效的客户ID,否则它不会接受无效或有效的客户ID 。 我知道我可能忽略了一些东西,这就是为什么我拼命地要求论坛的帮助。在编程方面我有强迫症的倾向,这是我的第一个java类,所以我对这门语言不太熟悉。过去两天我一直在困扰这个问题。请帮忙。

回答

1

您需要在方法recordTransaction()内实例化new Transaction(),并在适当的时候返回它。

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; 
} 
+0

这是有道理的。自从我之前已经打电话给我之后,我的印象就是这样,我不必再打电话给他。 – ComplexVolcano 2013-03-07 15:56:26

+0

目前尚不清楚“接受”是什么意思。 “不接受任何东西”意味着您收到“无法找到身份证”的消息。无论您提供的客户ID是什么?
您应该提供方法createPersonalCustomer()的代码。 – dotvav 2013-03-07 16:16:57

+0

是的,输出状态:“无法找到ID。”对于任何提供的ID。我不遵循你的第二条语句“你应该提供方法createPersonalCustomer()的代码。” – ComplexVolcano 2013-03-07 16:25:51

0

你的代码的一些说明:

if (trans != null) { 

什么是 “反式”?我猜它必须引用一个事务实例。

Transaction.goldWt = ... 

这不应该是“trans.goldWt”吗? (当然,对于plant和银也是如此)。如果它真的是Transaction.goldWt那么你总是改变这个单一的值。

return null; 

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

+0

我只会注意到,如果代码当前正在编译,那么'goldWt'是静态的,这意味着对于'Transaction'类型的所有类只有1个变量,并且它可能不应该是静态的。 – Dukeling 2013-03-07 15:45:58

+0

trans是指 – ComplexVolcano 2013-03-07 15:49:19

+0

嗯,问题出在我的getter和setter为我的customerID; – ComplexVolcano 2013-03-08 03:26:44

0

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

谢谢 Merci!