21xrx.com
2024-11-05 17:33:01 Tuesday
登录
文章检索 我的文章 写文章
C++管理系统案例代码:完整实现与源码分享
2023-07-05 04:55:51 深夜i     --     --
C++ 管理系统 案例代码 完整实现 源码分享

无论你是初学者还是已经有了一定编程基础的开发者,学习和实现C++管理系统都是非常有用的。C++管理系统是一种非常重要的应用程序,可以帮助管理者对自己的业务和项目进行更好地组织和规划,以提高效率和管理水平。在本文中,我们将分享一个 C++管理系统案例代码的完整实现和源码,帮助大家更好地理解如何实现一个C++管理系统。

一、项目介绍

这个C++管理系统是一个基于控制台的简单项目,旨在帮助用户管理商店中的库存和销售记录。这个C++管理系统的功能非常实用,方便管理者进行商店的管理。整个系统分为两个部分:商品管理和销售管理。在商品管理模块中,用户可以添加、修改、删除和查询商品信息,而在销售管理模块中,用户可以查看销售记录和计算销售数据。

二、系统实现

以下是C++管理系统案例代码的主要实现:

1、定义Product类,用于管理商品信息


class Product {

 int product_id;

 string product_name;

 float product_price;

 int product_quantity;

public:

 Product();

 Product(int id, string name, float price, int quantity);

 bool operator==(const Product& product) const;

 void display();

 int get_id();

 string get_name();

 float get_price();

 int get_quantity();

};

2、定义ProductManager类,用于管理商品信息


class ProductManager {

 vector<Product> products;

public:

 void add_product(Product product);

 void delete_product(int id);

 void update_product(int id, Product product);

 Product find_product(int id);

 void display_all();

};

3、定义Sale类,用于管理销售信息


class Sale {

 int sale_id;

 int product_id;

 float sale_price;

 int sale_quantity;

 float sale_total_price;

 time_t sale_time;

public:

 Sale();

 Sale(int id, int product_id, float price, int quantity, float total_price, time_t time);

 bool operator==(const Sale& sale) const;

 void display();

 int get_id();

 int get_product_id();

 float get_price();

 int get_quantity();

 float get_total_price();

 time_t get_time();

};

4、定义SalesManager类,用于管理销售信息


class SalesManager {

 vector<Sale> sales;

public:

 void add_sale(Sale sale);

 void delete_sale(int id);

 void display_all();

 float calculate_sales_revenue();

};

5、定义MainMenu类,实现控制台用户界面


class MainMenu {

 ProductManager product_manager;

 SalesManager sales_manager;

public:

 void run();

 void display_menu();

};

三、源码分享

以上是C++管理系统案例代码的主要实现,下面将分享完整的源代码,供学习和参考使用。


#include <iostream>

#include <vector>

#include <ctime>

using namespace std;

class Product {

 int product_id;

 string product_name;

 float product_price;

 int product_quantity;

public:

 Product() {}

 Product(int id, string name, float price, int quantity)

  product_id = id;

  product_name = name;

  product_price = price;

  product_quantity = quantity;

 

 bool operator==(const Product& product) const {

  if (product_id == product.product_id

    && product_name == product.product_name

    && product_price == product.product_price

    && product_quantity == product.product_quantity)

   return true;

  

  return false;

 }

 void display()

  cout << "ID: " << product_id << endl;

  cout << "Name: " << product_name << endl;

  cout << "Price: " << product_price << endl;

  cout << "Quantity: " << product_quantity << endl;

 

 int get_id()

  return product_id;

 

 string get_name()

  return product_name;

 

 float get_price()

  return product_price;

 

 int get_quantity()

  return product_quantity;

 

};

class ProductManager {

 vector<Product> products;

public:

 void add_product(Product product) {

  products.push_back(product);

 }

 void delete_product(int id) {

  for (int i = 0; i < products.size(); i++) {

   if (products[i].get_id() == id) {

    products.erase(products.begin() + i);

    break;

   }

  }

  cout << "Product with ID " << id << " has been deleted." << endl;

 }

 void update_product(int id, Product product) {

  for (int i = 0; i < products.size(); i++) {

   if (products[i].get_id() == id) {

    products[i] = product;

    break;

   }

  }

  cout << "Product with ID " << id << " has been updated." << endl;

 }

 Product find_product(int id) {

  for (int i = 0; i < products.size(); i++) {

   if (products[i].get_id() == id) {

    return products[i];

   }

  }

  return Product();

 }

 void display_all() {

  for (int i = 0; i < products.size(); i++) {

   products[i].display();

   cout << endl;

  }

 }

};

class Sale {

 int sale_id;

 int product_id;

 float sale_price;

 int sale_quantity;

 float sale_total_price;

 time_t sale_time;

public:

 Sale() {}

 Sale(int id, int product_id, float price, int quantity, float total_price, time_t time)

  sale_id = id;

  this->product_id = product_id;

  sale_price = price;

  sale_quantity = quantity;

  sale_total_price = total_price;

  sale_time = time;

 

