【发布时间】:2017-04-02 20:17:59
【问题描述】:
每当使用存款或取款方法时,它都会从原始余额中增加或减少,但如果您再次尝试增加或减少,它不会识别以前的交易,并且会再次从原始余额中增加或减少,而不是新的交易完成后的余额。
例如:如果我将 10 美元存入一个 20 美元的账户,它将等于 30 美元。如果我尝试再存入 5 美元,它将等于 25 美元而不是 35 美元。它以原始余额的所有交易为基础,这就是问题所在。我不知道如何解决它。
//This program will create a functioning mock ATM with the usual features of a real ATM.
import java.util.*;
public class ATM {
public static Scanner kbd;
/**
* This is the main method, everything that is returned from other methods is sent here to be processed and initialized into the atm.
* @param args
*/
public static void main(String[] args) {
String acctNum, acctPword;
String balance;
boolean menu = false;
double depAmnt = 0.0, withAmnt = 0.0;
int x = 1;
kbd = new Scanner(System.in);
//getting the user account username
System.out.print("Enter your account number: ");
acctNum = kbd.nextLine();
//getting the users password
System.out.print("Enter your account password: ");
acctPword = kbd.nextLine();
//incase the user enters invalid information
balance = checkID(acctNum, acctPword);
while (balance.equals("error") && x <4){
System.out.println("Wrong password try again.");
System.out.print("Enter your account password: ");
acctPword = kbd.nextLine();
x++;
}
//once they gained access to the atm machine, the balance will print for informative purposes.
double balance1 = Double.parseDouble(balance);
System.out.println("You currently have $" + String.format("%.2f",balance1));
//they only get 3 attempts to enter the correct password.
if (x == 4)
System.out.println("Maximum number of attempts reached, Please try again later.");
//This switch and while statement controls the main menu of the atm machine and is capable of calling all methods.
while (menu == false){
switch (menu()) {
case 1:
System.out.println("Your balance is $" + String.format("%.2f",balance1));
break;
case 2:
deposit(balance1, depAmnt);
break;
case 3:
withdraw(balance1, withAmnt);
break;
case 4:
System.out.println("Have a nice day.");
menu = true;
break;
}
}
kbd.close();
}
/**
* Determines if acctNum is a valid account number, and pwd is the correct
* password for the account.
* @param acctNum The account number to be checked
* @param pwd The password to be checked
* @return If the account information is valid, returns the current account
* balance, as a string. If the account information is invalid, 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";
//these 3 if statements and everything declared right before the if statements, first checks the users name and pword for gained access and allows the user to enter the code and change the password and username if they so please.
int pos1, pos2;
pos1 = a.indexOf(" ");
pos2 = a.lastIndexOf(" ");
String account, password;
account = a.substring(0, pos1);
password = a.substring(pos1+1,pos2);
if (acctNum.equals(account) && pwd.equals(password)){
result = a.substring(pos2+1);
return result;
}
int pos1b, pos2b;
pos1b = b.indexOf(" ");
pos2b = b.lastIndexOf(" ");
String accountb, passwordb;
accountb = b.substring(0, pos1b);
passwordb = b.substring(pos1b+1,pos2b);
if (acctNum.equals(accountb) && pwd.equals(passwordb)){
result = b.substring(pos2b+1);
return result;
}
int pos1c, pos2c;
pos1c = c.indexOf(" ");
pos2c = c.lastIndexOf(" ");
String accountc, passwordc;
accountc = c.substring(0, pos1c);
passwordc = c.substring(pos1c+1,pos2c);
if (acctNum.equals(accountc) && pwd.equals(passwordc)){
result = c.substring(pos2c+1);
return result;
}
return result;
}
/**
* This menu method will get the users input and allow them to choose 4 options otherwise they will receive an error message.
* @return x is returned as the users input and will dictate which option they choose.
*/
public static int menu(){
int x = 1;
// the physical aspect of the menu
System.out.println("1. Display Balance \n2. Deposit\n3. Withdraw\n4. Log Out");
x = kbd.nextInt();
//incase they dont enter valid info
if(x > 4){
System.out.println("That is not an option.");
x = 0;
x = kbd.nextInt();
}
return x;
}
/**
* This method will allow the user to use a deposit feature found on every atm if they wish to deposit money into their account.
* @param acctBal this is the account balance and will dictate how much they have before and after the deposit
* @param depAmnt this is how much they so choose to deposit to their accounts
* @return the account balance is returned and revised to what ever amount they chose to add.
*/
public static double deposit(double acctBal, double depAmnt){
System.out.print("How much money would you like to deposit? $");
depAmnt = kbd.nextDouble();
acctBal = acctBal + depAmnt;
System.out.println("Your balance is now at $" + String.format("%.2f", acctBal));
return acctBal;
}
/**
* This allows the user to take money from their account if they have money in the first place.
* @param acctBal the account balance will be returned as a new lesser value once the withdraw is made
* @param withAmnt this dictates how much is taken from the acct.
* @return the now lesser acct balance is returned
*/
public static double withdraw(double acctBal, double withAmnt){
System.out.print("How much money would you like to withdraw? $");
withAmnt = kbd.nextDouble();
if (acctBal <= 0){
System.out.println("You do not have any money.");
return acctBal;
}
if (acctBal < withAmnt){
System.out.print("You do not have enough money.\nHow much money would you like to withdraw? $");
withAmnt = kbd.nextDouble();
}
else{
acctBal = acctBal - withAmnt;
}
System.out.println("You now have $" + String.format("%.2f",acctBal));
return acctBal;
}
/**
* all this does is simply display the current balance of the user
* @param balance the balance as it stands during the program.
* @return
*/
public static double displayBalance(double balance){
System.out.println("Your balance is $" + String.format("%.2f", balance));
return balance;
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时返回 Minimal, Complete and Verifiable Example 来证明您的问题。
标签: java