Guvi Task3 - OOPs Programming
Solution - 1:
1) Management System
Design a Java program that uses OOP principles to model the Book Create two classes: Book and Library. The Book class should have attributes such as bookID, title, author, and isAvailable. The Library class should include an array to store book objects.
Provide methods to add books, remove book search books (using id)and display books. Write a Java program that demonstrates the use of these classes and methods by allowing the user to interact with the library system.
(Note: Output for the problem is given below)
import java.util.Scanner;
// Class book to store attributes to create a library
class Book {
private int bookID;
private String title;
private String author;
private boolean isAvailable;
// Constructor
public Book(int bookID, String title, String author, boolean isAvailable) {
this.bookID = bookID;
this.title = title;
this.author = author;
this.isAvailable = isAvailable;
}
// Getters and Setters
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean available) {
isAvailable = available;
}
// Book details will be displayed here
@Override
public String toString() {
return "BookID: " + bookID + ", Title: " + title + ", Author: " + author + ", Available: " + isAvailable;
}
}
// Library class to create book size
class Library {
private Book[] books;
private int bookCount;
// Size of the array is fixed based on attributes
public Library() {
books = new Book[5];
bookCount = 0;
}
// Method to add a book
public void addBook(Book book) {
if (bookCount < books.length) {
books[bookCount++] = book;
System.out.println("Book added successfully!");
} else {
System.out.println("Library is full! Cannot add more books.");
}
}
// Method to replace a book by bookID
public void replaceBook(int bookID, Book newBook) {
for (int i = 0; i < bookCount; i++) {
if (books[i].getBookID() == bookID) {
books[i] = newBook;
System.out.println("Book replaced successfully!");
return;
}
}
System.out.println("Book with ID " + bookID + " not found!");
}
// Method to display all books
public void displayBooks() {
if (bookCount == 0) {
System.out.println("No books in the library.");
} else {
for (int i = 0; i < bookCount; i++) {
System.out.println(books[i]);
}
}
}
// Method to search for a book by ID
public Book searchBookByID(int bookID) {
for (int i = 0; i < bookCount; i++) {
if (books[i].getBookID() == bookID) {
return books[i];
}
}
return null;
}
}
// Main class for Book Management
public class BookManagementSystem {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nLibrary Management System");
System.out.println("1. Add Book");
System.out.println("2. Replace Book");
System.out.println("3. Display Books");
System.out.println("4. Search Book by ID");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
// Add book to library
System.out.print("Enter Book ID: ");
int id = scanner.nextInt();
// prints newline
scanner.nextLine();
System.out.print("Enter Title: ");
String title = scanner.nextLine();
System.out.print("Enter Author: ");
String author = scanner.nextLine();
System.out.print("Is Available (true/false): ");
boolean isAvailable = scanner.nextBoolean();
Book book = new Book(id, title, author, isAvailable);
library.addBook(book);
break;
case 2:
// Replace book
/* replace can used to modify any type of information
of a book for example completely different book
or just author name which you can notice in output */
System.out.print("Enter Book ID to replace: ");
int replaceID = scanner.nextInt();
scanner.nextLine();
// prints newline
System.out.print("Enter New Book ID: ");
int newID = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter New Title: ");
String newTitle = scanner.nextLine();
System.out.print("Enter New Author: ");
String newAuthor = scanner.nextLine();
System.out.print("Is Available (true/false): ");
boolean newAvailability = scanner.nextBoolean();
Book newBook = new Book(newID, newTitle, newAuthor, newAvailability);
library.replaceBook(replaceID, newBook);
break;
case 3:
// Display books that are taken as input
library.displayBooks();
break;
case 4:
// Search book by ID number
System.out.print("Enter Book ID to search: ");
int searchID = scanner.nextInt();
Book foundBook = library.searchBookByID(searchID);
if (foundBook != null) {
System.out.println("Book Found: " + foundBook);
} else {
System.out.println("Book not found.");
}
break;
case 5:
// Exit case to complete the execution
System.out.println("Exiting the system. Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
}
OUTPUT:
Library Management System
Add Book
Replace Book
Display Books
Search Book by ID
Exit Enter your choice: 1
Enter Book ID: 101
Enter Title: The Psychology of Money
Enter Author: Morgan
Is Available (true/false): true
Book added successfully!
Library Management System
Add Book
Replace Book
Display Books
Search Book by ID
Exit Enter your choice: 2
Enter Book ID to replace: 101
Enter New Book ID: 102
Enter New Title: The psychology of Money
Enter New Author: Morgan Housel
Is Available (true/false): true
Book replaced successfully!
Library Management System
Add Book
Replace Book
Display Books
Search Book by ID
Exit Enter your choice: 3
BookID: 102 Title: The psychology of Money, Author: Morgan Housel, Available: true
Library Management System
Add Book
Replace Book
Display Books
Search Book by ID
Exit Enter your choice: 4
Enter Book ID to search: 101
Book not found.
Library Management System
Add Book
Replace Book
Display Books
Search Book by ID
Exit Enter your choice: 4
Enter Book ID to search: 102
Book Found: BookID: 102, Title: The psychology of Money, Author: Morgan Housel, Available: true
Library Management System
Add Book
Replace Book
Display Books
Search Book by ID
Exit Enter your choice: 5
Exiting the system. Goodbye!
Solution - 2:
2) Create Interface Taxable with members salesTax=7% and incomeTax=10.5%. create abstract method calc Tax().
a. Create class Employee(empId,name,salary) and implement Taxable to calculate income Tax on yearly salary.
b. Create class Product(pid,price,quantity) and implement Taxable to calculate sales Tax on unit price of product.
c. Create class for main method(Say DriverMain), accept employee information and a product information from user and print income tax and sales tax respectively
(Note: Solution for this problem has created with different class file and taken input from user)
// Interface Taxable with members salesTax and incomeTax
public interface Taxable {
// sales and income tax rate
double salesTax = 7.0;
double incomeTax = 10.5;
// Abstract method to calculate tax
void calcTax();
}
// Employee class implementing Taxable interface
import java.util.Scanner;
public class Employee implements Taxable {
int empId;
String name;
double salary;
// Constructor to initialize Employee
public Employee(int empId, String name, double salary) {
this.empId = empId;
this.name = name;
this.salary = salary;
}
// Implementing calcTax to calculate income tax
@Override
public void calcTax() {
double tax = (salary * incomeTax) / 100;
System.out.println("Income Tax for Employee: " + tax);
}
}
// Product class implementing Taxable interface
public class Product implements Taxable {
int pid;
double price;
int quantity;
// Constructor to initialize Product
public Product(int pid, double price, int quantity) {
this.pid = pid;
this.price = price;
this.quantity = quantity;
}
// Implementing calcTax to calculate sales tax on product price
@Override
public void calcTax() {
double salesTaxAmount = (price * quantity * salesTax) / 100;
System.out.println("Sales Tax for Product: " + salesTaxAmount);
}
}
// Main class to calculate the taxes
import java.util.Scanner;
public class DriverMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Accepting Employee information
System.out.print("Enter Employee ID: ");
int empId = sc.nextInt();
// prints newline character
sc.nextLine();
System.out.print("Enter Employee Name: ");
String name = sc.nextLine();
System.out.print("Enter Employee Salary: ");
double salary = sc.nextDouble();
// Create Employee object and calculate income tax
Employee employee = new Employee(empId, name, salary);
// Calculate income tax
employee.calcTax();
// Accepting Product information
System.out.print("Enter Product ID: ");
int pid = sc.nextInt();
System.out.print("Enter Product Price: ");
double price = sc.nextDouble();
System.out.print("Enter Product Quantity: ");
int quantity = sc.nextInt();
// Create Product object and calculate sales tax
Product product = new Product(pid, price, quantity);
product.calcTax();
// Calculate sales tax
}
}
// income tax is calculated after input given for employee
// sales tax is calculated after input is given for product
OUTPUT:
Enter Employee ID: 12454
Enter Employee Name: John
Enter Employee Salary: 45000
Income Tax for Employee: 4725.0
Enter Product ID: 201
Enter Product Price: 1500
Enter Product Quantity: 2
Sales Tax for Product: 210.0