C++ 面向对象编程实例练习:建立一个电影商店

要求:完成一个电影商店录像带租赁任务。

建立四个类:

录像带类:包括录像带的名字,买回来的价格,类别编号(电影类别),库存数量
    被租借的录像带类:录像带的名称,录像带的id,租借人,租借时间,应归还时间,当天日期,逾期天数
    用户类:包括用户名称,id号,该用户租借的录像带;完成的函数功能有:展示租借的录像带详细信息,判断是否超出了租借上限
    商店类:存放库存的video,存放借出video的report,存放逾期video的overdue等;完成借书还书,进货下货,更新报告等函数

 

videotape.h

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

#endif

bor_videotape.h

#ifndef BOR_VIDEOTAPE_H
#define BOR_VIDEOTAPE_H
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
class bor_videotape{
public:
	string user_name;
	double price;
	int number;
	string video_name;
	double video_id;
	int user_id;
	int time_bor;
	int time_re;
	int time_now;
	int day;
	bor_videotape(){
	}
	bor_videotape(string n, double p, int num, string video_n, double video_ind, int user_ind, int bor, int re, int now){
		user_name = n;
		price = p;
		number = num;
		video_name = video_n;
		video_id = video_ind;
		user_id = user_ind;
		time_bor = bor;
		time_re = re;
		time_now = now;
		day = time_now - time_bor;
	}
	~bor_videotape(){
	}
};

#endif

user.h

#ifndef USER_H
#define USER_H
#include<iostream>
#include<string>
#include"bor_videotape.h"
#include"videotape.h"
#include<vector>
using namespace std;
class user{
public:
	string name;
	int index = 0;
	int number = 0;
	vector<bor_videotape> b;
	user(string u_name, int ind);
	user();
	~user();
	int video_bor(bor_videotape t, int flag);
	int n_video();
	void list_video();
};

#endif

user.cpp

#include<iostream>
#include<string>
#include"bor_videotape.h"
#include"user.h"

user::user(string u_name, int ind){
	name = u_name;
	index = ind;
}
user::user(){
}
user::~user(){
}
int user::video_bor(bor_videotape t, int flag)
{
	if (flag == 0){
		//huanshu
		for (int i = 0; i < b.size(); i++){
			if (b[i].user_id == t.user_id&&b[i].video_id == t.video_id){
				if (b[i].number - t.number == 0){
					b.erase(b.begin() + i);
					number -= t.number;
					return 2;
				}
				else if (b[i].number - t.number > 0){
					b[i].number -= t.number;
					number -= t.number;
					return 2;
				}
				else{
					cout << "还书超出额度,请核查!!" << endl;
				}
			}
			else{
				cout << "还书信息有误,请核查!!" << endl;
				return 0;
			}
				
		}
	}
	if (flag == 1){
		if ((number + t.number) > 5){
			cout << "超出可借电影带数目" << endl;
			return 0;
		}
		else if ((number + t.number) <= 5){
			number += t.number;
			if (b.size() == 0){
				b.push_back(t);
				return 1;
			}
			else{
				for (int i = 0; i < b.size(); i++){
					if (b[i].user_id == t.user_id&&b[i].video_id == t.video_id){
						b[i].number += t.number;
						return 1;
					}
					else{
						b.push_back(t);
						return 1;
					}
				}
			}
		}
	}
}
int user::n_video()
{
	return number;
}
void user::list_video()
{
	for (int i = 0; i < number; i++){
		cout << "录像带名称:" << b[i].video_name;
		cout << " | 录像带id:" << b[i].video_id;
		cout << " | 借录像带的人:" << b[i].user_name;
		cout << " | 借阅时间:" << b[i].time_bor;
		cout << " | 应还时间:" << b[i].time_re << endl;
	}
}

shop.h

#ifndef SHOP_H
#define SHOP_H
#include<iostream>
#include<string>
#include<vector>
#include"bor_videotape.h"
#include"user.h"
#include"videotape.h"
using namespace std;
class shop{
public:

	vector<videotape> video;
	vector<bor_videotape> report;
	vector<bor_videotape> overdue;
	vector<user> u;
	int	day = 30;
	shop();
	~shop();
	void addVideo(videotape v);
	void delVideo(videotape v);
	void findvideo(string l);
	void findvideo(int l);
	void adduser(string user_name);
	void finduser(string l);
	void finduser(int l);
	void List_video();
	void List_user();
	void borrowVideo(int video_ID,int user_ID,int number,int d);
	void returnVideo(int video_ID, int user_ID, int d);
	void List_report();
	void overduereport_update(int d);
	void overduereport(int d);
};

#endif

shop.cpp

#include<iostream>
#include<string>
#include"bor_videotape.h"
#include"user.h"
#include"videotape.h"
#include"shop.h"

shop::shop(){
	day = 30;
}
shop::~shop(){

}
void shop::addVideo(videotape v){
	if (video.size() > 0){
		int flag = 0;
		for (int i = 0; i < video.size(); i++){
			if (video[i].index == v.index&&video[i].name == v.name){
				video[i].number += v.number;
				flag = 1;
			}
		}
		if (flag != 1){
			video.push_back(v);
		}
	}
	else{
		video.push_back(v);
	}
}

void shop::delVideo(videotape v){
	int flag = 0;
	for (int i = 0; i < video.size(); i++){
		if (video[i].index == v.index&&video[i].name == v.name){
			flag = 1;
			if (video[i].number >= v.number){
				video[i].number -= v.number;
				cout << "录像带名称:" << video[i].name;
				cout << " | 录像带id:" << video[i].index;
				cout << " | 录像带剩余数量:" << video[i].number;
				cout << " | 录像带价格:" << video[i].price << endl;
			}
			else{
				cout << "录像带名称:" << video[i].name;
				cout << " | 录像带id:" << video[i].index;
				cout << " | 录像带数量:" << video[i].number;
				cout << " | 录像带价格:" << video[i].price << endl;
				if (video[i].number==0)
					cout << "库存中没有该录像带!!请核对记录!!" << endl;
				else 
					cout << "录像带库存数量不足!!请核对记录!!" << endl;
			}
			break;
		}
	}
	if (flag == 0){
		cout << "库存中没有该录像带!!请核对记录!!" << endl;
	}
}

void shop::findvideo(string l){
	int flag = 0;
	for (int i = 0; i < video.size(); i++){
		if (video[i].name == l){
			flag = 1;
			cout << "录像带名称:" << video[i].name;
			cout << " | 录像带id:" << video[i].index;
			cout << " | 录像带数量:" << video[i].number;
			cout << " | 录像带价格:" << video[i].price << endl;
			for (int j = 0; j < report.size(); j++){
				if (report[i].video_name == l){
					cout << "录像带名称:" << report[i].video_name;
					cout << " | 录像带id:" << report[i].video_id;
					cout << " | 借录像带的人:" << report[i].user_name;
					cout << " | 借阅时间:" << report[i].time_bor;
					cout << " | 应还时间:" << report[i].time_re << endl;
				}
			}
		}
	}
	if (flag == 0)
		cout << "库存中没有该录像带!!" << endl;
}

void shop::findvideo(int l){
	int flag = 0;
	for (int i = 0; i < video.size(); i++){
		if (video[i].index == l){
			flag = 1;
			cout << "录像带名称:" << video[i].name;
			cout << " | 录像带id:" << video[i].index;
			cout << " | 录像带数量:" << video[i].number;
			cout << " | 录像带价格:" << video[i].price << endl;
			for (int j = 0; j < report.size(); j++){
				if (report[i].video_id == l){
					cout << "录像带名称:" << report[i].video_name;
					cout << " | 录像带id:" << report[i].video_id;
					cout << " | 借录像带的人:" << report[i].user_name;
					cout << " | 借阅时间:" << report[i].time_bor;
					cout << " | 应还时间:" << report[i].time_re << endl;
				}
			}
		}
	}
	if (flag == 0)
		cout << "库存中没有该录像带!!" << endl;
}

void shop::adduser(string user_name){
	user u1(user_name, u.size()+1);
	u.push_back(u1);
}

void shop::finduser(string l){
	int flag = 0;
	for (int i = 0; i < u.size(); i++){
		if (u[i].name == l){
			flag = 1;
			cout << " | 用户名称:" << u[i].name;
			cout << " | 用户ID:" << u[i].index;
			cout << " | 已借录像带数:" << u[i].n_video() << endl;
		}
	}
	if (flag == 0)
		cout << "用户中没有该用户!!" << endl;
}

