我创建了一个主要的 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;
}
}
问题是当我为存款、取款、转账输入负数时,我没有收到我在主类中输入的错误
有什么想法吗?