Didart

Browser History

Dec 27th, 2022
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package StacksAndQueues;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5.  
  6. public class BrowserHistory {
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String input = scanner.nextLine();
  12.  
  13.         String currentURL = null;
  14.  
  15.         ArrayDeque<String> history = new ArrayDeque<>();
  16.  
  17.         while (!input.equals("Home")) {
  18.  
  19.             if (input.equals("back")) {
  20.                 if (history.isEmpty()) {
  21.                     System.out.println("no previous URLs");
  22.                     input = scanner.nextLine();
  23.                     continue;
  24.                 } else {
  25.                     currentURL = history.pop();
  26.                 }
  27.             } else {
  28.                 if (currentURL != null) {
  29.                     history.push(currentURL);
  30.                 }
  31.                 currentURL = input;
  32.             }
  33.             System.out.println(currentURL);
  34.             input = scanner.nextLine();
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment