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

Bank cash (deposit and withdrawal) - for educational purposes

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

代码如下:

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


    }
}

这是我为它的功能

制作的子class
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(" ");
        }
}

这是我的提款class。我继承了它。但我仍然不知道它是如何工作的。

代码如下:

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(" ");
    }
}
  • 首先,永远不要像对象构造函数那样命名你的方法 public double Deposite(double cash).
  • 其次,为什么您的提款 class 会延长存款?这有什么原因吗?

这就是我实现一些银行业务逻辑的方式:

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

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

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

当你

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

它创建了两个不同的实例。 Deposite 之一和 Withdraw 之一。 您假设两个实例共享相同的 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");
                }
            }

        }
    }
}