Created
January 17, 2020 07:28
-
-
Save AleksandarBoev/ee8c1d9460932fd53510d9492ae309db to your computer and use it in GitHub Desktop.
Order History Upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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