kalinikov

Browser

Dec 15th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.Timestamp;
  3.  
  4. public class Browser {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. List<String> tabs = new ArrayList<>();
  9. List<Long> timestamps = new ArrayList<>();
  10. String blankTab = "about:blank";
  11. Timestamp timestamp = new Timestamp(System.currentTimeMillis());
  12. long ts = timestamp.getTime();
  13.  
  14. tabs.add(blankTab);
  15. timestamps.add(ts);
  16.  
  17. String currentTab = blankTab;
  18.  
  19. String input = scanner.nextLine();
  20.  
  21. while (!input.equals("END")) {
  22. String[] tokens = input.split(" ");
  23. String command = tokens[0];
  24. switch (command) {
  25. case "GO":
  26. int index = tabs.indexOf(currentTab);
  27. currentTab = tokens[1];
  28. timestamp = new Timestamp(System.currentTimeMillis());
  29. ts = timestamp.getTime();
  30. tabs.set(index, currentTab);
  31. timestamps.set(index, ts);
  32. break;
  33. case "INSERT":
  34. index = tabs.indexOf(currentTab);
  35. currentTab = tokens[1];
  36. timestamp = new Timestamp(System.currentTimeMillis());
  37. ts = timestamp.getTime();
  38. tabs.add(index + 1, currentTab);
  39. timestamps.add(index + 1, ts);
  40. break;
  41. case "BACK":
  42. index = tabs.indexOf(currentTab);
  43. if (index > 0) {
  44. currentTab = tabs.get(index - 1);
  45. }
  46. break;
  47. case "FORWARD":
  48. index = tabs.indexOf(currentTab);
  49. if (index != tabs.size() - 1) {
  50. currentTab = tabs.get(index + 1);
  51. }
  52. break;
  53. case "REMOVE":
  54. index = tabs.indexOf(currentTab);
  55. if (index < tabs.size() - 1) {
  56. currentTab = tabs.get(index + 1);
  57. tabs.remove(index);
  58. timestamps.remove(index);
  59. } else if (index > 0) {
  60. currentTab = tabs.get(0);
  61. tabs.remove(index);
  62. timestamps.remove(index);
  63. } else {
  64. tabs.clear();
  65. timestamps.clear();
  66. tabs.add(blankTab);
  67. timestamp = new Timestamp(System.currentTimeMillis());
  68. ts = timestamp.getTime();
  69. timestamps.add(ts);
  70. }
  71. break;
  72. case "PRINT":
  73. for (int i = 0; i < tabs.size(); i++) {
  74. if (tabs.get(i).equals(currentTab)) {
  75. System.out.printf(">%s %d%n", tabs.get(i), timestamps.get(i));
  76. } else {
  77. System.out.printf("%s %d%n", tabs.get(i), timestamps.get(i));
  78. }
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84.  
  85. input = scanner.nextLine();
  86. }
  87.  
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment