-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vending Machine.java
48 lines (38 loc) · 1.43 KB
/
Vending Machine.java
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
import java.util.Scanner;
import javax.swing.JOptionPane;
public class vending {
public static void main(String[] args) {
int Dollars, Quaters, Nickels;
Scanner Keyboard = new Scanner(System.in);
JOptionPane.showMessageDialog(null, "Welcome to the vending Machine");
String Price = JOptionPane.showInputDialog("Enter the price of the item");
Double Cost = Double.parseDouble(Price)*100;
String Amount = JOptionPane.showInputDialog("Enter the amount given");
Double Value = Double.parseDouble(Amount)*100;
int Remaining = (int)(Value-Cost);
if(Remaining >= 100) {
Dollars = Math.floorDiv(Remaining, 100);
Remaining = Remaining%100;
Quaters = Math.floorDiv(Remaining,25);
Remaining %= 25;
Nickels = Math.floorDiv(Remaining,10);
Remaining %= 10;
JOptionPane.showMessageDialog(null, Dollars+" Dollars "+Quaters+" Quaters "+Nickels+" Nickels "+ Remaining+" Pennies");
}
else if (Remaining < 100 && Remaining > 0) {
Quaters = Math.floorDiv(Remaining, 25);
Remaining %= 25;
Nickels = Math.floorDiv(Remaining, 10);
Remaining %= 10;
JOptionPane.showMessageDialog(null, "0 Dollars "+Quaters+" Quaters "+Nickels+" Nickels "+Remaining+" Pennies");
}
else if(Remaining == 0) {
System.out.print("No Change Due");
}
else {
System.out.println("Insufficient Amount Given\n"
+"Please Inesert the Remaining Amount: "+Math.abs(Value-Cost)+"Pennies");
}
Keyboard.close();
}
}