改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
3. 注意编程规范。
1、类图

2、源代码
#include<iostream>
#include <list>
using namespace std;
class Memento {
private:
string account;
string password;
string telNo;
public:
string getAccount() {
return account;
}
void setAccount(string account) {
this->account = account;
}
string getPassword() {
return password;
}
void setPassword(string password) {
this->password = password;
}
string getTelNo() {
return telNo;
}
void setTelNo(string telNo) {
this->telNo = telNo;
}
Memento(string account, string password, string telNo) {
this->account = account;
this->password = password;
this->telNo = telNo;
}
};
class UserInfoDTO {
private:
string account;
string password;
string telNo;
public:
string getAccount() {
return account;
}
void setAccount(string account) {
this->account = account;
}
string getPassword() {
return password;
}
void setPassword(string password) {
this->password = password;
}
string getTelNo() {
return telNo;
}
void setTelNo(string telNo) {
this->telNo = telNo;
}
Memento* saveMemento() {
return new Memento(account,password,telNo);
}
void restoreMemento(Memento *memento) {
this->account=memento->getAccount();
this->password=memento->getPassword();
this->telNo=memento->getTelNo();
}
void show() {
cout<<"Account:"<<this->account<<endl;
cout<<"Password:"<<this->password<<endl;
cout<<"TelNo:"<<this->telNo<<endl;
}
};
class Caretaker {
private:
list<Memento*> ll;
public:
Memento* getMemento() {
ll.pop_front();
Memento* mm=ll.front();
return mm;
}
void setMemento(Memento *memento) {
ll.push_front(memento);
}
};
int main(){
UserInfoDTO *user=new UserInfoDTO();
Caretaker *c=new Caretaker();
user->setAccount("zhangsan");
user->setPassword("123456");
user->setTelNo("1310000000");
cout<<"状态一:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl;
user->setPassword("111111");
user->setTelNo("1310001111");
cout<<"状态二:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl;
user->setPassword("zyx666");
user->setTelNo("15733333333");
cout<<"状态三:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl;
user->setPassword("777777");
user->setTelNo("15511111111");
cout<<"状态四:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl;
user->setPassword("666666");
user->setTelNo("17455555555");
cout<<"状态五:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl;
user->restoreMemento(c->getMemento());
cout<<"回到状态四:"<<endl;
user->show();
cout<<"-----------------------------"<<endl;
user->restoreMemento(c->getMemento());
cout<<"回到状态三:"<<endl;
user->show();
cout<<"-----------------------------"<<endl;
user->restoreMemento(c->getMemento());
cout<<"回到状态二:"<<endl;
user->show();
cout<<"-----------------------------"<<endl;
user->restoreMemento(c->getMemento());
cout<<"回到状态一:"<<endl;
user->show();
cout<<"-----------------------------"<<endl;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2023-11-25 《程序员修炼之道——从小工到专家》读后感6
2023-11-25 《程序员修炼之道——从小工到专家》读后感5
2023-11-25 11.24(周五)总结
2023-11-25 11.23(周四)总结
2023-11-25 11.22(周三)总结
2023-11-25 11.21(周二)总结
2023-11-25 11.20(周一)总结