Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package com.scalefocus;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. // write your code here
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. String input = "";
  12. double insertedCoin = 0;
  13. double productPrice = 0;
  14. double insertedSum = 0;
  15. double change = 0;
  16.  
  17. while (!input.equals("Start")) {
  18. input = sc.nextLine();
  19. if (input.equals("Start")) {
  20. input = "Start";
  21. } else {
  22. insertedCoin = Double.parseDouble(input);
  23. if (insertedCoin != 0.1 && insertedCoin != 0.2 && insertedCoin != 0.5 && insertedCoin != 1 && insertedCoin != 2) {
  24. System.out.printf("Cannot accept %.2f%n", insertedCoin);
  25. } else {
  26. insertedSum += insertedCoin;
  27. }
  28. }
  29. }
  30.  
  31. while (!input.equals("End")) {
  32. input = sc.nextLine();
  33. if (!input.equals("End")) {
  34. switch (input) {
  35. case "Nuts":
  36. productPrice = 2.0;
  37. break;
  38. case "Water":
  39. productPrice = 0.7;
  40. break;
  41. case "Crisps":
  42. productPrice = 1.5;
  43. break;
  44. case "Soda":
  45. productPrice = 0.8;
  46. break;
  47. case "Coke":
  48. productPrice = 1.0;
  49. break;
  50. default:
  51. System.out.println("Invalid product");
  52. break;
  53. }
  54.  
  55. if (insertedSum - productPrice < 0) {
  56. System.out.println("Sorry, not enough money");
  57. // else if (input.equals("Nuts") || input.equals("Water") || input.equals("Crisps") || input.equals("Soda") || input.equals("Coke")
  58. } else if (input.matches("Nuts|Water|Crisps|Soda|Coke")) {
  59. System.out.println("Purchased " + input);
  60. insertedSum = insertedSum - productPrice;
  61. }
  62. }
  63. }
  64. System.out.printf("Change: %.2f", insertedSum);
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement