2

入金後に出金したいのですが、エラーが発生します。なぜですか?たとえば、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;
    }

} 
4

2 に答える 2

1

BankAccount静的変数を使用する代わりに、新しいインスタンスを宣言しています。

public static void main(String args[]) {
    // ...
    myAccount = new BankAccount(withdraw, name);
    int rc = getRC();
    processor(num, rc);
}
于 2013-02-10T15:36:21.577 に答える
1

良い実験。小さな間違いだけ。

  1. balアカウント自体にバランスがあるため、不要な変数
  2. myAccountどこでもオブジェクトを使用する必要があります。

預金の変更

    deposit = Integer.parseInt(dep);
    JOptionPane.showMessageDialog(null, "You have deposited $" 
                 + dep + " into the account of " + name);
    myAccount.setBalance(myAccount.getBalance() + deposit);

撤退

    withdraw = Integer.parseInt(with);
    if (myAccount.getBalance() - withdraw > 0) {

getBalance

 JOptionPane.showMessageDialog(null, "The balance in the account of " 
    + name + " with the pin number " + num
    + " is $" + myAccount.getBalance());
于 2013-02-10T15:47:15.920 に答える