Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ExamPrep {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7.  
  8. String[] inputNames = scan.nextLine().split(", ");
  9.  
  10. List<String> tanks = new ArrayList<>(Arrays.asList(inputNames));
  11.  
  12. int n = Integer.parseInt(scan.nextLine());
  13. while (n-- > 0) {
  14. String[] tokens = scan.nextLine().split(", ");
  15. String command = tokens[0];
  16.  
  17. if (command.equals("Add")) {
  18. AddTank(tanks, tokens[1]);
  19. } else if (command.equals("Remove At")) {
  20. RemoveAtTank(tanks, Integer.parseInt(tokens[1]));
  21. } else if (command.equals("Remove")) {
  22. RemoveTank(tanks, tokens[1]);
  23. } else if (command.equals("Insert")) {
  24. InsertTank(tanks, Integer.parseInt(tokens[1]), tokens[2]);
  25. }
  26. }
  27. for (int i = 0; i < tanks.size() ; i++) {
  28. if (i == tanks.size() - 1) {
  29. System.out.println(tanks.get(i));
  30. } else {
  31. System.out.print(tanks.get(i) + ", ");
  32. }
  33. }
  34. }
  35.  
  36. private static void InsertTank(List<String> tanks, int index, String tankName) {
  37. if (index <= tanks.size()) {
  38. if (tanks.contains(tankName)) {
  39. System.out.println("Tank is already bought");
  40. } else {
  41. for (int i = 0; i < tanks.size() ; i++) {
  42. if (i == index) {
  43. tanks.add(i, tankName);
  44. System.out.println("Tank successfully bought");
  45. }
  46. }
  47. }
  48. } else {
  49. System.out.println("Index out of range");
  50. }
  51. }
  52.  
  53. private static void RemoveAtTank(List<String> tanks, int index) {
  54. if (index <= tanks.size()) {
  55. tanks.remove(index);
  56. System.out.println("Tank successfully sold");
  57. } else {
  58. System.out.println("Index out of range");
  59. }
  60. }
  61.  
  62.  
  63. private static void RemoveTank(List<String> tanks, String tankName) {
  64. if (tanks.contains(tankName)) {
  65. for (int i = 0; i < tanks.size(); i++) {
  66. if (tanks.get(i).equals(tankName)) {
  67. tanks.remove(i);
  68. System.out.println("Tank successfully sold");
  69. break;
  70. }
  71. }
  72. } else {
  73. System.out.println("Tank not found");
  74. }
  75. }
  76.  
  77. private static void AddTank(List<String> tanks, String tankName) {
  78. if (!tanks.contains(tankName)) {
  79. tanks.add(tankName);
  80. System.out.println("Tank successfully bought");
  81. } else {
  82. System.out.println("Tank is already bought");
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement