Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Stack;
  3.  
  4. /**
  5. *
  6. * TODO class to mimic the browsing of a website
  7. * TODO uses stacks to imitate each webpage, has methods to scroll through
  8. * each line/webpage
  9. *
  10. * @author Sean Lee
  11. * @version Jan 20, 2020
  12. * @author Period: TODO
  13. * @author Assignment: JMCh21_3Browsing
  14. *
  15. * @author Sources: TODO
  16. */
  17. public class BrowserModel
  18. {
  19. private BrowserView view;
  20. private Stack<Integer> backStk;
  21. private Stack<Integer> forwardStk;
  22. private int topLine;
  23.  
  24. /**
  25. * constructor
  26. * @param view - top of the page
  27. */
  28. public BrowserModel(BrowserView view)
  29. {
  30. backStk = new Stack<Integer>();
  31. forwardStk = new Stack<Integer>();
  32. this.view = view;
  33. view.update(0);
  34. backStk.add( 0 );
  35. }
  36.  
  37. /**
  38. *
  39. * TODO goes to the back line/link
  40. */
  41. public void back()
  42. {
  43. if (hasBack())
  44. {
  45. int n = backStk.pop();
  46. forwardStk.add( n );
  47. topLine = backStk.peek();
  48. view.update(topLine);
  49. }
  50. }
  51.  
  52. /**
  53. *
  54. * TODO goes to the forward line/link
  55. */
  56. public void forward()
  57. {
  58. if (hasForward())
  59. {
  60. topLine = forwardStk.peek();
  61. int n = forwardStk.pop();
  62. backStk.add( n );
  63. view.update(topLine);
  64. }
  65. }
  66.  
  67. /**
  68. *
  69. * TODO goes to line 0 at the top
  70. */
  71. public void home()
  72. {
  73. if (topLine != 0)
  74. {
  75. backStk.add( 0 );
  76. topLine = 0;
  77. view.update(topLine);
  78. }
  79. }
  80.  
  81. /**
  82. *
  83. * TODO goes to the line directed by the link
  84. * @param n - line to go to
  85. */
  86. public void followLink(int n)
  87. {
  88. backStk.add( n );
  89. topLine = n;
  90. view.update(topLine);
  91. }
  92.  
  93. /**
  94. *
  95. * TODO method to see if it is possible to go back
  96. * @return - true if can go back, false if can't go back
  97. */
  98. public boolean hasBack()
  99. {
  100. System.out.println("back stack size:" + backStk.size() + backStk);
  101. if (backStk.size() == 1 && backStk.peek() == 0)
  102. {
  103. return false;
  104. }
  105. return !backStk.isEmpty();
  106. }
  107.  
  108. /**
  109. *
  110. * TODO method to see if it is possible to go forward
  111. * @return - true if can go forward, false if can't go forward
  112. */
  113. public boolean hasForward()
  114. {
  115. System.out.println("fwd stack size:" + forwardStk.size() + forwardStk);
  116. return !forwardStk.isEmpty();
  117. }
  118.  
  119. // The following are for test purposes only
  120. /**
  121. *
  122. * TODO testing method
  123. * @return backStk
  124. */
  125. public Stack<Integer> getBackStk()
  126. {
  127. return backStk;
  128. }
  129.  
  130. /**
  131. *
  132. * TODO testing method
  133. * @return forwardStk
  134. */
  135. public Stack<Integer> getForwardStk()
  136. {
  137. return forwardStk;
  138. }
  139.  
  140. /**
  141. *
  142. * TODO testing method
  143. * @return topline
  144. */
  145. public int getTopLine()
  146. {
  147. return topLine;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement