COMP2396
Loading...
Searching...
No Matches
Main.java
Go to the documentation of this file.
1import java.io.*;
2
3public 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
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}
Definition Main.java:3
static void main(String[] args)
Definition Main.java:5
void addProduct(Product p)
String execute(VendingMachine v, String[] cmdParts)