patternjavaMinor
Basic Internet banking application
Viewed 0 times
internetbankingbasicapplication
Problem
I programmed in Java before, but I feel that I lack the "true way" of programming (OOP concepts, design, algorithm), so I started to learn all of these but I need your opinions and suggestions so I can improve my skills and to be sure that I got all the concepts right. I know that there are multiple ways of doing some things, but I am trying to learn the most efficient and the best practices ways.
Application description:
-
After a successful login, the user has two choices:
InternetBankingApp.java class:
```
package main;
import java.util.HashMap;
import accounts.Account;
import operations.AccountOperations;
import operations.OperationFactory;
import utils.InputScanner;
public class InternetBankingApp {
private static boolean successLogin;
private static InputScanner input;
private static HashMap accounts;
private static Account userAccount;
public static void main(String[] args) {
accounts = new HashMap();
successLogin = false;
Account account1 = new Account(1, "password1", 1000.00);
Account account2 = new Account(2, "password2", 2000.00);
Account account3 = new Account(3, "password3", 3000.00);
accounts.put(account1.getAccountNo(), account1);
accounts.put(account2.getAccountNo(), account2);
accounts.put(account3.getAccountNo(), account3);
login();
if(successLogin) {
performOperation();
}
}
private static void login() {
int loginTries = 0;
input = InputScanner.getInstance();
System.out.println("Enter Account no.: ");
String userIn
Application description:
- A user is prompted to insert his login credentials (account number and account password).
- If an account number is found in the database then the password is verified (in this case I have a
HashMapwith the key holding the account number and the value holding the account to whom the account number belongs).
-
After a successful login, the user has two choices:
- Check account balance
- Deposit money
InternetBankingApp.java class:
```
package main;
import java.util.HashMap;
import accounts.Account;
import operations.AccountOperations;
import operations.OperationFactory;
import utils.InputScanner;
public class InternetBankingApp {
private static boolean successLogin;
private static InputScanner input;
private static HashMap accounts;
private static Account userAccount;
public static void main(String[] args) {
accounts = new HashMap();
successLogin = false;
Account account1 = new Account(1, "password1", 1000.00);
Account account2 = new Account(2, "password2", 2000.00);
Account account3 = new Account(3, "password3", 3000.00);
accounts.put(account1.getAccountNo(), account1);
accounts.put(account2.getAccountNo(), account2);
accounts.put(account3.getAccountNo(), account3);
login();
if(successLogin) {
performOperation();
}
}
private static void login() {
int loginTries = 0;
input = InputScanner.getInstance();
System.out.println("Enter Account no.: ");
String userIn
Solution
Dessign Issue
In your daily codding life just remember one thing:
The Best Code is Not Code At All
You have an additional wrapper for Scanner. You will need to refactor, maintain this code in the future.
InputScanner are used in 2 places in the code, we can replace it with builtin Scanner:
Example for AccountDeposit
The same you can do in main method.
Possible Error Issue
I want to check if user input is correct, because user can write any number and this number can be negative or equal to zero.
Additional function for checking user input.
In your daily codding life just remember one thing:
The Best Code is Not Code At All
You have an additional wrapper for Scanner. You will need to refactor, maintain this code in the future.
InputScanner are used in 2 places in the code, we can replace it with builtin Scanner:
Example for AccountDeposit
Scanner sc = new Scanner(System.in);
double userInputBalance = sc.NextDouble(); // this line is more shortly than initialThe same you can do in main method.
Possible Error Issue
I want to check if user input is correct, because user can write any number and this number can be negative or equal to zero.
double userInputBalance =
Double.parseDouble(InputScanner.getInstance().readInput());
boolean isBalanceCorrect = checkInputBalance(userInputBalance);
if (isBalanceCorrect) {
balance += userInputBalance;
account.setBalance(balance);
System.out.println("New account balance: " + account.getBalance());
} // else you can call this function again or to do somethingAdditional function for checking user input.
private boolean checkInputBalance(double inputBalance) {
if (inputBalance < 0) {
System.out.println("You can't insert negative values on your deposit");
return false;
} else if (inputBalance == 0) {
System.out.println("You current balance will not change");
return false;
}
}Code Snippets
Scanner sc = new Scanner(System.in);
double userInputBalance = sc.NextDouble(); // this line is more shortly than initialdouble userInputBalance =
Double.parseDouble(InputScanner.getInstance().readInput());
boolean isBalanceCorrect = checkInputBalance(userInputBalance);
if (isBalanceCorrect) {
balance += userInputBalance;
account.setBalance(balance);
System.out.println("New account balance: " + account.getBalance());
} // else you can call this function again or to do somethingprivate boolean checkInputBalance(double inputBalance) {
if (inputBalance < 0) {
System.out.println("You can't insert negative values on your deposit");
return false;
} else if (inputBalance == 0) {
System.out.println("You current balance will not change");
return false;
}
}Context
StackExchange Code Review Q#130118, answer score: 2
Revisions (0)
No revisions yet.