void shop::finduser(int l){
	int flag = 0;
	for (int i = 0; i < u.size(); i++){
		if (u[i].index == l){
			flag = 1;
			cout << " | 用户名称:" << u[i].name;
			cout << " | 用户ID:" << u[i].index;
			cout << " | 已借录像带数:" << u[i].n_video() << endl;
		}
	}
	if (flag == 0)
		cout << "用户中没有该用户!!" << endl;
}

void shop::List_video(){
	for (int i = 0; i < video.size(); i++){
		cout << "录像带名称:" << video[i].name;
		cout << " | 录像带id:" << video[i].index;
		cout << " | 录像带数量:" << video[i].number;
		cout << " | 录像带价格:" << video[i].price << endl;
	}
}

void shop::List_user(){
	for (int i = 0; i < u.size(); i++){
		cout << " | 用户名称:" << u[i].name;
		cout << " | 用户ID:" << u[i].index;
		cout << " | 已借录像带数:" << u[i].n_video() << endl;
	}
}

void shop::borrowVideo(int video_ID, int user_ID, int number,int d){
	int flag = 0;
	for (int i = 0; i < video.size(); i++){
		if (video[i].index == video_ID){
			flag = 1;
			if (video[i].number == 0){
				flag = 2;
				cout << "该录像带已全部借出!!" << endl;
				for (int j = 0; j < report.size(); j++){
					if (report[j].video_id == video_ID){
						cout << "录像带名称:" << report[j].video_name;
						cout << " | 录像带id:" << report[j].video_id;
						cout << " | 借录像带的人:" << report[j].user_name;
						cout << " | 借阅时间:" << report[j].time_bor;
						cout << " | 应还时间:" << report[j].time_re << endl;
					}
				}
			}
			else{
				
				bor_videotape bo(u[user_ID - 1].name, video[i].price, number,video[i].name, video[i].index, user_ID, d, d + day, d);
				report.push_back(bo);
				int flag1=0;
				flag1 = u[user_ID - 1].video_bor(bo, 1);//借书
				if (flag1 == 1)
					video[i].number -= 1;
				cout << " | 用户名称:" << u[user_ID - 1].name;
				cout << " | 用户ID:" << u[user_ID - 1].index;
				cout << " | 已借录像带数:" << u[user_ID - 1].n_video() << endl;
			}
		}
	}
	if (flag == 0)
		cout << "库存中没有该录像带!!" << endl;
	overduereport_update(d);
	if (flag==1)
		u[user_ID - 1].list_video();
}

void shop::returnVideo(int video_ID, int user_ID,int d){
	overduereport_update(d);
	for (int i = 0; i < report.size(); i++){
		if (report[i].video_id == video_ID&&report[i].user_id == user_ID){
			bor_videotape re = report[i];
			report.erase(report.begin()+i);
			u[user_ID - 1].video_bor(re,0);//还书

		}
	}
	for (int i = 0; i < video.size(); i++){
		if (video[i].index == video_ID){
			video[i].number++;
		}
	}
	for (int i = 0; i < overdue.size(); i++){
		if (overdue[i].video_id == video_ID&&overdue[i].user_id == user_ID){
			if (overdue[i].day <= 0  )cout << "未逾期~" << endl;
			else{
				cout << "录像带名称:" << overdue[i].video_name;
				cout << " | 录像带id:" << overdue[i].video_id;
				cout << " | 借录像带的人:" << overdue[i].user_name;
				cout << " | 借阅时间:" << overdue[i].time_bor;
				cout << " | 应还时间:" << overdue[i].time_re;
				cout << " | 逾期时间:" << overdue[i].day << endl;
			}
			overdue.erase(overdue.begin() + i);
		}
	}
}

void shop::List_report(){
	for (int i = 0; i < report.size(); i++){
		cout << "录像带名称:" << report[i].video_name;
		cout << " | 录像带id:" << report[i].video_id;
		cout << " | 借录像带的人:" << report[i].user_name;
		cout << " | 借阅时间:" << report[i].time_bor;
		cout << " | 应还时间:" << report[i].time_re << endl;
	}
}

