3 of 3
Removed rough language from user interaction
Vogel612
  • 25.2k
  • 7
  • 57
  • 140

Java ATM, supporting deposits and withdrawals

I have created a basic ATM application where we can deposit, withdraw money from our accounts. It works, but I want to improve the coding standards. Kindly suggest what coding standards im breaking and why I should change my code.

 package Standaloneapplication;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.text.AbstractDocument.BranchElement;

public class Atm {

    /**
     * @param args
     */
    static BufferedReader br;
    static Scanner sc;
    static{
        
        File file = new File("myfile.txt");
        try {
            boolean resdd=file.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    //FileReader fr;
    }
    
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        
        sc = new Scanner(System.in);
do{
    br = new BufferedReader(new FileReader("myfile.txt"));
    System.out.println("Enter your account number to login:");
    int acc=sc.nextInt();
    boolean result=validate_acc(acc);
    
    
    if(!result){System.out.println("file has no content ");}
}while(true);
        
        
    }

    private static boolean validate_acc(int acc) throws IOException {
        // TODO Auto-generated method stub
        String input;
        boolean res=false;boolean found=false;
        //String ;
        while((!res)&& ( input=br.readLine()) != null && (!res))
        {
            String[] ar=input.split(":");
            if(Integer.parseInt(ar[0])==acc){
                found=true;
                br.close();
            
                 res=validatepassword(ar[1]);
                 if(!res){System.out.println(" password is wrong");}
                 else
                     {System.out.println("you successfully login into your account ");
                     AccountDetails sd = new AccountDetails(Integer.parseInt(ar[0]),Integer.parseInt(ar[2]));
                     showmenu(sd);res=true;}
            }
            
        }
        if(found)return found;else {System.out.println("account did not exist,lets create  a new acount ");
        createaccount();br.close();
        return res=true;}}

    private static void showmenu(AccountDetails sd) throws IOException {
        // TODO Auto-generated method stub
        int ca;
        do{
            
            System.out.println("select any option");
            System.out.println("check balance            :1");
            System.out.println("Deposit to your  balance :2");
            System.out.println("Withdraw  money          :3");
            System.out.println("Exit                     :0");
             ca=sc.nextInt();
            switch(ca){
            case 1:sd.showbalance();break;
            case 2:System.out.println("enter amount to deposit");int depo=sc.nextInt();
            sd.Deposit(depo);break;
            case 3:System.out.println("enter amount to withdraw");int with=sc.nextInt();
            sd.withdraw(with);System.out.println("your current  balance is "+sd.getBalance());break;
            
            
                
            }
            
        }while(ca!=0);
        updatedata(sd);
        
        
        
    }

    private static void updatedata(AccountDetails sd) throws IOException {
        // TODO Auto-generated method stubString 
        ArrayList<String> list =  new ArrayList<String>();
        int act=sd.getAccount_Number();
        int bal=(int) sd.getBalance();
        br = new BufferedReader(new FileReader("myfile.txt"));
        
        String input;
        while( ( input=br.readLine()) != null )
        {
            String[] ar=input.split(":");
            if(Integer.parseInt(ar[0])==act){
                input=ar[0]+":"+ar[1]+":"+bal;
            }
            list.add(input);
            
        
    }
        br.close();
        FileWriter f = new FileWriter("myfile.txt");
        BufferedWriter bw = new BufferedWriter(f);
        for(String inp : list){
        bw.write(inp);
        bw.newLine();}
         bw.flush();
         bw.close();}

    private static boolean validatepassword(String ar) {
        // TODO Auto-generated method stub
        System.out.println("enter password:");
        String password=sc.next();
        if(password.equals(ar))return true;
        return false;
    }

    private static void createaccount() throws IOException {
        // TODO Auto-generated method stub
        AccountDetails ad = new AccountDetails();
        int acn;
        String p;
        System.out.println("enter customer name");
        ad.setCustomername(sc.next());
        System.out.println("enter account number");
        ad.setAccount_Number(acn=sc.nextInt());
        System.out.println("set password");
        String pwd=sc.next();
        ad.setBalance(10000000);
        
        FileWriter f = new FileWriter("myfile.txt",true);
        BufferedWriter bw = new BufferedWriter(f);
        String details=acn+":"+pwd+":"+ad.getBalance();
        
        bw.write(details);
        bw.newLine();
        bw.flush();
        bw.close();
        System.out.println("your account got created , now try loggin in again.");
        
        
    }

    
}

and my other class at same package Standaloneapplication;

public class AccountDetails {

    /**
     * @param args
     */
    private String Customername;
    private int Account_Number;
    private int balance;
    public AccountDetails(int ar, int ba) {
        // TODO Auto-generated constructor stub
        
        this.Account_Number=ar;
        this.balance=ba;
    }
    public AccountDetails() {
        // TODO Auto-generated constructor stub
    }
    public String getCustomername() {
        return Customername;
    }
    public void setCustomername(String customername) {
        Customername = customername;
    }
    public int getAccount_Number() {
        return Account_Number;
    }
    public void setAccount_Number(int i) {
        Account_Number = i;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    public void Deposit(int depo) {
        // TODO Auto-generated method stub
        this.balance=balance+depo;
        showbalance();
        
    }
    public void withdraw(int with) {
        // TODO Auto-generated method stub
        this.balance=balance-with;
        showbalance();
        
    }
    public void showbalance() {
    System.out.println("your current balance is "+this.getBalance());
    }
}

`