C++ 面向对象编程实例练习:建立一个产品库存管理

要求:产品库存管理——创建一个管理产品库存的应用。
建立一个产品类:价格、id、库存数量。
建立一个库存类:记录各种产品并能计算库存的总价值。

理论分析:【python 面向对象编程实例练习:建立一个产品库存管理

产品类:product.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include<iostream>
#include<string>
using namespace std;
class product{
public:
	string name;
	double price;
	int number;
	int index;
	product(){
	}
	product(string n, double p, int num, int ind){
		name = n;
		price = p;
		number = num;
		index = ind;
	}
	~product(){
	}
};

#endif

库存类:stock.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include<iostream>
using namespace std;

class stock{
public:
	product p[100];
	int n=0;
	stock();
	~stock();
	void add(product t);
	void dele(product t);
	void List(string l);
	void List(int l);
	void List_all();
};
#endif

stock.cpp

#include"stock.h"
#include"product.h"
#include<string>
using namespace std;

void stock::add(product t){
	if (n >0){
		int flag = 0;
		for (int i = 0; i < n; i++){
			if (p[i].index == t.index){
				p[i].number += t.number;
				flag = 1;
				break;
			}
			if (flag != 0){
				p[n] = t;
				n++;
			}
		}
	}
	else{
		p[n] = t;
		n++;
	}
	cout << "添加成功!!" << endl;
}
void stock::dele(product t){
	int flag = 0;
	for (int i = 0; i < n; i++){
		if (p[i].index == t.index&&p[i].name == t.name){
			flag = 1;
			if (p[i].number >= t.number){
				p[i].number -= t.number;
				cout << "产品ID:" << p[i].index;
				cout << " | 产品名称:" << p[i].name;
				cout << " | 产品剩余数量:" << p[i].number;
				cout << " | 产品价格:" << p[i].price << endl;
			}
			else{
				cout << "产品ID:" << p[i].index;
				cout << " | 产品名称:" << p[i].name;
				cout << " | 产品剩余数量:" << p[i].number;
				cout << " | 产品价格:" << p[i].price << endl;
				cout << "库存数目不足!!" << endl;
			}
		}
		if (flag == 0) cout << "库存中没有该产品!!" << endl;
	}
}
void stock::List(string l){
	int flag = 0;
	for (int i = 0; i < n; i++){
		if (p[i].name == l){
			flag = 1;
			cout << "产品ID:" << p[i].index;
			cout << " | 产品名称:" << p[i].name;
			cout << " | 产品剩余数量:" << p[i].number;
			cout << " | 产品价格:" << p[i].price << endl;
		}
	}
	if (flag == 0) cout << "库存中没有该产品!!" << endl;
}
void stock::List(int l){
	int flag = 0;
	for (int i = 0; i < n; i++){
		if (p[i].index == l){
			flag = 1;
			cout << "产品ID:" << p[i].index;
			cout << " | 产品名称:" << p[i].name;
			cout << " | 产品剩余数量:" << p[i].number;
			cout << " | 产品价格:" << p[i].price << endl;
		}
	}
	if (flag == 0) cout << "库存中没有该产品!!" << endl;
}
void stock::List_all(){
	int num = 0;
	double cost = 0;
	for (int i = 0; i < n; i++){
		if (p[i].number!=0){
			num += p[i].number;
			cost += p[i].number*p[i].price;
			cout << "产品ID:" << p[i].index;
			cout << " | 产品名称:" << p[i].name;
			cout << " | 产品剩余数量:" << p[i].number;
			cout << " | 产品价格:" << p[i].price << endl;
		}
	}
	cout << "-----------------------------------------------------------------" << endl;
	cout << "'总计:     |             | 产品总数:" << num << " | 产品总价:" << cost << endl;

}
stock::stock(){}
stock::~stock(){}

测试:main.cpp

#include<iostream>
#include"product.h"
#include"stock.h"

using namespace std;

int main(){

	stock s;
	cout << "*************test 1**************" << endl;
	cout << endl;
	product p1("apple",100,2,100001);
	s.add(p1);
	
	cout << "*************test 2**************" << endl;
	cout << endl;
	s.List_all();
	
	cout << "*************test 3**************" << endl;
	cout << endl;
	product p2("apple", 100, 1, 100001);
	s.dele(p2);
	
	cout << "*************test 4**************" << endl;
	cout << endl;
	s.List("apple");
	s.List(100001);
	
	cout << "*************test 5**************" << endl;
	cout << endl;
	product p3("apple", 100, 2, 100001);
	s.dele(p3);

	cout << "*************test 6**************" << endl;
	cout << endl;
	product p4("appl", 100, 2, 100001);
	s.dele(p4);
	system("pause");
	return -1;

}

结果:

猜你喜欢

转载自blog.csdn.net/weixin_38715903/article/details/89381063