Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class DeckOfCards {
- public static void main(String[] args) {
- Scanner myScan = new Scanner(System.in);
- ArrayList<String> vehicles = new ArrayList<>(Arrays.asList(myScan.nextLine().split(", ")));
- int n = Integer.parseInt(myScan.nextLine());
- for (int i = 1; i <= n; i++) {
- String[] tokens = myScan.nextLine().split(", ");
- String command = tokens[0];
- String cardName = tokens[1];
- int cardIndex = -1;
- String newCard = "";
- if (tokens.length == 3) {
- newCard = tokens[2];
- }
- switch (command) {
- case "Add":
- addCard(vehicles, cardName);
- break;
- case "Remove":
- removeCard(vehicles, cardName);
- break;
- case "Remove At":
- cardIndex = Integer.parseInt(cardName);
- removeCardAt(vehicles, cardIndex);
- break;
- case "Insert":
- cardIndex = Integer.parseInt(cardName);
- if (cardIndex < 0 || cardIndex >= vehicles.size()) {
- System.out.println("Index out of range");
- } else {
- insertCard(vehicles, cardIndex, newCard);
- }
- break;
- }
- }
- String joined = String.join(", ", vehicles);
- System.out.println(joined);
- }
- private static void insertCard(ArrayList<String> vehicles, int cardIndex, String newCard) {
- if (vehicles.contains(newCard)) {
- System.out.println("Card is already bought");
- } else {
- System.out.println("Card successfully bought");
- vehicles.add(cardIndex, newCard);
- }
- }
- private static void removeCardAt(ArrayList<String> vehicles, int cardIndex) {
- if (cardIndex > -1 && cardIndex < vehicles.size()) {
- System.out.println("Card successfully sold");
- vehicles.remove(cardIndex);
- } else {
- System.out.println("Index out of range");
- }
- }
- private static void removeCard(ArrayList<String> vehicles, String cardName) {
- if (!vehicles.contains(cardName)) {
- System.out.println("Card not found");
- } else {
- System.out.println("Card successfully sold");
- vehicles.remove(cardName);
- }
- }
- private static void addCard(ArrayList<String> vehicles, String cardName) {
- if (vehicles.contains(cardName)) {
- System.out.println("Card is already bought");
- } else {
- System.out.println("Card successfully bought");
- vehicles.add(cardName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment