Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ExamPrep {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] inputNames = scan.nextLine().split(", ");
- List<String> tanks = new ArrayList<>(Arrays.asList(inputNames));
- int n = Integer.parseInt(scan.nextLine());
- while (n-- > 0) {
- String[] tokens = scan.nextLine().split(", ");
- String command = tokens[0];
- if (command.equals("Add")) {
- AddTank(tanks, tokens[1]);
- } else if (command.equals("Remove At")) {
- RemoveAtTank(tanks, Integer.parseInt(tokens[1]));
- } else if (command.equals("Remove")) {
- RemoveTank(tanks, tokens[1]);
- } else if (command.equals("Insert")) {
- InsertTank(tanks, Integer.parseInt(tokens[1]), tokens[2]);
- }
- }
- for (int i = 0; i < tanks.size() ; i++) {
- if (i == tanks.size() - 1) {
- System.out.println(tanks.get(i));
- } else {
- System.out.print(tanks.get(i) + ", ");
- }
- }
- }
- private static void InsertTank(List<String> tanks, int index, String tankName) {
- if (index <= tanks.size()) {
- if (tanks.contains(tankName)) {
- System.out.println("Tank is already bought");
- } else {
- for (int i = 0; i < tanks.size() ; i++) {
- if (i == index) {
- tanks.add(i, tankName);
- System.out.println("Tank successfully bought");
- }
- }
- }
- } else {
- System.out.println("Index out of range");
- }
- }
- private static void RemoveAtTank(List<String> tanks, int index) {
- if (index <= tanks.size()) {
- tanks.remove(index);
- System.out.println("Tank successfully sold");
- } else {
- System.out.println("Index out of range");
- }
- }
- private static void RemoveTank(List<String> tanks, String tankName) {
- if (tanks.contains(tankName)) {
- for (int i = 0; i < tanks.size(); i++) {
- if (tanks.get(i).equals(tankName)) {
- tanks.remove(i);
- System.out.println("Tank successfully sold");
- break;
- }
- }
- } else {
- System.out.println("Tank not found");
- }
- }
- private static void AddTank(List<String> tanks, String tankName) {
- if (!tanks.contains(tankName)) {
- tanks.add(tankName);
- System.out.println("Tank successfully bought");
- } else {
- System.out.println("Tank is already bought");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement