-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_01.cpp
More file actions
171 lines (144 loc) · 3.99 KB
/
Project_01.cpp
File metadata and controls
171 lines (144 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Product {
protected:
int productId;
string productName;
double price;
int quantity;
public:
// Constructor
Product(int id, string name, double p, int q) {
productId = id;
productName = name;
price = p;
quantity = q;
}
// Pure virtual function (Abstraction)
virtual double calculateBill() = 0;
// Virtual destructor
virtual ~Product() {}
// Encapsulation (Getters)
string getName() {
return productName;
}
int getQuantity() {
return quantity;
}
void displayBasicInfo() {
cout << "ID: " << productId
<< " | Name: " << productName
<< " | Price: " << price
<< " | Qty: " << quantity << endl;
}
};
class FoodProduct : public Product {
private:
double gst; // Encapsulation
public:
FoodProduct(int id, string name, double p, int q)
: Product(id, name, p, q) {
gst = 0.05; // 5% GST
}
// POLYMORPHISM (Method Overriding)
double calculateBill() override {
double total = price * quantity;
return total + (total * gst);
}
};
class ElectronicProduct : public Product {
private:
double gst;
public:
ElectronicProduct(int id, string name, double p, int q)
: Product(id, name, p, q) {
gst = 0.18; // 18% GST
}
// POLYMORPHISM
double calculateBill() override {
double total = price * quantity;
return total + (total * gst);
}
};
class DailySell {
private:
vector<Product*> products;
public:
void addProduct(Product* p) {
products.push_back(p);
}
void showAllProducts() {
cout << "\n----- Product List -----\n";
for (auto p : products) {
p->displayBasicInfo();
}
}
void generateBill() {
double grandTotal = 0;
cout << "\n----- BILL DETAILS -----\n";
for (auto p : products) {
double bill = p->calculateBill(); // Polymorphism
cout << p->getName() << " Bill: ₹" << bill << endl;
grandTotal += bill;
}
cout << "-------------------------\n";
cout << "Grand Total: ₹" << grandTotal << endl;
}
~DailySell() {
for (auto p : products) {
delete p;
}
}
};
int main() {
DailySell shop;
int choice;
do {
cout << "\n===== DAILY SELL SYSTEM =====\n";
cout << "1. Add Food Product\n";
cout << "2. Add Electronic Product\n";
cout << "3. Show Products\n";
cout << "4. Generate Bill\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
int id, qty;
string name;
double price;
cout << "Enter Food ID: ";
cin >> id;
cout << "Enter Food Name: ";
cin >> name;
cout << "Enter Price: ";
cin >> price;
cout << "Enter Quantity: ";
cin >> qty;
shop.addProduct(new FoodProduct(id, name, price, qty));
}
else if (choice == 2) {
int id, qty;
string name;
double price;
cout << "Enter Electronic ID: ";
cin >> id;
cout << "Enter Product Name: ";
cin >> name;
cout << "Enter Price: ";
cin >> price;
cout << "Enter Quantity: ";
cin >> qty;
shop.addProduct(new ElectronicProduct(id, name, price, qty));
}
else if (choice == 3) {
shop.showAllProducts();
}
else if (choice == 4) {
shop.generateBill();
}
} while (choice != 0);
cout << "Thank you! Visit again.\n";
return 0;
}