IMS FINAL COPY

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

#include <iostream>

#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <limits> // for std::numeric_limits

using namespace std;

const string ADMIN_USERNAME = "admin";


const string ADMIN_PASSWORD = "admin123";

void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}

class Item {
public:
int id;
string name;
double price;
int quantity;
double totalPrice;
string supplierName;

void inputItemDetails() {
cout << endl;
cout << "Enter item ID=> ";
while (!(cin >> id)) {
cout << "Invalid input. Please enter a valid item ID=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter item name=> ";
cin.ignore();
getline(cin, name);
cout << "Enter item price=> ";
while (!(cin >> price)) {
cout << "Invalid input. Please enter a valid item price=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter item quantity=> ";
while (!(cin >> quantity)) {
cout << "Invalid input. Please enter a valid item quantity=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter supplier name=> ";
cin.ignore();
getline(cin, supplierName);

totalPrice = price * quantity;


}
void displayItem() {
cout << left << setw(20) << id << setw(30) << name << setw(20) << price <<
setw(20) << quantity << setw(30) << supplierName << endl;
}

void displayItemWithTotal() {
cout << left << setw(10) << id << setw(20) << name << setw(10) << price <<
setw(20) << quantity << setw(10) << totalPrice << setw(20) << supplierName << endl;
}
};

class Sale {
public:
int id;
string name;
double price;
int quantity;
double totalPrice;
string customerName;

void displaySale() {
cout << left << setw(20) << id << setw(30) << name << setw(20) << price <<
setw(20) << quantity << setw(20) << totalPrice << setw(30) << customerName << endl;
}
};

bool login() {
string username, password;
cout << "WELCOME TO INVENTORY MANAGEMENT SYSTEM :)" << endl;
cout << "Enter username=> ";
cin >> username;
cout << "Enter password=> ";
cin >> password;
return (username == ADMIN_USERNAME && password == ADMIN_PASSWORD);
}

void addItem() {
clearScreen();
ofstream inventoryFile("Inventorydashboard.txt", ios::app);
ofstream purchaseFile("Purchasebook.txt", ios::app);

Item item;
cout << "CREATING A PURCHASE" << endl;
item.inputItemDetails();

inventoryFile << item.id << " " << item.name << " " << item.price << " " <<
item.quantity << " " << item.totalPrice << " " << item.supplierName << endl;
purchaseFile << item.id << " " << item.name << " " << item.price << " " <<
item.quantity << " " << item.totalPrice << " " << item.supplierName << endl;

inventoryFile.close();
purchaseFile.close();

cout << "Purchase successful from " << item.supplierName << " with quantity "
<< item.quantity << " at price Rs " << item.price << " for a total of Rs. " <<
item.totalPrice << "." << endl;
}

void viewItems() {
clearScreen();
cout << "INVENTORY ITEMS" << endl;
ifstream inventoryFile("Inventorydashboard.txt");
Item item;
string line;
cout << left << setw(20) << "ID" << setw(30) << "Name" << setw(20) << "Price"
<< setw(20) << "Quantity" << setw(30) << "Supplier" << endl;
while (getline(inventoryFile, line)) {
stringstream ss(line);
ss >> item.id >> item.name >> item.price >> item.quantity >>
item.totalPrice >> item.supplierName;
item.displayItem();
}
inventoryFile.close();
}

void displayAvailableItems() {
ifstream inventoryFile("Inventorydashboard.txt");
Item item;
string line;
cout << left << setw(20) << "ID" << setw(30) << "Name" << setw(20) << "Price"
<< setw(20) << "Quantity" << endl;
while (getline(inventoryFile, line)) {
stringstream ss(line);
ss >> item.id >> item.name >> item.price >> item.quantity >>
item.totalPrice;
item.displayItem();
}
inventoryFile.close();
}

void editItem() {
clearScreen();
viewItems();
vector<Item> items;
ifstream inventoryFile("Inventorydashboard.txt");
ofstream tempFile("temp.txt");
Item item;
string line;

while (getline(inventoryFile, line)) {


stringstream ss(line);
ss >> item.id >> item.name >> item.price >> item.quantity >>
item.totalPrice >> item.supplierName;
items.push_back(item);
}
inventoryFile.close();

int editId;
cout << "Enter the ID of the item to edit: ";
while (!(cin >> editId)) {
cout << "Invalid input. Please enter a valid item ID: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

for (auto& i : items) {


if (i.id == editId) {
cout << "Enter new name=> ";
cin.ignore();
getline(cin, i.name);
cout << "Enter new price=> ";
while (!(cin >> i.price)) {
cout << "Invalid input. Please enter a valid price=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter new quantity=> ";
while (!(cin >> i.quantity)) {
cout << "Invalid input. Please enter a valid quantity=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter new supplier name=> ";
cin.ignore();
getline(cin, i.supplierName);
i.totalPrice = i.price * i.quantity;
}
tempFile << i.id << " " << i.name << " " << i.price << " " << i.quantity <<
" " << i.totalPrice << " " << i.supplierName << endl;
}
tempFile.close();
cout << "ITEMS UPDATED SUCCESSFULLY" << endl;

remove("Inventorydashboard.txt");
rename("temp.txt", "Inventorydashboard.txt");
}

void sellItem() {
clearScreen();
vector<Item> items;
ifstream inventoryFile("Inventorydashboard.txt");
ofstream tempFile("temp.txt");
ofstream salesFile("Salesbook.txt", ios::app);
Item item;
string line;

while (getline(inventoryFile, line)) {


stringstream ss(line);
ss >> item.id >> item.name >> item.price >> item.quantity >>
item.totalPrice >> item.supplierName;
items.push_back(item);
}
inventoryFile.close();

cout << "CHOOSE ITEMS TO SELL" << endl;


displayAvailableItems(); // Call the function to display available items

int sellId, sellQty;


string customerName;

cout << "Enter the ID of the item to sell=>";


while (!(cin >> sellId)) {
cout << "Invalid input. Please enter a valid item ID=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter quantity to sell=>";
while (!(cin >> sellQty)) {
cout << "Invalid input. Please enter a valid quantity=> ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

cout << "Enter customer name=> ";


cin.ignore();
getline(cin, customerName);

bool sold = false;


for (auto& i : items) {
if (i.id == sellId) {
if (i.quantity >= sellQty) {
i.quantity -= sellQty;
double totalSalePrice = i.price * sellQty;
i.totalPrice = i.price * i.quantity;
salesFile << i.id << " " << i.name << " " << i.price << " " <<
sellQty << " " << totalSalePrice << " " << customerName << endl;
cout << "Sold " << sellQty << " units of " << i.name << " to " <<
customerName << " for a total of " << totalSalePrice << "." << endl;
sold = true;
}
else {
cout << "Not enough stock available." << endl;
}
}
tempFile << i.id << " " << i.name << " " << i.price << " " << i.quantity <<
" " << i.totalPrice << " " << i.supplierName << endl;
}
if (!sold) {
cout << "Sale unsuccessful. Item not found or insufficient stock." << endl;
}
tempFile.close();
salesFile.close();

remove("Inventorydashboard.txt");
rename("temp.txt", "Inventorydashboard.txt");
}

void viewPurchaseBook() {
clearScreen();
ifstream purchaseFile("Purchasebook.txt");
Item item;
string line;
cout << left << setw(10) << "ID" << setw(20) << "Name" << setw(10) << "Price"
<< setw(10) << "Quantity" << setw(20) << "Total" << setw(20) << "Supplier" << endl;
while (getline(purchaseFile, line)) {
stringstream ss(line);
ss >> item.id >> item.name >> item.price >> item.quantity >>
item.totalPrice >> item.supplierName;
item.displayItemWithTotal();
}
purchaseFile.close();
}

void viewSalesBook() {
clearScreen();
ifstream salesFile("Salesbook.txt");
Sale sale;
string line;
cout << left << setw(20) << "ID" << setw(30) << "Name" << setw(20) << "Price"
<< setw(20) << "Quantity" << setw(20) << "Total" << setw(30) << "Customer" << endl;
while (getline(salesFile, line)) {
stringstream ss(line);
ss >> sale.id >> sale.name >> sale.price >> sale.quantity >>
sale.totalPrice >> sale.customerName;
sale.displaySale();
}
salesFile.close();
}

int main() {
bool loggedIn = false;

do {
if (!login()) {
clearScreen();
cout << "Login failed. Incorrect username or password." << endl;
cout << "Press 1 to try again or any other key to exit: ";
char choice;
cin >> choice;
if (choice != '1') {
cout << "Exiting program." << endl;
return 1;
}
clearScreen();
}
else {
loggedIn = true;
}
} while (!loggedIn);

int choice;
do {
clearScreen();
cout << "---------------------------------------------INVENTORY MANAGEMENT
SYSTEM------------------------------------------------" << endl;
cout << "1. Add Item" << endl;
cout << "2. View Items" << endl;
cout << "3. Edit Item" << endl;
cout << "4. Sell Item" << endl;
cout << "5. View Purchase Book" << endl;
cout << "6. View Sales Book" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
while (!(cin >> choice)) {
cout << "Invalid input. Please enter a valid choice: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

switch (choice) {
case 1:
addItem();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 2:
viewItems();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 3:
editItem();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 4:
sellItem();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 5:
viewPurchaseBook();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 6:
viewSalesBook();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 7:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
}
} while (choice != 7);

return 0;
}

You might also like