2024/11/27
软件设计实验二十
[实验任务一]:多次撤销
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
public class Memento {
private String account;
private String password;
private String telephone;
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 getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Memento(String account, String password, String telephone) {
this.account = account;
this.password = password;
this.telephone = telephone;
}
}
public class Caretaker {
private List<Memento> list=new ArrayList<>();
public Memento getMemento() {
Memento mm=list.get(list.size()-2);
list.remove(list.size()-2);
return mm;
}
public void setMemento(Memento memento) {
list.add(memento);
}
}
public class UserInfo {
private String account;
private String password;
private String telephone;
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 getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Memento saveMemento() {
return new Memento(account,password,telephone);
}
public void restoreMemento(Memento memento) {
this.account=memento.getAccount();
this.password=memento.getPassword();
this.telephone=memento.getTelephone();
}
public void show() {
System.out.println("账号:"+this.account);
System.out.println("密码:"+this.password);
System.out.println("电话号码:"+this.telephone);
}
}
public class App
{
public static void main( String[] args )
{
// TODO Auto-generated method stub
UserInfo user=new UserInfo();
Caretaker c=new Caretaker();
user.setAccount("gonghanbin");
user.setPassword("ghb188");
user.setTelephone("180xxxx9148");
System.out.println("状态一:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("++++++++++++++++++++++++++++++");
user.setPassword("gonghanbin188");
user.setTelephone("188xxxx3768");
System.out.println("状态二:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("++++++++++++++++++++++++++++++");
user.setPassword("gonghanbin666");
user.setTelephone("188xxxx9148");
System.out.println("状态三:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("++++++++++++++++++++++++++++++");
user.setPassword("gonghanbin");
user.setTelephone("180xxxx3768");
System.out.println("状态四:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("++++++++++++++++++++++++++++++");
user.setPassword("ghb666");
user.setTelephone("xxxxxxxxx");
System.out.println("状态五:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("++++++++++++++++++++++++++++++");
user.restoreMemento(c.getMemento());
System.out.println("回到状态四:");
user.show();
System.out.println("++++++++++++++++++++++++++++++");
user.restoreMemento(c.getMemento());
System.out.println("回到状态三:");
user.show();
System.out.println("++++++++++++++++++++++++++++++");
user.restoreMemento(c.getMemento());
System.out.println("回到状态二:");
user.show();
System.out.println("++++++++++++++++++++++++++++++");
user.restoreMemento(c.getMemento());
System.out.println("回到状态一:");
user.show();
System.out.println("++++++++++++++++++++++++++++++");
}
}