怎么了

发布于 2024-12-08 12:19:26 字数 3931 浏览 0 评论 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);
    }
}
}

I've created two classes. When I choose to retrieve the deposit or withdraw historical I can only get the dates, the values that appear are only the last one deposited.
I do not understand why it is showing only the dates.

This is the first class:

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

And this is the second one:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-12-15 12:19:26

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

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 变量 - 您确实希望在每次迭代中使用单独的变量,这将迫使您要么获取现有的变量,要么获取现有的变量。要更新/显示的对象,或创建要添加的新对象。

The problem is you're repeatedly adding references to the same object:

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

You should be creating a new object each time you want to add a record to the list, e.g.

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

... and likewise elsewhere. Basically, get rid of that single atm variable declared outside the loop - you really want to be using a separate variable on each iteration, which will force you to either fetch an existing object to update/display, or create a new object to add.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文