In: Computer Science
Implement a simple banking system in Java. Your application should demonstrate the following. Upon running the java code, first the application should welcome the user, then ask the user to give one of the options: 1) display the balance summary, 2) withdraw money, or 3) deposit money.
Based on the options your code should perform the following operations such as 1) read the transaction details from a file and display them on the screen. 2) ask the user to enter the amount he wants to withdraw and debit the withdrawal amount from the balance amount. It has to update the file and display the transaction summary. The system should not allow an attempt to withdraw an amount greater than the balance. 3) ask the user to enter the amount he wants to deposit, credit the balance and update the file. It should display the transaction summary.
The records in the file should contain transaction number,
transaction type, amount withdrawn, or amount deposited, and
balance.
Example:
1 Deposit 100.0$ 1100.0$
2 Withdraw 50.0$ 1050.0$
The welcome screen should look like:
Welcome to our Banking System!
Enter your Option in a number: 1. Display balance 2. Deposit amount
3. Withdraw amount
We assume that there is an opening balance of 1000 available in the
system (Assign balance=1000.0 in the beginning of the code). Also,
while running first start by choosing deposit option or withdraw
option.
The package structure of the project is as follows:-
The codes are as below:-
//The output of the program is:-
//banksummary.txt snapshot
//The text file of the codes are provided below:-
//BankingSystem.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BankingSystem {
public static void main(String[] args) throws
IOException {
// TODO Auto-generated method
stub
List<Record> recordList =
new ArrayList<>();// list of records to be written on
file
float balance=1000;//initial
opening balance
float total_balance = balance;//
total balance now
int transaction_nos = 0; // initial
no transaction is done hence 0
System.out.println("Welcome to
our Banking System!");
//As per question the bank summary
transaction details should be written in a file
BufferedWriter out = null;
File outf = new
File("banksummary.txt"); // Creation of File Descriptor for output
file
FileWriter fw = new
FileWriter(outf, true); // Creation of File Writer object
out = new BufferedWriter(fw);
//Initialization of FileWriter
done
loop1: while (true) {
Scanner sc = new
Scanner(System.in);
System.out.println(
"Enter your Option in a
number: 1. Display balance 2. Deposit amount 3. Withdraw amount
4.Exit");
int option = sc.nextInt();
if (option ==
1) {
//Displaying in console
System.out.println(".....................Balance
Summary............................");
System.out.println("Initial_Balance
"+balance+"$");
for (Record record : recordList) {
System.out.println(record.getTransaction_number() + " " +
record.getTransaction_type() + " "
+ record.getTransaction_amount() + "$ " +
record.getTotal_balance() + "$ ");
}
} else if
(option == 2) {
// choosing deposit
System.out.println("Enter Deposit
Amount:");
float deposit_amt = sc.nextFloat();
total_balance = total_balance + deposit_amt;// balance after deposit
Record record = new Record();
transaction_nos++;// increasing transaction
number by 1
record.setTransaction_number(transaction_nos);
record.setTransaction_type("Deposit");
record.setTransaction_amount(deposit_amt);
record.setTotal_balance(total_balance);
recordList.add(record);
// displaying transaction summary:-
System.out.println(record.getTransaction_number() + " " +
record.getTransaction_type() + " "
+
record.getTransaction_amount() + "$ " + record.getTotal_balance() +
"$ ");
//Writing in file
out.append((record.getTransaction_number() + " "
+ record.getTransaction_type() + " "
+
record.getTransaction_amount() + "$ " + record.getTotal_balance() +
"$ \n"));
} else if
(option == 3) {
System.out.println("Enter Withdrawal
Amount:");
float withdrawal_amt;
while (true) {
withdrawal_amt =
sc.nextFloat();
if (withdrawal_amt >
total_balance) {
System.out.println(
"Withdrawal Amount is greater
than Available Balance \nRe-Enter correct Withdrawal
Amount");
continue;
// logic
to prevent user from entering Withdrawal amt> Available total
balance
// ask
user to re-enter and continue the loop unless user enters correct
amount
} else {
break;
//correct amount entered, move to next step
}
}
total_balance = total_balance -
withdrawal_amt;// balance after withdrawal
Record record = new Record();
transaction_nos++;// increasing transaction
number by 1
record.setTransaction_number(transaction_nos);
record.setTransaction_type("Withdraw");
record.setTransaction_amount(withdrawal_amt);
record.setTotal_balance(total_balance);
recordList.add(record);
// displaying transaction summary:-
System.out.println(record.getTransaction_number() + " " +
record.getTransaction_type() + " "
+
record.getTransaction_amount() + "$ " + record.getTotal_balance() +
"$ ");
//Writing in file
out.append((record.getTransaction_number() + " "
+ record.getTransaction_type() + " "
+
record.getTransaction_amount() + "$ " + record.getTotal_balance() +
"$ \n"));
} else if
(option == 4) {
break loop1;
}
else
{
continue loop1;
}
} // labelled while loop for
displaying options
out.close();
}
}
//Record.java
public class Record {
int transaction_number;
String transaction_type;
float transaction_amount;
float total_balance;
public int getTransaction_number() {
return transaction_number;
}
public void setTransaction_number(int transaction_nos)
{
this.transaction_number =
transaction_nos;
}
public String getTransaction_type() {
return transaction_type;
}
public void setTransaction_type(String
transaction_type) {
this.transaction_type =
transaction_type;
}
public float getTransaction_amount() {
return transaction_amount;
}
public void setTransaction_amount(float
transaction_amount) {
this.transaction_amount =
transaction_amount;
}
public float getTotal_balance() {
return total_balance;
}
public void setTotal_balance(float total_balance)
{
this.total_balance =
total_balance;
}
}