COMP2396
Loading...
Searching...
No Matches
Main.java
Go to the documentation of this file.
1
import
java.io.*;
2
3
public
class
Main
{
4
5
public
static
void
main
(String[] args)
throws
IOException {
6
BufferedReader input =
new
BufferedReader(
new
InputStreamReader(System.in));
7
String inputLine =
""
;
8
9
VendingMachine
v =
new
VendingMachine
();
10
11
// Adds products to the vending machine
12
v.
addProduct
(
new
Product
(
"Cocacola"
, 4, 1));
// Price: $4, Quantity: 1
13
v.
addProduct
(
new
Product
(
"Pepsi"
, 5, 3));
// Price: $5, Quantity: 3
14
15
System.out.println(
"Welcome to COMP2396B Assignment 3 - Vending Machine"
);
16
17
// Reads user inputs continuously
18
while
(
true
) {
19
inputLine = input.readLine();
20
21
// Split the input line
22
String[] cmdParts = inputLine.split(
" "
);
23
24
Command
cmdObj =
null
;
25
26
if
(cmdParts[0].equalsIgnoreCase(
"Exit"
)) {
27
break
;
28
}
else
if
(cmdParts[0].equalsIgnoreCase(
"Check"
)) {
29
cmdObj =
new
CmdCheckProductInfo
();
30
}
else
if
(cmdParts[0].equalsIgnoreCase(
"Insert"
)) {
31
cmdObj =
new
CmdInsertCoin
();
32
}
else
if
(cmdParts[0].equalsIgnoreCase(
"Reject"
)) {
33
cmdObj =
new
CmdRejectCoins
();
34
}
else
if
(cmdParts[0].equalsIgnoreCase(
"Buy"
)) {
35
cmdObj =
new
CmdPurchase
();
36
}
else
{
37
System.out.println(
"Unknown user command."
);
38
}
39
40
if
(cmdObj !=
null
) {
41
System.out.println(cmdObj.
execute
(v, cmdParts));
42
}
43
44
inputLine =
""
;
45
}
46
47
System.out.println(
"Bye"
);
48
}
49
}
CmdCheckProductInfo
Definition
CmdCheckProductInfo.java:1
CmdInsertCoin
Definition
CmdInsertCoin.java:1
CmdPurchase
Definition
CmdPurchase.java:3
CmdRejectCoins
Definition
CmdRejectCoins.java:3
Main
Definition
Main.java:3
Main.main
static void main(String[] args)
Definition
Main.java:5
Product
Definition
Product.java:1
VendingMachine
Definition
VendingMachine.java:4
VendingMachine.addProduct
void addProduct(Product p)
Definition
VendingMachine.java:16
Command
Definition
Command.java:1
Command.execute
String execute(VendingMachine v, String[] cmdParts)
assignment3
Main.java
Generated on Sat Dec 7 2024 17:10:09 for COMP2396 by
1.12.0