【问题标题】:Bank cash (deposit and withdrawal) - for educational purposes银行现金(存款和取款)- 用于教育目的
【发布时间】:2017-08-19 05:09:10
【问题描述】:

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

代码如下:

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

这是给我的退学课的。我继承它。但我仍然不知道它是如何工作的。

代码如下:

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

【问题讨论】:

  • 你有两个不同的班级。你在哪里存款和从哪里取款?
  • 所以只能由一个班级完成?

标签: java bank


【解决方案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);
        }
    }

【讨论】:

  • 赞成,因为它可以帮助 OP。一些建议:对于初学者来说可能太复杂了。考虑在没有Bank 类的情况下这样做。操作可以直接对Account进行操作。同样有用的是,Account 应该有 setMoney() 方法。
【解决方案2】:

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

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

【讨论】:

  • 最好在Bank 中有withdraw()deposite() 方法
  • 是的,最好在银行类本身中包含这些方法。 @c0der
【解决方案3】:

当你这样做时

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

        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多