ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ATM客户端STL版

2021-10-14 08:33:49  阅读:23  来源: 互联网

标签:string STL void ATM C++ Date int date op



#include<iostream>
#include<string>
#include <vector>
#include <map>
using namespace std;

class Date
{
public:
	Date() {}
	Date(int year, int month, int day) :year(year), month(month), day(day) {}
	~Date() {}

	void setYear(int year);

	void setMonth(int month);

	void setDay(int day);

	int getYear();

	int getMonth();

	int getDay();

	friend ostream &operator<<(ostream &out, const Date &date);
	friend istream &operator>>(istream &in, Date &date);
	bool operator<(const Date &date) const;

private:
	int year;
	int month;
	int day;
};

void Date::setYear(int year)
{
	this->year = year;
}

void Date::setMonth(int month)
{
	this->month = month;
}

void Date::setDay(int day)
{
	this->day = day;
}

int Date::getYear()
{
	return year;
}

int Date::getMonth()
{
	return month;
}

int Date::getDay()
{
	return day;
}

ostream &operator<<(ostream &out, const Date &date)
{
	out << date.year << "/" << date.month << "/" << date.day;
	return out;
}
istream &operator>>(istream &in, Date &date)
{

	while (1)
	{
		in >> date.year;
		if (date.year >= 2000 && date.year <= 2099)
		{
			break;
		}
		else
		{
			cout << "年份不对,请重输!!!" << endl;

		}
	}
	while (1)
	{
		in >> date.month;
		if (date.month >= 1 && date.month <= 12)
		{
			break;
		}
		else
		{
			cout << "月份不对,请重输!!!" << endl;

		}
	}
	while (1)
	{
		in >> date.day;
		if ((date.year % 400 == 0 || (date.year % 4 == 0 && date.year % 100 != 0)) && (date.month == 2) && (date.day >= 1 && date.day <= 29))
		{
			break;

		}
		else if ((date.month == 1 || date.month == 3 || date.month == 5 || date.month == 7 || date.month ==
			8 || date.month == 10 || date.month == 12) && (date.day >= 1 && date.day <= 31))
		{
			break;

		}
		else if ((date.month == 2) && (date.day >= 1 && date.day <= 28))
		{

			break;
		}
		else if ((date.month == 4 || date.month == 6 || date.month == 11) && (date.day >= 1 && date.day <= 30))
		{

			break;
		}
		else
		{
			cout << "日子不对,请重输!!!" << endl;

		}

	}
	return in;
}
bool  Date::operator<(const Date &date) const
{
	if (year < date.year)
	{
		return true;
	}
	else if (year == date.year)
	{
		if (month < date.month)
		{
			return true;
		}
		else if (month == date.month)
		{
			if (day <= date.day)
			{
				return true;
			}
		}

	}
	return false;


}


void testDate()
{
	Date date;
	cin >> date;
	cout << date;
}
class Record
{
public:
	Record() {}
	Record(string name, Date date, string type, double amount, double balance);

	void setName(string name);

	void setDate(Date date);

	void setType(string type);

	void setAmount(double amount);

	Date getDate();

	double getBalance();

	string getType();

	double getAmount();

	string getName();

	friend ostream & operator <<(ostream & out, const Record & r);


private:

	string name;
	Date date;
	double balance;
	double amount;
	string type;

};
void testRecord()
{
	Date d(2021, 4, 15);
	Record r("张三", d, "存钱", 1000, 1000);
	cout << r << endl;
	r.setName("李四");
	Date d1(2021, 4, 14);
	r.setDate(d1);
	r.setType("取钱");
	r.setAmount(100);
	cout<<r;
}
Record::Record(string name, Date date, string type, double amount, double balance)
{
	this->name = name;
	this->date = date;
	this->type = type;
	this->amount = amount;
	this->balance = balance;
}

void Record::setName(string name)
{
	this->name = name;
}

void Record::setDate(Date date)
{
	this->date = date;
}

void Record::setType(string type)
{
	this->type = type;
}

void Record::setAmount(double amount)
{
	this->amount = amount;
}

Date Record::getDate()
{
	return date;
}

double Record::getBalance()
{
	return balance;
}

string Record::getType()
{
	return type;
}

double Record::getAmount()
{
	return amount;
}

string Record::getName()
{
	return name;
}


ostream & operator <<(ostream & out, const Record & r)
{
	out << "交易时间:" << r.date << "\n" << "用户姓名:" << r.name << "\n" << "交易类型:" << r.type << "\n" << "交易金额:" << r.amount << "\n" << "余额:" << r.balance << endl;
	return out;
}
class User
{
public:
	User() {};
	User(string name, string cid1, string uid1) :uname(name), cid(cid1), uid(uid1)
	{
		this->balance = 0;
	}
	~User() {};
	friend ostream &operator<<(ostream &out, const User &user);
	friend istream &operator>>(istream &in, User &user);
	void setUname(string uname);

