Advertisement
IrinaIgnatova

Easter Gifts

Jun 26th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String[] input = scanner.nextLine().split(" +");
  13.         List<String> gifts = new ArrayList<>();
  14.         for (int i = 0; i < input.length; i++) {
  15.             gifts.add(input[i]);
  16.         }
  17.  
  18.         String command = scanner.nextLine();
  19.  
  20.         while (!command.equals("No Money")) {
  21.             String[] tokens = command.split(" +");
  22.  
  23.             if (command.contains("OutOfStock")) {
  24.                 for (int i = 0; i < gifts.size(); i++) {
  25.                     String currentGift = gifts.get(i);
  26.                     String giftToNone = tokens[1];
  27.                     if (currentGift.equals(giftToNone)) {
  28.                         gifts.set(i,"None");
  29.                     }
  30.  
  31.  
  32.                 }
  33.  
  34.  
  35.             } else if (command.contains("Required")) {
  36.                 for (int i = 0; i < gifts.size(); i++) {
  37.                     String currentGift = gifts.get(i);
  38.                     String giftToReplaceWith = tokens[1];
  39.                     int index = Integer.valueOf(tokens[2]);
  40.  
  41.                     if (index >= 0 && index < gifts.size()) {
  42.                         currentGift = gifts.set(index, giftToReplaceWith);
  43.                     }
  44.                 }
  45.  
  46.             } else if (command.contains("JustInCase")) {//firdt second third   third second first
  47.                 for (int i = 0; i < gifts.size(); i++) {
  48.                     String giftToAdd = tokens[1];
  49.                     String currentGift = gifts.get(i);
  50.                     String lastGift = gifts.get(gifts.size() - 1);
  51.                    gifts.remove(lastGift);
  52.                    gifts.add(giftToAdd);
  53.  
  54.  
  55.  
  56.                 }
  57.             }
  58.             command = scanner.nextLine();
  59.         }
  60.  
  61.         for (int i = 0; i < gifts.size(); i++) {
  62.             String currentGift=gifts.get(i);
  63.             if(!currentGift.equals("None")){
  64.                 System.out.print(gifts.get(i)+" ");
  65.             }
  66.         }
  67.  
  68.     }
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement