0

我创建了一个主要的 Java 程序,它的工作方式类似于银行向用户提供余额、提款和转账。

import java.util.Scanner;

public class Lab12 {
    public static void main(String[] args)
    {
        //Creating Two BankAccounts
        BankAccount B1 = new BankAccount(1000, "ASU_ACCOUNT_110");
        BankAccount B2 = new BankAccount(500, "ASU_ACCOUNT_100");
        double amount;
        Scanner scan  = new Scanner(System.in);

        //Account Deposit
        System.out.println("Please Enter amount to deposit into ASU_ACCOUNT_110 Account");
        amount = scan.nextDouble();
        if(!B1.deposit(amount))
            System.out.println("Error depositing amount in account. Please enter positive value.");
        else
            System.out.println("Successfully deposited $"+amount+". The current balance is "+B1.getBalance() );

        //Account Withdrawal
        System.out.println("Please Enter the amount you would like to withdraw from ASU_ACCOUNT_110");
        amount = scan.nextDouble();
        if(!B1.withdraw(amount))
            System.out.println("Error withdrawing amount. You are either overdrawing or you have entered a negative value");
        else
            System.out.println("Successfully withdrew $"+amount+". The current balance is "+B1.getBalance());

        //Account Transfer
        System.out.println("Please Enter the amount you would like to transfer from ASU_ACCOUNT_110 to ASU_ACCOUNT_100");
        amount = scan.nextDouble();
        if(!B1.transfer(amount, B2))
        {
            System.out.println("Error transfering amount. You are either overdrawing or you have entered a negative value");
        }
        else
        {
            System.out.println("Successfully transferred $"+amount+".\nThe current balance in ASU_ACCOUNT_110 is "+B1.getBalance());
            System.out.println("The current balance in ASU_ACCOUNT_100 is "+B2.getBalance());
        }

        //Account Display
        System.out.println("\nThe details of the two accounts are:");
        System.out.println("------------------------------------------");
        display(B1);
        System.out.println("------------------------------------------");
        display(B2);
    }

    public static void display(BankAccount B)
    {
        System.out.println("The Account Number is "+B.getAccountNumber());
        System.out.println("The balance is "+B.getBalance());
    }
}

我创建了第二个类,它有方法并获取和调用余额

public class BankAccount {

    private String AccountNumber;
    private double balance;

    public BankAccount(double balance, String accountNumber) 
    {
        this.balance = balance;
        this.AccountNumber = accountNumber;
    }

    public boolean deposit(double amount) 
    {
        return true;
    }

    public double getBalance() 
    {
        return balance;
    }

    public boolean withdraw(double amount) 
    {
        return true;
    }

    public boolean transfer(double amount, BankAccount b2) 
    {
        return true;
    }

    public String getAccountNumber() 
    {
        return AccountNumber;
    }
}

问题是当我为存款、取款、转账输入负数时,我没有收到我在主类中输入的错误

有什么想法吗?

4

3 回答 3

2

您不检查 BankAccount.deposit(double) 中任何地方的数字是否为负数。您需要检查代码是否为负,如下所示:

public boolean deposit(double amount) 
{
    if(amount<0)
    {
        return false;
    }else{
        return true;
    }
}
于 2013-04-06T02:34:16.050 回答
2

您在 BankAccount 中的方法总是返回 true。除非您提供这样做的实现,否则您不能期望它们有时会发出失败信号。

例如:

public boolean withdraw(double amount) 
{
    return true;
}

永远不会发出失败信号,因为它总是返回 true。为了让您的方法按照您希望的方式运行,您必须添加更复杂的行为。就像是:

public boolean withdraw(double amount) 
{  
    //if the withdrawal is positive and does not exceed the balance
    if (amount >= 0 && amount <= balance)
    {
         //remove money and signal success
         balance -= amount;
         return true;
    }
    //if either condition failed, signal failure
    return false;
}

看起来您只是忘记实现这些方法。要么就是你,要么你对编程有根本的误解。

于 2013-04-06T02:34:30.540 回答
0

存款、取款和转账方法总是返回 true。因此,您将永远不会看到错误消息。

于 2013-04-06T02:31:58.173 回答