RGAlex

O5. Drum Set / Lists - More Exercise

Feb 19th, 2021 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package ListsMoreExercise;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class O5DrumSet {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         double money = Double.parseDouble(scanner.nextLine());
  13.         List<Integer> a = Arrays.stream(scanner.nextLine().split("\\s+"))
  14.                 .map(Integer::parseInt)
  15.                 .collect(Collectors.toList());
  16.  
  17.         List<Integer> priceList = new ArrayList<>(a);
  18.         String command = scanner.nextLine();
  19.  
  20.         while (!command.equals("Hit it again, Gabsy!")) {
  21.  
  22.             for (int i = 0; i < a.size(); i++) {
  23.                 int power = Integer.parseInt(command);
  24.                 a.set(i, a.get(i) - power);
  25.                 if (a.get(i) <= 0) {
  26.                     double price = priceList.get(i) * 3;
  27.                    
  28.                     if (money >= price) {
  29.                         a.set(i, priceList.get(i));
  30.                         money -= price;
  31.                     } else {
  32.                         a.remove(i);
  33.                         priceList.remove(i);
  34.                         i--;
  35.                     }
  36.                 }
  37.             }
  38.             command = scanner.nextLine();
  39.         }
  40.         for (int drum : a) {
  41.             System.out.print(drum + " ");
  42.         }
  43.         System.out.println();
  44.         System.out.printf("Gabsy has %.2flv.", money);
  45.     }
  46. }
  47.  
Add Comment
Please, Sign In to add comment