 bool operator==(const Sale& sale) const {

  if (sale_id == sale.sale_id

    && product_id == sale.product_id

    && sale_price == sale.sale_price

    && sale_quantity == sale.sale_quantity

    && sale_total_price == sale.sale_total_price

    && sale_time == sale.sale_time)

   return true;

  

  return false;

 }

 void display() {

  cout << "ID: " << sale_id << endl;

  cout << "Product ID: " << product_id << endl;

  cout << "Price: " << sale_price << endl;

  cout << "Quantity: " << sale_quantity << endl;

  cout << "Total Price: " << sale_total_price << endl;

  cout << "Time: " << ctime(&sale_time) << endl;

 }

 int get_id()

  return sale_id;

 

 int get_product_id()

  return product_id;

 

 float get_price()

  return sale_price;

 

 int get_quantity()

  return sale_quantity;

 

 float get_total_price()

  return sale_total_price;

 

 time_t get_time()

  return sale_time;

 

};

class SalesManager {

 vector<Sale> sales;

public:

 void add_sale(Sale sale) {

  sales.push_back(sale);

 }

 void delete_sale(int id) {

  for (int i = 0; i < sales.size(); i++) {

   if (sales[i].get_id() == id) {

    sales.erase(sales.begin() + i);

    break;

   }

  }

  cout << "Sale with ID " << id << " has been deleted." << endl;

 }

 void display_all() {

  for (int i = 0; i < sales.size(); i++) {

   sales[i].display();

   cout << endl;

  }

  cout << "Total Sales Revenue: " << calculate_sales_revenue() << endl;

 }

 float calculate_sales_revenue() {

  float total = 0;

  for (int i = 0; i < sales.size(); i++) {

   total += sales[i].get_total_price();

  }

  return total;

 }

};

class MainMenu {

 ProductManager product_manager;

 SalesManager sales_manager;

public:

 void run() {

  int choice = 0;

  do {

   display_menu();

   cin >> choice;

   switch (choice) {

    case 1:

     add_product();

     break;

    case 2:

     delete_product();

     break;

    case 3:

     update_product();

     break;

    case 4:

     find_product();

     break;

    case 5:

     display_products();

     break;

    case 6:

     add_sale();

     break;

    case 7:

     delete_sale();

     break;

    case 8:

     display_sales();

     break;

    case 9:

     break;

    default:

     cout << "Invalid choice. Please try again." << endl;

     break;

   }

  } while (choice != 9);

 }

 void display_menu()

  cout << "1. Add Product" << endl;

  cout << "2. Delete Product" << endl;

  cout << "3. Update Product" << endl;

  cout << "4. Find Product" << endl;

  cout << "5. Display All Products" << endl;

  cout << "6. Add Sale" << endl;

  cout << "7. Delete Sale" << endl;

  cout << "8. Display All Sales" << endl;

  cout << "9. Exit" << endl;

 

 void add_product() {

  int id, quantity;

  string name;

  float price;

  cout << "Enter product id: ";

  cin >> id;

  cout << "Enter product name: ";

  cin >> name;

  cout << "Enter product price: ";

  cin >> price;

  cout << "Enter product quantity: ";

  cin >> quantity;

  Product product(id, name, price, quantity);

  product_manager.add_product(product);

  cout << "Product added successfully." << endl;

 }

 void delete_product() {

  int id;

  cout << "Enter product id: ";

  cin >> id;

  product_manager.delete_product(id);

 }

 void update_product() {

  int id, quantity;

  string name;

  float price;

  cout << "Enter product id: ";

  cin >> id;

  cout << "Enter product name: ";

  cin >> name;

  cout << "Enter product price: ";

  cin >> price;

  cout << "Enter product quantity: ";

  cin >> quantity;

  Product product(id, name, price, quantity);

  product_manager.update_product(id, product);

 }

 void find_product() {

  int id;

  cout << "Enter product id: ";

  cin >> id;

  Product product = product_manager.find_product(id);

  if (product == Product())

   cout << "Product not found." << endl;

   else {

   product.display();

  }

 }

 void display_products() {

  product_manager.display_all();

 }

 void add_sale() {

  int id, product_id, quantity;

  float price, total_price;

  time_t sale_time;

  cout << "Enter sale id: ";

  cin >> id;

  cout << "Enter product id: ";

  cin >> product_id;

  cout << "Enter price: ";

  cin >> price;

  cout << "Enter quantity: ";

  cin >> quantity;

  total_price = price * quantity;

  sale_time = time(0);

  Sale sale(id, product_id, price, quantity, total_price, sale_time);

  sales_manager.add_sale(sale);

  cout << "Sale added successfully." << endl;

 }

 void delete_sale() {

  int id;

  cout << "Enter sale id: ";

  cin >> id;

  sales_manager.delete_sale(id);

 }

 void display_sales() {

  sales_manager.display_all();

 }

};

int main() {

 MainMenu menu;

 menu.run();

 return 0;

}

四、总结

通过上面的实现和源码分享,您可以看到如何使用C++创建一个基于控制台的简单管理系统。通过学习和使用类似的实例,您可以了解如何构建更为复杂的管理系统,为自己的业务和项目提供更高效的管理方式。再次强调,C++管理系统是非常重要和有用的应用程序,我们希望这个实例能够帮助大家更好地学习和实现C++管理系统。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复