Design a Java program that simulates bank accounts. Your main function should co
ID: 3904880 • Letter: D
Question
Design a Java program that simulates bank accounts. Your main function should contain a menu to allow the user to create, modify, retrieve (display) and delete a bank account.
To modify/retrieve a bank account you need to enter either a last name or SSN.
You need to design a class for Date, a class for person, and a class for BankAccount. A Person inherites from date as each person has
a birth date, in addition to first name, last name, address and SSN.
A BankAccount has a maximum of three holders (person) and a balance, an interest rate, opening date, last date interest was payed. *Use vectors for holders*
A method to compute the interest.
The code for Date and Person is already completed down below.
///////////////////////////////////////////////////////////
public class Date {
private int day;
private int month;
private int year;
private static int[] Days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public Date() {
day = 1;
month = 1;
year = 1970;
}
public Date(int m, int d, int y) {
day = d;
month = m;
year = y;
}
public Date(String s) {
setDate(s);
}
public void setDate(String s) {
try {
if (s.contains("/")) {
// the format is mm/dd/yyyy
String[] result = s.split("/");
year = Integer.parseInt(result[2]);
setMonth(Integer.parseInt(result[0]));
setDay(Integer.parseInt(result[1]));
} else if (s.indexOf('-') > 0) {
String[] result = s.split("-");
year = Integer.parseInt(result[0]);
setMonth(Integer.parseInt(result[1]));
setDay(Integer.parseInt(result[2]));
} else
throw new Exception("Invalid date");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public String toString() {
String st = month + "/" + day + "/" + year;
return st;
}
// getters
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
// setters
public void setYear(int y) {
year = y;
}
public void setMonth(int m) {
try {
if (m < 1 || m > 12) {
throw new Exception("Invalid month");
}
month = m;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void setDay(int d) {
try {
if ((isLeap() && month == 2)) {
if (d < 1 || d > 29) {
throw new Exception("Invalid day, because of leap year");
}
day = d;
}
else{
if(d < 1 || d > Days[month]){
throw new Exception("Invalid day");
}
day = d;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
day = d;
}
public boolean isLeap() {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
public class Person extends Date {
private String fName, lName, Address;
private int ssn;
//default constructor
public Person() {
//we call the default constructor of Date
super();
//set all private members to default values
fName = "unknown";
lName = "unknown";
Address = "unknown";
ssn = 0;
}
public Person (String dob, String fn, String ln, String addr, int s) {
super(dob);
fName = fn;
lName = ln;
Address = addr;
ssn = s;
}
//getters
public String getFName() {
return fName;
}
public String getLName() {
return lName;
}
public String getAddress() {
return Address;
}
public int getSSN() {
return ssn;
}
//setters
public void setFName(String n) {
fName = n;
}
public void setLName(String n) {
lName = n;
}
public void setAddress(String a){
Address = a;
}
public void setSSN(int n) {
ssn = n;
}
public void setDOB(String s){
this.setDate(s);
}
public void setDOB(int m, int d, int y){
String date = m+"/"+d+"/"+y;
this.setDate(date);
}
public String toString() {
String st = "DoB: " + super.toString() + ", SSN: " + ssn + ", First Name: "
+ fName + ", Last Name: " + lName + " Address: "+ Address;
return st;
}
public void read(Scanner in) {
System.out.println("enter your birth day (mm/dd/yyyy):");
String d = in.nextLine();
this.setDate(d);
System.out.println("enter SSN: " );
this.setSSN(in.nextInt());
this.setFName(in.nextLine());//the carriage return will be still inthe buffer
//nextLinen if invoked will read one character (carriage return) and return
//we need to empty the buffer before reading a string, we read a line for nothing
System.out.println("enter first name: " );
this.setFName(in.nextLine());
System.out.println("enter last name: " );
this.setLName(in.nextLine());
System.out.println("enter address: " );
this.setAddress(in.nextLine());
}
}
Explanation / Answer
BankAccount.java
public class BankAccount {
private Person person;
private double balance;
private double interest;
private Date openingdate;
private Date interestpaiddate;
public BankAccount(double bal, double intr, Person p, Date o)
{
balance = bal;
interest = intr;
person = p;
openingdate = o;
interestpaiddate = new Date();
}
//getters
public double getBalance() {
return balance;
}
public double getInterest() {
return interest;
}
public String getOpeningDate() {
return openingdate.toString();
}
public String getInterestPaidDate() {
return interestpaiddate.toString();
}
public String getLastName()
{
return person.getLName();
}
public int getSSN()
{
return person.getSSN();
}
//setters
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void addInterest(String d)
{
balance = balance + balance * interest;
interestpaiddate = new Date(d);
}
public void setFName(String n) {
person.setFName(n);
}
public void setLName(String n) {
person.setLName(n);
}
public void setAddress(String a){
person.setAddress(a);
}
public void setSSN(int n) {
person.setSSN(n);
}
public void setDOB(String s){
person.setDate(s);
}
public void setDOB(int m, int d, int y){
String date = m+"/"+d+"/"+y;
person.setDate(date);
}
public String toString() {
String st = "Bank Account Information " +
"Account Holder: " +
person.toString() + " " +
"Balance Amount: " + getBalance() + " " +
"Opening Date: " + getOpeningDate() + " " +
"Last Interest Added Date: " + getInterestPaidDate();
return st;
}
}
Main.java
public class Main
{
//Method used to get the Bank Account
public static BankAccount getBankAccount(ArrayList<BankAccount> bankAccounts)
{
Scanner sc = new Scanner(System.in);
//Run loop until user select option of search
//by SSN or LastName
boolean isValid = false;
int bankAccountIndx = -1;
int choice = 0;
while(!isValid)
{
System.out.println("1. Get Bank Account by SSN");
System.out.println("2. Get Bank Account by Person LastName");
System.out.println("Please select any one option to get the Bank Account");
choice = sc.nextInt();
sc.nextLine();
if(choice < 1 || choice > 2)
{
System.out.println("Please select valid option! Try again.");
}
else
{
isValid = true;
}
}
//If search account by SSN
if(choice == 1)
{
int ssn;
System.out.print("Enter SSN: ");
ssn = sc.nextInt();
//Run bankAccounts arraylist in loop to find bank account by
//user entered SSN
for(int i=0; i< bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getSSN() == ssn)
{
bankAccountIndx = i;
}
}
}
//If search account by LastName
else if(choice == 2)
{
String lastName;
System.out.print("Enter Person LastName: ");
lastName = sc.nextLine();
//Run bankAccounts arraylist in loop to find bank account by
//user entered LastName
for(int i=0; i< bankAccounts.size(); i++)
{
if(bankAccounts.get(i).getLastName().equals(lastName))
{
bankAccountIndx = i;
}
}
}
//If bank account not found then return null
if(bankAccountIndx < 0)
{
return null;
}
//If bank account found then bank account object
else
{
return bankAccounts.get(bankAccountIndx);
}
}
//Method used to modify the Bank Account
public static void modifyAccount(BankAccount b)
{
Scanner sc = new Scanner(System.in);
//Run loop until user select option of deposit
//or withdraw or add interest
boolean isValid = false;
int choice = 0;
while(!isValid)
{
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Add Interest");
System.out.println("Please choose any one from above options");
choice = sc.nextInt();
if(choice < 1 || choice > 3)
{
System.out.println("Please select valid option! Try again.");
}
else
{
isValid = true;
}
}
//If for deposit to bank account
if(choice == 1)
{
double depositAmount;
System.out.print("Enter amount to deposit: ");
depositAmount = sc.nextDouble();
//Deposit user entered amount to bank account
b.deposit(depositAmount);
System.out.println("Amount " + depositAmount + " successfully deposited ");
}
//If for withdraw to bank account
else if(choice == 2)
{
double withdrawAmount;
System.out.print("Enter amount to withdraw: ");
withdrawAmount = sc.nextDouble();
//If withdraw amount is greater than balance
//then show error message as insufficient balance
if(withdrawAmount > b.getBalance())
{
System.out.println("Insufficient balance to withdraw!");
}
//If withdraw amount is lesser than balance the proceed
//withdrawing amount
else
{
//Withdraw user entered amount to bank account
b.withdraw(withdrawAmount);
System.out.println("Amount " + withdrawAmount + " successfully withdrawn ");
}
}
//If for adding interest to bank account
else if(choice == 3)
{
String interestDate;
System.out.print("Enter date to add interest: ");
interestDate = sc.nextLine();
//Add Interest to bank account
b.addInterest(interestDate);
System.out.println("Successfully added interest to account ");
}
}
//Main program where execution starts
public static void main(String[] args) {
boolean canProceed = true;
//Array of BankAccount object
ArrayList<BankAccount> bankAccounts = new ArrayList<BankAccount>();
//Run loop till user select exit
while(canProceed)
{
//Display menu to choose
System.out.println("Welcome to Bank");
System.out.println("*****************************************");
System.out.println("1. Create Account");
System.out.println("2. Modify Account");
System.out.println("3. Retrieve/Display Account");
System.out.println("4. Delete Account");
System.out.println("5. Exit");
System.out.println("Please choose any one from above options");
Scanner sc = new Scanner(System.in);
//Run loop till user select valid menu option
boolean isValid = false;
int userInput = 0;
while(!isValid)
{
userInput = sc.nextInt();
sc.nextLine();
if(userInput < 1 || userInput > 5)
{
System.out.println("Please select valid menu number! Try again.");
}
else
{
isValid = true;
}
}
//To create Bank Account
if(userInput == 1)
{
//Get user inputs to create Person and BankAccount objects
Person p = new Person();
p.read(sc);
System.out.println("Enter Opening Balance: ");
double balance = sc.nextDouble();
sc.nextLine();
Date openingDate = new Date();
System.out.println("Enter Opening Date (mm/dd/yyyy):");
String d = sc.nextLine();
openingDate.setDate(d);
System.out.println("Enter Interest Rate: ");
double interest = sc.nextDouble();
sc.nextLine();
//Create BankAccount object based on user input
BankAccount bankAccount = new BankAccount(balance, interest, p, openingDate);
//Add newly created BankAccount object to BankAccount ArrayList
bankAccounts.add(bankAccount);
System.out.println("Account Created for user " + p.getFName() + " " + p.getLName());
}
//To modify Bank Account
else if(userInput == 2)
{
//Get bank account based on user search by calling method
//getBankAccount
BankAccount b = getBankAccount(bankAccounts);
//If account not found then display error message
if(b.equals(null))
{
System.out.print("No Bank Account found for given SSN/LastName!");
}
//If account found then proceed modifying account
else
{
modifyAccount(b);
}
}
//To display Bank Account information
else if(userInput == 3)
{
//Get bank account based on user search by calling method
//getBankAccount
BankAccount b = getBankAccount(bankAccounts);
//If account not found then display error message
if(b.equals(null))
{
System.out.print("No Bank Account found for given SSN/LastName!");
}
//If account found then proceed displaying account information
else
{
System.out.println(b.toString());
}
}
//To delete Bank Account
else if(userInput == 4)
{
//Get bank account based on user search by calling method
//getBankAccount
BankAccount b = getBankAccount(bankAccounts);
//If account not found then display error message
if(b.equals(null))
{
System.out.print("No Bank Account found for given SSN/LastName!");
}
//If account found then delete the account
else
{
bankAccounts.remove(b);
System.out.print("Successfully deleted the account");
}
}
//To exit the program
else if(userInput == 5)
{
//Set the variable to false to exit
canProceed = false;
}
}
}
}
BankAccount using Vectors
public class BankAccount {
private Vector<Person> holder;
private double balance;
private Person person;
private double interest;
private Date openingdate;
private Date interestpaiddate;
public BankAccount(double bal, double intr,Vector<Person> holders, Date o)
{
balance = bal;
interest = intr;
holder = holders;
openingdate = o;
interestpaiddate = new Date();
}
//getters
public double getBalance() {
return balance;
}
public double getInterest() {
return interest;
}
public String getOpeningDate() {
return openingdate.toString();
}
public String getInterestPaidDate() {
return interestpaiddate.toString();
}
public Vector<Person> getHolder(){
return holder;
}
public void setHolder(Vector<Person> inHolder){
holder = inHolder;
}
//setters
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void addInterest(String d)
{
balance = balance + balance * interest;
interestpaiddate = new Date(d);
}
public String toString() {
String st = "Bank Account Information " +
"Account Holders: " +
holder.toString() + " " +
"Balance Amount: " + getBalance() + " " +
"Opening Date: " + getOpeningDate() + " " +
"Last Interest Added Date: " + getInterestPaidDate();
return st;
}
}