Java 以双重价值发行

Java 以双重价值发行,java,Java,无论何时使用存款或取款方法,都会从原始余额中进行加减,但如果您尝试再次加减,则不会识别以前的交易,并会在交易完成后再次从原始余额中进行加减,而不是从新余额中进行加减 例如:如果我把10美元存入一个20美元的账户,它将等于30美元。如果我再尝试存入5美元,它将等于25美元,而不是35美元。它以原始余额的所有交易为基础,这就是问题所在。我不知道如何修理它 //This program will create a functioning mock ATM with the usual features

无论何时使用存款或取款方法,都会从原始余额中进行加减,但如果您尝试再次加减,则不会识别以前的交易,并会在交易完成后再次从原始余额中进行加减,而不是从新余额中进行加减

例如:如果我把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;
    }

}
//此程序将创建一个功能正常的模拟ATM,具有真实ATM的常见功能。
导入java.util.*;
公共级自动取款机{
公共静态扫描仪kbd;
/**
*这是主方法,从其他方法返回的所有内容都被发送到这里进行处理并初始化到atm中。
*@param args
*/
公共静态void main(字符串[]args){
字符串acctNum,acctword;
弦天平;
布尔菜单=假;
双depannt=0.0,其中amnt=0.0;
int x=1;
kbd=新扫描仪(System.in);
//获取用户帐户用户名
系统输出打印(“输入您的账号:”);
acctNum=kbd.nextLine();
//获取用户密码
System.out.print(“输入您的帐户密码:”);
acctPword=kbd.nextLine();
//如果用户输入了无效信息
余额=支票ID(acctNum、acctPword);
而(平衡等于(“误差”)&&x 4){
System.out.println(“这不是一个选项”);
x=0;
x=kbd.nextInt();
}
返回x;
}
/**
*如果用户希望将钱存入其账户,此方法将允许用户使用每个atm机上的存款功能。
*@param acctBal这是账户余额,将指示存款前后的余额
*@param depAmnt这是他们选择存入账户的金额
*@return返回帐户余额,并将其修改为他们选择添加的金额。
*/
公共静态双重存款(双重账户、双重存款){
System.out.print(“您想存多少钱?$”;
depAmnt=kbd.nextDouble();
acctBal=acctBal+depAmnt;
System.out.println(“您的余额现在为$”+String.format(“%.2f”,acctBal));
返回acctBal;
}
/**
*这允许用户从他们的帐户中拿钱,如果他们首先有钱的话。
*@param acctBal一旦提款,账户余额将作为新的较小值返回
*@param with amnt这表示从帐户中提取的金额。
*@return返回现在较小的账户余额
*/
公共静态双退(双账户,双带AMNT){
System.out.print(“您想取多少钱?$”;
withAmnt=kbd.nextDouble();

如果(acctBal问题出现在您的
开关
语句中:

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;
}
您的
存款
取款
功能都会返回新余额,但不会更新它。因此,您必须将新值存储在
余额1
中。在调用存款(余额1,depAmnt)时,只需执行
余额1=存款(…)
余额1=取款(…)
,则不会将返回值保存在任何位置。请注意,该方法会增加deposit()方法本地变量balance1的值,而不是main()中的变量balance1的值

使用double存储帐户值时会遇到很多问题。您应该使用长整数来保存美分数


例如,如果您添加了100次$0.01,您可能会惊讶于当您提取$1.00时会发生什么情况。欢迎使用Stack Overflow!看起来您需要学习使用调试器。请自行解决一些问题。如果以后您仍然有问题,请随时返回以演示您的问题。