patternjavaModerate
Bank transaction system
Viewed 0 times
banksystemtransaction
Problem
I was hoping someone could provide some feedback with regards to this:
```
import java.util.InputMismatchException;
import java.util.Scanner;
public class Bank {
protected double myBalance = 10.40;
protected double bankBalance = 1000000;
protected double creditRating = 0.1;
private static final int PIN = 1234;
boolean access = false;
protected Scanner s = new Scanner(System.in);
public Bank() {
int pin = 0000;
int option;
System.out.print("Please enter your 4 digit pin code: ");
try {
pin = s.nextInt();
} catch (InputMismatchException e) {
System.out.print("Invalid character entered, bye bye...");
}
if (pin != PIN) {
System.err.println("Incorrect PIN entered, please try again later.");
System.exit(0);
}
access = true;
System.out.println("Welcome to JCBank please choose from the following options");
System.out.println();
System.out.println("Withdraw (1)\tDeposit (2)\tCheck Balance (3)\tApply for Loan(4) \tQuit (5)");
System.out.println();
do {
option = s.nextInt();
} while(!selectOption(option));
}
public boolean selectOption(int option) {
switch (option) {
case 1:
System.out.println("You would like to withdraw");
System.out.println();
break;
case 2:
System.out.println("You would like to Deposit");
System.out.println();
break;
case 3:
System.out.println("Your account balance is £" + myBalance);
System.out.println();
break;
case 4:
System.out.println("Please wait whilst we perform a credit check.");
try {
checkCreditRating();
} catch (InterruptedException e) {
Syst
```
import java.util.InputMismatchException;
import java.util.Scanner;
public class Bank {
protected double myBalance = 10.40;
protected double bankBalance = 1000000;
protected double creditRating = 0.1;
private static final int PIN = 1234;
boolean access = false;
protected Scanner s = new Scanner(System.in);
public Bank() {
int pin = 0000;
int option;
System.out.print("Please enter your 4 digit pin code: ");
try {
pin = s.nextInt();
} catch (InputMismatchException e) {
System.out.print("Invalid character entered, bye bye...");
}
if (pin != PIN) {
System.err.println("Incorrect PIN entered, please try again later.");
System.exit(0);
}
access = true;
System.out.println("Welcome to JCBank please choose from the following options");
System.out.println();
System.out.println("Withdraw (1)\tDeposit (2)\tCheck Balance (3)\tApply for Loan(4) \tQuit (5)");
System.out.println();
do {
option = s.nextInt();
} while(!selectOption(option));
}
public boolean selectOption(int option) {
switch (option) {
case 1:
System.out.println("You would like to withdraw");
System.out.println();
break;
case 2:
System.out.println("You would like to Deposit");
System.out.println();
break;
case 3:
System.out.println("Your account balance is £" + myBalance);
System.out.println();
break;
case 4:
System.out.println("Please wait whilst we perform a credit check.");
try {
checkCreditRating();
} catch (InterruptedException e) {
Syst
Solution
A number of points:
- Do not use floating-point data types (
floatordouble) for amounts of money. Floating-point data types have limited precision, they cannot represent all decimal values exactly, and you're going to get roundoff errors sooner or later. Use integers instead (intorlong) and store cents instead (for example, 1040 cents) or useBigDecimal.
- Make member variables
privateby default; only make themprotected(or another access level) if there is a good reason to do so.
- Prefer local variables above member variables. Is there a good reason why
sis a member variable instead of a local variable?
- Don't write the main part of a program in the constructor of a class. You have the main part of your program in the constructor of the
Bankclass. The constructor is meant to initialize the state of a new object, so the only thing that the constructor should do is initialize member variables.
- In your constructor, you catch an
InputMismatchExceptionand print an error message ("bye bye...") but you are not actually returning from the constructor. So execution will continue and print "Incorrect PIN entered ..." which is not what you meant.
- The program doesn't actually do what you'd expect it to do; withdrawal, deposit etc. functionality never gets called anywhere.
- You need a
public static void main(String[] args)method to be able to run this as a stand-alone program.
Context
StackExchange Code Review Q#6210, answer score: 16
Revisions (0)
No revisions yet.