Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.sql.Timestamp;
- public class Browser {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> tabs = new ArrayList<>();
- List<Long> timestamps = new ArrayList<>();
- String blankTab = "about:blank";
- Timestamp timestamp = new Timestamp(System.currentTimeMillis());
- long ts = timestamp.getTime();
- tabs.add(blankTab);
- timestamps.add(ts);
- String currentTab = blankTab;
- String input = scanner.nextLine();
- while (!input.equals("END")) {
- String[] tokens = input.split(" ");
- String command = tokens[0];
- switch (command) {
- case "GO":
- int index = tabs.indexOf(currentTab);
- currentTab = tokens[1];
- timestamp = new Timestamp(System.currentTimeMillis());
- ts = timestamp.getTime();
- tabs.set(index, currentTab);
- timestamps.set(index, ts);
- break;
- case "INSERT":
- index = tabs.indexOf(currentTab);
- currentTab = tokens[1];
- timestamp = new Timestamp(System.currentTimeMillis());
- ts = timestamp.getTime();
- tabs.add(index + 1, currentTab);
- timestamps.add(index + 1, ts);
- break;
- case "BACK":
- index = tabs.indexOf(currentTab);
- if (index > 0) {
- currentTab = tabs.get(index - 1);
- }
- break;
- case "FORWARD":
- index = tabs.indexOf(currentTab);
- if (index != tabs.size() - 1) {
- currentTab = tabs.get(index + 1);
- }
- break;
- case "REMOVE":
- index = tabs.indexOf(currentTab);
- if (index < tabs.size() - 1) {
- currentTab = tabs.get(index + 1);
- tabs.remove(index);
- timestamps.remove(index);
- } else if (index > 0) {
- currentTab = tabs.get(0);
- tabs.remove(index);
- timestamps.remove(index);
- } else {
- tabs.clear();
- timestamps.clear();
- tabs.add(blankTab);
- timestamp = new Timestamp(System.currentTimeMillis());
- ts = timestamp.getTime();
- timestamps.add(ts);
- }
- break;
- case "PRINT":
- for (int i = 0; i < tabs.size(); i++) {
- if (tabs.get(i).equals(currentTab)) {
- System.out.printf(">%s %d%n", tabs.get(i), timestamps.get(i));
- } else {
- System.out.printf("%s %d%n", tabs.get(i), timestamps.get(i));
- }
- }
- break;
- default:
- break;
- }
- input = scanner.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment