入金後に出金したいのですが、エラーが発生します。なぜですか?たとえば、100を入金し、その後90を引き出したいのですが、エラーが発生するだけです。もう1つは、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;
}
}