Java Programing 1 - 24206-CSC325-0
Project 3
Date Due: 2/28/2020
Bank Account – Part 2
Modify the Bank Account class you created to have the following additional functionality
setAccountPassWord ()
enCryptData()
Example
12345 %21 - this returns 18 which is the remainder
656564 % 21 – this returns 20 which is the remainder
Add the following additional methods
Withdraw ()
Deposit()
Transfer()
DisplayBalance()
Test your code with the followings
Display Balance
/* Comment are placed for all functions and variables to help explain the code.
Main class consists of the main function that is used for testing our code.
Scanner class is used to take input from user. */
//importing the necessary files and packages
import java.util.Scanner;
import java.io.*;
import java.util.*;
//This is the class account that form 2 accounts -savings
account and checking account
class Account{
private int savingsBalance; //stores the savings account
balance
private int checkingBalance; //stores the checkings account
balance
private int securityPin; //stores the encrypted security pin
int encryptData(int n) //encrypt a number to mod21 form
{
return n%21;
}
int setAccountPassword() //used to set password when account is
created (called by constructor)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter new pin number:");
String pin = s.nextLine();
if(pin.length()>=4&&pin.length()<=8)
{
for(int i=0;i<pin.length();i++)
if(!(pin.charAt(i)>='0'&&pin.charAt(i)<='9'))
return setAccountPassword();
}
else
{
return setAccountPassword();
}
return encryptData(Integer.parseInt(pin));
}
Account() // Account class constructor
{
savingsBalance = 0;
checkingBalance = 0;
securityPin = setAccountPassword();
}
void withdraw() // withdraw an amount from savings or checkings
account. Display error for insufficient balance.
{
System.out.println("********* WITHDRAW AMOUNT *********");
Scanner s = new Scanner(System.in);
System.out.println("Enter account pin number:");
String pin = s.nextLine();
if(encryptData(Integer.parseInt(pin))!=securityPin)
withdraw();
System.out.println("press 1 to withdraw from savings account or 2
to withdraw from checking account");
int option = s.nextInt();
if(option == 1) //withdraw from savings account
{
System.out.println("Enter amount to withdraw:");
int amount = s.nextInt();
if(savingsBalance>=amount)
{
savingsBalance-=amount;
System.out.println("Total amount of "+amount+" is
withdrawn from savings account");
}
else
{
System.out.println("Insufficient balance to withdraw amount of
"+amount+" from savings account");
}
}
else //withdraw from checkings account
{
System.out.println("Enter amount to withdraw:");
int amount = s.nextInt();
if(checkingBalance>=amount)
{
checkingBalance-=amount;
System.out.println("Total amount of "+amount+" is
withdrawn from checking account");
}
else
{
System.out.println("Insufficient balance to withdraw amount of
"+amount+" from checking account");
}
}
}
void deposit() // deposit an amount to savings or checkings
account
{
System.out.println("********* DEPOSIT AMOUNT *********");
Scanner s = new Scanner(System.in);
System.out.println("Enter account pin number:");
String pin = s.nextLine();
if(encryptData(Integer.parseInt(pin))!=securityPin)
deposit();
System.out.println("press 1 to deposit to savings account or 2 to
deposit to checking account");
int option = s.nextInt();
System.out.println("Enter amount to deposit:");
int amount = s.nextInt();
if(option == 1) //deposit to savings account
{
savingsBalance+=amount;
System.out.println("Total amount of "+amount+" is
deposited to savings account");
}
else //deposit to checking account
{
checkingBalance+=amount;
System.out.println("Total amount of "+amount+" is
deposited to checking account");
}
}
void transfer() // transfer an amount from checking to savings
or from savings to checking
{
System.out.println("********* TRANSFER AMOUNT *********");
Scanner s = new Scanner(System.in);
System.out.println("Enter account pin number:");
String pin = s.nextLine();
if(encryptData(Integer.parseInt(pin))!=securityPin)
transfer();
System.out.println("press 1 to transfer from checking to savings or
2 to transfer from savings to checking");
int option = s.nextInt();
if(option==1)// transfer an amount from checking to savings
{
System.out.println("Enter amount to transfer from checking to
savings:");
int amount = s.nextInt();
if(checkingBalance>=amount)
{
checkingBalance-=amount;
savingsBalance+=amount;
System.out.println("Total amount of "+amount+" is
transferred from checking account to savings account");
}
else
{
System.out.println("Insufficient balance to transfer amount of
"+amount+" from checking account to savings account");
}
}
else// transfer an amount from savings to checking
{
System.out.println("Enter amount to transfer from savings to
checking:");
int amount = s.nextInt();
if(savingsBalance>=amount)
{
savingsBalance-=amount;
checkingBalance+=amount;
System.out.println("Total amount of "+amount+" is
transferred from savings account to checking account");
}
else
{
System.out.println("Insufficient balance to transfer amount of
"+amount+" from savings account to checking account");
}
}
}
void displayBalance() //Display balances of both savings and
checking account
{
System.out.println("********* DISPLAY BALANCE *********");
Scanner s = new Scanner(System.in);
System.out.println("Enter account pin number:");
String pin = s.nextLine();
if(encryptData(Integer.parseInt(pin))!=securityPin)
displayBalance();
System.out.println("Savings account balance:
"+savingsBalance);
System.out.println("checking account balance:
"+checkingBalance);
}
}
//Testing our code
class Main{
public static void main(String args[])
{
Account acc= new Account(); //creating the
account
acc.displayBalance();
acc.deposit();
acc.deposit();
acc.displayBalance();
acc.transfer();
acc.displayBalance();
acc.withdraw();
acc.withdraw();
acc.displayBalance();
}
}
Get Answers For Free
Most questions answered within 1 hours.