0
\$\begingroup\$

Rate my code and tell its quality like how well its formatted and all.

I want to see how well its written. I have recently started learning Java. so some suggestions will also be appreciated.

package e.banksolutions;

import java.util.Scanner;

import java.util.Random;
import java.util.Date;

public class EBankSolutions {

    long accountNumber;            // Variable for Storing account number
    long accountNumberGenerator=0000;   // Variable for generating and assigning account number
    String AccountHolderName;     // Variable for Storing account holder name
    String AccountType;           // Variable for Storing account type in string
    int AccTypeNumVal;
    long accountBalance;          // Variable to Store Current Account Balance. 
    String password;                //27-06-2018
    //----------------------------------------------------------------------------//

public void createAccount() throws InterruptedException
{
    Random rand = new Random();
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    System.out.println("----------------Welcome to Account Creation Portal-------------------");
    //accountNumberGenerator++; //Incrementing the account number generator var
    accountNumber=rand.nextInt(1000);; //assigning the account number
    System.out.println("Enter Your Name(Without Space Between Name) : ");
    AccountHolderName=scan.next();
    System.out.println("Enter New Password : ");                //27-06-2018
    password=scan.next();                                       //27-06-2018
    System.out.println("Enter Your Account Type Savings/Current");
    AccountType=scan.next();

   /* if(AccountType == "Savings"){
        AccTypeNumVal=1;
    }
    else if(AccountType == "Current"){
        AccTypeNumVal=2;
    }*/

bal:System.out.println("Enter your starting balance : ");
    accountBalance=scan.nextLong();
    if(accountBalance<5000){
        System.out.println("Oops!!! Your Account balance should be minimum 5000 or more.\nYou need to fill the form again.");
        System.exit(0);
    }

    System.out.println("Account Created Successfully.\nYour Account Number is: "+accountNumber+" Please Note it Down.");
    System.out.println("------------------------------------------------------------");
    //Delay code below
    Thread.sleep(5000);
    System.out.flush();
}

public void DisplayAccount() throws InterruptedException
{
    System.out.println("Displaying Account Information for Account Number : "+accountNumber);
    System.out.println("------------------------------------------------------------");
    System.out.println("Account Number  : "+accountNumber);
    System.out.println("Account Name    : "+AccountHolderName);
    System.out.println("Account Type    : "+AccountType);
    System.out.println("Account balance : "+accountBalance);
    System.out.println("Your Password   : _Hidden_"); //27-06-2018
    System.out.println("------------------------------------------------------------");
    Thread.sleep(5000);
    System.out.flush();
}

public void DepositAmount() throws InterruptedException
{
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    long depositAmt;
    System.out.println("---------------Welcome to Deposit Portal----------------");
    System.out.println("You are Depositing amount for Account Number: "+accountNumber);
    System.out.println("\nEnter the Amount to Deposit : ");
    depositAmt=scan.nextLong();
    accountBalance=accountBalance+depositAmt;
    System.out.println("Amount Deposited Successfully.. \nUpdated Balance: "+accountBalance);
    System.out.println("------------------------------------------------------------");
    Thread.sleep(5000);
    System.out.flush();
}


public void WithdrawAmount() throws InterruptedException
{
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    long withdrawAmt;
    System.out.println("---------------Welcome to Deposit Portal----------------");
    System.out.println("You are Withdrawing amount for Account Number: "+accountNumber);
    System.out.println("\nEnter the Amount to Withdraw : ");
    withdrawAmt=scan.nextLong();
    accountBalance=accountBalance-withdrawAmt;
    System.out.println("Amount Withdrawn Successfully.. \nUpdated Balance: "+accountBalance);
    System.out.println("------------------------------------------------------------");
    Thread.sleep(5000);
    System.out.flush();
}

public void login() throws InterruptedException
{
    int ch=0;
    Scanner scan = new Scanner(System.in);
    long accNum=0;
    String passWd="0";
    System.out.println("Enter Account Number : ");
    accNum=scan.nextLong();
    System.out.println("Enter Your Password  : ");
    passWd=scan.next();           
    if (accNum==this.accountNumber && passWd.equals(passWd)) {
        System.out.println("Logged In Successfully with account number: "+accountNumber+"\n-------------------------------------");   
        System.out.println("Choose Option Number From Below Menu");
        System.out.println("1.Deposit Amount\n2.Withdraw Amount\n3.Display Account Info\n4.Close Account\n5.Exit");
        ch=scan.nextInt();
        switch(ch)
        {
            case 1: DepositAmount();
                    break;
            case 2: WithdrawAmount();
                    break;
            case 3: DisplayAccount();
                    break;
            case 4: System.out.println("You cannot close your account. Feature Coming Soon...");
                    break;
            case 5: System.exit(0);
                    break;
        }
    } 
    else if(accNum!=accountNumber && passWd!=password) {
        System.out.println("You have Entered Incorrect Account Number or Password. Please Check Again.");
        Thread.sleep(5000);
        System.out.flush();
        System.exit(0);
    }
    else{
        System.out.println("Unknown Error Occured. Try Agian Later");
        Thread.sleep(5000);
        System.out.flush();
        System.exit(0);
    }
}


EBankSolutions()
{
    accountNumber=0000;
    AccountHolderName="UNDEFINED";
    AccountType="UNDEFINED";
    accountBalance=0000;
    accountNumberGenerator=0000;
    AccTypeNumVal=9;
}

public static void main(String[] args) throws InterruptedException {
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    EBankSolutions a1 = new EBankSolutions();
    int ch;
    int i=0;
    System.out.println("Welcome to Bank E Portal\n");
   while(i!=5)
   {
    System.out.println("Select any Choice Number From below menu...");
    System.out.println("1. Create Account\n2. Login\n3. Exit Portal");
    System.out.print("Enter Your Choice Code 1-4: ");
    ch=scan.nextInt();
    switch(ch)
    {
        case 1: a1.createAccount();
                break;

        case 2: a1.login();
                break; 

        case 3: System.exit(0);
                break;

    }

   }
}

}
\$\endgroup\$
1
  • \$\begingroup\$ Hey, welcome to Code Review! It would help if you could add a short description of what your code does in the question body. \$\endgroup\$
    – Graipher
    Commented Jun 27, 2018 at 17:19

