0
我有一项任务是创建一个小型银行业务计划,该计划可以保留两个帐户,定期存款和储蓄。然而,当我准备完成一个粗略的版本时,我读到了提示,我想用我们了解到的那天我生病的那一天,例外。在atm项目中实现例外
我不清楚如何使用异常,或者如果我应该制作自己的异常类。我想要一个例外,以便在程序中的任何时候,如果输入以'q'或'Q'开头的内容,程序退出并结束。另一项规定是,如果储蓄低于25美元以冻结帐户。我想象例外对于这个功能来说是理想的。
public abstract class BankAccount {
int balance;
int deposits;
int withdrawals;
int annualInterestRate;
int charges;
public void _BankAccount(int newBalance, int newInterest) {
balance = newBalance;
annualInterestRate = newInterest;
}
public void Deposit(int newDeposit) {
balance = balance + newDeposit;
deposits++;
}
public void Withdraw(int newWithdraw) {
balance = balance - newWithdraw;
withdrawals++;
}
public void calcInterest() {
int monthlyInterestRate = (annualInterestRate/12);
int monthlyInterest = balance * monthlyInterestRate;
balance = balance + monthlyInterest;
}
public void monthlyProcess() {
balance = balance - charges;
calcInterest();
deposits = 0;
withdrawals = 0;
}
}
public class SavingsAccount extends BankAccount {
boolean status;
public void savingWithdraw(int newWithdraw) {
if (balance < 25) {
System.out.println("Error – Not enough funds.");
} else {
Withdraw(newWithdraw);
}
}
public void savingDeposit(int newDeposit) {
if (balance < 25) {
Deposit(newDeposit);
System.out.println("Savings account is now active.");
} else {
Deposit(newDeposit);
}
}
public void savingMonthlyProcess() {
if (withdrawals > 4) {
charges = ((withdrawals - 4) * 1);
balance = balance - ((withdrawals - 4) * 1);
if (balance < 25) {
System.out.println("Savings account is now inactive.");
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String choice;
int num = 0;
boolean quit = false;
do {
System.out.println("Which account would you like to access, regular or savings?:");
choice = in.nextLine();
if (choice.equals("regular")) {
num = 0;
}
if (choice.equals("savings")) {
num = 1;
}
switch (num) {
case 0:
System.out.println("What action do you wish to perform \n(Withdraw, deposit, monthly processing)?:");
choice = in.nextLine();
if (choice.equals("withdraw")) {
num = 0;
}
if (choice.equals("deposit")) {
num = 1;
}
if (choice.equals("monthly processing")) {
num = 2;
}
switch (num) {
case 0:
System.out.println("Enter amount to withdraw:");
Withdraw(in.nextInt());
case 1:
System.out.println("Enter amount to withdraw:");
Deposit(in.nextInt());
case 2:
MonthlyProcess();
}
case 1:
System.out.println("What action do you wish to perform \n(Withdraw, deposit, monthly processing)?:");
choice = in.nextLine();
if (choice.equals("withdraw")) {
num = 0;
}
if (choice.equals("deposit")) {
num = 1;
}
if (choice.equals("monthly processing")) {
num = 2;
}
switch (num) {
case 0:
System.out.println("Enter amount to withdraw:");
savingsWithdraw(in.nextInt());
case 1:
System.out.println("Enter amount to withdraw:");
savingsDeposit(in.nextInt());
case 2:
savingMonthlyProcess();
}
}
}
}
}
''我想要一个异常,以便在程序的任何时候,如果输入以'q'或'Q'开始的东西,程序退出并结束。“ - 我不确定我会用这种行为的例外。相反,如果用户输入预期数字的非数字输入,或者如果用户试图提取超过账户持有量,我会使用异常。 –
例外通常保留用于不规则的非标准行为。如果按'Q'是退出程序的预期方法,则不应该通过异常来处理。您应该处理例外情况,例如输入“$ 34aby53”作为存款金额等。或者,如果将负值发送到“Deposit”和“Withdraw”方法,则应以“$ 25”存款作为“$ 25”等等 – nhgrif
您需要首先将** break; **添加到您的switch-case语句中。并继续。 – MouseLearnJava