[實驗任務一]:多次撤銷
改進課堂上的“使用者資訊操作撤銷”例項,使得系統可以實現多次撤銷(可以使用HashMap、ArrayList等集合資料結構實現)。
實驗要求:
1. 畫出對應的類圖;
2. 提交原始碼;
import java.util.ArrayList;
import java.util.List;
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 Memento {
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(String account, String password, String telNo) {
this.account = account;
this.password = password;
this.telNo = telNo;
}
}
public 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("Account:"+this.account);
System.out.println("Password:"+this.password);
System.out.println("TelNo:"+this.telNo);
}
}
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserInfoDTO user=new UserInfoDTO();
Caretaker c=new Caretaker();
user.setAccount("zhangsan");
user.setPassword("123456");
user.setTelNo("1310000000");
System.out.println("狀態一:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------");
user.setPassword("111111");
user.setTelNo("1310001111");
System.out.println("狀態二:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------");
user.setPassword("zyx666");
user.setTelNo("15733333333");
System.out.println("狀態三:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------");
user.setPassword("777777");
user.setTelNo("15511111111");
System.out.println("狀態四:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------");
user.setPassword("666666");
user.setTelNo("17455555555");
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("-----------------------------");
}
}
3. 注意程式設計規範。