Advertisement
yovkovbpfps

EXAM 20 - 21 APRIL 04.EASTER SHOP

Apr 30th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class EasterShop {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int initialEggAmount = Integer.parseInt(scanner.nextLine());
  8.  
  9. int soldEggs = 0;
  10.  
  11. String command = "";
  12. while (!"Close".equals(command = scanner.nextLine())) {
  13.  
  14. int amount = Integer.parseInt(scanner.nextLine());
  15.  
  16. switch (command) {
  17. case "Buy":
  18. soldEggs += amount;
  19. int eggsNow = initialEggAmount;
  20. initialEggAmount -= amount;
  21. if (initialEggAmount < 0) {
  22. System.out.println("Not enough eggs in store!");
  23. System.out.printf("You can buy only %d.%n", Math.abs(eggsNow));
  24. return;
  25. }
  26. break;
  27. case "Fill":
  28. initialEggAmount += amount;
  29. break;
  30. }
  31. }
  32.  
  33. System.out.println("Store is closed!");
  34. System.out.printf("%d eggs sold.%n", soldEggs);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement