有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ATM用于3个账户,每个账户有3个密码,一旦转账就看不到以前的余额

我有一个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;
    }

    }

共 (1) 个答案

  1. # 1 楼答案

    问题在于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个类,那么理解和逻辑上遵循代码会容易得多。一个表示Account,另一个处理Input,另一个用于读取和写入文件(FileHandler