#include #include #include #include using namespace std; // Lớp đại diện cho mặt hàng class Product { public: string code; string name; string manufacturer; Product(string code, string name, string manufacturer) : code(code), name(name), manufacturer(manufacturer) {} }; // Lớp đại diện cho hóa đơn mua bán class Invoice { public: string invoiceCode; string productCode; string type; int quantity; string date; double price; Invoice(string invoiceCode, string productCode, string type, int quantity, string date, double price) : invoiceCode(invoiceCode), productCode(productCode), type(type), quantity(quantity), date(date), price(price) {} }; // Lớp quản lý cửa hàng class StoreManager { private: vector products; vector invoices; public: // Đọc thông tin từ file void loadFromFile(string fileName) { ifstream file(fileName); if (file.is_open()) { // Đọc danh sách mặt hàng int productCount; file >> productCount; for (int i = 0; i < productCount; i++) { string code, name, manufacturer; file >> code >> name >> manufacturer; products.push_back(Product(code, name, manufacturer)); } // Đọc danh sách hóa đơn int invoiceCount; file >> invoiceCount; for (int i = 0; i < invoiceCount; i++) { string invoiceCode, productCode, type, date; int quantity; double price; file >> invoiceCode >> productCode >> type >> quantity >> date >> price; invoices.push_back(Invoice(invoiceCode, productCode, type, quantity, date, price)); } file.close(); } } // Ghi thông tin vào file void saveToFile(string fileName) { ofstream file(fileName); if (file.is_open()) { // Ghi danh sách mặt hàng file << products.size() << endl; for (const Product& product : products) { file << product.code << " " << product.name << " " << product.manufacturer << endl; } // Ghi danh sách hóa đơn file << invoices.size() << endl; for (const Invoice& invoice : invoices) { file << invoice.invoiceCode << " " << invoice.productCode << " " << invoice.type << " " << invoice.quantity << " " << invoice.date << " " << invoice.price << endl; } file.close(); } } // Nhập một mặt hàng mới void addProduct(string code, string name, string manufacturer) { products.push_back(Product(code, name, manufacturer)); } // Hiển thị danh sách mặt hàng void displayProducts() { for (const Product& product : products) { cout << "Mã hàng: " << product.code << ", Tên hàng: " << product.name << ", Nhà sản xuất: " << product.manufacturer << endl; } } // Thống kê số lượng tồn của mỗi mặt hàng void displayInventory() { cout << "Thống kê số lượng tồn của mỗi mặt hàng:" << endl; for (const Product& product : products) { int totalQuantity = 0; for (const Invoice& invoice : invoices) { if (invoice.productCode == product.code && invoice.type == "Mua") { totalQuantity += invoice.quantity; } if (invoice.productCode == product.code && invoice.type == "Bán") { totalQuantity -= invoice.quantity; } } cout << "Mã hàng: " << product.code << ", Tên hàng: " << product.name << ", Số lượng tồn: " << totalQuantity << endl; } } }; int main() { StoreManager store; // Đọc thông tin từ file store.loadFromFile("store_data.txt"); while (true) { cout << "Chọn chức năng:" << endl; cout << "1. Nhập mặt hàng mới" << endl; cout << "2. Hiển thị danh sách mặt hàng" << endl; cout << "3. Thống kê số lượng tồn" << endl; cout << "4. Lưu và thoát" << endl; int choice; cin >> choice; if (choice == 1) { string code, name, manufacturer; cout << "Nhập mã hàng: "; cin >> code; cout << "Nhập tên hàng: "; cin >> name; cout << "Nhập nhà sản xuất: "; cin >> manufacturer; store.addProduct(code, name, manufacturer); } else if (choice == 2) { store.displayProducts(); } else if (choice == 3) { store.displayInventory(); } else if (choice == 4) { // Lưu thông tin vào file và thoát store.saveToFile("store_data.txt"); break; } else { cout << "Chức năng không hợp lệ. Vui lòng chọn lại." << endl; } } return 0; }