0% found this document useful (0 votes)
4 views11 pages

Unit 2- Hands-On Scenario Based Program - Solution

The document outlines the development of a restaurant billing system and a shopping cart system. The restaurant system automates bill generation with unique bill numbers and allows users to input item details, while the shopping cart system utilizes operator overloading for merging carts and comparing total amounts. Both systems maintain static counters to track the total number of bills and carts generated.

Uploaded by

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

Unit 2- Hands-On Scenario Based Program - Solution

The document outlines the development of a restaurant billing system and a shopping cart system. The restaurant system automates bill generation with unique bill numbers and allows users to input item details, while the shopping cart system utilizes operator overloading for merging carts and comparing total amounts. Both systems maintain static counters to track the total number of bills and carts generated.

Uploaded by

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

Develop a program for a restaurant to automate bill generation,

assigning each bill a unique bill number using a static counter.


Implement a Bill class with attributes for bill number and total
amount, methods to generate a bill number and display bill details.
Maintain a static counter to track total bills generated. Provide a user
interface to input item details, calculate total amount, and display
generated bill number and amount.
Output
Menu:
1. Burger - $5.99
2. Pizza - $8.99
3. Pasta - $7.49
4. Salad - $4.99
5. Soda - $1.99
Enter the item number to order (0 to stop): 3
Enter quantity for Pasta: 2
Do you want to add another item? (y/n): y
Menu:
1. Burger - $5.99
2. Pizza - $8.99
3. Pasta - $7.49
4. Salad - $4.99
5. Soda - $1.99
Enter the item number to order (0 to stop): 5
Enter quantity for Soda: 2
Do you want to add another item? (y/n): n
Bill Number: 1
Total Amount: $18.96
Do you want to generate another bill? (y/n): y
Menu:
1. Burger - $5.99
2. Pizza - $8.99
3. Pasta - $7.49
4. Salad - $4.99
5. Soda - $1.99
Enter the item number to order (0 to stop): 4
Enter quantity for Salad: 5
Do you want to add another item? (y/n): n
Bill Number: 2
Total Amount: $24.95
Do you want to generate another bill? (y/n): n
Total bills generated: 2

Develop a shopping cart system that utilizes operator overloading through friend
functions. The program should:

1. Assign a unique cart ID to each shopping cart using a static counter.


2. Allow customers to add items to their cart by specifying quantity and price.
3. Overload the + operator as a friend function to merge two shopping carts into a
new cart, combining their total items and amount.
4. Overload the > and < operators as friend functions to compare two shopping carts
based on total amount spent.
5. Display cart details, including Cart ID, total items, and total amount.
6. Provide a user-friendly interface for customers to create and manage shopping carts.

Output
Cart 1 Details:
Cart ID: 1
Total Items: 3
Total Amount: $37.5
Cart 2 Details:
Cart ID: 2
Total Items: 5
Total Amount: $40
Merged Cart Details (Cart 3):
Cart ID: 4
Total Items: 8
Total Amount: $77.5

Cart 2 is more expensive than Cart 1.


Total Carts Created: 4
SOLUTION
1. #include <iostream>
#include <string>

using namespace std;

class Bill {
private:
static int billCounter; // Static counter to track total bills generated
int billNumber; // Unique bill number
float totalAmount; // Total bill amount
public:
// Constructor to initialize the bill
Bill() {
billNumber = ++billCounter; // Assign a unique bill number
totalAmount = 0.0f;
}

// Method to add an item's price to the bill


void addItem(float itemPrice) {
totalAmount += itemPrice;
}
// Method to display the bill details
void displayBill() const {
cout << "\nBill Number: " << billNumber << endl;
cout << "Total Amount: $" << totalAmount << endl;
}

// Static method to get total number of bills generated


static int getTotalBillsGenerated() {
return billCounter;
}
};

// Initialize the static counter


int Bill::billCounter = 0;

