Choose a real-life application or game ideally with Graphic User Interface (not
ID: 3808510 • Letter: C
Question
Choose a real-life application or game ideally with Graphic User Interface (not required). Try to apply various Java programming techniques such as inheritance, polymorphism, abstract class, interface, overriding, overloading methods, and exception handling in your Java program. You are supposed to submit a final project report which details the problem description, class design, system functionality and some user manual. Your system will be tested and see if all proposed features work properly.
Bank Account Manager - Create a class called “Account” which will be an abstract class for three other classes called “CheckingAccount”, “SavingsAccount” and “BusinessAccount”. Add necessary instance variables to each of these classes; create some overriding and overloaded methods; calculate interests for these accounts using different formula;
Explanation / Answer
Solution:
Executable Code:
TestProgram.java
package testprogram;
import java.util.Scanner;
// Test program
public class TestProgram {
static Scanner in = new Scanner(System.in);
public static void main(String[] args){
// Create object of CheckingAccount
Account ca1 = new CheckingAccount(1234);
// Create object of SavingsAccount
Account sa1 = new SavingsAccount(1235);
// Create object of BusinessAccount
Account ba1 = new BusinessAccount(1236);
// Set balance for BusinessAccount
ba1.setBalance(100000);
// Set balance for CheckingAccount
ca1.setBalance(750000);
// Set balance for SavingsAccount
sa1.setBalance(50000);
int ch;
do
{
System.out.println("Select the acc");
System.out.println("1.Checking Account 2.Savings Account 3.Business Account 4.Exit Enter the option");
ch = Integer.parseInt(in.nextLine());
// Switch the choice
switch (ch){
case 1:
acc_operation(sa1);
break;
case 2:
acc_operation(ca1);
break;
case 3:
acc_operation(ba1);
break;
case 4:
break;
default:
System.out.println("Invalid choice");
}
}while(ch!=4);
}
public static void acc_operation(Account acc){
System.out.println("1.Check balance 2.Withdrawal 3.Deposit 4.Exit Enter choice");
int choice = Integer.parseInt(in.nextLine());
switch (choice){
case 1:
acc.disBalance();
break;
case 2:
System.out.println("Enter amount to withdrawal");
double am = Double.parseDouble(in.nextLine());
try {
acc.withdrawal(am);
}catch (RuntimeException e1){
System.out.println(e1.getMessage());
}finally {
acc.disBalance();
}
break;
case 3:
System.out.println("Enter amount to dep");
am = Double.parseDouble(in.nextLine());
acc.dep(am);
acc.disBalance();
break;
case 4:
System.out.println("Thanks!!!");
System.exit(1);
break;
default:
System.out.println("Invalid choice");
}
}
}
package testprogram;
public abstract class Account {
private int accNumber;
private double accBalance;
// Parametrized Constructor
public Account(int accNumber){
this.accNumber = accNumber;
}
// Constructot
public Account(){
}
// Method to set the account number
public void setAccNumber(int accNumber){
this.accNumber = accNumber;
}
// Method to retrieve the account number
public int getAccNumber(){
return accNumber;
}
// Method to det the balance
public void setBalance(double accBalance){
this.accBalance = accBalance;
}
// Method to retrieve the account balance
public double getAccBalance(){
return accBalance;
}
// Method to deposit amount
public abstract void dep(double am);
// Method to display the balance
public abstract void disBalance();
// Method for withdrawal
public abstract double withdrawal(double am);
}
CheckingAccount.java
package testprogram;
// Class CheckingAccount
public class CheckingAccount extends Account {
// Parametrized constructor
public CheckingAccount(int accNum){
super(accNum);
}
// Default constructor
public CheckingAccount(){
}
// Method to deposit the amount
public void dep(double am){
// Set the balance
setBalance(am + getAccBalance());
}
// Method to withdraw the amount
public double withdrawal(double am){
// If the balance is less than am
if(getAccBalance() <= am){
// Throw exceptions
throw new RuntimeException("Insufficient funds");
}
// Set balance
setBalance(getAccBalance() - am);
// Return
return getAccBalance();
}
// Display the balance
public void disBalance(){
System.out.println("Account balance : " + getAccBalance());
}
}
SavingsAccount.java
// Class SavingsAccount
package testprogram;
public class SavingsAccount extends Account {
// Parametrized constructor
public SavingsAccount(int accNum){
super(accNum);
}
// Default constructor
public SavingsAccount(){
}
// Method to deposit amount
public void dep(double am){
// Add the amount
setBalance(am + getAccBalance());
}
// Method to withdraw amount
public double withdrawal(double am){
// If there is no sufficient amount
if(getAccBalance() <= am){
// Throw exceptions
throw new RuntimeException("Insufficient funds");
}
// Set the balance
setBalance(getAccBalance() - am);
// return the balance
return getAccBalance();
}
// Display the balance
public void disBalance(){
System.out.println("Account balance : " + getAccBalance());
}
}
BusinessAccount.java
package testprogram;
// Class BusinessAccount
public class BusinessAccount extends Account {
// Parametrized constructor
public BusinessAccount(int accNum){
super(accNum);
}
// Default constructor
public BusinessAccount(){
}
// Method to deposit
public void dep(double am){
// Add the amount
setBalance(am + getAccBalance());
}
// Method to withdraw amount
public double withdrawal(double am){
// If the fund is insufficient
if(getAccBalance() <= am){
// Print error
throw new RuntimeException("Insufficient funds");
}
// set the balance
setBalance(getAccBalance() - am);
// Return the balance
return getAccBalance();
}
// Method to display the balance
public void disBalance(){
System.out.println("Account balance : " + getAccBalance());
}
}
Output:
run:
Select Account to use
1.Savings Account
2.Checking Account
3.Business Account
4.Exit
Enter choice
1
1.Check balance
2.Withdrawal
3.Deposit
4.Exit
Enter choice
1
Account balance : 50000.0
Select Account to use
1.Savings Account
2.Checking Account
3.Business Account
4.Exit
Enter choice
1
1.Check balance
2.Withdrawal
3.Deposit
4.Exit
Enter choice
2
Enter amount to withdraw
500
Account balance : 49500.0
Select Account to use
1.Savings Account
2.Checking Account
3.Business Account
4.Exit
Enter choice
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.