Java 3个账户的ATM,每个账户有3个密码,一次转账即可';我看不到以前的余额

Java 3个账户的ATM,每个账户有3个密码,一次转账即可';我看不到以前的余额,java,Java,我有一个3个账户的ATM项目,每个账户都有一个单独的密码,问题是余额一旦我换了账户,例如我从账户a转账到账户b,转账后我看不到我以前的余额,我只能看到我转账的账户的余额 /** * Class to implement the ATM (in OOP),support * 1.deposit * 2.withdraw * 3.transfer * 4.display balance */ public final class A

我有一个3个账户的ATM项目,每个账户都有一个单独的密码,问题是余额一旦我换了账户,例如我从账户a转账到账户b,转账后我看不到我以前的余额,我只能看到我转账的账户的余额



   /**
    * Class to implement the ATM (in OOP),support
    * 1.deposit
    * 2.withdraw
    * 3.transfer
    * 4.display balance
   */

     public final class Atm02 {

    // declaring variable
    private int service;
    private double balance;
    private double amount;
    private String account;
    private String password;
    private String destinationAccount;
    static Scanner console = new Scanner(System.in);

    /**
     * default constructor
     *
     * @throws FileNotFoundException
     */

    public Atm02() throws FileNotFoundException {
        printWlecome();

        inputAccount();

        if (isSuccessfullLogin());

        do {
            selectService();
            provideService();
        } while (this.service != 0);
        printGoodbye();
    }

    /**
     * default constructor
     *
     * @param newService
     * @param newBalance
     * @param newAmount
     */

    public Atm02(int newService, double newBalance, double newAmount) {
        this.service = newService;
        this.balance = newBalance;
        this.amount = newAmount;
    }

    /**
     * method to ask account number from the user
     *
     */
    public void inputAccount() {
        System.out.print("Please enter your account:");
        this.account = console.next();
    }

    /**
     * method to ask the password from the user
     */
    public void inputPassword() {
        System.out.print("Please enter your password:");
        this.password = console.next();
    }

    /**
     * method to check the user and his password of the account
     *
     * @return true if user and password match
     * @throws FileNotFoundException
     */

    public boolean isSuccessfullLogin() throws FileNotFoundException {

        String savedAccount;
        String savedPassword;
        final int MAX_TRIES = 3;

        // load the user.txt  we need loop to match the right account 
         //load the user.txt
                Scanner inFile = new Scanner(new FileReader("user.txt"));

            // to find the user and password of this  account 
            do {
                savedAccount = inFile.next();
                savedPassword = inFile.next();
            } while (!savedAccount.equals(this.account));

            inFile.close();

        // compare with the account password
        for (int i = 1; i <= MAX_TRIES; i++) {
            // input the password 
            inputPassword();

            if (this.password.equals(savedPassword)) 
                return true;
             else 
                System.out.println("The password is wrong.");

        }
        System.out.printf("You input password %d times wrong.%n", MAX_TRIES);
        return false;
    }

    /**
     * method to print Welcome message
     *
     */

    public void printWlecome() {
        System.out.println("Welcom to the TD ATM");
    }

    /**
     * method to print good bye
     */
    public void printGoodbye() {
        System.out.println("Goodbye thank you for using TD BANK ATM. ");
    }

    /**
     * method to print menu for all the services
     */
    public void printMenu() {
        System.out.println("Please select the service");
        System.out.println("1. Deposit ");
        System.out.println("2. Withdraw ");
        System.out.println("3. Transfer ");
        System.out.println("4. Display the current balance ");
        System.out.println("0. Quit ");
        System.out.println("Your choice is: ");
    }

    /**
     * Method to check if the user selected service is valid, if not please ask
     * user to select service again until valid
     *
     */
    public void checkServiceValid() {
        while (this.service < 0 || this.service > 4) {
            System.out.println("Invalid service");
            printMenu();
            this.service = console.nextInt();
        }
    }

    /**
     * Method ask the user select the service and check if the service is valid
     */
    public void selectService() {
        // ask the user to select the service
        printMenu();
        this.service = console.nextInt();

        // check if the servicce is valid 
        checkServiceValid();

    }

    /**
     * Method to check if the amount user input is valid , if not please ask
     * user to select amount again until is valid
     */
    public void checkAmountValid() {
        while (this.amount <= 0) {
            System.out.println("Invalid amount, please enter the amount again");
            this.amount = console.nextDouble();
        }
    }

    /**
     * Method to ask the user to input amount and check if it valid
     */
    public void inputAmount() {
        switch(this.service){
            case 1:
                System.out.println("How much do you want to deposit today");
                break;
            case 2:
                System.out.println("How much do you want to withdraw today?");
                break;
            case 3:
                System.out.println("How much do you want to transfer today");
                break;
        }

        this.amount = console.nextDouble();

        // check if the amount is valid 
        checkAmountValid();
    }

    /**
     * Method to update the balance based on the service user select
     *
     */
    public void updateBalance() {
        switch (this.service) {
            case 1:   // deposit
                this.balance += this.amount;
                break;
            case 2:   // withraw 
            case 3:
                this.balance -= this.amount;
                break;

        }
    }

    /**
     * method to deposit an amount to the account
     *
     * @throws FileNotFoundException
     */
    public void deposit() throws FileNotFoundException {
        inputAmount();
        this.balance = readBalanceFile(this.account);
        updateBalance();
        writeBalanceFile(this.account, this.balance);
        writeHistoryFile(this.account, this.balance);
        printSucces();
    }

    /**
     * method to withdraw an amount from the account
     *
     * @throws FileNotFoundException
     */
    public void withdraw() throws FileNotFoundException {
        inputAmount();
        this.balance = readBalanceFile(this.account);
        updateBalance();
        writeBalanceFile(this.account, this.balance);
        writeHistoryFile(this.account, this.balance);
        printSucces();
    }

    /**
     * Method to print the current balance
     */
    public void printBalance() {
        System.out.printf("Your current balance is: $%.2f%n%n", this.balance);
    }

    public void displayBalance() throws FileNotFoundException {
        balance = readBalanceFile(this.account);
        printBalance();
    }

    /**
     * method to provide the service
     *
     * @throws FileNotFoundException
     */
    public void provideService() throws FileNotFoundException {

        switch (service) {
            case 1:   // withdraw
                deposit();
                break;
            case 2:   // deposit
                withdraw();
                break;
            case 3: // transfer
                transfer();
            case 4:  // print balance
                displayBalance();
                break;
        }
    }

    /**
     * print the notice for success
     */
    public void printSucces() {
        switch (this.service) {
            case 1:
                System.out.println("Deposit successfuly...");
                break;
            case 2:
                System.out.println("Withdraw successfuly...");
                break;
            case 3:
                System.out.println("Transfer successfuly...");
                break;
        }
    }

    /**
     * Ask the user to input the account he/she want to transfer
     *
     */
    public void inputDestinationAccount() {
        System.out.println("Which account you want to transfer to? ");
        this.destinationAccount = console.next();
    }

    public void transfer() throws FileNotFoundException {
        double destinationBalance;

        inputDestinationAccount();
        inputAmount();
        this.balance = readBalanceFile(this.account);  // read my balance
        destinationBalance = readBalanceFile(this.destinationAccount);   // read 
     my detination balance
        updateBalance();     // update my balance
        writeBalanceFile(this.account, this.balance);  // write my new balance
        writeBalanceFile(this.destinationAccount, destinationBalance + 
    this.amount);   // write the detination new balance
       // setService(2);
        writeHistoryFile(this.account, this.balance);   // write my history 
      //  setService(1);
        writeHistoryFile(this.destinationAccount, destinationBalance + 
    this.amount);   // write the 
        printSucces();
    }

    public void displaybalance(){

    }

    /**
     * 
     */


    /**
     * REad th ebalance from a txt file
     *
     * @param account the account u want to read
     * @return the balance of that account
     * @throws FileNotFoundException
     */
    public double readBalanceFile(String account) throws FileNotFoundException {

        double balance;
        Scanner inFile = new Scanner(new FileReader(account + "_balance.txt"));
        balance = inFile.nextDouble();

        inFile.close();

        return balance;

    }

    /**
     * Method to write balance to the file
     *
     * @param account
     * @param balance the current balance
     * @throws FileNotFoundException
     */
    public void writeBalanceFile(String account, double balance) throws 
    FileNotFoundException {
        PrintWriter outFile = new PrintWriter(new FileOutputStream(new 
     File(this.account + "_balance.txt")));
        outFile.print(balance);

        outFile.close();
    }

      public void writeHistoryFile(String amount, double balance) throws 
       FileNotFoundException {
        PrintWriter outFile = new PrintWriter(new FileOutputStream(new 
            File(this.account + "_history.txt"), true));

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-DD 
            HH:mm:ss");
          //String DEPOSIT = "\u001B[32m";

        //System.out.println((char)27 + "[33mYELLOW");
        switch (service) {

            case 1:  // deposit
                outFile.printf("%-40s%-20s%-10s%n", "DATE", "DEPOSIT", " BALANCE 
              ");
                outFile.printf("%-40s%-20.2f%-10.2f%n", 
       dtf.format(LocalDateTime.now()), this.amount, balance);
                break;
            case 2:  // withdraw
                outFile.printf("%-30s%-30s%-10s%n", " DATE ", " WITHDRAW ", " 
           BALANCE ");
                outFile.printf("%-30s%-30.2f%10.2f%n", 
         dtf.format(LocalDateTime.now()), this.amount, balance);
                break;
            case 3:
                outFile.printf("%-50s%-10.2f%10.2f%n", 
        dtf.format(LocalDateTime.now()), this.amount, balance);
                break;
        }

        outFile.close();
    }

    //---------------- GET METHOD ----------------------------//
    public int getService() {
        return service;
    }

    public double getBalance() {
        return balance;
    }

    public double getAmount() {
        return amount;
    }

    public String getAcount() {
        return account;
    }

    public String getPassword() {
        return password;
    }
    //----------------- SET METHOD ----------------------------//

    public void setService(int service) {
        this.service = service;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public void setAcount() {
        this.account = account;
    }

    public void setPassword() {
        this.password = password;
    }
     //----------------------------------------------------------//

    public String getDestinationAcaount() {
        return destinationAccount;
    }

    public void setDestinationAcaount(String destinationAcaount) {
        this.destinationAccount = destinationAcaount;
    }

    }

/**
*类来实现ATM(在OOP中),支持
*1.存款
*2.撤回
*3.转让
*4.显示天平
*/
公开期末考试Atm02{
//声明变量
私人互联网服务;
私人双平衡;
私人双倍金额;
私人字符串帐户;
私有字符串密码;
私有字符串destinationAccount;
静态扫描仪控制台=新扫描仪(System.in);
/**
*默认构造函数
*
*@抛出FileNotFoundException
*/
public Atm02()引发FileNotFoundException{
printWlecome();
inputAccount();
如果(isSuccessfullLogin());
做{
选择服务();
providevice();
}而(this.service!=0);
打印再见();
}
/**
*默认构造函数
*
*@param newService
*@param-newBalance
*@param newAmount
*/
公共Atm02(国际新闻服务、双新余额、双新金额){
this.service=newService;
这个平衡=新平衡;
this.amount=新金额;
}
/**
*方法向用户询问帐号
*
*/
public void inputAccount(){
系统输出打印(“请输入您的帐户:”);
this.account=console.next();
}
/**
*方法向用户询问密码
*/
public void inputPassword(){
System.out.print(“请输入密码:”);
this.password=console.next();
}
/**
*方法检查帐户的用户及其密码
*
*@如果用户和密码匹配,则返回true
*@抛出FileNotFoundException
*/
公共布尔值isSuccessfullLogin()引发FileNotFoundException{
字符串savedAccount;
字符串保存密码;
最终整数最大值=3;
//加载user.txt,我们需要循环来匹配正确的帐户
//加载user.txt文件
Scanner infle=新扫描仪(新文件读取器(“user.txt”);
//查找此帐户的用户和密码
做{
savedAccount=infle.next();
savedPassword=infle.next();
}而(!savedAccount.equals(this.account));
infle.close();
//与帐户密码进行比较
对于(int i=1;i 4){
System.out.println(“无效服务”);
打印菜单();
this.service=console.nextInt();
}
}
/**
*方法要求用户选择服务并检查该服务是否有效
*/
公共服务(){
//请用户选择服务
打印菜单();
this.service=console.nextInt();
//检查服务是否有效
checkServiceValid();
}
/**
*方法检查用户输入的金额是否有效,如果无效,请询问
*用户再次选择金额,直到有效
*/
public void checkAmountValid(){

而(this.amount问题在于writeBalanceFile方法。您使用了
this.account
而不是传递给该方法的帐户。请将其更正为:

public void writeBalanceFile(String account, double balance) throws 
FileNotFoundException {
    PrintWriter outFile = new PrintWriter(new FileOutputStream(new 
            File(account + "_balance.txt")));
    outFile.print(balance);

    outFile.close();
}

另外,请允许我提一个建议:如果您将这个类划分为3个类,那么理解和逻辑上遵循代码就会容易得多。一个类表示
帐户
,另一个类处理
输入
,另一个类用于读写文件(
FileHandler
).

你的类
Atm02
做了太多的事情。它试图成为ATM和帐户。你可以举一个用户的例子。text?谢谢TM00是的,这是问题所在,这是我使用java的第一个月。我将尝试你的建议,用3个类,非常感谢你的时间。我的荣幸!我相信,一旦你学会了java,你会发现java是一种非常令人愉快的语言如果你认为我的答案是有帮助的和可以接受的,你应该把它标记为这个问题的可接受答案。