我创建了一个主要的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;
}
}
问题是,当我输入存款,提款,转账的负数时,我没有收到我在主类中输入的错误
有什么想法吗?
答案 0 :(得分:2)
您不会在BankAccount.deposit(double)中的任何位置检查该数字是否为负数。您需要检查代码是否为负数:
public boolean deposit(double amount)
{
if(amount<0)
{
return false;
}else{
return true;
}
}
答案 1 :(得分: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;
}
看起来你只是忘了实现这些方法。或者你对编程有一个基本的误解。
答案 2 :(得分:0)
存款,取款和转账方式总是正确的。因此,您永远不会看到错误消息。