[实验任务一]:多次撤销
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
3. 注意编程规范。
1. 类图
2.源代码
import java.util.ArrayList; import java.util.List; public class Caretaker { private Memento memento; private List<Memento> mementos = new ArrayList<Memento>(); //从备忘录中获取 public Memento getMemento() { memento = mementos.get(mementos.size() - 1); mementos.remove(mementos.size() - 1); return memento; } //设置备忘录 public void setMemento(Memento memento) { this.memento = memento; mementos.add(memento); } } class Memento { private String account; private String password; private String telNo; public Memento(String account, String password, String telNo) { this.account = account; this.password = password; this.telNo = telNo; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelNo() { return telNo; } public void setTelNo(String telNo) { this.telNo = telNo; } } //用户信息 class UserInfoDTO { private String account; private String password; private String telNo; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelNo() { return telNo; } public void setTelNo(String telNo) { this.telNo = telNo; } public Memento saveMemento() { return new Memento(account, password, telNo); } public void restoreMemento(Memento memento) { this.account = memento.getAccount(); this.password = memento.getPassword(); this.telNo = memento.getTelNo(); } public void show() { System.out.println("账户:" + this.account); System.out.println("密码:" + this.password); System.out.println("电话:" + this.telNo); } } public class Client { public static void main(String[] args) { UserInfoDTO user = new UserInfoDTO(); Caretaker c = new Caretaker(); user.setAccount("gaoxuan1"); user.setPassword("123456"); user.setTelNo("13000000000"); System.out.println("状态一:"); user.show(); //保存备忘录 c.setMemento(user.saveMemento()); System.out.println("---------------------------"); user.setPassword("111111"); user.setTelNo("13100001111"); System.out.println("状态二:"); user.show(); //保存备忘录 c.setMemento(user.saveMemento()); System.out.println("---------------------------"); user.setPassword("222222"); user.setTelNo("13200002222"); System.out.println("状态三:"); user.show(); //保存备忘录 c.setMemento(user.saveMemento()); System.out.println("---------------------------"); user.restoreMemento(c.getMemento()); user.restoreMemento(c.getMemento()); System.out.println("回到状态二:"); user.show(); System.out.println("---------------------------"); user.restoreMemento(c.getMemento()); System.out.println("回到状态一:"); user.show(); System.out.println("---------------------------"); } }
运行截图
标签:account,telNo,20,String,备忘录,实验,user,password,public From: https://www.cnblogs.com/po3a/p/18556033