java - 我如何在存款或取款时更新我的余额?
问题描述
我希望在我存款或取款时更新我的余额。我还是 Java、GUI、ArrayList 的新手。我想创建一个简单的银行程序,只需简单的登录显示选项(取款、存款、余额)。我是否在存款或取款时使用了错误的代码来更新余额?我想得到你的帮助或提示。我很抱歉,如果它这么久。// 账户类
public class Account {
private double Withdraw;
private double Deposit;
private double Balance;
public Account (double Withdraw, double Deposit, double Balance) {
this.Withdraw = Withdraw;
this.Deposit = Deposit;
this.Balance = Balance;
}
public Account () {
}
public void Withdraw (double Sum) {
this.Balance = this.Balance - Sum;
}
public void Deposit (double Sum) {
this.Balance = this.Balance - Sum;
}
public void setBalance (double Blnc) {
this.Balance = Blnc;
}
public double getBalance () {
return this.Balance;
}
public double getWithdraw()
{
return this.Withdraw;
}
public double getDeposit()
{
return this.Deposit;
}
}
// Balance class
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
public class Balance implements ActionListener {
private List<Account> store;
private static JLabel balance;
private static JTextField Balance;
JFrame frame = new JFrame();
JButton OK = new JButton("OK");
// JButton Show = new JButton ("SHOW");
// JTextArea text = new JTextArea();
Balance() {
store = new ArrayList<Account>();
frame.setSize(500, 300);
frame.setTitle("BALANCE ACCOUNT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
balance = new JLabel("YOUR BALANCE IS: " + acnt.getBalance());
balance.setBounds(200, 50, 200, 50);
frame.add(balance);
// text = new JTextArea();
// text.setBounds(200, 80, 100, 50);
// frame.add(text);
// Show.addActionListener(this);
// Show.setBounds(250, 200, 100, 20);
// frame.add(Show);
OK.addActionListener(this);
OK.setBounds(380, 200, 100, 20);
frame.add(OK);
frame.setVisible(true);
}
Account acnt = new Account();
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()== OK) {
acnt.setBalance(acnt.getBalance()+Integer.parseInt(balance.getText()));
frame.setVisible(false);
ATMOptionPanel atmoptionpanel = new ATMOptionPanel();
// text.append("\nBalance="+acnt.getBalance());
}
}
}
// Deposit Class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
// import java.util.Scanner;
public class Deposit implements ActionListener{
private List<Account> store;
//Scanner scanner = new Scanner(System.in);
int Balance = 0, Deposit, Withdraw;
private static JLabel deposit;
private static JTextField depositText;
JButton submit = new JButton("SUBMIT");
JButton back = new JButton("BACK");
JFrame frame = new JFrame();
Deposit () {
store = new ArrayList<Account>();
frame.setSize(500, 300);
frame.setTitle("DEPOSIT MENU");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
deposit = new JLabel("DEPOSIT: ");
deposit.setBounds(150, 50, 100, 50);
frame.add(deposit);
depositText = new JTextField(20);
depositText.setBounds(250, 65, 100, 20);
frame.add(depositText);
submit.addActionListener(this);
submit.setBounds(230, 100, 100, 20);
frame.add(submit);
back.addActionListener(this);
back.setBounds(340, 100, 100, 20);
frame.add(back);
frame.setVisible(true);
}
Account acnt = new Account();
@Override
public void actionPerformed(ActionEvent e) {
// int Deposit = Integer.parseInt(depositText.getText());
if (e.getSource()== submit) {
store.add(acnt);
frame.setVisible(false);
Balance balance = new Balance();
acnt.setBalance(acnt.getBalance());
}
else if (e.getSource()== back) {
frame.setVisible(false);
store.add(acnt);
ATMOptionPanel atmoptionpanel = new ATMOptionPanel();
}
}
}
// 退出类
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Withdraw implements ActionListener{
private List<Account> store;
int Balance = 0, Withdraw, Deposit;
private static JLabel withdraw;
private static JTextField withdrawText;
JButton back = new JButton("BACK");
JButton submit = new JButton("SUBMIT");
JFrame frame = new JFrame();
Withdraw() {
store = new ArrayList<Account>();
frame.setSize(500, 300);
frame.setTitle("WITHDRAW MENU");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
withdraw = new JLabel("WITHDRAW: ");
withdraw.setBounds(150, 50, 100, 50);
frame.add(withdraw);
withdrawText = new JTextField(20);
withdrawText.setBounds(250, 65, 100, 20);
frame.add(withdrawText);
back.setBounds(320, 150, 100, 20);
frame.add(back);
submit.addActionListener(this);
submit.setBounds(200, 150, 100, 20);
frame.add(submit);
frame.setVisible(true);
}
Account acnt = new Account();
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource()== submit) {
if (Balance <= Withdraw) {
acnt.setBalance(acnt.getBalance()-Integer.parseInt(withdrawText.getText()));
store.add(acnt);
JOptionPane.showMessageDialog(null, "Withdraw success!.", "SUCCESS!", JOptionPane.INFORMATION_MESSAGE);
} else if (Balance >= Withdraw)
{
JOptionPane.showMessageDialog(null, "You don't have enough balance\nPlease deposit an amount first.", "INSUFFICIENT BALANCE!", JOptionPane.ERROR_MESSAGE);
}
if (e.getSource()== back) {
frame.setVisible(false);
}
}
}
}
// OptionPanel
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ATMOptionPanel implements ActionListener {
private static JLabel BankOptions;
JFrame frame = new JFrame();
JButton withdraw = new JButton("WITHDRAW");
JButton deposit = new JButton("DEPOSIT");
JButton balance = new JButton("BALANCE");
ATMOptionPanel() {
frame.setSize(500, 300);
frame.setTitle("BANK ACCOUNT");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BankOptions = new JLabel ("MENU");
BankOptions.setBounds(230, 10, 200, 20);
frame.add(BankOptions);
withdraw.addActionListener(this);
withdraw.setBounds(100, 80, 140, 25);
withdraw.setFocusable(false);
frame.add(withdraw);
deposit.addActionListener(this);
deposit.setBounds(250, 80, 140, 25);
deposit.setFocusable(false);
frame.add(deposit);
balance.addActionListener(this);
balance.setBounds(180, 120, 140, 25);
balance.setFocusable(false);
frame.add(balance);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==withdraw) {
frame.setVisible(false);
Withdraw withdraw = new Withdraw();
}
else if (e.getSource()==deposit) {
frame.setVisible(false);
Deposit deposit = new Deposit();
}
else if (e.getSource()==balance) {
frame.setVisible(false);
Balance balance = new Balance();
}
}
}
// Login
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Login implements ActionListener {
private static JLabel welcome;
private static JLabel userLabel;
private static JTextField userText;
private static JLabel passwordLabel;
private static JTextField passwordText;
JFrame frame = new JFrame();
JButton login = new JButton("Login"); // create a Button with a text of "Login"
Login() {
frame.setSize(500, 300);
frame.setTitle("BANK ACCOUNT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.BLACK);
frame.setLayout(null);
welcome = new JLabel ("WELCOME TO THE LOGIN PANEL",JLabel.CENTER);
welcome.setBounds(150, 10, 220, 20);
frame.add(welcome);
userLabel = new JLabel ("USERNAME:\t"); // Word "User" will appear.
userLabel.setBounds(100, 50, 100, 50); // (x, y, width, height)
frame.add(userLabel);
userText = new JTextField(20); // rectangle box will appear at the left side of the word "User".
userText.setBounds(250, 65, 80, 20);
frame.add(userText);
passwordLabel = new JLabel ("PASSWORD:\t"); // Word "Password" will appear.
passwordLabel.setBounds(100, 90, 100, 50);
frame.add(passwordLabel);
passwordText = new JTextField(20); // rectangle box will appear at the left side of the word "Password".
passwordText.setBounds(250, 105, 80, 20);
frame.add(passwordText);
login.addActionListener(this);
login.setBounds(200, 150, 100, 20);
frame.add(login);
frame.setVisible(true);
System.out.println("USERNAME:\tBank");
System.out.println("PASSWORD:\tabcd123");
}
@Override
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String password = passwordText.getText();
if (user.equals("Bank") && (password.equals("abcd123"))) {
System.out.println("\nLogin Successfully!");
frame.setVisible(false);
ATMOptionPanel atmoptionpanel = new ATMOptionPanel();
}
else
{
JOptionPane.showMessageDialog(null, "PLEASE INPUT THE CORRECT USERNAME & PASSWORD! \n Write what is on the console.", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
// ATM主类
public class ATMMain {
public static void main(String[] args) {
Login login = new Login();
}
}
解决方案
推荐阅读
- android - 当应用程序处于后台或被杀死时,FCM onMessageReceived() 未被调用
- .net - 连接字符串中缺少必需的空白错误是什么?
- java - 我使用递归按字母顺序获取第一个字符串的 java 代码给出了不正确的输出。我需要添加辅助方法吗?
- mysql - Discord.js 中的节点 mysql 返回空结果/数组
- sql - 具有多个(相同ID不同日期和不同图像)的SQL服务器查询(按最后日期过滤)
- kotlin - XMLPullParser 只返回一个元素
- multithreading - 为什么 Rust 会阻止多个可变引用?
- linux - 绕过 Xserver 显示管理
- methods - 在 Julia 中运行 Union 类型
- operating-system - 进程/线程可以在中断被禁用时运行吗?