C++ATM客户端STL版

2021/10/14 9:14:19

本文主要是介绍C++ATM客户端STL版,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#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;
}



这篇关于C++ATM客户端STL版的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程