Design a library catalog system with a base class Item and derived classes Book, DVD, and Magazine. Each item has an item ID and the number of days overdue. Implement a virtual function, calculateLateFees, to calculate the total overdue amount. Calculate the total overdue amount for all items and display it.Overdue Amount Calculation:For Books: Rs. 0.25 per day overdueFor DVDs: Rs. 0.50 per day overdueFor Magazines: Rs. 0.10 per day overdue
Question
Design a library catalog system with a base class Item and derived classes Book, DVD, and Magazine. Each item has an item ID and the number of days overdue. Implement a virtual function, calculateLateFees, to calculate the total overdue amount. Calculate the total overdue amount for all items and display it.Overdue Amount Calculation:For Books: Rs. 0.25 per day overdueFor DVDs: Rs. 0.50 per day overdueFor Magazines: Rs. 0.10 per day overdue
Solution
Here is a simple implementation in C++:
#include<iostream>
using namespace std;
class Item {
protected:
string itemID;
int daysOverdue;
public:
Item(string id, int days) : itemID(id), daysOverdue(days) {}
virtual double calculateLateFees() = 0; // Pure virtual function
};
class Book : public Item {
public:
Book(string id, int days) : Item(id, days) {}
double calculateLateFees() {
return daysOverdue * 0.25;
}
};
class DVD : public Item {
public:
DVD(string id, int days) : Item(id, days) {}
double calculateLateFees() {
return daysOverdue * 0.50;
}
};
class Magazine : public Item {
public:
Magazine(string id, int days) : Item(id, days) {}
double calculateLateFees() {
return daysOverdue * 0.10;
}
};
int main() {
Book b("B1", 5);
DVD d("D1", 3);
Magazine m("M1", 7);
double totalOverdue = b.calculateLateFees() + d.calculateLateFees() + m.calculateLateFees();
cout << "Total overdue amount: Rs. " << totalOverdue << endl;
return 0;
}
In this code, we have a base class Item with two data members itemID and daysOverdue. The calculateLateFees function is a pure virtual function, meaning it must be implemented in any derived classes.
We then have three derived classes Book, DVD, and Magazine, each implementing the calculateLateFees function according to their respective rates.
In the main function, we create one instance of each item type, calculate their late fees, and add them up to get the total overdue amount.
Similar Questions
Create a class called library with data attributes like acc_number, publisher, titleand author. The methods of the class should includei). read() – acc_number, title, author.ii). compute() - to accept the number of days late, calculate and display the finecharged at the rate of $1.50 per day.iii). display the data
In a Library Management System, create a class ‘LibUser’ and get the name, age, address, book name, book type, date of issue, date of return (current date) and penalty amount. For ‘age’ create user defined exception class and check if age is a negative number and print “Please enter correct age”.Calculate the number of days of book in use as (current date - date of issue). If number of days of book in use is greater than 15 days and then handle the exception and print “Fine to be paid”. Penalty amount = (number of days of book in use – 15) * 10.If the name entered contains numbers or any special characters (other than alphabets and spaces), then handle it using exception handling and print “Enter your Correct Name”.InputName (String)Age (Integer)address (String)book name (String)book type (String)date of issue (format: yyyy-mm-dd) (String)Current date (format: yyyy-mm-dd) (String)Penalty amount (Integer)OutputUser defined string according to exception
Dinesh is working in a supermarket and he is developing a program to calculate the cost of different types of items. Help him write the program that does the following:a) Create a base class, ItemType, with one virtual function double calculateAmount()b) Create a class called wooden that extends ItemType class with a number of items and cost as its private attributes. Obtain the data members and override the virtual function and calculate the total amount.c) Create a class called electronics that extends ItemType class with cost as its private attribute. Obtain the data member and override the virtual function and calculate the amount with 20% discount.Note: This question helps in clearing Infosys tests.Input format :The first line consists of an integer choice (1 or 2) representing the choice of item type.If the choice is 1 (wooden items), the next line consists of two space-separated integers: noOfItems and cost, representing the number of wooden items and their individual cost, respectively.If the choice is 2 (electronics), the next line consists of a single floating-point number cost, representing the cost of the electronic item.Output format :The output prints a floating-point number representing the calculated total cost of the chosen item type rounded off to two decimal places.Code constraints :10 < cost < 1060 < noOfItems < 20Sample test cases :Input 1 :15 840.5Output 1 :4202.50Input 2 :21800.56Output 2 :1440.45Note :
The following data give the time (in minutes) that each of 20 students waitedin line at their bookstore to pay for their textbooks in the beginning of Spring2009 semester. (Note: To prepare a stem-and-leaf display, each number in thisdata set can be written as a two-digit number. For Example, 8 can be written as08, for which the stem is 0 and the leaf is 8.)15 8 23 21 4 17 31 22 31 65 6 14 17 16 25 27 3 31 8Construct a stem-and-leaf display for these data. Arrange the leaves for eachstem in increasing order
You are tasked with implementing a basic library system in Java. The program should allow users to add books to the library, borrow books, and return books. The system maintains a record of the available quantity of each book.Question:Write a Java program that accomplishes the following tasks:1. Implement the following options:Add BooksBorrow BooksReturn BooksExit2. For "Add Books":Prompt the user to enter the book title, author, and quantity.If the book already exists in the library, update the quantity.If the book is new, add it to the library.3. For "Borrow Books":Prompt the user to enter the book title and the number of books to borrow.Check if the requested number of books is available in the library.If the books are available, update the quantity and display a success message.If not, display an error message.4. For "Return Books":Prompt the user to enter the book title and the number of books to return.Check if the books being returned belong to the library system.If they do, update the quantity and display a success message.If not, display an error message.5. Handle invalid input and display appropriate error messages.6. Implement an exit option to allow the user to exit the program.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.