Oopex 6
Oopex 6
6 PACKAGES
06-09-2024 URK23AI1035
Aim
To develop an application in JAVA for automating the Banking Operations using packages and import the
packagesinto a Test Class.
Description
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Example:
Simple{
{
System.out.println("Welcome to package");
}
}
Program :
Package 1
package pkg1;
package pkg2;
import pkg1.Account;
public class SavingsAccount extends
Account {private double interestRate;
public SavingsAccount(String accountNumber, double initialBalance, double interestRate) {
super(accountNumber, initialBalance);
this.interestRate = interestRate;
}
public void addInterest() {
double interest = balance * interestRate / 100;
balance += interest;
System.out.println("Interest added: " + interest);
}
}
package pkg2;
import pkg1.Account;
public class CurrentAccount extends
Account {private double overdraftLimit;
public CurrentAccount(String accountNumber, double initialBalance, double overdraftLimit) {
super(accountNumber, initialBalance);
this.overdraftLimit = overdraftLimit;
}
@Override
public boolean withdraw(double amount) {
if (amount > 0 && (balance + overdraftLimit) >= amount) {
balance -= amount;
System.out.println("Withdrew: " + amount);return
true;
} else {
System.out.println("Overdraft limit exceeded or invalid amount");
return false;
}
Test Class (Menu-driven):
package oopa;
import pkg1.Account;
import
pkg2.SavingsAccount;
import
pkg2.CurrentAccount;
import
java.util.Scanner;
{
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);Account account =
null;
switch (choice) {
case 1:
System.out.println("Enter interest rate: "); double
interestRate = scanner.nextDouble();
account = new SavingsAccount(accountNumber, initialBalance, interestRate);
break;
case 2:
System.out.println("Enter overdraft limit: ");
double overdraftLimit = scanner.nextDouble();
account = new CurrentAccount(accountNumber, initialBalance, overdraftLimit);
break;
default:
System.out.println("Invalid choice");
System.exit(0);
}
boolean running = true;
while (running) {
System.out.println("\nChoose operation: ");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Balance Enquiry");
System.out.println("4. Exit");
int operation = scanner.nextInt();
switch (operation{
case 1:
System.out.println("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.println("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
System.out.println("Balance: " + account.getBalance());
break;
case 4:
running = false;
break;
default:
System.out.println("Invalid operation");
}
Result:
This all-in-one code demonstrates the compartmentalization of the banking operations system,
using Javapackages and classes with inheritance is successfully created and verified.