No.
5 COMPARTMENTALIZING THE CODE
30/08/24 URK23AI1035
Aim
Develop an application in Java for automating the Banking Operations using interfaces.
Create an interface called “Transaction” which contains the functions such as deposit,
withdraw, and viewBalance. Create another interface called “Displayable” which
contains the Display () function to display the account details.
Create an abstract class called “Account” with bank account details such as acc_name,
acc_no, and balance. Add necessary constructors.
Create a “Bank” class which implements the “Transaction”, “Displayable” interfaces
and inherits “Account” class.
Perform menu driven operations like Deposit, Withdraw and Balance Enquiry, View
Account Details from a Main class. Write logics in the corresponding methods.
Description
1. Transaction Interface: Contains the deposit(), withdraw(), and viewBalance() methods that define
basic transaction operations.
2. Displayable Interface: Contains the display() method to show account details.
3. Account Abstract Class: Stores the common account information such as account name, number,
and balance. It serves as a base class for Bank.
4. Bank Class: Implements the interfaces and provides the logic for depositing, withdrawing, viewing
balance, and displaying account details.
5. Main Class: Uses a menu-driven system to allow the user to choose between depositing,
withdrawing, viewing balance, and displaying account details.
6.
How the Application Works:
• The user is prompted to enter account details when the program starts.
• The menu allows for various banking operations:
1. Deposit: Allows the user to deposit an amount.
2. Withdraw: Allows the user to withdraw an amount if sufficient balance is available.
3. View Balance: Displays the current balance.
4. View Account Details: Displays account holder’s name, account number, and current balance.
5. Exit: Exits the program.
Program
import java.util.Scanner;
// Transaction Interface
interface Transaction {
void deposit(double amount);
void withdraw(double amount);
double viewBalance();
}
// Displayable Interface
interface Displayable {
void display();
}
// Abstract class Account
abstract class Account {
protected String acc_name;
protected long acc_no;
protected double balance;
// Constructor to initialize account details
public Account(String acc_name, long acc_no, double balance) {
this.acc_name = acc_name;
this.acc_no = acc_no;
this.balance = balance;
}
}
// Bank class inheriting from Account and implementing Transaction, Displayable
class Bank extends Account implements Transaction, Displayable {
// Constructor to initialize bank account
public Bank(String acc_name, long acc_no, double balance) {
super(acc_name, acc_no, balance);
}
// Method to deposit money
@Override
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Amount deposited successfully.");
} else {
System.out.println("Invalid deposit amount.");
}
}
// Method to withdraw money
@Override
public void withdraw(double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
System.out.println("Amount withdrawn successfully.");
} else if (amount > balance) {
System.out.println("Insufficient balance.");
} else {
System.out.println("Invalid withdrawal amount.");
}
}
// Method to view balance
@Override
public double viewBalance() {
return balance;
}
// Method to display account details
@Override
public void display() {
System.out.println("Account Holder Name: " + acc_name);
System.out.println("Account Number: " + acc_no);
System.out.println("Account Balance: " + balance);
}
}
// Main class for menu-driven operations
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Creating a bank account
System.out.println("Enter Account Holder Name:");
String acc_name = scanner.nextLine();
System.out.println("Enter Account Number:");
long acc_no = scanner.nextLong();
System.out.println("Enter Initial Balance:");
double balance = scanner.nextDouble();
// Creating a Bank object with the inputted details
Bank bankAccount = new Bank(acc_name, acc_no, balance);
// Menu-driven operations
while (true) {
System.out.println("\n*** Banking Operations Menu ***");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. View Balance");
System.out.println("4. View Account Details");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: // Deposit
System.out.println("Enter amount to deposit:");
double depositAmount = scanner.nextDouble();
bankAccount.deposit(depositAmount);
break;
case 2: // Withdraw
System.out.println("Enter amount to withdraw:");
double withdrawAmount = scanner.nextDouble();
bankAccount.withdraw(withdrawAmount);
break;
case 3: // View Balance
System.out.println("Current Balance: " + bankAccount.viewBalance());
break;
case 4: // View Account Details
bankAccount.display();
break;
case 5: // Exit
System.out.println("Exiting the application...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please choose a valid option.");
}
}
}
}
Output;
Write a program to implement the Library Information System using
packages with the following instructions
a) Create a Books class in pkg1.
b) Create an Admin class in pkg2.
c) Create a User class in pkg3.
d) Import all packages in Test class and do the following operations in a menu-driven
fashion. Add books, Search books, and List books.
Program
package pkg1;
public class Books {
private String title;
private String author;
private String isbn;
// Constructor to initialize book details
public Books(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
// Getters for book details
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
// Method to display book details
public void displayBookDetails() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + isbn);
}
}
package pkg2;
import pkg1.Books;
import java.util.ArrayList;
public class Admin {
private ArrayList<Books> bookList;
// Constructor to initialize the book list
public Admin() {
bookList = new ArrayList<>();
}
// Method to add a book to the list
public void addBook(String title, String author, String isbn) {
Books book = new Books(title, author, isbn);
bookList.add(book);
System.out.println("Book added successfully!");
}
// Method to search for a book by title
public Books searchBook(String title) {
for (Books book : bookList) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
// Method to list all books
public void listBooks() {
if (bookList.isEmpty()) {
System.out.println("No books available in the library.");
} else {
for (Books book : bookList) {
book.displayBookDetails();
System.out.println("----------------------------");
}
}
}
}
package pkg3;
import pkg1.Books;
public class User {
// Method to view book details
public void viewBookDetails(Books book) {
if (book != null) {
System.out.println("Book Details:");
book.displayBookDetails();
} else {
System.out.println("Book not found.");
}
}
}import pkg1.Books;
import pkg2.Admin;
import pkg3.User;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Admin admin = new Admin();
User user = new User();
Scanner scanner = new Scanner(System.in);
int choice;
// Menu-driven system
do {
System.out.println("\nLibrary Information System:");
System.out.println("1. Add Book");
System.out.println("2. Search Book");
System.out.println("3. List All Books");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
// Add a book
System.out.print("Enter Book Title: ");
String title = scanner.nextLine();
System.out.print("Enter Book Author: ");
String author = scanner.nextLine();
System.out.print("Enter Book ISBN: ");
String isbn = scanner.nextLine();
admin.addBook(title, author, isbn);
break;
case 2:
// Search for a book
System.out.print("Enter the title of the book to search: ");
String searchTitle = scanner.nextLine();
Books foundBook = admin.searchBook(searchTitle);
user.viewBookDetails(foundBook);
break;
case 3:
// List all books
admin.listBooks();
break;
case 4:
// Exit the program
System.out.println("Exiting the Library Information System.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
scanner.close();
}
}
Output
Result
Successfully implemented and executed the creation and manipulation of classes and objects in
Java to solve real-world problems using inheritance, method overriding, and runtime
polymorphism.