【问题标题】:What is happening怎么了
【发布时间】:2011-10-07 05:58:45
【问题描述】:

我创建了两个类。当我选择检索存款或取款历史时,我只能获取日期,出现的值只是最后一个存款。
我不明白为什么它只显示日期。

这是第一堂课:

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);
    }
}
}

【问题讨论】:

    标签: java object arraylist


    【解决方案1】:

    问题是您重复添加对同一个对象的引用:

    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);
    

    您应该在每次想要向列表中添加记录时创建一个 new 对象,例如

    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 变量 - 你真的想要在每次迭代中使用单独的变量,这将迫使你要么获取现有对象以更新/显示,或者创建一个新对象来添加。

    【讨论】:

    • 谢谢老哥,成功了。我明白什么意思了。我只有一个问题。当我在循环中创建一个对象时,这个对象只会在这个循环中被访问,对吧?在 for 循环中,要访问 atm 对象,我必须创建 Atm atm=atm.getDate();为什么我必须写类名 Atm?干杯
    • @Camus:你在循环中声明了 variable - 你在声明变量时写下类名。您可以在循环外声明变量,只要您仍然在循环的每次迭代中创建一个新实例...但通常最好在尽可能小的范围内声明变量可能。
    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 2015-06-29
    • 2012-09-05
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多