class Restaurant {
private:
static const int menuSize = 5; // Fixed menu size
string menuItems[menuSize] = {"Burger", "Pizza", "Pasta", "Salad", "Soda"}; // Menu items
float menuPrices[menuSize] = {5.99, 8.99, 7.49, 4.99, 1.99}; // Prices of items
public:
// Method to display the menu
void displayMenu() {
cout << "\nMenu:\n";
for (int i = 0; i < menuSize; ++i) {
cout << i + 1 << ". " << menuItems[i] << " - $" << menuPrices[i] << endl;
}
}
// Method to create a bill
void createBill() {
Bill bill;
int itemChoice, quantity;
char moreItems;

do {
displayMenu();
cout << "\nEnter the item number to order (0 to stop): ";
cin >> itemChoice;

if (itemChoice == 0) break; // Exit if user enters 0

if (itemChoice > 0 && itemChoice <= menuSize) {


cout << "Enter quantity for " << menuItems[itemChoice - 1] << ": ";
cin >> quantity;

// Add item price * quantity to the bill


bill.addItem(menuPrices[itemChoice - 1] * quantity);
} else {
cout << "Invalid item number. Please try again.\n";
}

cout << "Do you want to add another item? (y/n): ";
cin >> moreItems;
} while (moreItems == 'y' || moreItems == 'Y');

// Display the generated bill


bill.displayBill();
}
};

int main() {
Restaurant restaurant;
char continueBilling;

do {
restaurant.createBill();
cout << "\nDo you want to generate another bill? (y/n): ";
cin >> continueBilling;
} while (continueBilling == 'y' || continueBilling == 'Y');

cout << "\nTotal bills generated: " << Bill::getTotalBillsGenerated() << endl;

return 0;
}
1. #include <iostream>
using namespace std;

class ShoppingCart {
private:
static int cartCounter; // Static counter for unique cart IDs
int cartID; // Unique cart ID
int totalItems; // Total number of items in cart
float totalAmount; // Total cost of items

public:
// Constructor
ShoppingCart() {
cartID = ++cartCounter; // Assign unique cart ID
totalItems = 0;
totalAmount = 0.0f;
}

// Method to add items to the cart


void addItem(int quantity, float price) {
totalItems += quantity;
totalAmount += (quantity * price);
}

// Friend function to overload + operator for merging two carts


friend ShoppingCart operator+(const ShoppingCart &cart1, const ShoppingCart &cart2);

// Friend functions to overload > and < for cart comparison based on total amount
friend bool operator>(const ShoppingCart &cart1, const ShoppingCart &cart2);
friend bool operator<(const ShoppingCart &cart1, const ShoppingCart &cart2);

// Method to display cart details


void displayCart() const {
cout << "\nCart ID: " << cartID << endl;
cout << "Total Items: " << totalItems << endl;
cout << "Total Amount: $" << totalAmount << endl;
}

// Static method to get total carts created


static int getTotalCarts() {
return cartCounter;
}
};

// Initialize static counter


int ShoppingCart::cartCounter = 0;

// Overloaded + operator as a non-member function


ShoppingCart operator+(const ShoppingCart &cart1, const ShoppingCart &cart2) {
ShoppingCart mergedCart;
mergedCart.totalItems = cart1.totalItems + cart2.totalItems;
mergedCart.totalAmount = cart1.totalAmount + cart2.totalAmount;
return mergedCart;
}

// Overloaded > operator to compare carts based on total amount


bool operator>(const ShoppingCart &cart1, const ShoppingCart &cart2) {
return cart1.totalAmount > cart2.totalAmount;
}

// Overloaded < operator to compare carts based on total amount


bool operator<(const ShoppingCart &cart1, const ShoppingCart &cart2) {
return cart1.totalAmount < cart2.totalAmount;
}

int main() {
ShoppingCart cart1, cart2, cart3;

// Adding items to cart1


cart1.addItem(2, 15.00); // 2 items, each $15.00
cart1.addItem(1, 7.50); // 1 item, $7.50

// Adding items to cart2


cart2.addItem(3, 10.00); // 3 items, each $10.00
cart2.addItem(2, 5.00); // 2 items, each $5.00

cout << "Cart 1 Details:";


cart1.displayCart();

cout << "Cart 2 Details:";


cart2.displayCart();

// Merging cart1 and cart2 using operator +


cart3 = cart1 + cart2;
cout << "Merged Cart Details (Cart 3):";
cart3.displayCart();

// Comparing carts using > and <


if (cart1 > cart2)
cout << "\nCart 1 is more expensive than Cart 2.\n";
else if (cart1 < cart2)
cout << "\nCart 2 is more expensive than Cart 1.\n";
else
cout << "\nCart 1 and Cart 2 have the same total amount.\n";

// Display total carts created


cout << "\nTotal Carts Created: " << ShoppingCart::getTotalCarts() << endl;

return 0;
}

You might also like