0

I have a homework in java. I am tasked to build a bank that can withdraw, deposit and inquire balance. My problem is that I couldn't get to update my balance after deposits and withdrawals... I've tried everything I could do but still cant get the logic. Can someone help add to my program... thanks

import java.util.Scanner;

public class bankJava 
{

    Scanner input = new Scanner(System.in);


    double balance;
    double amount;

    public void withdraw() 
    {

        System.out.println("Enter amount: ");
        amount = input.nextInt();

        balance = balance - amount;


    }

    public void deposit()
    {

        System.out.println("Enter amount: ");
        amount = input.nextInt();
        balance = balance + amount;

    }

    public void accBalance()
    {


    }



}

---------------------------------MAIN--------------------------------

import java.util.Scanner;

public class bankJavaTest {

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        int action;

        bankJava wdraw = new bankJava();
        bankJava dposit = new bankJava();
        bankJava balanceInquiry = new bankJava();
        bankJava amount = new bankJava();

        do{

        System.out.println("Choose Action: ");
        System.out.println("(1) Withdraw");
        System.out.println("(2) Deposit");
        System.out.println("(3) Balance Inquiry");
        System.out.println("(4) Exit");
        action = input.nextInt();


        switch(action){



        //---------WITHDRAW------------//
        case 1 :

            System.out.println("******Withdraw******");
            wdraw.withdraw();
            System.out.println("***************************");


            break;

        //---------DEPOSIT------------//
        case 2 :

            System.out.println("******Deposit******");
            dposit.deposit();
            System.out.println("***************************");
            break;

        //-----------Balance Inquiry-------//
        case 3 :

            System.out.println("******Balance Inquiry******");
            balanceInquiry.accBalance();
            System.out.println("***************************");

            break;

        case 4 :

            System.out.println("Thank you for choosing our bank!");
            break;

        default :

            System.out.println("Invalid action.");
            break;

        } 
        }while(action != 4);

    }


}
2se4n
  • 1
  • 1
  • 6
  • I tried setting balance to 0 and as I deposit and check balance, it is still 0. I have no idea what code to add to update balance after doing some actions. – 2se4n Jun 21 '19 at 16:12

2 Answers2

0

Why you are instatiating 4 different JavaBank? Doing this for each operation you will execute each method in a different object. If I understand well your question I think you could resolve your problem easily working in the same object.

import java.util.Scanner;

public class bankJavaTest {

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        int action;

        bankJava myJavaBank = new bankJava(); //creating the bank


        do{

        System.out.println("Choose Action: ");
        System.out.println("(1) Withdraw");
        System.out.println("(2) Deposit");
        System.out.println("(3) Balance Inquiry");
        System.out.println("(4) Exit");
        action = input.nextInt();


        switch(action){



        //---------WITHDRAW------------//
        case 1 :

            System.out.println("******Withdraw******");
            myJavaBank.withdraw(); //withdrawing from it
            System.out.println("***************************");


            break;

        //---------DEPOSIT------------//
        case 2 :

            System.out.println("******Deposit******");
            myJavaBank.deposit(); //deposit from it
            System.out.println("***************************");
            break;

        //-----------Balance Inquiry-------//
        case 3 :

            System.out.println("******Balance Inquiry******");
            myJavaBank.accBalance();
            //You don't post this method but I suppose it will refer to the same bank
            System.out.println("***************************");

            break;

        case 4 :

            System.out.println("Thank you for choosing our bank!");
            break;

        default :

            System.out.println("Invalid action.");
            break;

        } 
        }while(action != 4);

    }


}

Now should work. With your code you had 4 different bank, one only for deposit, one only for withdraw and so on. So one bank will continue to increase money, and one continue to decrease in negative.

Morover the amount parameter should not be a JavaBank parameter, bur a local variable inside each method so that it does not define a Bank.

Something like