2 Answers 2

1
\$\begingroup\$

Here are my comments in order of veverity:

Bugs

1.1 String equality
Make sure you do that properly (passWd!=password)

1.2 Withdrawal
There is no check of the requested amount against the deposit.

1.3 Statement Labels
This is not a bug in the sense that it produces error. However, the use of Statement Labels in Java is necessary only for break and continue commands and is discouraged.

Security

2.1 Password policy
You should enforce a policy on the password. Something like minimum length and combination of letters and digits and so on. Otherwise, your lazy customers will all choose "password"...

2.1 Password encryption
When you decide to persist the account details, yuo will need to persist the password and of course you will need to encrypt the value. There are several libraries that can help you in this task.

Design

3.1 Prepare for multiple accounts
Currently, your Bank application cna handle one account only. As a beginner exercise that is fine, but you can prepare the application for the time where you will want to expand the capabilities of your application. Start by creating a separate class for Account, put all the properties in that class. The Bank class can hold one Account and later You can decide how to handle multiple accounts.

3.2 Separate business logic from input/output processing
Right now, your application can only receive input from the console and produce output to the console. If you want to expand the capabilities, you need to separate the business logic from the output channels and create an API between the two systems.

Conventions

4.1 Naming Conventions
variable names in Java start with lower case letter.

\$\endgroup\$
0
\$\begingroup\$

Thanks for sharing youtr code.

Code style

Braces

There is an ongoing discussion where openning braches ({) should go: same line as the preceeding statement or in a separate line. But all agree that it should be one or the other. You mix this style.

So configure your IDEs auto formatter and rely on it.

Naming Conventions.

Please read (and follow) the Java Naming Conventions.

Eg: Methods start with a lower case letter. Only class names start with an uppercase letter.

Order of class content

Elements in classes should be in a specific order. Most IDEs ave a possibility to sort the elements in a class file. The result of this sortimg may not always be the best outcome since it does not honor the relationship of methods of same visibility, but You should use this for a while just to get used to the common ordering.

Comments

Connents should explain why the code is like it is. Anything else should be explaint by the identifiers themselves. So if you feel you need a comment first try to find better names for your classes, methods and variables.

Single letter and abbreviated names

Avoid single letter and abbreviated names. Although this abbreviation makes sense to you (now) anyone reading your code being not familiar with the problem has a hard time finding out what this means.

If you do this to save typing work: remember that you way more often read your code than actually typing something. Also for Java you have good IDE support with code completion so that you most likely type a long identifier only once and later on select it from the IDEs code completion proposals.

Declare local variables as late as possible.

You declare ch before the while block but you use it only inside.

The problem with that is that you confuse the automated refactoring feature of your IDE. E.g.: you can select the complete body of the while block and aplly the extract method automated refactroing of your IDE to move this into a parameterized method. With the variable ch declared before the whicl this new method would have a extra parameter int ch which is useless and clutters the methods signature.

\$\endgroup\$

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.