Application: Simulation simple bank account

Application: Bank account / ATM simple Java 

Create a prototype of an ATM in Java programming language. Earlier, it asks your name and initial value in the account. The program must provide a 4-digit number - account number - to the user (use Random). These data will be used to create an object of class "" 

Then, shows a menu with options (this menu should be a method): 
- Bank Statementt: displays the name, account number, balance, and how many operations have been performed 
- Withdraw: get the amount to be withdrawn, reports can be drawn up (can not be negative) and shows the balance 
- Deposit: receives the amount to be deposited and shows the new balance 
- Exit 

This menu appears until the user chooses to exit. The other options are methods that should be part of the "" (getters and setters). Note that the user can withdraw at most 5 times per day. 
In this exercise we will not explicitly use the words 'set' and 'get' method names. But this is not necessary.What  we do is use its idea: the idea behind the 'set' is to change variable values, for this we use the methods 'withdraw' and 'deposit', which changes the value of the balance, the idea behind the 'get' is simply the variables information, as is the case of the method 'bankStatement'. 

Use the main only to start the application

Initially, our file "", which contains the 'main', create an account, asking for a name and an initial value. 
Through the command: 1 + number.nextInt (9999) ,it will gave us an account number of up to 4 digits (nextInt (9999) generates numbers from 0 to 9998, plus 1 to generates between 1 and 9999). 

With these data, we created an account, which is actually the object 'MyAccount' of "" class.
We started our bank or ATM using the method 'start()'. 

Note that tried to code just a few line inside the 'main' method, because it is a good practice. Overall, it is used only as a 'trigger' to start the program and not to be filled with variables and thousandslines of code. 
Let's go to "" class.

Simple banking system in Java

Our attributes (variables) are: name, balance, account and withdrawals.
Here we use the real constructor function: initialize variables. In this case it is totally necessary, because there's no way, in a banking system,  to create an account without having, at least, those data.

Let's see now the main methods of banking:
Simple method that displays all user information.

cashOut(int value)
This method changes the variable 'balance'. In this case, it reduces it.
However, it only makes sense to reduce (get money) if 'value' is less than 'balance', so treatment is done through the conditional test if .
If it is possible to perform the service, we increment the variable 'looting', to control the number of terms looting. If not possible, we display a message stating the problem and nothing happens.
That is, is a fine example of how to use the set method.

deposit (int value)
Simply adds a value to the current balance.

Here is the main screen of our ATM Progressive Java.
It uses the do while loop that will run the mini-banking system while the user does not select the option to exit, which is the number 4 (while (option! = 4)).
Each iteration is displayed the menu using the method showMenu(), is an entry application (number) to the user and that number is sent to the method that will direct the program to the option chosen by the user,escolheOpcao ().

A simple method that the banking system is 'exibeMenu ()', which takes no arguments nor return any variable. As the name suggests, it simply displays a list of options from our system.

chooseOption(int option)
We will actually choose the option that we want in method 'escolheOpcao', which receives a number.
But what number is this?
It is related to the menu. You see the options, enter the number and this method is used to choose the desired option.
Choosing an option, the right case ... it reminds you of something? Yes, making choices, the switch command.
If you choose option 1, they send us the method to 'bankStatement()'.
If a 2, should go for method 'draw ()', but it is not always that we serve. We can if we have done less than 3 withdrawals.
If you can hold serve both the case 2 with case 3 should receive a value from the user, which is the amount that will be cashed or deposited.
The case 4 is to shut down the system and any other option that falls into default charges as error.

Java Application Source Code
import java.util.Scanner;
import java.util.Random;

public class ATM {
    public static void main(String[] args){
        // Declaring variables, Scanner and Random objects
        String name;
        double initial;
        Scanner input = new Scanner(;
        Random numero = new Random();
        int number = 1 + numero.nextInt(9999);
        //Getting the initial values
        System.out.println("Registering a new client.");
        System.out.print("Type your name: ");
        name = input.nextLine();
        System.out.print("Enter the initial amount of this account: ");
        initial = input.nextDouble();
        //Creating the new account
        Account myAccount = new Account(name, number, initial);

import java.util.Scanner;

public class Account {
    private String name;
    private int number, withdrawals;
    private double balance;
    Scanner input = new Scanner(;
    public Conta(String name, int number, double initialBalance){;
    public void bankStatement(){
        System.out.println("\tBank Statement");
        System.out.println("Name: " +;
        System.out.println("Account number: " + this.number);
        System.out.printf("Balance: %.2f\n",this.balance);
        System.out.println("Withdrawals made: " + this.withdrawals + "\n");
    public void cashOut(double value){
        if(balance >= value){
            balance -= value;
            System.out.println("Cash value out: " + value);
            System.out.println("New balance: " + balance + "\n");
        } else {
            System.out.println("Not enough balance amount. Deposit some value.\n");
    public void deposit(double value)
        balance += value;
        System.out.println("Deposit: " + value);
        System.out.println("New balance: " + balance + "\n");
    public void start(){
        int option;

            option = input.nextInt();
    public void showMenu(){
        System.out.println("\t Choose the right option");
        System.out.println("1 - Check bank statement");
        System.out.println("2 - Cash out");
        System.out.println("3 - Deposit");
        System.out.println("4 - Exit\n");
        System.out.print("Option: ");
    public void chooseOption(int option){
        double value;
        switch( option ){
            case 1:    
            case 2: 
                        System.out.print("How much do you want cash out: ");
                        value = input.nextDouble();
                    } else{
                        System.out.println("Withdrawals dayly limit reached.\n");
            case 3:
                    System.out.print("How much do you want deposit: ");
                    value = input.nextDouble();
            case 4: 
                    System.out.println("Closed system.");
                    System.out.println("Invalid option");

Hacker Training: 
Find errors, gaps and missing details in the application above. 
The answers are in white, below, simply select text to view. 
But do not look before attempting to think hard. 

If you type letter in application where is waited a number, the system will collapse.

Note that the amounts received by the methods 'cashOut' and 'deposit' should be positive. However, we did nothing check it out, and therefore, it's possible hack this 'bank'.
For example, enter a negative value of -1 million dollars and will have 1 million dollars in your account ;)
Because that there is no the button with the symbol '-' int keyboard banks ...

Similarly, negative values ​​can be deposited in our account. But who would do that?

No comments: