Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.Stack;
- /**
- *
- * TODO class to mimic the browsing of a website
- * TODO uses stacks to imitate each webpage, has methods to scroll through
- * each line/webpage
- *
- * @author Sean Lee
- * @version Jan 20, 2020
- * @author Period: TODO
- * @author Assignment: JMCh21_3Browsing
- *
- * @author Sources: TODO
- */
- public class BrowserModel
- {
- private BrowserView view;
- private Stack<Integer> backStk;
- private Stack<Integer> forwardStk;
- private int topLine;
- /**
- * constructor
- * @param view - top of the page
- */
- public BrowserModel(BrowserView view)
- {
- backStk = new Stack<Integer>();
- forwardStk = new Stack<Integer>();
- this.view = view;
- view.update(0);
- backStk.add( 0 );
- }
- /**
- *
- * TODO goes to the back line/link
- */
- public void back()
- {
- if (hasBack())
- {
- int n = backStk.pop();
- forwardStk.add( n );
- topLine = backStk.peek();
- view.update(topLine);
- }
- }
- /**
- *
- * TODO goes to the forward line/link
- */
- public void forward()
- {
- if (hasForward())
- {
- topLine = forwardStk.peek();
- int n = forwardStk.pop();
- backStk.add( n );
- view.update(topLine);
- }
- }
- /**
- *
- * TODO goes to line 0 at the top
- */
- public void home()
- {
- if (topLine != 0)
- {
- backStk.add( 0 );
- topLine = 0;
- view.update(topLine);
- }
- }
- /**
- *
- * TODO goes to the line directed by the link
- * @param n - line to go to
- */
- public void followLink(int n)
- {
- backStk.add( n );
- topLine = n;
- view.update(topLine);
- }
- /**
- *
- * TODO method to see if it is possible to go back
- * @return - true if can go back, false if can't go back
- */
- public boolean hasBack()
- {
- System.out.println("back stack size:" + backStk.size() + backStk);
- if (backStk.size() == 1 && backStk.peek() == 0)
- {
- return false;
- }
- return !backStk.isEmpty();
- }
- /**
- *
- * TODO method to see if it is possible to go forward
- * @return - true if can go forward, false if can't go forward
- */
- public boolean hasForward()
- {
- System.out.println("fwd stack size:" + forwardStk.size() + forwardStk);
- return !forwardStk.isEmpty();
- }
- // The following are for test purposes only
- /**
- *
- * TODO testing method
- * @return backStk
- */
- public Stack<Integer> getBackStk()
- {
- return backStk;
- }
- /**
- *
- * TODO testing method
- * @return forwardStk
- */
- public Stack<Integer> getForwardStk()
- {
- return forwardStk;
- }
- /**
- *
- * TODO testing method
- * @return topline
- */
- public int getTopLine()
- {
- return topLine;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement