首页 > 其他分享 >软件设计实验20

软件设计实验20

时间:2022-10-30 19:26:15浏览次数:49  
标签:account telNo 20 string 软件设计 void 实验 return password

实验 20:备忘录模式

[实验任务一]:多次撤销

改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。

直接放源码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
//备忘录
class Memento {
private:
    string account;
    string password;
    string telNo;
public:
    Memento(string account, string password, string telNo) {
        this->account = account;
        this->password = password;
        this->telNo = telNo;
    }

    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;
    }
};
//发起者
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() {
        Memento memento(account, password, telNo);
        return memento;
    }

    void restoreMenmento(Memento memento) {
        this->account = memento.getAccount();
        this->password = memento.getPassword();
        this->telNo = memento.getTelNo();
    }
    void show() {
        cout << "帐号:" + this->account << endl;
        cout << "密码:" + this->password << endl;
        cout << "电话:" + this->telNo << endl;
    }
};
//管理者
class Caretaker {
private:
    vector<Memento>mementos;
public:
    Memento getMemento(int i) {
        return mementos.at(i);
    }
    void setMemento(Memento memento) {
        mementos.push_back(memento);
    }
};
//测试函数
int main() {
    int index = -1;
    UserInfoDTO user;
    Caretaker taker;

    user.setAccount("aaa");
    user.setPassword("123456");
    user.setTelNo("18800000000");
    cout << "状态一:" << endl;
    taker.setMemento(user.saveMemento());
    index++;
    user.show();
    cout << "----------------------------------" << endl;

    user.setPassword("111111");
    user.setTelNo("18800001111");
    cout << "状态二:" << endl;
    taker.setMemento(user.saveMemento());
    index++;
    user.show();
    cout << "----------------------------------" << endl;

    user.setPassword("222222");
    user.setTelNo("18800002222");
    cout << "状态三:" << endl;
    user.show();
    cout << "----------------------------------" << endl;

    cout << "回到状态二:" << endl;
    index--;
    user.restoreMenmento(taker.getMemento(1));
    user.show();
    cout << "----------------------------------" << endl;

    cout << "回到状态一:" << endl;
    index --;
    user.restoreMenmento(taker.getMemento(0));
    user.show();
    cout << "----------------------------------" << endl;

}

  

标签:account,telNo,20,string,软件设计,void,实验,return,password
From: https://www.cnblogs.com/lyf3701/p/16841945.html

相关文章

  • 2022-2023-1 20221425 《计算机基础与程序设计》第九周学习总结
    学期(如2022-2023-1)学号(如:20221425)《计算机基础与程序设计》第九周学习总结作业信息这个作业属于哪个课程<班级的链接>(如2022-2023-1-计算机基础与程序设计)这......
  • 2022-2023-1 20221415 《计算机基础与程序设计》第九周学习总结
    2022-2023-120221415《计算机基础与程序设计》第九周学习总结作业信息这个作业属于哪个课程<班级的链接>(2022-2023-1-计算机基础与程序设计)这个作业要求在哪......
  • P8817 CSP-S 2022 假期计划
    P8817CSP-S2022假期计划-洛谷|计算机科学教育新生态(luogu.com.cn)下文中,\(u\tov\)可达意为\(u\tov\)可以经过不多于\(k\)次转车到达,即\(u\)到\(v\)......
  • 决策树算法实验
    决策树算法实验 【实验目的】1、理解决策树算法原理,掌握决策树算法框架;2、理解决策树学习算法的特征选择、树的生成和树的剪枝;3、能根据不同的数据类型,选择不同的......
  • 2022.10代码大全阅读心得1
    第11章:变量名的力量问题:怎样给一个变量命名?长名字还是短名字?命名的最佳实践有哪些?有哪些常见的命名方法?在命名中应该要避免的东西有哪些?怎样给一个变量命名?通......
  • HN CSP-2022 游寄
    备注这是我第一次参加CSP,也是第一次写游寄,写的不好请见谅这里实测是指在各大欧钩上评测,并非官方数据(也不是我测的CSP-S,我还不知道实测分数初赛初赛其实不太记得了,只记......
  • 2022.10代码大全阅读心得2
    第十四章组织直线型代码14.1必须有明确顺序的代码对于具有明显的顺序关系的代码,应该使用顺序结构。对于隐含的顺序关系,应该:去除不合理的依赖关系(如不应该在Calculat......
  • CSP-S2022游记
    Day-inf考前9~10月闭关,两个月吧。学了好多的,码力还是很菜,思路急需打开。。。第一次CSP-S/第一次OI比赛/第一次4Hour全程————没有什么很大的心理波动,比较清楚自己的......
  • 【2022.10.28】Vue基础学习(4)
    内容概括1.计算属性重写过滤案例2.监听属性3.组件介绍和定义4.父子通信之父传子5.父子通信之子传父6.ref属性7.动态组件keep-alive8.插槽9.vue-cli内容......
  • [2022.10.30]常用类—Date与DateFormat
    importorg.junit.Test;importjava.util.Date;/*1.用system类中的currentTimeMillis()方法2.java.util.Date类1)两个构造器的使用......