Skip to content

Instantly share code, notes, and snippets.

@AleksandarBoev
Created January 17, 2020 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AleksandarBoev/ee8c1d9460932fd53510d9492ae309db to your computer and use it in GitHub Desktop.
Save AleksandarBoev/ee8c1d9460932fd53510d9492ae309db to your computer and use it in GitHub Desktop.
Order History Upgrade
package PastCourse.StacksAndQueues;
import java.util.ArrayDeque;
import java.util.Scanner;
public class BrowserHistoryUpgrade {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayDeque<String> browserHistory = new ArrayDeque<>();
ArrayDeque<String> forwardHistory = new ArrayDeque<>();
String command;
while (!"Home".equals(command = scanner.nextLine())) {
if (command.equals("back")) {
if (browserHistory.size() < 2) {
System.out.println("no previous URLs");
} else {
forwardHistory.addFirst(browserHistory.peek());
browserHistory.pop();
System.out.println(browserHistory.peek());
}
} else if (command.equals("forward")) {
if (forwardHistory.size() < 1) {
System.out.println("no next URLs");
} else {
System.out.println(forwardHistory.peek());
browserHistory.push(forwardHistory.pop());
}
} else {
System.out.println(command);
browserHistory.push(command);
forwardHistory.clear();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment