如何简化逻辑/方法以增强ATM java项目
这不是一个新问题,也不是一个需要作为作业提交的问题,因为这已经在大约一年前提交了。去年夏天我开始学习Java,有一项任务要求我们编写一个简单的ATM应用程序。下面是我的代码和结果,但当我在一年后查看代码时,我看到了可以改进的地方。我正在查看主程序中的do while循环以及下面的Switch语句。我认为print语句可以被删除并放在其各自的方法中,只需调用这些方法即可。与上面的do相比,我觉得这一部分更具自解释性,我觉得逻辑可以简化,比如第一个甚至第二个if语句,并将其置于循环之外。然而,当我尝试它时,我总是得到一个异常错误。无论如何,下面是我的原始项目。如有任何意见和建议,将不胜感激如何简化逻辑/方法以增强ATM java项目,java,switch-statement,logical-operators,Java,Switch Statement,Logical Operators,这不是一个新问题,也不是一个需要作为作业提交的问题,因为这已经在大约一年前提交了。去年夏天我开始学习Java,有一项任务要求我们编写一个简单的ATM应用程序。下面是我的代码和结果,但当我在一年后查看代码时,我看到了可以改进的地方。我正在查看主程序中的do while循环以及下面的Switch语句。我认为print语句可以被删除并放在其各自的方法中,只需调用这些方法即可。与上面的do相比,我觉得这一部分更具自解释性,我觉得逻辑可以简化,比如第一个甚至第二个if语句,并将其置于循环之外。然而,当我尝
import java.util.*;
public class ATM {
public static Scanner kbd = new Scanner(System.in);
// The checkID method determines if acctNum is a valid account number
// and pwd is the correct password for the account. If the account information
// is valid, the method returns the current account balance, as a string.
// If the account information is invalid, the method returns the string "error".
public static String checkID(String acctNum, String pwd)
{
String result = "error";
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
String a = "44567-5 mypassword 520.36";
String b = "1234567-6 anotherpassword 48.20";
String c = "4321-0 betterpassword 96.74";
if (acctNum.equals(a.substring(0, a.indexOf(" "))) &&
pwd.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
return result = a.substring(a.lastIndexOf(" ") + 1);
if (acctNum.equals(b.substring(0, b.indexOf(" "))) &&
pwd.equals(b.substring(b.indexOf(" ")+1,b.lastIndexOf(" "))))
return result = b.substring(b.lastIndexOf(" ") + 1);
if (acctNum.equals(c.substring(0, c.indexOf(" "))) &&
pwd.equals(c.substring(c.indexOf(" ") + 1,c.lastIndexOf(" "))))
return result = c.substring(c.lastIndexOf(" ") + 1);
return result;
}
public static int menu()
{
int menuChoice;
do
{
System.out.print("\nPlease Choose From the Following Options:"
+ "\n 1. Display Balance \n 2. Deposit"
+ "\n 3. Withdraw\n 4. Log Out\n\n");
menuChoice = kbd.nextInt();
if (menuChoice < 1 || menuChoice > 4){
System.out.println("error");
}
}while (menuChoice < 1 || menuChoice > 4);
return menuChoice;
}
public static void displayBalance(double x)
{
System.out.printf("\nYour Current Balance is $%.2f\n", x);
}
public static double deposit(double x, double y)
{
double depositAmt = y, currentBal = x;
double newBalance = depositAmt + currentBal;
System.out.printf("Your New Balance is $%.2f\n", newBalance);
return newBalance;
}
public static double withdraw(double x, double y)
{
double withdrawAmt = y, currentBal = x, newBalance;
newBalance = currentBal - withdrawAmt;
System.out.printf("Your New Balance is %.2f\n",newBalance);
return newBalance;
}
public static void main(String[] args) {
String accNum, pass, origBal = "error";
int count = 0, menuOption = 0;
double depositAmt = 0, withdrawAmt = 0, currentBal=0;
boolean foundNonDigit;
//loop that will count the number of login attempts
//you make and will exit program if it is more than 3.
//as long as oriBal equals an error.
do{
foundNonDigit = false;
System.out.println("Please Enter Your Account Number: ");
accNum = kbd.next();
System.out.println("Enter Your Password: ");
pass = kbd.next();
origBal = checkID(accNum, pass);
count++;
if (count >= 3 && origBal.equals("error")){
System.out.print("Maximum Login Attempts Reached.");
System.exit(0);
}
if (!(origBal.equals("error"))){
System.out.println("\nYour New Balance is: $ "+ origBal);
}
else
System.out.println(origBal);
}while(origBal.equals("error"));
currentBal=Double.parseDouble(origBal);
//this loop will keep track of the options that
//the user inputs in for the menu. and will
//give the option of deposit, withdraw, or logout.
while (menuOption != 4)
{
menuOption=menu();
switch (menuOption)
{
case 1:
displayBalance(currentBal);
break;
case 2:
System.out.print("\nEnter Amount You Wish to Deposit: $ ");
depositAmt = kbd.nextDouble();
currentBal=deposit(depositAmt, currentBal);
break;
case 3:
System.out.print("\nEnter Amount You Wish to Withdrawl: $ ");
withdrawAmt = kbd.nextDouble();
while(withdrawAmt>currentBal){
System.out.print("ERROR: INSUFFICIENT FUNDS!! "
+ "PLEASE ENTER A DIFFERENT AMOUNT: $");
withdrawAmt = kbd.nextDouble();
}
currentBal = withdraw(currentBal, withdrawAmt);
break;
case 4:
System.out.print("\nThank For Using My ATM. Have a Nice Day. Good-Bye!");
System.exit(0);
break;
}
}
}
}
import java.util.*;
公共级自动取款机{
公共静态扫描仪kbd=新扫描仪(System.in);
//checkID方法确定acctNum是否为有效的帐号
//pwd是帐户的正确密码。如果
//如果有效,该方法将以字符串形式返回当前帐户余额。
//如果帐户信息无效,该方法将返回字符串“error”。
公共静态字符串检查ID(字符串帐户、字符串密码)
{
字符串result=“error”;
//字符串a、b和c包含有效的帐号和密码。
//对于每个字符串,首先列出帐号,然后是
//空格,后跟帐户密码,后跟空格,
//其次是当前余额。
字符串a=“44567-5我的密码520.36”;
字符串b=“1234567-6其他密码48.20”;
字符串c=“4321-0 betterpassword 96.74”;
if(acctNum.equals(a.substring(0,a.indexOf(“”))和
pwd.equals(a.substring(a.indexOf(“”+1,a.lastIndexOf(“”)))
返回结果=a.substring(a.lastIndexOf(“”+1);
if(acctNum.equals(b.substring(0,b.indexOf(“”))和
pwd.equals(b.substring(b.indexOf(“”+1,b.lastIndexOf(“”)))
返回结果=b.substring(b.lastIndexOf(“”+1);
if(acctNum.equals(c.substring(0,c.indexOf(“”))和
pwd.equals(c.substring(c.indexOf(“”+1,c.lastIndexOf(“”)))
返回结果=c.substring(c.lastIndexOf(“”+1);
返回结果;
}
公共静态int菜单()
{
国际货币基金组织;
做
{
System.out.print(“\n请从以下选项中选择:”
+“\n 1.显示余额\n 2.存款”
+“\n 3.撤消\n 4.注销\n\n”);
menuChoice=kbd.nextInt();
如果(menuChoice<1 | | menuChoice>4){
System.out.println(“错误”);
}
}而(menuChoice<1 | | menuChoice>4);
返回menuChoice;
}
公共静态无效显示余额(双x)
{
System.out.printf(“\n您当前的余额为%.2f\n”,x);
}
公共静态双重存款(双x、双y)
{
双倍存款金额=y,当前余额=x;
双新天平=存款金额+当前余额;
System.out.printf(“您的新余额为$%.2f\n”,newBalance);
返回新平衡;
}
公共静态双退(双x,双y)
{
双支取金额=y,当前余额=x,新余额;
新余额=当前余额-提取金额;
System.out.printf(“您的新余额为%.2f\n”,新余额);
返回新平衡;
}
公共静态void main(字符串[]args){
字符串accNum,pass,origBal=“error”;
整数计数=0,菜单选项=0;
双倍存款金额=0,取款金额=0,当前余额=0;
布尔非数字;
//循环,该循环将计算登录尝试次数
//如果超过3,则创建并退出程序。
//只要oriBal等于一个错误。
做{
foundNonDigit=false;
System.out.println(“请输入您的账号:”);
accNum=kbd.next();
System.out.println(“输入密码:”);
pass=kbd.next();
origBal=检查ID(accNum,pass);
计数++;
如果(计数>=3&&origBal.equals(“错误”)){
System.out.print(“已达到最大登录尝试次数”);
系统出口(0);
}
如果(!(origBal.equals(“错误”)){
System.out.println(“\n您的新余额为:$”+origBal);
}
其他的
系统输出打印LN(origBal);
}而(origBal.equals(“错误”));
currentBal=Double.parseDouble(origBal);
//此循环将跟踪
//用户输入菜单。并将
//给予存款、取款或注销的选择权。
while(菜单选项!=4)
{
菜单选项=菜单();
开关(菜单选项)
{
案例1:
显示余额(当前余额);
打破
案例2:
System.out.print(“\n输入您希望存入的金额:$”;
存款金额=kbd.nextDouble();
currentBal=存款(存款金额,currentBal);
打破
案例3:
System.out.print(“\n输入您希望提取的金额:$”;
取款金额=kbd.nextDouble();
同时(取款金额>当前余额){
System.out.print(“错误:资金不足!!”
+“请输入其他金额:$”;
取款金额=kbd.nextDouble();
}
currentBal=退出(当前)