2011-10-07 287 views
0

我創建了兩個類。當我選擇取回存款或取消歷史時,我只能得到日期,出現的值只是最後一個存入的值。
我不明白爲什麼它只顯示日期。發生了什麼

這是第一類:

import javax.swing.*; 
import java.text.*; 
import java.util.*; 

public class AtmTime{ 
private String date; 
private double cash, funds; 
private DecimalFormat dfmt = new DecimalFormat("$,###,##0.00"); 

public void addFunds(double moneyIn){ 
    funds = funds + moneyIn; 
} 
public void withdrawFunds(double moneyOut){ 
    while(moneyOut > funds){ 
     JOptionPane.showMessageDialog(null,"You have tried to withdraw " + dfmt.format(moneyOut), "Insufficient funds: " + dfmt.format(funds), JOptionPane.INFORMATION_MESSAGE); 
     moneyOut = Double.parseDouble(JOptionPane.showInputDialog(null,"Try again ", "Withdraw", JOptionPane.INFORMATION_MESSAGE)); 
    } 
    funds = funds - moneyOut; 
} 
public double getFunds(){ 
    return funds; 
} 
public double getCash(){ 
    return cash; 
} 
public String getDate(){ 
    DateFormat dfm = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
    Date dates = new Date(); 
    return dfm.format(dates); 
} 
} 

這是第二個:

import javax.swing.*; 
import java.text.*; 
import java.util.*; 

public class AtmTimeApp{ 
public static void main(String []args){ 
    int control = 0; 
    String date; 
    double cash = 0; 
    DecimalFormat dfmt = new DecimalFormat("$,###,##0.00"); 
    List<AtmTime> atmListIn = new ArrayList<AtmTime>(); 
    List<AtmTime> atmListOut = new ArrayList<AtmTime>(); 
    AtmTime atm = new AtmTime(); 

    while(control !=6){ 
     control = Integer.parseInt(JOptionPane.showInputDialog(null,"1- Funds \n 2- Add money \n 3- Withdraw \n4- Deposit history \n5- Withdraw history \n6- Exit", "Choose an option below", JOptionPane.INFORMATION_MESSAGE)); 
     if(control == 2){ 
      date = atm.getDate(); 
      double moneyIn = Double.parseDouble(JOptionPane.showInputDialog(null,"Amount ", "Add Money", JOptionPane.INFORMATION_MESSAGE)); 
      atm.setDate(date); 
      atm.addFunds(moneyIn); 
      atmListIn.add(atm); 
      JOptionPane.showMessageDialog(null,"Your new funds " + dfmt.format(atm.getFunds()), "On " + atm.getDate(), JOptionPane.INFORMATION_MESSAGE); 
     } 
     else if(control == 1){ 
       JOptionPane.showMessageDialog(null,"" + dfmt.format(atm.getFunds()), "Total in your account", JOptionPane.INFORMATION_MESSAGE); 
     } 
      else if(control == 3){ 
       date = atm.getDate(); 
       double moneyOut = Double.parseDouble(JOptionPane.showInputDialog(null,"Amount ", "Withdraw Money", JOptionPane.INFORMATION_MESSAGE)); 
       atm.withdrawFunds(moneyOut); 
       atmListOut.add(atm); 
       JOptionPane.showMessageDialog(null,"New funds " + dfmt.format(atm.getFunds()), "Operation Successful completed", JOptionPane.INFORMATION_MESSAGE); 
      } 
       else if (control == 4){ 
         for (int i=0;i<atmListIn.size();i++){ 
          atm = atmListIn.get(i); 
          JOptionPane.showMessageDialog(null,"Deposit of " + dfmt.format(atm.getCash()), "When " + atm.getDate(), JOptionPane.INFORMATION_MESSAGE); 
         } 
        } 
        else if (control == 5){ 
         for (int i=0; i<atmListOut.size();i++){ 
          atm = atmListOut.get(i); 
          JOptionPane.showMessageDialog(null,"Withdraw of " + dfmt.format(atm.getCash()), "When " + atm.getDate(), JOptionPane.INFORMATION_MESSAGE); 
         } 
        } 
       else if(control !=1 && control !=2 && control !=3 && control !=4 && control !=5 && control !=6) 
        JOptionPane.showMessageDialog(null,"Please choose a valid option", "Invalid number", JOptionPane.ERROR_MESSAGE); 
    } 
} 
} 

回答

2

的問題是您不斷添加引用相同的對象:

List<AtmTime> atmListIn = new ArrayList<AtmTime>(); 
List<AtmTime> atmListOut = new ArrayList<AtmTime>(); 
AtmTime atm = new AtmTime(); 

while(control !=6){ 
    control = Integer.parseInt(...); 
    if(control == 2){ 
     date = atm.getDate(); 
     double moneyIn = Double.parseDouble(...); 
     atm.setDate(date); 
     atm.addFunds(moneyIn); 
     atmListIn.add(atm); 

您應該創建一個新的對象每次喲你想添加一個記錄到列表中,例如

List<AtmTime> atmListIn = new ArrayList<AtmTime>(); 
List<AtmTime> atmListOut = new ArrayList<AtmTime>(); 

while(control != 6) { 
    control = Integer.parseInt(...); 
    if(control == 2) { 
     date = atm.getDate(); 
     double moneyIn = Double.parseDouble(...); 
     AtmTime atm = new AtmTime(date, moneyIn); 
     atmListIn.add(atm); 

...和其他地方一樣。基本上,擺脫那個單一的atm變量聲明的循環外 - 你真的想要每次迭代使用一個單獨的變量,這將迫使你要獲取現有的對象更新/顯示,或創建一個新的對象加。

+0

謝謝你,它的工作。我明白了什麼意思。我只有一個問題。當我在一個循環中創建一個對象時,這個對象只能在這個循環中被訪問,對嗎?在for循環中,要訪問atm對象,我必須創建Atm atm = atm.getDate();爲什麼我必須寫出類名Atm?乾杯 – Camus

+0

@Camus:你在循環中聲明瞭*變量* - 當你聲明變量的時候你寫了類名。只要你仍然在循環的每次迭代中創建一個新的實例,你就可以在循環之外聲明該變量......但是將變量聲明爲儘可能小的範圍通常是個好主意。 –