	void setCid(string cid);

	void setUid(string uid);

	void setBalance(double balance);


	string getUname();

	string getCid();

	string getUid();

	double getBalance();


private:
	string uname;
	string cid;
	string uid;
	double balance;

};

void User::setUname(string uname)
{
	this->uname = uname;
}

void User::setCid(string cid)
{
	this->cid = cid;
}

void User::setUid(string uid)
{
	this->uid = uid;
}

void User::setBalance(double balance)
{
	this->balance = balance;
}

string User::getUname()
{
	return uname;
}

string User::getCid()
{
	return cid;
}

string User::getUid()
{
	return uid;
}

double User::getBalance()
{
	return balance;
}

ostream &operator<<(ostream &out, const User &user)
{
	out << user.uname << "  " << user.cid << "  " << user.uid << "  " << user.balance << endl;
	return out;
}
istream &operator>>(istream &in, User &user)
{
	in >> user.uname >> user.cid >> user.uid >> user.balance;
	return in;
}


class Operation
{
public:
	Operation() {}
	Operation(User user, Date date) :user(user), date(date)
	{}

	~Operation() {}

	void add(Record &r1);

	void drawmoney(double dm);

	void deposite(double dp);

	void querybydate(Date &d1, Date &d2);

	void querybytype(string type);


	void show(int i);

	void show();

	void showuser();
	
	void setDate(Date &date);

private:
	User user;
	Date date;
	vector<Record> r;
	multimap<string, Record> m1;
	multimap<Date, Record> m2;
};

void Operation::add(Record &r1)
{
	r.push_back(r1);
	m1.insert(make_pair(r1.getType(), r1));
	m2.insert(make_pair(r1.getDate(), r1));
}

void Operation::drawmoney(double dm)
{

	if (dm <= user.getBalance())
	{
		user.setBalance(user.getBalance() - dm);
		Record r1(user.getUname(), date, "取钱", dm, user.getBalance());
		add(r1);
	}
	else
	{
		cout << date;
		cout << "此时余额" << user.getBalance() << endl;
		cout << "操作错误余额不足!" << endl;
		cout << "****************************************" << endl;
		Record r1(user.getUname(), date, "取钱", 0, user.getBalance());
		add(r1);
	}
}

void Operation::deposite(double dp)
{
	user.setBalance(user.getBalance() + dp);
	Record r1(user.getUname(), date, "存钱", dp, user.getBalance());
	add(r1);
}

void Operation::querybydate(Date &d1, Date &d2)
{
	for (multimap<Date, Record>::iterator it = m2.lower_bound(d1); it != m2.upper_bound(d2); ++it)
	{

		cout << "交易记录:" << (*it).second;
	}

}



void Operation::querybytype(string type)
{
	for (multimap<string, Record>::iterator it = m1.lower_bound(type); it != m1.upper_bound(type); ++it)
	{

		cout << "交易记录:" << it->second;
	}
}

void Operation::show(int i)
{
	cout << r[i];
}

void Operation::show()
{

	for (vector<Record>::iterator it=r.begin();it!=r.end();it++)
	{
		cout << *it<<endl;
	}
}

void Operation::showuser()
{
	cout << user << endl;
	show();
}

void Operation::setDate(Date &date)
{
	this->date = date;
}

void test()
{
	User user("张三", "111111", "341204");
	Date date(2021, 4, 15);
	Date date1(2021, 3, 15);
	Date date2(2020, 2, 15);
	Date date3(2019, 6, 30);
	Date date4(2019, 3, 4);
	Date date5(2019, 6, 30);


	Operation op(user, date);
	op.deposite(1000);
	op.drawmoney(500);
	op.setDate(date1);
	op.deposite(10000);
	op.setDate(date2);
	op.drawmoney(5000);
	op.setDate(date3);
	op.drawmoney(1000);
	op.setDate(date4);
	op.deposite(50000);
	op.setDate(date5);
	op.deposite(600);
	//op.show(0);
	//op.show(2);
	//op.show();
	//op.querybytype("存钱");
	//op.querybytype("取钱");
	//op.querybydate(date4, date1);
	op.showuser();

}


int main()
{
	//testDate();
	//testRecord();
	test();
	return 0;
}

标签:string,STL,void,ATM,C++,Date,int,date,op
来源: https://www.cnblogs.com/dss-99/p/15404615.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有