我想在存了一些钱后提取一些钱,但我得到的是一个错误..为什么?例如,我存入 100,然后我想提取 90,但我得到的只是一个错误。另一个是,在存入 100 之后,我想打印我当前的余额,但它打印 (0) 零。为什么?请帮忙。
主班
import java.awt.HeadlessException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Banko{
private static String name;
private static double bal;
private static double withdraw;
private static BankAccount myAccount;
public static void main(String args[]) {
name = JOptionPane.showInputDialog(null, "Enter your name: ");
String num;
int pin;
num = JOptionPane.showInputDialog("Enter your pin number: ");
pin = Integer.parseInt(num);
JOptionPane.showMessageDialog(null, "Login Success\n" + "Name : " + name + "\n" + "Pin Number : " + pin);
BankAccount myAccount = new BankAccount(withdraw, name);
int rc = getRC();
processor(num, rc);
}
private static int getRC(){
String[] buttons = { "Deposit", "Withdraw", "Print Balance", "Exit" };
int rc = JOptionPane.showOptionDialog(
null,
"What would you like to do?",
"Confirmation",
JOptionPane.INFORMATION_MESSAGE,
0,
null,
buttons,
buttons[2]);
return rc;
}
private static void processor(String num, int rc) {
switch(rc) {
case 0:
processDeposit(num, rc);
break;
case 1:
processWithdraw(num, myAccount, rc);
break;
case 2:
processBalance(num, rc);
default:
processExit(rc);
break;
}
}
private static void processExit(int rc) throws HeadlessException {
if(rc == -1) {
JOptionPane.showMessageDialog(null, "\nThank you. Have a good day!");
System.exit(0);
}
}
private static void processDeposit(String num, int rc) throws HeadlessException, NumberFormatException {
int deposit;
String dep = JOptionPane.showInputDialog("How much would you like to deposit?\n\t$ ");
deposit = Integer.parseInt(num);
JOptionPane.showMessageDialog(null, "You have deposited $" + dep + " into the account of " + name);
String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
if(proceeds.equalsIgnoreCase("y")) {
rc = getRC();
processor(num, rc);
} else {
processExit(-1);
}
}
private static void processBalance(String num, int rc) throws HeadlessException {
JOptionPane.showMessageDialog(null, "The balance in the account of " + name + " with the pin number " + num
+ " is $" + bal);
String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
if(proceeds.equalsIgnoreCase("y")) {
rc = getRC();
processor(num, rc);
} else {
processExit(-1);
}
}
private static void processWithdraw(String num, BankAccount myAccount, int rc) throws HeadlessException, NumberFormatException {
double withdraw;
String with = JOptionPane.showInputDialog("How much would you like to withdraw?\n\t$");
withdraw = Integer.parseInt(num);
if(bal - withdraw > 0) {
myAccount.withdraw(withdraw);
JOptionPane.showMessageDialog(null, "You have withdrawn $" + withdraw + " from the account of " + name
+ ". The new balance is: " + myAccount.getBalance());
} else {
JOptionPane.showMessageDialog(
null,
"Sorry, you have insufficient funds for this operation. Your existing balance is $"
+ myAccount.getBalance());
}
String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
if(proceeds.equalsIgnoreCase("y")) {
rc = getRC();
processor(num, rc);
} else {
processExit(-1);
}
}
}
基类:
import java.util.Scanner;
public class BankAccount {
private double balance;
private String name;
public BankAccount(double b, String n) {
this.balance = b;
this.name = n;
}
public void deposit(double d) {
balance += d;
}
public void withdraw(double w) {
balance -= w;
}
public String nickname() {
System.out.print("Enter a new name: ");
Scanner kbIn = new Scanner(System.in);
String n = kbIn.nextLine();
return n;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}