我制作了一个名为BankAccount
的类,该类具有一种用于从给定帐户中存款和提取一定余额的方法。当我在主方法中调用该方法时,它不会使用新余额更新帐户。该程序应该允许用户选择三个帐户之一,并确定他们是否要从该银行帐户中存款或提款。然后它将询问他们是否愿意进行更多交易。如果他们不希望再进行任何交易,则可以选择在另一天进行交易,这将显示每个银行帐户的更新数据,而我的帐户目前不这样做,因为余额不会从任何一个帐户更新。存款或取款。任何帮助将不胜感激。
public class BankAccount
{
public final int MIN_LENGTH = 2;
public final int MAX_LENGTH = 40;
public final String DEFAULT_NAME = "nobody";
public final double MIN_BALANCE = 0.0;
public final int DEFAULT_NUMBER = 0;
public final int MIN_NUMBER = 0;
public final int MAX_NUMBER = 999999;
private double balance;
private String owner;
private int accountNumber;
public BankAccount(double initBal, String name, int number)
{
balance = initBal;
owner = name;
accountNumber = number;
}
public BankAccount(double initBal, String name)
{
balance = initBal;
owner = name;
accountNumber = 45678;
}
public BankAccount(String name)
{
owner = name;
balance = MIN_BALANCE;
accountNumber = 34567;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance -= amount;
}
public void withdraw(double amount, double fee)
{
balance -= (amount + fee);
}
public double getBalance()
{
return balance;
}
public String getOwner()
{
return owner;
}
public int getAccountNumber()
{
return accountNumber;
}
public void setAccountNumber(int newAccountNumber)
{
accountNumber = newAccountNumber;
}
public void setOwner(String name)
{
owner = name;
}
public void display()
{
System.out.println("Account number: " + getAccountNumber());
System.out.println("Account owner: " + getOwner());
System.out.println("Account balance: " + getBalance() + "\n");
}
public void close()
{
owner = "CLOSED";
balance = 0.0;
}
private static boolean isValidName(String name)
{
final int MIN_LENGTH = 2;
final int MAX_LENGTH = 40;
if(name.length() >= MIN_LENGTH && name.length() <= MAX_LENGTH && name != null)
{
return true;
}
else
{
return false;
}
}
private static boolean isValidBalance(double initBalance)
{
if(initBalance > 0)
{
return true;
}
else
{
return false;
}
}
private static boolean isValidNumber(int acctNum)
{
final int MIN_NUMBER = 0;
final int MAX_NUMBER = 999999;
if(acctNum >= MIN_NUMBER && acctNum <= MAX_NUMBER)
{
return true;
}
else
{
return false;
}
}
}
import java.util.Scanner;
public class Transaction
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int accountChoice;
double moneyAmount;
String accountAction, transactionChoice = "nothing";
BankAccount accountOne = new BankAccount(1000.0,"Matthew Johnson",12345);
BankAccount accountTwo = new BankAccount(1000.0,"Sue Alexander",45678);
BankAccount accountThree = new BankAccount(1000.0,"James William",34567);
System.out.println("The following accounts are available:\n");
do
{
accountOne.display();
accountTwo.display();
accountThree.display();
System.out.print("Enter the number of the account you would like to access: ");
accountChoice = keyboard.nextInt();
System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
accountAction = keyboard.nextLine();
keyboard.nextLine();
System.out.print("Enter the amount: ");
moneyAmount = keyboard.nextDouble();
if(accountChoice == 12345)
{
if(accountAction.equalsIgnoreCase("D"))
{
accountOne.deposit(moneyAmount);
}
else if(accountAction.equalsIgnoreCase("W"))
{
accountOne.withdraw(moneyAmount);
}
}
else if(accountChoice == 45678)
{
if(accountAction.equalsIgnoreCase("D"))
{
accountTwo.deposit(moneyAmount);
}
else if(accountAction.equalsIgnoreCase("W"))
{
accountTwo.withdraw(moneyAmount);
}
}
else if(accountChoice == 34567)
{
if(accountAction.equalsIgnoreCase("D"))
{
accountThree.deposit(moneyAmount);
}
else if(accountAction.equalsIgnoreCase("W"))
{
accountThree.withdraw(moneyAmount);
}
}
keyboard.nextLine();
System.out.println("More transactions? (y/n)");
transactionChoice = keyboard.nextLine();
if(transactionChoice.equalsIgnoreCase("Y"))
{
continue;
}
else if(transactionChoice.equalsIgnoreCase("N"))
{
System.out.println("Would you like to enter transactions for another day? (y/n)");
transactionChoice = keyboard.nextLine();
if(transactionChoice.equalsIgnoreCase("Y"))
{
continue;
}
else if(transactionChoice.equalsIgnoreCase("N"))
{
break;
}
}
else
{
System.out.println("Invlid Input");
break;
}
} while(!transactionChoice.equalsIgnoreCase("N"));
}
}
答案 0 :(得分:1)
null
不会占用缓冲区中剩余的换行符,该换行符是由(您的第一个)nextInt
语句捕获的,并且将nextLine
设置为空字符串。通常,这就是为什么我不使用这种工作流程,而是依靠accountAction
并分别解析该行的原因,例如...
nextLine
遇到此类问题时,请使用System.out.print("Enter the number of the account you would like to access: ");
accountChoice = new Scanner(keyboard.nextLine()).nextInt();
System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
accountAction = keyboard.nextLine();
System.out.print("Enter the amount: ");
moneyAmount = keyboard.nextDouble();
打印出变量,以便您可以确切地了解正在发生的事情,直到了解如何使用调试器;)
您还可以通过使用一个或多个循环来改进代码,而不必依靠System.out.println
语句,这将减少重复代码的数量,但这超出了问题的范围;)< / p>