Advertisement
aneliabogeva

VendingMachine

Jan 12th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package main;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class VendingMachine {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String command = scanner.nextLine();
  9. double sum = 0;
  10. String message = null;
  11.  
  12. while(!command.equals("Start")){
  13. double cns = Double.parseDouble(command);
  14. if((cns != 0.1) && (cns!= 0.2) && (cns!= 0.5) && (cns!= 1.0) && (cns!= 2.0)){
  15. System.out.printf("Cannot accept %.2f\n", cns);
  16. }else{
  17. sum += cns;
  18. }
  19. command = scanner.nextLine();
  20. }
  21.  
  22. command = scanner.nextLine();
  23. while(!command.equals("End")){
  24. switch(command){
  25. case "Nuts":
  26. if(sum < 2.0){
  27. System.out.println("Sorry, not enough money");
  28. }else{
  29. System.out.printf("Purchased %s\n",command);
  30. sum -= 2.0;
  31. }
  32. break;
  33. case "Water":
  34. if(sum < 0.7){
  35. System.out.println("Sorry, not enough money");
  36. }else{
  37. System.out.printf("Purchased %s\n",command);
  38. sum -= 0.7;
  39. }
  40. break;
  41. case "Crisps":
  42. if(sum < 1.5){
  43. System.out.println("Sorry, not enough money");
  44. }else{
  45. System.out.printf("Purchased %s\n",command);
  46. sum -= 1.5;
  47. }
  48. break;
  49. case "Soda":
  50. if(sum < 0.8){
  51. System.out.println("Sorry, not enough money");
  52. }else{
  53. System.out.printf("Purchased %s\n",command);
  54. sum -= 0.8;
  55. }
  56. break;
  57. case "Coke":
  58. if(sum < 1.0){
  59. System.out.println("Sorry, not enough money");
  60. }else{
  61. System.out.printf("Purchased %s\n",command);
  62. sum -= 1.0;
  63. }
  64. break;
  65. default:
  66. System.out.println("Invalid product");
  67. }
  68. command = scanner.nextLine();
  69. }
  70. if(sum >= 0){
  71. System.out.printf("Change: %.2f", sum);
  72. }
  73. }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement