0% found this document useful (0 votes)
6 views6 pages

Oopex 6

The document outlines the development of a Java application for automating banking operations using packages. It includes the creation of two packages, one for account management and another for specific account types, along with a test class for user interaction. The application demonstrates the use of inheritance and encapsulation in Java to manage different account functionalities.

Uploaded by

isaacbenjamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Oopex 6

The document outlines the development of a Java application for automating banking operations using packages. It includes the creation of two packages, one for account management and another for specific account types, along with a test class for user interaction. The application demonstrates the use of inheritance and encapsulation in Java to manage different account functionalities.

Uploaded by

isaacbenjamin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex. No.

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

A java package is a group of similar types of classes, interfaces and sub-packages.

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.

The package keyword is used to create a package in java.

Example:

package mypack; public class

Simple{

public static void main(String args[])

{
System.out.println("Welcome to package");
}
}
Program :

Package 1

package pkg1;

public class Account {


private String
accountNumber;protected
double balance;

public Account(String accountNumber, double initialBalance)


{this.accountNumber = accountNumber;
this.balance = initialBalance;
}

public String getAccountNumber() {


return accountNumber;
}

public double getBalance()


{return balance;
}

public void deposit(double amount)


{if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Invalid deposit amount");
}
}

public boolean withdraw(double amount)


{if (amount > 0 && balance >=
amount) {
balance -= amount;
System.out.println("Withdrew: " + amount);return
true;
} else {
System.out.println("Insufficient balance or invalid amount");
return false;
}
}
}
Package 2

SavingsAccount Class in pkg2:

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);
}
}

CurrentAccount Class in pkg2

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 class Test

{
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);Account account =
null;

System.out.println("Choose account type:


");System.out.println("1. Savings
Account"); System.out.println("2.
Current Account"); int choice =
scanner.nextInt();

System.out.println("Enter account number: ");


String accountNumber = scanner.next();

System.out.println("Enter initial balance: ");


double initialBalance = scanner.nextDouble();

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");
}

if (account instanceof SavingsAccount) {


((SavingsAccount) account).addInterest();
}
}
}
}
Output :

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.

You might also like