Advertisement
Merry123

Untitled

Dec 19th, 2022
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. package Lab;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class BrowserHistoryUpgrade {
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         String command = sc.nextLine();
  12.  
  13.         String currentURL = "Blank";
  14.         ArrayDeque<String> historyStack = new ArrayDeque<>();
  15.         ArrayDeque<String> forwardQueue = new ArrayDeque<>();
  16.  
  17.  
  18.         while (!command.equals("Home")) {
  19.  
  20.             if (command.equals("back")) {
  21.                 if (!historyStack.isEmpty()) {
  22.                     currentURL = historyStack.pop();
  23.  
  24.                 } else {
  25.                     System.out.println("no previous URLs");
  26.                     command = sc.nextLine();
  27.                     continue;
  28.                 }
  29.             } else if (command.equals("forward")) {
  30.                 if (!forwardQueue.isEmpty()) {
  31.                     currentURL = forwardQueue.poll();
  32.  
  33.                 } else {
  34.                     System.out.println("no next URLs");
  35.                     command = sc.nextLine();
  36.                     continue;
  37.                 }
  38.  
  39.             } else {
  40.                 if (!currentURL.equals("Blank")) {
  41.                     historyStack.push(currentURL);
  42.                     forwardQueue.offer(command);
  43.                 }
  44.                 currentURL = command;
  45.             }
  46.             System.out.println(currentURL);
  47.             command = sc.nextLine();
  48.         }
  49.     }
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement