2013-03-07 53 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!