0
\$\begingroup\$

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());
    }
}

`

\$\endgroup\$
1

1 Answer 1

3
\$\begingroup\$

Unprofessional language [edited out]

Please refrain for using explicit language. See showBalance() and createaccount().

Actually seeing this stuff in code makes me less interested in doing a full code review. This kind of stuff does not show respect to either people using your application, but also for the people reading your code.

IDE generated comments

Remove them.

IDE code formatting

You could use the format code function of your IDE to cleanly format your code in a structural and consistent manner.

More?

There is many more to be gained, but I seriously doubt that you want to learn something, because your code does not show respect, does not show effort in cleaning up, nor show what 'coding standards' you want to achieve.

\$\endgroup\$
4
  • 4
    \$\begingroup\$ I disagree. I didn't do a review on this code based on those usages. It's childish and unprofessional, and I'm not interested in wasting my time. \$\endgroup\$
    – Eric Stein
    Commented May 17, 2018 at 11:46
  • 1
    \$\begingroup\$ @EricStein Yes, it may be childish or unprofessional. I just don't think that it implies disrespect or unwillingness to learn. Maybe the OP simply thought it would be funny. The problem here is trying to be funny for the sake of being funny, because it is ignorant of the fact that not everyone might share one's own sense of humor. But it's not like the insults were directed at anyone, so I don't really find it offensive, but I understand that someone who doesn't find it funny would be annoyed by it. \$\endgroup\$
    – Stingy
    Commented May 17, 2018 at 20:22
  • \$\begingroup\$ I'm really sorry guys,, i thought i removed all those comments, somehow it got pasted..i apologize.and i didnt meant to be disrespectful to anyone.i was developing this project to teach my friend..hence the language.I'm sorry. \$\endgroup\$ Commented May 18, 2018 at 17:27
  • \$\begingroup\$ i think it's because I pasted them with unedited content at first and then edited, somehow my last change didnt go through i suppose.again my apologies for anyone who got offended by it. \$\endgroup\$ Commented May 18, 2018 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.