银行现金(存款和取款) - 用于教育目的

时间:2017-08-19 05:09:11

标签: java bank

我的程序有问题。而我的问题是我不能减去我的存款价值。

以下代码:

=INDEX('workers list'!B2:B10,MATCH(A1,'workers list'!A2:A10,0))

这是我为其功能

做的子类
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");
            }
        }


    }
}

这是我的退学课程。我继承了它。但我还是不知道它是如何工作的。

以下代码:

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

3 个答案:

答案 0 :(得分:1)

  • 首先,永远不要将您的方法命名为对象构造函数 public double Deposite(double cash)
  • 其次,为什么你的Withdraw课程会扩展Deposite?这有什么理由吗?

这就是我如何实现一些银行逻辑:

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

答案 1 :(得分:0)

你的程序有一些基本问题,这里是代码::: 您应该已经存入和取出单一帐户。那是你的基本错误。

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

答案 2 :(得分:0)

当你这样做时

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

它创建了两个不同的实例。其中一个DepositeWithdraw之一。 您假设两个实例共享相同的double cash,但它们不共享。

如果你想从简单的事情开始,你可以做一些事情:

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

        }
    }
}