In: Computer Science
It is required to develop an application in Java to perform some operations of a bank. The application will have the following classes:
firstName (String), lastName(String), gender(char), cpr(long), mobile(String) and following methods:
accountHolder(Person), ccountNum(long), balance(double)and following methods:
interestRate(double) and following methods:
The above mentioned classes are implemented as below:
//Person.java
public class Person {
private String firstName;
private String lastName;
private char gender;
private long cpr;
private String mobile;
public Person(String firstName,String lastName,char gender,long cpr,String mobile){
this.firstName=firstName;
this.lastName=lastName;
this.gender=gender;
this.cpr=cpr;
this.mobile=mobile;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public long getCpr() {
return cpr;
}
public void setCpr(long cpr) {
this.cpr = cpr;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String toString(){
return firstName+" "+lastName+" "+gender+" "+cpr+" "+mobile;
}
}
//Account.java
public abstract class Account {
protected Person accountHolder;
protected long accountNum;
protected double balance;
public Account(Person accountHolder, long accountNum, double balance) {
super();
this.accountHolder = accountHolder;
this.accountNum = accountNum;
this.balance = balance;
}
public Person getAccountHolder() {
return accountHolder;
}
public long getAccountNum() {
return accountNum;
}
public double getBalance() {
return balance;
}
abstract void updateBalance(double amount);
abstract void withDraw(double amount);
abstract void deposit(double amount);
public String toString(){
return accountHolder.toString()+" "+ accountNum+" "+balance;
}
}
//SavingsAccount.java
public class SavingsAccount extends Account {
double interestRate;
public SavingsAccount(Person accountHolder, long accountNum, double balance, double interestRate) {
super(accountHolder, accountNum, balance);
this.interestRate=interestRate;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
@Override
void updateBalance(double amount) {
this.balance=amount;
}
@Override
void withDraw(double amount) {
if(amount>this.balance){
System.out.println("You don't have sufficicent balance to Withdraw");
}else{
updateBalance(this.balance-amount);
}
}
@Override
void deposit(double amount) {
updateBalance(this.balance+amount);
}
public String toString(){
return accountHolder.getFirstName()+" "+accountHolder.getLastName() + " with Account number "+accountNum+" has current balance "+balance+" with interest rate of "+interestRate;
}
}
//Bank.java
public class Bank {
public static void main(String[] args){
Person p1 = new Person("Sam", "Roy", 'F', 15, "978901789");
SavingsAccount s1 = new SavingsAccount(p1, 1148027110, 89900, 8);
System.out.println("Before Withdraw");
System.out.println(s1.toString());
s1.withDraw(19800);
System.out.println("After Withdraw and Before Deposit");
System.out.println(s1.toString());
s1.deposit(10700);
System.out.println("After Deposit");
System.out.println(s1.toString());
}
}
The ouput screenshot for the above program is as below:
If you have any queries regarding this answer, please reach out through the comment section.