-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandyMachine.java
More file actions
89 lines (73 loc) · 3.06 KB
/
CandyMachine.java
File metadata and controls
89 lines (73 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//Derrick Cheah
//CS 210
//HW Core Topics: Method Calling, Returns, Parameters, Conditional Statements, Print Formatting, Scanner.
//
//This program will create a candy machine that prompts user for money, displays items available, prompts for selection, and determines if user can afford selection.
import java.util.*;
public class CandyMachine {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
initialization();
double money = money(console);
double cost = candy(console);
dispense(money, cost);
}
//Initialize the candy machine program by providing a greeting.
public static void initialization() {
System.out.println("Welcome to Bellevue College's Computer Candy Machine!");
System.out.println("(All candy provided is virtual.)");
System.out.println();
}
//Asks user how much money they have.
public static double money(Scanner console) {
System.out.print("How much money do ya got? > $");
double money = console.nextDouble();
while (!(money >= 0)) {
System.out.print("Please enter a valid amount. > ");
money = console.nextDouble();
}
System.out.println();
System.out.printf("$%.2f, that's all?\n", money);
System.out.println();
return money;
}
//Provides all items available in the candy machine, prompts for selection, and returns cost of selection.
public static double candy(Scanner console) {
System.out.println("Well, lemme tell ya what we got here.");
System.out.println("A $0.65 Twix");
System.out.println("B $0.50 Chips");
System.out.println("C $0.75 Nutter Butter");
System.out.println("D $0.65 Peanut Butter Cup");
System.out.println("E $0.55 Juicy Fruit Gum");
System.out.println();
System.out.print("So, What'll ya have? > ");
String choice = console.next();
while (!(choice.equals("a") || choice.equals("b") || choice.equals("c") || choice.equals("d") || choice.equals("e"))) {
System.out.print("Please enter a valid option. > ");
choice = console.next();
}
System.out.println();
if (choice.toLowerCase().equals("a")) {
return 0.65;
} else if (choice.toLowerCase().equals("b")) {
return 0.50;
} else if (choice.toLowerCase().equals("c")) {
return 0.75;
} else if (choice.toLowerCase().equals("d")) {
return 0.65;
} else if (choice.toLowerCase().equals("e")) {
return 0.55;
}
return 0;
}
//Checks to see if user can afford the selected candy and provides suitable output.
public static void dispense(double money, double cost) {
if (money >= cost) {
double change = money - cost;
System.out.println("Thanks for purchasing candy through us.");
System.out.printf("Please take your candy and your $%.2f change!", change);
} else {
System.out.printf("You're broke. Take your $%.2f and go elsewhere.\n", money);
}
}
}