Account.java

package btclass;

import java.util.Scanner;


public class Account {

    private float amount;//actual amount
    private static float balance;//balance amout
    Scanner keyboard = new Scanner(System.in);
    public void deposit() {
        System.out.println("Enter the amount you want to deposit: ");
        float depos = keyboard.nextFloat();
        if (depos < 0) {
            System.out.println("Amount you entered is < 0! It's possible!");
        } else {
            amount = amount + depos;
            System.out.println("So the actual amount now you have is: "+ amount);
            balance = amount;
        }
    }
    public void withdraw() {
        System.out.println("Enter the amount you want to withdraw: ");
        float getOut = keyboard.nextFloat();
        if (getOut > amount) {
            System.out.println("Cannot! the amount you want to withdraw is greater than actual amount!");
        } else {
            balance = amount - getOut;
            System.out.println("The amount left is: " + balance);
        }
    }
    public float getBalance() {
        return balance;
    }
}

AccountTest.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package btclass;

import java.util.Scanner;

/**
 *
 * @author Oner
 */
public class AccountTest {

    String accountNo;
    String setAccountNo = "123456";
    public static boolean goOn;
    public static boolean pinT = false;

    public static void main(String[] args) {
        Account myAccount = new Account();
        AccountTest AT1 = new AccountTest();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Welcome to use ATM machine :D");
        System.out.println("Enter your PIN: ");
        while (pinT == false) {
            String pin = keyboard.nextLine();
            AT1.accountNo = pin;
            if (!AT1.accountNo.equalsIgnoreCase(AT1.setAccountNo)) {
                System.out.println("Incorrect PIN! Please Enter the correct PIN!");
                pinT = false; 
                continue;
            } else {
                pinT = true;
                goOn = true;
            }
        }
        do {
            System.out.println("*************************************************");
            System.out.println("1: Depositting cash: ");
            System.out.println("2: Withdrawing cash: ");
            System.out.println("3: Quit.");
            System.out.println("*************************************************");
            System.out.println("Please choose the options you want:");
            int choice = keyboard.nextInt();
            switch (choice) {
                case 1:
                    myAccount.deposit();
                    goOn = false;
                    break;
                case 2:
                    myAccount.withdraw();
                    goOn = false;
                    break;
                case 3:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Nothing!");
            }
        } while (goOn == true);
        {
            System.out.println("goOn now is:" +goOn);
            System.out.println("Do you want to do another tranaction? ");
            String next = keyboard.next();
            if (next.equalsIgnoreCase("y")) {
                goOn = true;
                System.out.println("goOn now is:" +goOn);
            }
            if (next.equalsIgnoreCase("n")) {
                System.exit(0);
            }
        }
        }
    }

In the AccountTest.java I use a do-while loop for the user who want to do another transaction but it did not work properly although my static variable goOn is set to true. I don't know why it did not repeat another do-while.
Please help me solve this problem!
Thanks a lot.

Recommended Answers

All 2 Replies

    {
        System.out.println("goOn now is:" +goOn);
        System.out.println("Do you want to do another tranaction? ");
        String next = keyboard.next();
        if (next.equalsIgnoreCase("y")) {
            goOn = true;
            System.out.println("goOn now is:" +goOn);
        }
        if (next.equalsIgnoreCase("n")) {
            System.exit(0);
        }
    }

Move this code inside your do - while. :)

Inside the cases of the do-while you set goOn to false. So you will exit the while-loop:

..
case 1:
  myAccount.deposit();
  goOn = false;
  break;
..

} while (goOn == true);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.