Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

Given the following code:

DocumentMemento.java

package MementoLabOne;
public class DocumentMemento {
    private final String content;
    private final String fontName;
    private final int fontSize;

    public DocumentMemento(String content, String fontName, int fontSize) {
        this.content = content;
        this.fontName = fontName;
        this.fontSize = fontSize;
    }

    public String getContent() {
        return content;
    }

    public String getFontName() {
        return fontName;
    }

    public int getFontSize() {
        return fontSize;
    }
}

History.java

package MementoLabOne;
import java.util.ArrayDeque;
import java.util.Deque;

public class History {
    private Deque<DocumentMemento> mementos = new ArrayDeque<>();

    public void push(DocumentMemento memento) {
        mementos.push(memento);
    }

    public DocumentMemento pop() {
        return mementos.pop();
    }
}

Document.java

package MementoLabOne;
public class Document {
    private String content;
    private String fontName;
    private int fontSize;

    public DocumentMemento createMemento() {
        return new DocumentMemento(content, fontName, fontSize);
    }

    public void restore(DocumentMemento memento) {
        this.content = memento.getContent();
        this.fontName = memento.getFontName();
        this.fontSize = memento.getFontSize();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFontName() {
        return fontName;
    }

    public void setFontName(String fontName) {
        this.fontName = fontName;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    @Override
    public String toString() {
        return "Document{" +
                "content='" + content + '\'' +
                ", fontName='" + fontName + '\'' +
                ", fontSize=" + fontSize +
                '}';
    }
}

Complete the following driver by accomplishing the TO-DOs

package MementoLabOne;

public class Lab1 {
    public static void main(String[] args) {
        //TODO Create a new instance of the Document class
    
        //TODO  Create a new instance of the History class
        
        //TODO set the content of the Document instance to "Hello".
       
        //TODO  save the current state of the Document instance into the History instance by
        // creating a memento of the document and pushing it onto the history.
        

        //TODO  change the font of the Document instance and save the state to History.
        

        //TODO  line sets the font size of the Document instance to 10, but don't save it.
       

        //TODO print the current state of the Document instance to the console.
       

        //TODO restore the Document instance to its previous state by popping the
        // last saved state from History and then print the current state of the Document
        // instance to the console.
        

        //TODO Again, restore the Document instance to its previous state by popping the
        // last saved state from History and then print the current state of the Document
        // instance to the console.
        
    }
}

The output should look like the following:

  • No labels