Write a C++ program to print the bill of various items. Including GST
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
class Item
{
public:
std::string name;
int quantity;
float price;
Item(std::string name, int quantity, float price) : name(name), quantity(quantity), price(price) {}
};
class Invoice
{
private:
std::vector<Item> items;
float gst;
float total;
public:
Invoice() : gst(0.15) {}
void addItem()
{
int n;
std::cout << "Enter the number of items: ";
std::cin >> n;
system("clear");
for (int i = 0; i < n; i++)
{
std::string name;
int quantity;
float price;
std::cout << "------------------------------------\n";
std::cout << "Enter item " << i + 1 << " name: ";
std::cin >> name;
std::cout << "------------------------------------\n";
std::cout << "Enter item " << i + 1 << " quantity: ";
std::cin >> quantity;
std::cout << "------------------------------------\n";
std::cout << "Enter item " << i + 1 << " unit price: ";
std::cin >> price;
Item item(name, quantity, price);
items.push_back(item);
system("clear");
}
}
void generateInvoice()
{
std::cout << "-----------------------------------------\n";
std::cout << "ITEM\t\tQUANTITY\tPRICE\n";
std::cout << "-----------------------------------------\n";
for (auto item : items)
{
total += item.quantity * item.price;
std::cout << item.name << "\t\t" << item.quantity << "\t\t" << item.price * item.quantity << std::endl;
}
total = total + total * gst;
std::cout << "-----------------------------------------\n";
std::cout << "CGST @15%\t\t\t" << total * gst << std::endl;
std::cout << "Total\t\t\t\t" << total << std::endl;
std::cout << "-----------------------------------------\n";
std::cout << "\n\nPress any key and then Enter to exit ";
std::string c;
std::cin >> c;
system("clear");
}
};
class AdvanceInvoice : public Invoice
{
private:
std::vector<Item> items;
float gst;
float sgst;
float total;
public:
AdvanceInvoice() : gst(0.075), sgst(0.075) {}
void addItem()
{
int n;
std::cout << "Enter the number of items: ";
std::cin >> n;
system("clear");
for (int i = 0; i < n; i++)
{
std::string name;
int quantity;
float price;
std::cout << "------------------------------------\n";
std::cout << "Enter item " << i + 1 << " name: ";
std::cin >> name;
std::cout << "------------------------------------\n";
std::cout << "Enter item " << i + 1 << " quantity: ";
std::cin >> quantity;
std::cout << "------------------------------------\n";
std::cout << "Enter item " << i + 1 << " unit price: ";
std::cin >> price;
Item item(name, quantity, price);
items.push_back(item);
system("clear");
}
}
void generateInvoice()
{
std::cout << "-----------------------------------------\n";
std::cout << "ITEM\t\tQUANTITY\tPRICE\n";
std::cout << "-----------------------------------------\n";
for (auto item : items)
{
total += item.quantity * item.price;
std::cout << item.name << "\t\t" << item.quantity << "\t\t" << item.price * item.quantity << std::endl;
}
total = total + total * gst + total * sgst;
std::cout << "-----------------------------------------\n";
std::cout << "SGST @7.5%\t\t\t" << total * sgst << std::endl;
std::cout << "CGST @7.5%\t\t\t" << total * gst << std::endl;
std::cout << "Total\t\t\t\t" << total << std::endl;
std::cout << "-----------------------------------------\n";
std::cout << "\n\nPress any key and then Enter to exit ";
std::string c;
std::cin >> c;
system("clear");
}
};
int main()
{
AdvanceInvoice invoice;
int choice;
std::string c_name = "";
do
{
std::cout << "------------------------------------\n";
std::cout << "\tInvoice Generator\n";
std::cout << "------------------------------------\n";
std::cout << "Menu:\n1. Add details\n2. Generate invoice\n3. Exit"
<< "\n------------------------------------\n"
<< "Enter your choice: ";
std::cin >> choice;
switch (choice)
{
case 1:
{
system("clear");
std::cout << "------------------------------------\n";
std::cout << "Enter coustomer name: ";
std::cin >> c_name;
invoice.addItem();
break;
}
case 2:
{
system("clear");
std::cout << "-----------------------------------------\n";
std::cout << "Invoice to: " << c_name << "\n";
std::time_t t = std::time(nullptr);
std::tm *now = std::localtime(&t);
std::cout << "Date: " << now->tm_mday << '/' << (now->tm_mon + 1) << '/'
<< (now->tm_year + 1900) << std::endl;
invoice.generateInvoice();
break;
}
case 3:
{
break;
}
default:
{
system("clear");
std::cout << "Invalid choice\n";
break;
}
}
} while (choice != 3);
return 0;
}
Awesome....
ReplyDelete