void shop::overduereport_update(int d){
	for (int i = 0; i < report.size(); i++){
		if ((d - report[i].time_re) > 0){
			overdue.push_back(report[i]);
		}
	}
	for (int i = 0; i < overdue.size(); i++){
		overdue[i].day = d - overdue[i].time_re;
	}
}
void shop::overduereport(int d){
	overduereport_update(d);
	for (int i = 0; i < overdue.size(); i++){
		cout << "录像带名称:" << overdue[i].video_name;
		cout << " | 录像带id:" << overdue[i].video_id;
		cout << " | 借录像带的人:" << overdue[i].user_name;
		cout << " | 借阅时间:" << overdue[i].time_bor;
		cout << " | 应还时间:" << overdue[i].time_re;
		cout << " | 逾期时间:" << overdue[i]. day<< endl;
	}
}

测试part:
 

#include<iostream>
#include"bor_videotape.h"
#include"shop.h"
#include"user.h"
#include"videotape.h"

using namespace std;

int main(){
	shop s;
	
	videotape v1("蜡笔小新", 50, 3201, 10);
	videotape v2("霸王别姬", 100, 1201, 2);
	videotape v3("黄金瞳", 50, 4208, 3);
	s.addVideo(v1);
	s.addVideo(v2);
	s.addVideo(v3);
	s.List_video();
	cout << "---------------------------------------------------------" << endl;
	
	s.addVideo(v1);
	s.List_video();
	cout << "---------------------------------------------------------" << endl;

	s.findvideo(3201);
	cout << "---------------------------------------------------------" << endl;

	s.findvideo(3200);
	cout << "---------------------------------------------------------" << endl;

	s.findvideo("蜡笔小新");
	cout << "---------------------------------------------------------" << endl;

	s.findvideo("蜡笔");
	cout << "---------------------------------------------------------" << endl;

	videotape v4("蜡笔小新", 50, 3201, 2);
	s.delVideo(v4);
	s.List_video();
	cout << "---------------------------------------------------------" << endl;

	videotape v5("霸王别姬", 100, 1201, 2);
	s.delVideo(v5);
	s.List_video();
	cout << "---------------------------------------------------------" << endl;

	videotape v6("蜡笔", 50, 3201, 2);
	s.delVideo(v6);
	cout << "---------------------------------------------------------" << endl;

	videotape v7("蜡笔小新", 50, 3200, 2);
	s.delVideo(v7);
	cout << "---------------------------------------------------------" << endl;

	s.adduser("小A");
	s.adduser("小B");
	s.adduser("小C");
	s.List_user();
	cout << "---------------------------------------------------------" << endl;

	s.finduser(1);
	s.finduser("小A");
	s.finduser(" ");
	s.finduser(4);
	cout << "---------------------------------------------------------" << endl;

	//借书
	s.borrowVideo(3201, 2, 1,2);
	cout << "---------------------------------------------------------" << endl;
	s.borrowVideo(1201, 2, 1, 2);
	cout << "---------------------------------------------------------" << endl;

	s.borrowVideo(4208, 2, 1, 2);
	cout << "---------------------------------------------------------" << endl;
	s.borrowVideo(4208, 1, 1, 2);
	cout << "---------------------------------------------------------" << endl;
	s.borrowVideo(4208, 2, 1, 1);
	cout << "---------------------------------------------------------" << endl;
	s.borrowVideo(4208, 2, 1, 2);
	cout << "---------------------------------------------------------" << endl;
	s.List_video();
	s.List_user();
	cout << "---------------------------------------------------------" << endl;
	
	s.List_report();
	cout << "---------------------------------------------------------" << endl;

	//还书
	cout << endl;
	cout << endl;
	s.List_video();
	s.List_user();
	cout << "---------------------------------------------------------" << endl;
	s.returnVideo(3201, 2, 33);
	s.List_video();
	s.List_user();
	cout << "---------------------------------------------------------" << endl;

	s.returnVideo(4208, 1, 30);
	s.List_video();
	s.List_user();
	cout << "---------------------------------------------------------" << endl;

	

	system("pause");
	return 0;
}

结果:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值