RJ Berin
RJ Berin

Reputation: 13

Bank cash (deposit and withdrawal) - for educational purposes

i have a problem on my program. and my problem is that i cannot minus my withdrawal from my deposit value.

code below:

public static void main(String[] args) {
    double cash;
    boolean more = true;

    Deposite dep = new Deposite();
    Withdraw with = new Withdraw();

    while (more) {
        cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Deposite"));
        dep.Deposite(cash);
        dep.print(); 


        int con = JOptionPane.YES_NO_OPTION;
     int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?","DEPOSITORY",con);

        if (con1 == 1) {
            int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?","WITHDRAWAL",con);
            if (con3 == 0) {
                cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
                with.Withdraw(cash);
                with.print();
                System.out.println("Thanks");
            }
        }


    }
}

and this is my subclass that i have made for its functions

public class Deposite {
    private double depcash;

        public double Deposite(double cash){
            depcash += cash;

            return this.depcash;
        }
        void print(){
            System.out.printf("Your deposite is $%5.2f",depcash);
            System.out.println(" ");
        }
}

and this is for my withdrawal class. i inherit it. but i still dont know how it works.

code below :

public class Withdraw extends Deposite {
    double cash;

    public double Withdraw(double withdraw){
        super.Deposite(withdraw);
        cash -=withdraw;
        return cash;
    }
    void print (){
        System.out.printf("You Cash Balance now is $%5.2f",cash);
        System.out.println(" ");
    }
}

Upvotes: 0

Views: 1076

Answers (3)

c0der
c0der

Reputation: 18792

When you do

Deposite dep = new Deposite();
Withdraw with = new Withdraw();

it creates two different instances. One of Deposite and one of Withdraw. You assume that both instances share the same double cash , but they don't.

If you want to start with something simple you could do something like :

import javax.swing.JOptionPane;

public class Cash {

    private double depcash;

    public double deposite(double cash){ //stick to java naming conventions

        depcash += cash;
        return depcash;
    }

    public double withdraw(double withdraw){

        return deposite(- withdraw);
    }

    void print(){

        //wrong - System.out.printf("Your deposite is $%5.2f",depcash);
        System.out.printf("Your cash balance is $%5.2f",depcash);
        System.out.println(" ");
    }


    public static void main(String[] args) {

        double sum;
        boolean more = true;

        Cash cash = new Cash();

        while (more) { //how do you stop ? what makes more false ? 

            sum = Double.parseDouble(JOptionPane.showInputDialog("Cash deposite"));
            cash.deposite(sum);
            cash.print();

            int con = JOptionPane.YES_NO_OPTION;
            int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?","DEPOSITORY",con);

            if (con1 == 1) {
                int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?","WITHDRAWAL",con);
                if (con3 == 0) {
                    sum = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
                    cash.withdraw(sum);
                    cash.print();
                    System.out.println("Thanks");
                }
            }

        }
    }
}

Upvotes: 0

Abhishek Honey
Abhishek Honey

Reputation: 645

Your program have some basic problem here is the code::: You should have made the single account for deposit and withdraw. That was your basic mistake.

import javax.swing.JOptionPane;

public class Bank {
    public static double totalCash = 0;

    public static void main(String[] args) {
        boolean more = true;
        Deposite dep = new Deposite();
        Withdraw with = new Withdraw();
        while (more) {
            double cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Deposite"));
            dep.depositeCash(cash);
            dep.print();
            int con = JOptionPane.YES_NO_OPTION;
            int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?", "DEPOSITORY", con);
            if (con1 == 1) {
                int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?", "WITHDRAWAL", con);
                if (con3 == 0) {
                    cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
                    with.withdrawCash(cash);
                    with.print();
                    System.out.println("Thanks");
                    more = false;
                }
            }
        }
    }
}

class Withdraw {
    public double withdrawCash(double withdraw) {
        Bank.totalCash -= withdraw;
        return Bank.totalCash;
    }

    void print() {
        System.out.printf("You Cash Balance now is $%5.2f", Bank.totalCash);
        System.out.println(" ");
    }
}

class Deposite {
    public double depositeCash(double cash) {
        Bank.totalCash += cash;
        System.out.println(Bank.totalCash);
        return Bank.totalCash;
    }

    void print() {
        System.out.printf("Your deposite is :" + Bank.totalCash);
        System.out.println(" ");
    }
}

Upvotes: 0

vk23
vk23

Reputation: 498

  • First of all, never name your methods like object constructors public double Deposite(double cash).
  • Secondly, why would your Withdraw class extend Deposite? Is there any reason for this?

That is how I would implement some banking logic:

  Bank bank = new Bank();
  Account account = new Account(123.50);
  bank.execute(account, new Deposit(), 1);
  bank.execute(account, new Withdraw(), 13.50);


    private static interface Operation {
        double apply(Account account, double value);
    }

    private static class Deposit implements Operation {

        @Override
        public double apply(Account account, double value) {
            return account.getMoney() - value;
        }
    }

    private static class Withdraw implements Operation {

        @Override
        public double apply(Account account, double value) {
            return account.getMoney() + value;
        }
    }

    private static class Account {
        private final double money;

        public Account(double money) {
            this.money = money;
        }

        public double getMoney() {
            return money;
        }

    }

    private static class Bank {
        public void execute(Account account, Operation operation, double amount) {
            operation.apply(account, amount);
        }
    }

Upvotes: 1

Related Questions