MiniMi2022

Deck Of Cards

Mar 3rd, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class DeckOfCards {
  4.     public static void main(String[] args) {
  5.         Scanner myScan = new Scanner(System.in);
  6.         ArrayList<String> vehicles = new ArrayList<>(Arrays.asList(myScan.nextLine().split(", ")));
  7.         int n = Integer.parseInt(myScan.nextLine());
  8.         for (int i = 1; i <= n; i++) {
  9.             String[] tokens = myScan.nextLine().split(", ");
  10.             String command = tokens[0];
  11.             String cardName = tokens[1];
  12.             int cardIndex = -1;
  13.             String newCard = "";
  14.             if (tokens.length == 3) {
  15.                 newCard = tokens[2];
  16.             }
  17.             switch (command) {
  18.                 case "Add":
  19.                     addCard(vehicles, cardName);
  20.                     break;
  21.                 case "Remove":
  22.                     removeCard(vehicles, cardName);
  23.                     break;
  24.                 case "Remove At":
  25.                     cardIndex = Integer.parseInt(cardName);
  26.                     removeCardAt(vehicles, cardIndex);
  27.                     break;
  28.                 case "Insert":
  29.                     cardIndex = Integer.parseInt(cardName);
  30.                     if (cardIndex < 0 || cardIndex >= vehicles.size()) {
  31.                         System.out.println("Index out of range");
  32.                     } else {
  33.                         insertCard(vehicles, cardIndex, newCard);
  34.                     }
  35.                     break;
  36.             }
  37.         }
  38.         String joined = String.join(", ", vehicles);
  39.         System.out.println(joined);
  40.     }
  41.  
  42.     private static void insertCard(ArrayList<String> vehicles, int cardIndex, String newCard) {
  43.         if (vehicles.contains(newCard)) {
  44.             System.out.println("Card is already bought");
  45.         } else {
  46.             System.out.println("Card successfully bought");
  47.             vehicles.add(cardIndex, newCard);
  48.         }
  49.     }
  50.  
  51.     private static void removeCardAt(ArrayList<String> vehicles, int cardIndex) {
  52.         if (cardIndex > -1 && cardIndex < vehicles.size()) {
  53.             System.out.println("Card successfully sold");
  54.             vehicles.remove(cardIndex);
  55.         } else {
  56.             System.out.println("Index out of range");
  57.         }
  58.     }
  59.  
  60.     private static void removeCard(ArrayList<String> vehicles, String cardName) {
  61.         if (!vehicles.contains(cardName)) {
  62.             System.out.println("Card not found");
  63.         } else {
  64.             System.out.println("Card successfully sold");
  65.             vehicles.remove(cardName);
  66.         }
  67.     }
  68.  
  69.     private static void addCard(ArrayList<String> vehicles, String cardName) {
  70.         if (vehicles.contains(cardName)) {
  71.             System.out.println("Card is already bought");
  72.         } else {
  73.             System.out.println("Card successfully bought");
  74.             vehicles.add(cardName);
  75.         }
  76.     }
  77.  
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment