Advertisement
markopizzy

Untitled

Nov 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Tourist_shop {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. double budget = Double.parseDouble(scanner.nextLine());
  8. String productName = scanner.nextLine();
  9. int productCount = 0;
  10. boolean isFinished = false;
  11. boolean isBigger = false;
  12. double priceTotal = 0;
  13. double priceNeeded = 0;
  14.  
  15. while (!"Stop".equals(productName)) {
  16. double productPrice = Double.parseDouble(scanner.nextLine());
  17. productCount++;
  18.  
  19. if (productCount > 1 && productCount % 3 == 0) {
  20. productPrice *= 0.50;
  21. }
  22. if (productPrice > budget) {
  23. priceNeeded = productPrice - budget;
  24. isBigger = true;
  25. break;
  26. }
  27. budget -= productPrice;
  28.  
  29. if (budget <= 0) {
  30. isFinished = true;
  31. break;
  32. }
  33. priceTotal += productPrice;
  34. productName = scanner.nextLine();
  35. }
  36. if (isBigger) {
  37. System.out.println("You don't have enough money!");
  38. System.out.printf("You need %.2f leva!", priceNeeded);
  39. } else {
  40. if (isFinished) {
  41. System.out.printf("You bought %d products for %.2f leva.", productCount, priceTotal);
  42. } else {
  43. System.out.printf("You bought %d products for %.2f leva.", productCount, priceTotal);
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement