2

I want to withdraw some money after depositing some but what I get is an error..why? for example, I deposit 100 and after that I want to withdraw 90, but all I got is an error. Another is, after depositing 100, I want to print my current balance but it prints (0) zero. why? please help.

Main Class

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

    }
}

Base Class:

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
  • 1
    Just some advice - try making a clear, more relevant question title to attract more people to try and answer. Commented Feb 10, 2013 at 15:27
  • What error are you getting? Commented Feb 10, 2013 at 15:29
  • sirAndrewMartin, sir I think I can make my question more clearer if I post my entire program, that's what I think ^__^ @GuillaumePolet sir, this is the error: run: Exception in thread "main" java.lang.NullPointerException at Banko.processWithdraw(Banko.java:107) at Banko.processor(Banko.java:49) at Banko.processDeposit(Banko.java:76) at Banko.processor(Banko.java:46) at Banko.main(Banko.java:26) Java Result: 1 Commented Feb 10, 2013 at 15:32
  • You initiate your BankAccount with withdraw but withdraw has not been initialized so it is initialized with the value 0.0. Piece of advice: drop all your static keywords and start using objects. Learning Object-Oriented programming will truly help you. Commented Feb 10, 2013 at 15:32

2 Answers 2

1

Good Experiment. Only minor mistakes.

  1. Unnecessary variable bal since you have balance in account itself
  2. Must use object myAccount everywhere.

changes in deposit

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

withdraw

    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());
Sign up to request clarification or add additional context in comments.

1 Comment

how can I thank you sir.. oh my god,, Ive been doing these in 2 straight days.... thank you very much.
1

You are declaring a new BankAccount instance, instead of using the static variable:

public static void main(String args[]) {
    // ...
    myAccount = new BankAccount(withdraw, name);
    int rc = getRC();
    processor(num, rc);
}

1 Comment

great thanks,, still when I deposit 100 and try to withdraw 90 still got an error,, why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.