public class bankJava 
{

Scanner input = new Scanner(System.in);


double balance;

public void withdraw() 
{

    System.out.println("Enter amount: ");
    double amount = input.nextInt();

    balance = balance - amount;


}

public void deposit()
{

    System.out.println("Enter amount: ");
    double amount = input.nextInt();
    balance = balance + amount;

}

I also suggest to change the input.nextInt() with input.nextDouble() so that you create the amount as a double.

If you don't see the Balance Inquiry is because obviously you have the accBalance() method blank. Edit it like this:

public void accBalance(){
System.out.println("Your balance is: "+this.balance);
}
micrus_
  • 16
  • 2
  • Your code works but the Balance Inquiry method must be shown. For example, Im going to deposit 300 and then withdraw 100 then as I choose Balance Inquiry, there is an output saying "Your current balance is 200". – 2se4n Jun 21 '19 at 16:46
  • This because your _accInquiry_ method is blank. Insert inside _accInquiry_ (in the javaBank class) a simple `System.out.println("Your current balance is "+this.balance);` – micrus_ Jun 21 '19 at 16:56
  • Oh yea got it! Thank you so much for your time. Thank you very much! – 2se4n Jun 21 '19 at 17:01
  • I edited the main answer so that you could read easily. Your welcome bro, glad to have helped you! :) – micrus_ Jun 21 '19 at 17:01
  • Thanks sir! +reputation – 2se4n Jun 21 '19 at 17:06
0
import java.util.Scanner;

public class BankJava {

    double balance = 0;
    double amount;

    public void withdraw(int amount) {

        balance = balance - amount;

    }

    public void deposit(int amount) {

        balance = balance + amount;
    }

    public double showBalance() {
        return balance;
    }


    public static void main(String[] args) {
        BankJava bank = new BankJava();
        Scanner input = new Scanner(System.in);
        int action;
        int amount;
         do{

                System.out.println("Choose Action: ");
                System.out.println("(1) Withdraw");
                System.out.println("(2) Deposit");
                System.out.println("(3) Balance Inquiry");
                System.out.println("(4) Exit");
                action = input.nextInt();
                switch(action){
                //---------WITHDRAW------------//
                case 1 :

                    System.out.println("******Withdraw******");
                    System.out.println("enter amount:");
                    amount = input.nextInt();
                    bank.withdraw(amount);
                    System.out.println("***************************");
                    System.out.println("Your balance is now: " + bank.showBalance());
                    break;

                //---------DEPOSIT------------//
                case 2 :

                    System.out.println("******Deposit******");
                    System.out.println("enter amount:");
                    amount = input.nextInt();
                    bank.deposit(amount);
                    System.out.println("***************************");
                    System.out.println("Your balance is now: " + bank.showBalance());
                    break;

                //-----------Balance Inquiry-------//
                case 3 :

                    System.out.println("******Balance Inquiry******");
                    System.out.println("Your balance is: " + bank.showBalance());
                    System.out.println("***************************");

                    break;

                case 4 :

                    System.out.println("Thank you for choosing our bank!");
                    break;

                default :

                    System.out.println("Invalid action.");
                    break;

                } 
                }while(action != 4);

            }
    }

Try this code, compare with yours and figure out whats wrong, also you can ask me if you need more help

BugsForBreakfast
  • 712
  • 10
  • 30
  • There is a problem on this statement " System.out.println("Your balance is now: " + bank.showBalance());" it says 'void' type not allowed here... and also, in Balance Inquiry method, as soon as i choose that action, it must have its own class to perform. – 2se4n Jun 21 '19 at 16:48
  • Did you copy my showBalance() method? make sure it is public double showBalance() { return balance; } – BugsForBreakfast Jun 21 '19 at 16:49
  • oh got it. I missed that part hehe. Now it works perfectly! Thank you so much sir for helping me! Thank you very much! – 2se4n Jun 21 '19 at 16:58
  • Inquiry method shows the actual balance? just use showBalance() method there then :) edited my answer so you just have to copy and paste it, and you are welcome mate, you can ask me anything about that – BugsForBreakfast Jun 21 '19 at 16:58
  • Yup. Now I have to organize and clean the code(your code technically). Thanks man! Appreciate alot! – 2se4n Jun 21 '19 at 17:04