Advertisement
Guest User

TaskA

a guest
Nov 13th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class TaskA {
  5.  
  6.     public static Stack<Car> stack = new Stack<>();
  7.    
  8.     public static void add(int count, String load) {
  9.             stack.push(new Car(count, load));
  10.     }
  11.  
  12.     public static void delete(int count) {
  13.         while (count!=0) {
  14.             if (stack.peek().count<count) {
  15.                 count-=stack.peek().count;
  16.                 stack.pop();
  17.             } else {
  18.                 stack.peek().count-=count;
  19.                 count=0;
  20.             }
  21.         }
  22.     }
  23.  
  24.     public static int get(String load) {
  25.         int count = 0;
  26.         for (int i = 0; i < stack.size(); i++) {
  27.             if (stack.get(i).load.equals(load)) {
  28.                 count+=stack.get(i).count;
  29.             }
  30.         }
  31.         return count;
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         Scanner in = new Scanner(System.in);
  36.         int n = in.nextInt();
  37.         for (int i = 0; i<=n; i++) {
  38.             String str=in.nextLine();
  39.             String[] split = str.split(" ");
  40.             if (split[0].equals("add")) {
  41.                 add(Integer.parseInt(split[1]), split[2]);
  42.                 continue;
  43.             }
  44.             if (split[0].equals("delete")) {
  45.                 delete(Integer.parseInt(split[1]));
  46.                 continue;
  47.             }
  48.             if (split[0].equals("get")) {
  49.                 System.out.println(get(split[1]));
  50.                 continue;
  51.             }
  52.         }
  53.  
  54.     }
  55.     public static class Car {
  56.         int count;
  57.         String load;
  58.         public Car(int count, String load) {
  59.             this.count=count;
  60.             this.load=load;
  61.         }
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement