From 5a81a04720c634be8625e6be8a81537f954e473c Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Tue, 17 Sep 2024 16:43:30 +0800 Subject: [PATCH] Tutorial 3 --- T3Q1-1/src/VendingMachineApp.java | 14 +++ T3Q1-2/src/VendingMachine.java | 104 +++++++++++++++++++++ T3Q1-2/src/VendingMachineApp.java | 14 +++ T3Q1-3/src/VendingMachine.java | 117 +++++++++++++++++++++++ T3Q1-3/src/VendingMachineApp.java | 13 +++ T3Q1-3/src/drinks/Beer.java | 10 ++ T3Q1-3/src/drinks/Coke.java | 10 ++ T3Q1-3/src/drinks/Drink.java | 19 ++++ T3Q1-3/src/drinks/GreenTea.java | 10 ++ T3Q1-4/src/VendingMachine.java | 124 +++++++++++++++++++++++++ T3Q1-4/src/VendingMachineApp.java | 9 ++ T3Q1-4/src/drinks/Beer.java | 10 ++ T3Q1-4/src/drinks/Coke.java | 10 ++ T3Q1-4/src/drinks/Drink.java | 19 ++++ T3Q1-4/src/drinks/GreenTea.java | 10 ++ T3Q1-extra1/src/VendingMachine.java | 100 ++++++++++++++++++++ T3Q1-extra1/src/VendingMachineApp.java | 9 ++ T3Q1-extra1/src/coins/Coin.java | 30 ++++++ T3Q1-extra1/src/coins/Dollar.java | 13 +++ T3Q1-extra1/src/coins/Fifty.java | 13 +++ T3Q1-extra1/src/coins/Ten.java | 13 +++ T3Q1-extra1/src/coins/Twenty.java | 13 +++ T3Q1-extra1/src/drinks/Beer.java | 11 +++ T3Q1-extra1/src/drinks/Coke.java | 11 +++ T3Q1-extra1/src/drinks/Drink.java | 23 +++++ T3Q1-extra1/src/drinks/GreenTea.java | 11 +++ T3Q2-1/T3Q2-1.iml | 11 +++ T3Q2-1/src/Circle.java | 14 +++ T3Q2-1/src/Cylinder.java | 17 ++++ T3Q2-1/src/Test.java | 21 +++++ T3Q2-2/src/Circle.java | 14 +++ T3Q2-2/src/Cylinder.java | 18 ++++ T3Q2-2/src/Point.java | 13 +++ T3Q2-2/src/Test.java | 21 +++++ T3Q2-3/src/Circle.java | 14 +++ T3Q2-3/src/Cylinder.java | 17 ++++ T3Q2-3/src/Point.java | 14 +++ T3Q2-3/src/Test.java | 21 +++++ T3Q2-4/src/Circle.java | 15 +++ T3Q2-4/src/Cylinder.java | 17 ++++ T3Q2-4/src/Point.java | 13 +++ T3Q2-4/src/Test.java | 21 +++++ 42 files changed, 1001 insertions(+) create mode 100644 T3Q1-1/src/VendingMachineApp.java create mode 100644 T3Q1-2/src/VendingMachine.java create mode 100644 T3Q1-2/src/VendingMachineApp.java create mode 100644 T3Q1-3/src/VendingMachine.java create mode 100644 T3Q1-3/src/VendingMachineApp.java create mode 100644 T3Q1-3/src/drinks/Beer.java create mode 100644 T3Q1-3/src/drinks/Coke.java create mode 100644 T3Q1-3/src/drinks/Drink.java create mode 100644 T3Q1-3/src/drinks/GreenTea.java create mode 100644 T3Q1-4/src/VendingMachine.java create mode 100644 T3Q1-4/src/VendingMachineApp.java create mode 100644 T3Q1-4/src/drinks/Beer.java create mode 100644 T3Q1-4/src/drinks/Coke.java create mode 100644 T3Q1-4/src/drinks/Drink.java create mode 100644 T3Q1-4/src/drinks/GreenTea.java create mode 100644 T3Q1-extra1/src/VendingMachine.java create mode 100644 T3Q1-extra1/src/VendingMachineApp.java create mode 100644 T3Q1-extra1/src/coins/Coin.java create mode 100644 T3Q1-extra1/src/coins/Dollar.java create mode 100644 T3Q1-extra1/src/coins/Fifty.java create mode 100644 T3Q1-extra1/src/coins/Ten.java create mode 100644 T3Q1-extra1/src/coins/Twenty.java create mode 100644 T3Q1-extra1/src/drinks/Beer.java create mode 100644 T3Q1-extra1/src/drinks/Coke.java create mode 100644 T3Q1-extra1/src/drinks/Drink.java create mode 100644 T3Q1-extra1/src/drinks/GreenTea.java create mode 100644 T3Q2-1/T3Q2-1.iml create mode 100644 T3Q2-1/src/Circle.java create mode 100644 T3Q2-1/src/Cylinder.java create mode 100644 T3Q2-1/src/Test.java create mode 100644 T3Q2-2/src/Circle.java create mode 100644 T3Q2-2/src/Cylinder.java create mode 100644 T3Q2-2/src/Point.java create mode 100644 T3Q2-2/src/Test.java create mode 100644 T3Q2-3/src/Circle.java create mode 100644 T3Q2-3/src/Cylinder.java create mode 100644 T3Q2-3/src/Point.java create mode 100644 T3Q2-3/src/Test.java create mode 100644 T3Q2-4/src/Circle.java create mode 100644 T3Q2-4/src/Cylinder.java create mode 100644 T3Q2-4/src/Point.java create mode 100644 T3Q2-4/src/Test.java diff --git a/T3Q1-1/src/VendingMachineApp.java b/T3Q1-1/src/VendingMachineApp.java new file mode 100644 index 0000000..85d0c3c --- /dev/null +++ b/T3Q1-1/src/VendingMachineApp.java @@ -0,0 +1,14 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + double drinkCost = 0; + double amountInserted = 0.0; + + VendingMachine vendingMachine = new VendingMachine(); + drinkCost = vendingMachine.selectDrink(); + amountInserted = vendingMachine.insertCoins(drinkCost); + vendingMachine.checkChange(amountInserted, drinkCost); + vendingMachine.printReceipt(); + } +} diff --git a/T3Q1-2/src/VendingMachine.java b/T3Q1-2/src/VendingMachine.java new file mode 100644 index 0000000..1c1e393 --- /dev/null +++ b/T3Q1-2/src/VendingMachine.java @@ -0,0 +1,104 @@ +import java.util.Scanner; + +public class VendingMachine { + + public VendingMachine() {} + + public double selectDrink() { + int drinkSelection; + double drinkCost = 0; + + drinkSelection = presentDrinkMenu(); + + if (drinkSelection == 1) drinkCost = 3.00; + else if (drinkSelection == 2) drinkCost = 1.00; + else if (drinkSelection == 3) drinkCost = 5.00; + return drinkCost; + } + + public double insertCoins(double drinkCost) { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + printCoinOptions(); + + do { + char coin = sc.next().charAt(0); + amount += processCoinAmount(coin); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < drinkCost); + return amount; + } + + public void checkChange(double amount, double drinkCost) + { + double change = 0.0; + if (amount > drinkCost) + { + change = amount - drinkCost; + System.out.printf("Change $ %.2f \n", change); + } + } + + public void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private int presentDrinkMenu() { + Scanner sc = new Scanner(System.in); + + int drinkSelection; + + System.out.println("====== Vending Machine ======"); + System.out.println("|1. Buy Beer ($3.00) |"); + System.out.println("|2. Buy Coke ($1.00) |"); + System.out.println("|3. Buy Green Tea ($5.00) |"); + System.out.println("|============================"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt(); + } while (drinkSelection < 1 || drinkSelection > 3); + + return drinkSelection; + } + + private void printCoinOptions() { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + } + + private double processCoinAmount(char coin) { + /* + Note. The code below is just an enhanced way of writing the following: + + switch (coin) + { + case 'Q': case 'q': return 0.10; + case 'T': case 't': return 0.20; + case 'F': case 'f': return 0.50; + case 'N': case 'n': return 1.00; + } + return 0.0; + + This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); + The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp + + The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html + */ + return switch (coin) { + case 'Q', 'q' -> 0.10; + case 'T', 't' -> 0.20; + case 'F', 'f' -> 0.50; + case 'N', 'n' -> 1.00; + default -> 0.0; + }; + } +} \ No newline at end of file diff --git a/T3Q1-2/src/VendingMachineApp.java b/T3Q1-2/src/VendingMachineApp.java new file mode 100644 index 0000000..85d0c3c --- /dev/null +++ b/T3Q1-2/src/VendingMachineApp.java @@ -0,0 +1,14 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + double drinkCost = 0; + double amountInserted = 0.0; + + VendingMachine vendingMachine = new VendingMachine(); + drinkCost = vendingMachine.selectDrink(); + amountInserted = vendingMachine.insertCoins(drinkCost); + vendingMachine.checkChange(amountInserted, drinkCost); + vendingMachine.printReceipt(); + } +} diff --git a/T3Q1-3/src/VendingMachine.java b/T3Q1-3/src/VendingMachine.java new file mode 100644 index 0000000..7b92f16 --- /dev/null +++ b/T3Q1-3/src/VendingMachine.java @@ -0,0 +1,117 @@ +import java.util.Scanner; +/* +You could also use "import drinks.*" instead of the 4 lines below + */ +import drinks.Beer; +import drinks.Coke; +import drinks.Drink; +import drinks.GreenTea; + +public class VendingMachine { + private static final Drink[] availableDrinks = new Drink[3]; + int drinkSelection = 0; + + public VendingMachine() { + availableDrinks[0] = new Beer(); + availableDrinks[1] = new Coke(); + availableDrinks[2] = new GreenTea(); + } + + public double selectDrink() { + drinkSelection = presentDrinkMenu() - 1; // This " - 1 " is to keep "drinkSelection" within the "availableDrinks" array range + return availableDrinks[drinkSelection].getPrice(); + } + + public double insertCoins() { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + printCoinOptions(); + + do { + char coin = sc.next().charAt(0); + amount += processCoinAmount(coin); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < availableDrinks[drinkSelection].getPrice()); + return amount; + } + + public void checkChange(double amount) + { + double change = 0.0; + if (amount > availableDrinks[drinkSelection].getPrice()) + { + change = amount - availableDrinks[drinkSelection].getPrice(); + System.out.printf("Change $ %.2f \n", change); + } + } + + public void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private Integer presentDrinkMenu() { + Scanner sc = new Scanner(System.in); + int drinkSelection; + + System.out.print("====== Vending Machine ====== \n"); + /* + Note. The for loop below is just an enhanced way of writing the following: + + for (int i = 0; i < availableDrinks.length; i++) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i + 1, availableDrinks[i].getName(), availableDrinks[i].getPrice()); + } + */ + int i = 1; + for (Drink drink : availableDrinks) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); + } + System.out.print("|============================ \n"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt(); + } while (drinkSelection < 1 || drinkSelection > (availableDrinks.length + 1)); + + return drinkSelection; + } + + private void printCoinOptions() { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + } + + private double processCoinAmount(char coin) { + /* + Note. The code below is just an enhanced way of writing the following: + + switch (coin) + { + case 'Q': case 'q': return 0.10; + case 'T': case 't': return 0.20; + case 'F': case 'f': return 0.50; + case 'N': case 'n': return 1.00; + } + return 0.0; + + This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); + The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp + + The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html + */ + return switch (coin) { + case 'Q', 'q' -> 0.10; + case 'T', 't' -> 0.20; + case 'F', 'f' -> 0.50; + case 'N', 'n' -> 1.00; + default -> 0.0; + }; + } +} \ No newline at end of file diff --git a/T3Q1-3/src/VendingMachineApp.java b/T3Q1-3/src/VendingMachineApp.java new file mode 100644 index 0000000..7f2a53f --- /dev/null +++ b/T3Q1-3/src/VendingMachineApp.java @@ -0,0 +1,13 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + double amountInserted = 0.0; + + VendingMachine vendingMachine = new VendingMachine(); + vendingMachine.selectDrink(); + amountInserted = vendingMachine.insertCoins(); + vendingMachine.checkChange(amountInserted); + vendingMachine.printReceipt(); + } +} diff --git a/T3Q1-3/src/drinks/Beer.java b/T3Q1-3/src/drinks/Beer.java new file mode 100644 index 0000000..0f65611 --- /dev/null +++ b/T3Q1-3/src/drinks/Beer.java @@ -0,0 +1,10 @@ +package drinks; + +public class Beer extends Drink { + private static final String NAME = "Beer"; + private static final double PRICE = 3.0; + + public Beer() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-3/src/drinks/Coke.java b/T3Q1-3/src/drinks/Coke.java new file mode 100644 index 0000000..0469a7f --- /dev/null +++ b/T3Q1-3/src/drinks/Coke.java @@ -0,0 +1,10 @@ +package drinks; + +public class Coke extends Drink { + private static final String NAME = "Coke"; + private static final double PRICE = 1.0; + + public Coke() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-3/src/drinks/Drink.java b/T3Q1-3/src/drinks/Drink.java new file mode 100644 index 0000000..c346f83 --- /dev/null +++ b/T3Q1-3/src/drinks/Drink.java @@ -0,0 +1,19 @@ +package drinks; + +public class Drink { + private String name; + private double price; + + public Drink(String name, double price) { + this.name = name; + this.price = price; + } + + public double getPrice() { + return price; + } + + public String getName() { + return name; + } +} diff --git a/T3Q1-3/src/drinks/GreenTea.java b/T3Q1-3/src/drinks/GreenTea.java new file mode 100644 index 0000000..c53fd7c --- /dev/null +++ b/T3Q1-3/src/drinks/GreenTea.java @@ -0,0 +1,10 @@ +package drinks; + +public class GreenTea extends Drink { + private static final String NAME = "Green Tea"; + private static final double PRICE = 5.0; + + public GreenTea() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-4/src/VendingMachine.java b/T3Q1-4/src/VendingMachine.java new file mode 100644 index 0000000..db668e9 --- /dev/null +++ b/T3Q1-4/src/VendingMachine.java @@ -0,0 +1,124 @@ +import drinks.Beer; +import drinks.Coke; +import drinks.Drink; +import drinks.GreenTea; + +import java.util.Scanner; + +public class VendingMachine { + private static final Drink[] availableDrinks = new Drink[3]; + int drinkSelection = 0; + + public VendingMachine() { + availableDrinks[0] = new Beer(); + availableDrinks[1] = new Coke(); + availableDrinks[2] = new GreenTea(); + } + + public void start() { + // This is an infinite loop. This is quite common in applications that must run 24/7 + do { + double amountInserted = 0.0; + selectDrink(); + insertCoins(); + checkChange(amountInserted); + printReceipt(); + } while (true); + } + + private void selectDrink() { + drinkSelection = presentDrinkMenu(); + } + + private void insertCoins() { + double amount = 0.0; + + Scanner sc = new Scanner(System.in); + printCoinOptions(); + + do { + char coin = sc.next().charAt(0); + amount += processCoinAmount(coin); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < availableDrinks[drinkSelection].getPrice()); + } + + private void checkChange(double amount) + { + double change = 0.0; + if (amount > availableDrinks[drinkSelection].getPrice()) + { + change = amount - availableDrinks[drinkSelection].getPrice(); + System.out.printf("Change $ %.2f \n", change); + } + } + + private void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private Integer presentDrinkMenu() { + Scanner sc = new Scanner(System.in); + int drinkSelection; + + System.out.print("====== Vending Machine ====== \n"); + /* + Note. The for loop below is just an enhanced way of writing the following: + + for (int i = 0; i < availableDrinks.length; i++) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i + 1, availableDrinks[i].getName(), availableDrinks[i].getPrice()); + } + */ + int i = 1; + for (Drink drink : availableDrinks) { + System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); + } + System.out.print("|============================ \n"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt() - 1; // This " - 1 " is to keep "drinkSelection" within the "availableDrinks" array range; + } while (drinkSelection < 0 || drinkSelection > availableDrinks.length); + + return drinkSelection ; + } + + private void printCoinOptions() { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + System.out.println("|Enter 'Q' for ten cents input |"); + System.out.println("|Enter 'T' for twenty cents input|"); + System.out.println("|Enter 'F' for fifty cents input |"); + System.out.println("|Enter 'N' for a dollar input |"); + System.out.println("=================================="); + } + + private double processCoinAmount(char coin) { + /* + Note. The code below is just an enhanced way of writing the following: + + switch (coin) + { + case 'Q': case 'q': return 0.10; + case 'T': case 't': return 0.20; + case 'F': case 'f': return 0.50; + case 'N': case 'n': return 1.00; + } + return 0.0; + + This enhanced way or writing switch-cases was introduced in Java 12 and Java 13 (we are currently at Java LTS 22, with Java 23 and Java 24 under early-access); + The "->" was introduced in Java 8. It denotes a Lambda expression: https://www.w3schools.com/java/java_lambda.asp + + The Switch with a lambda-like syntax expression was introduced in Java 14: https://www.javacodegeeks.com/2020/05/switch-as-an-expression-in-java-with-lambda-like-syntax.html + */ + return switch (coin) { + case 'Q', 'q' -> 0.10; + case 'T', 't' -> 0.20; + case 'F', 'f' -> 0.50; + case 'N', 'n' -> 1.00; + default -> 0.0; + }; + } +} \ No newline at end of file diff --git a/T3Q1-4/src/VendingMachineApp.java b/T3Q1-4/src/VendingMachineApp.java new file mode 100644 index 0000000..0684b49 --- /dev/null +++ b/T3Q1-4/src/VendingMachineApp.java @@ -0,0 +1,9 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + VendingMachine vendingMachine = new VendingMachine(); + vendingMachine.start(); + } + +} diff --git a/T3Q1-4/src/drinks/Beer.java b/T3Q1-4/src/drinks/Beer.java new file mode 100644 index 0000000..0f65611 --- /dev/null +++ b/T3Q1-4/src/drinks/Beer.java @@ -0,0 +1,10 @@ +package drinks; + +public class Beer extends Drink { + private static final String NAME = "Beer"; + private static final double PRICE = 3.0; + + public Beer() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-4/src/drinks/Coke.java b/T3Q1-4/src/drinks/Coke.java new file mode 100644 index 0000000..0469a7f --- /dev/null +++ b/T3Q1-4/src/drinks/Coke.java @@ -0,0 +1,10 @@ +package drinks; + +public class Coke extends Drink { + private static final String NAME = "Coke"; + private static final double PRICE = 1.0; + + public Coke() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-4/src/drinks/Drink.java b/T3Q1-4/src/drinks/Drink.java new file mode 100644 index 0000000..c346f83 --- /dev/null +++ b/T3Q1-4/src/drinks/Drink.java @@ -0,0 +1,19 @@ +package drinks; + +public class Drink { + private String name; + private double price; + + public Drink(String name, double price) { + this.name = name; + this.price = price; + } + + public double getPrice() { + return price; + } + + public String getName() { + return name; + } +} diff --git a/T3Q1-4/src/drinks/GreenTea.java b/T3Q1-4/src/drinks/GreenTea.java new file mode 100644 index 0000000..c53fd7c --- /dev/null +++ b/T3Q1-4/src/drinks/GreenTea.java @@ -0,0 +1,10 @@ +package drinks; + +public class GreenTea extends Drink { + private static final String NAME = "Green Tea"; + private static final double PRICE = 5.0; + + public GreenTea() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-extra1/src/VendingMachine.java b/T3Q1-extra1/src/VendingMachine.java new file mode 100644 index 0000000..63fe9ac --- /dev/null +++ b/T3Q1-extra1/src/VendingMachine.java @@ -0,0 +1,100 @@ +import drinks.*; +import coins.*; + +import java.util.Scanner; + +public class VendingMachine { + private static final Drink[] availableDrinks = new Drink[3]; + private static final Coin[] acceptableCoins = new Coin[4]; + private static final Scanner sc = new Scanner(System.in); + + int drinkSelection = 0; + private boolean isRunning = true; + + public VendingMachine() + { + availableDrinks[0] = new Beer(); + availableDrinks[1] = new Coke(); + availableDrinks[2] = new GreenTea(); + + acceptableCoins[0] = new Ten(); + acceptableCoins[1] = new Twenty(); + acceptableCoins[2] = new Fifty(); + acceptableCoins[3] = new Dollar(); + } + + public void start() + { + while (isRunning) + { + collectOrder(); + checkChange(collectCoins()); + printReceipt(); + } + } + + public void stop() + { + isRunning = false; + } + + private void collectOrder() + { + System.out.print("====== Vending Machine ====== \n"); + int i = 1; + for (Drink drink : availableDrinks) + System.out.printf("|%d. Buy %s ($%.2f) \n", i++, drink.getName(), drink.getPrice()); + System.out.print("|============================ \n"); + + do { + System.out.println("Please enter selection: "); + drinkSelection = sc.nextInt() - 1; + } while (drinkSelection < 0 || drinkSelection > availableDrinks.length); + } + + private double collectCoins() + { + double amount = 0.0; + presentCoinMenu(); + + do { + amount += processCoinAmount(); + System.out.printf("Coins inserted: %.2f \n", amount); + } while (amount < availableDrinks[drinkSelection].getPrice()); + + return amount; + } + + private void checkChange(double amount) + { + if (amount > availableDrinks[drinkSelection].getPrice()) + System.out.printf("Change $ %.2f \n", amount - availableDrinks[drinkSelection].getPrice()); + } + + private void printReceipt() + { + System.out.println("Please collect your drink"); + System.out.println("Thank you!!"); + } + + private void presentCoinMenu() + { + System.out.println("Please insert coins: "); + System.out.println("========== Coins Input ==========="); + for (Coin coin : acceptableCoins) + System.out.printf("|Enter '%c' for %s input \n", coin.getSymbol(), coin.getDescription()); + System.out.println("=================================="); + } + + private double processCoinAmount() + { + char insertedCoin = sc.next().charAt(0); + insertedCoin = Character.toUpperCase(insertedCoin); + + for (Coin coin : acceptableCoins) + if (insertedCoin == coin.getSymbol()) + return coin.getValue(); + + return 0.0; + } +} \ No newline at end of file diff --git a/T3Q1-extra1/src/VendingMachineApp.java b/T3Q1-extra1/src/VendingMachineApp.java new file mode 100644 index 0000000..0684b49 --- /dev/null +++ b/T3Q1-extra1/src/VendingMachineApp.java @@ -0,0 +1,9 @@ +public class VendingMachineApp { + + public static void main(String[] args) + { + VendingMachine vendingMachine = new VendingMachine(); + vendingMachine.start(); + } + +} diff --git a/T3Q1-extra1/src/coins/Coin.java b/T3Q1-extra1/src/coins/Coin.java new file mode 100644 index 0000000..6efa000 --- /dev/null +++ b/T3Q1-extra1/src/coins/Coin.java @@ -0,0 +1,30 @@ +package coins; + +public class Coin +{ + private final double value; + private final char symbol; + private final String description; + + public Coin(double value, char symbol, String description) + { + this.value = value; + this.symbol = symbol; + this.description = description; + } + + public double getValue() + { + return value; + } + + public char getSymbol() + { + return symbol; + } + + public String getDescription() + { + return description; + } +} diff --git a/T3Q1-extra1/src/coins/Dollar.java b/T3Q1-extra1/src/coins/Dollar.java new file mode 100644 index 0000000..461cf75 --- /dev/null +++ b/T3Q1-extra1/src/coins/Dollar.java @@ -0,0 +1,13 @@ +package coins; + +public class Dollar extends Coin +{ + private static final double VALUE = 1.00; + private static final char SYMBOL = 'N'; + private static final String DESCRIPTION = "a dollar"; + + public Dollar() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/coins/Fifty.java b/T3Q1-extra1/src/coins/Fifty.java new file mode 100644 index 0000000..8fd4bf8 --- /dev/null +++ b/T3Q1-extra1/src/coins/Fifty.java @@ -0,0 +1,13 @@ +package coins; + +public class Fifty extends Coin +{ + private static final double VALUE = 0.50; + private static final char SYMBOL = 'F'; + private static final String DESCRIPTION = "fifty cents"; + + public Fifty() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/coins/Ten.java b/T3Q1-extra1/src/coins/Ten.java new file mode 100644 index 0000000..a17d774 --- /dev/null +++ b/T3Q1-extra1/src/coins/Ten.java @@ -0,0 +1,13 @@ +package coins; + +public class Ten extends Coin +{ + private static final double VALUE = 0.10; + private static final char SYMBOL = 'Q'; + private static final String DESCRIPTION = "ten cents"; + + public Ten() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/coins/Twenty.java b/T3Q1-extra1/src/coins/Twenty.java new file mode 100644 index 0000000..19c2d86 --- /dev/null +++ b/T3Q1-extra1/src/coins/Twenty.java @@ -0,0 +1,13 @@ +package coins; + +public class Twenty extends Coin +{ + private static final double VALUE = 0.20; + private static final char SYMBOL = 'T'; + private static final String DESCRIPTION = "twenty cents"; + + public Twenty() + { + super(VALUE, SYMBOL, DESCRIPTION); + } +} diff --git a/T3Q1-extra1/src/drinks/Beer.java b/T3Q1-extra1/src/drinks/Beer.java new file mode 100644 index 0000000..c3dd5f1 --- /dev/null +++ b/T3Q1-extra1/src/drinks/Beer.java @@ -0,0 +1,11 @@ +package drinks; + +public class Beer extends Drink +{ + private static final String NAME = "Beer"; + private static final double PRICE = 3.0; + + public Beer() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-extra1/src/drinks/Coke.java b/T3Q1-extra1/src/drinks/Coke.java new file mode 100644 index 0000000..b4bbc19 --- /dev/null +++ b/T3Q1-extra1/src/drinks/Coke.java @@ -0,0 +1,11 @@ +package drinks; + +public class Coke extends Drink +{ + private static final String NAME = "Coke"; + private static final double PRICE = 1.0; + + public Coke() { + super(NAME, PRICE); + } +} diff --git a/T3Q1-extra1/src/drinks/Drink.java b/T3Q1-extra1/src/drinks/Drink.java new file mode 100644 index 0000000..5fe7fcd --- /dev/null +++ b/T3Q1-extra1/src/drinks/Drink.java @@ -0,0 +1,23 @@ +package drinks; + +public class Drink +{ + private final String name; + private final double price; + + public Drink(String name, double price) + { + this.name = name; + this.price = price; + } + + public double getPrice() + { + return price; + } + + public String getName() + { + return name; + } +} diff --git a/T3Q1-extra1/src/drinks/GreenTea.java b/T3Q1-extra1/src/drinks/GreenTea.java new file mode 100644 index 0000000..1a4190b --- /dev/null +++ b/T3Q1-extra1/src/drinks/GreenTea.java @@ -0,0 +1,11 @@ +package drinks; + +public class GreenTea extends Drink +{ + private static final String NAME = "Green Tea"; + private static final double PRICE = 5.0; + + public GreenTea() { + super(NAME, PRICE); + } +} diff --git a/T3Q2-1/T3Q2-1.iml b/T3Q2-1/T3Q2-1.iml new file mode 100644 index 0000000..82236ea --- /dev/null +++ b/T3Q2-1/T3Q2-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/T3Q2-1/src/Circle.java b/T3Q2-1/src/Circle.java new file mode 100644 index 0000000..51775ce --- /dev/null +++ b/T3Q2-1/src/Circle.java @@ -0,0 +1,14 @@ +public class Circle extends Point { + private double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } + public Circle(int x, int y) { super(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return "Circle of radius " + radius + " at point [" + x + "," + y + "]"; } +} diff --git a/T3Q2-1/src/Cylinder.java b/T3Q2-1/src/Cylinder.java new file mode 100644 index 0000000..0d2a1b2 --- /dev/null +++ b/T3Q2-1/src/Cylinder.java @@ -0,0 +1,17 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + public String toString() { return "Cylinder of height " + height + ", radius " + getRadius() + " at point [" + x + "," + y + "]"; } +} diff --git a/T3Q2-1/src/Test.java b/T3Q2-1/src/Test.java new file mode 100644 index 0000000..531bce8 --- /dev/null +++ b/T3Q2-1/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.println("Area of Circle 1 = " + testCircle1.area()); + System.out.println("Area of Circle 2 = " + testCircle2.area()); + System.out.println("Surface Area of Cylinder 1 = " + testCylinder1.area()); + System.out.println("Surface Area of Cylinder 2 = " + testCylinder2.area()); + System.out.println("Volume of Cylinder 1 = " + testCylinder1.volume()); + System.out.println("Volume of Cylinder 2 = " + testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1.toString()); + System.out.println("Description of Circle 2 = " + testCircle2.toString()); + System.out.println("Description of Cylinder 1 = " + testCylinder1.toString()); + System.out.println("Description of Cylinder 2 = " + testCylinder2.toString()); + } +} diff --git a/T3Q2-2/src/Circle.java b/T3Q2-2/src/Circle.java new file mode 100644 index 0000000..9b5c6d9 --- /dev/null +++ b/T3Q2-2/src/Circle.java @@ -0,0 +1,14 @@ +public class Circle extends Point { + private double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } + public Circle(int x, int y) { super(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return String.format("Circle of radius %.2f at point %s", radius, super.toString()); } +} diff --git a/T3Q2-2/src/Cylinder.java b/T3Q2-2/src/Cylinder.java new file mode 100644 index 0000000..dbbc986 --- /dev/null +++ b/T3Q2-2/src/Cylinder.java @@ -0,0 +1,18 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + // WRONG - It goes against Java's encapsulation rules. A class should only have access to its parent, without bypassing it. + public String toString() { return String.format("Cylinder of height %.2f, radius %.2f at point %s", height, getRadius(), super.super.toString()); } +} diff --git a/T3Q2-2/src/Point.java b/T3Q2-2/src/Point.java new file mode 100644 index 0000000..780e3e8 --- /dev/null +++ b/T3Q2-2/src/Point.java @@ -0,0 +1,13 @@ +public class Point { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return String.format("[%d,%d]", x, y); } +} \ No newline at end of file diff --git a/T3Q2-2/src/Test.java b/T3Q2-2/src/Test.java new file mode 100644 index 0000000..531bce8 --- /dev/null +++ b/T3Q2-2/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.println("Area of Circle 1 = " + testCircle1.area()); + System.out.println("Area of Circle 2 = " + testCircle2.area()); + System.out.println("Surface Area of Cylinder 1 = " + testCylinder1.area()); + System.out.println("Surface Area of Cylinder 2 = " + testCylinder2.area()); + System.out.println("Volume of Cylinder 1 = " + testCylinder1.volume()); + System.out.println("Volume of Cylinder 2 = " + testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1.toString()); + System.out.println("Description of Circle 2 = " + testCircle2.toString()); + System.out.println("Description of Cylinder 1 = " + testCylinder1.toString()); + System.out.println("Description of Cylinder 2 = " + testCylinder2.toString()); + } +} diff --git a/T3Q2-3/src/Circle.java b/T3Q2-3/src/Circle.java new file mode 100644 index 0000000..9b5c6d9 --- /dev/null +++ b/T3Q2-3/src/Circle.java @@ -0,0 +1,14 @@ +public class Circle extends Point { + private double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { super(x, y); this.radius = radius; } + public Circle(int x, int y) { super(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return String.format("Circle of radius %.2f at point %s", radius, super.toString()); } +} diff --git a/T3Q2-3/src/Cylinder.java b/T3Q2-3/src/Cylinder.java new file mode 100644 index 0000000..3fcf66e --- /dev/null +++ b/T3Q2-3/src/Cylinder.java @@ -0,0 +1,17 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + public String toString() { return String.format("Cylinder of height %.2f, radius %.2f at point %s", height, getRadius(), printPoint() ); } +} diff --git a/T3Q2-3/src/Point.java b/T3Q2-3/src/Point.java new file mode 100644 index 0000000..2b6a423 --- /dev/null +++ b/T3Q2-3/src/Point.java @@ -0,0 +1,14 @@ +public class Point { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return String.format("[%d,%d]", x, y); } + public String printPoint() { return toString(); } +} \ No newline at end of file diff --git a/T3Q2-3/src/Test.java b/T3Q2-3/src/Test.java new file mode 100644 index 0000000..531bce8 --- /dev/null +++ b/T3Q2-3/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.println("Area of Circle 1 = " + testCircle1.area()); + System.out.println("Area of Circle 2 = " + testCircle2.area()); + System.out.println("Surface Area of Cylinder 1 = " + testCylinder1.area()); + System.out.println("Surface Area of Cylinder 2 = " + testCylinder2.area()); + System.out.println("Volume of Cylinder 1 = " + testCylinder1.volume()); + System.out.println("Volume of Cylinder 2 = " + testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1.toString()); + System.out.println("Description of Circle 2 = " + testCircle2.toString()); + System.out.println("Description of Cylinder 1 = " + testCylinder1.toString()); + System.out.println("Description of Cylinder 2 = " + testCylinder2.toString()); + } +} diff --git a/T3Q2-4/src/Circle.java b/T3Q2-4/src/Circle.java new file mode 100644 index 0000000..d7d6884 --- /dev/null +++ b/T3Q2-4/src/Circle.java @@ -0,0 +1,15 @@ +public class Circle { + protected Point center = new Point(); + protected double radius; + + public Circle() { radius = 1; } + public Circle(double radius) { this.radius = radius; } + public Circle(int x, int y, double radius) { center.setPoint(x, y); this.radius = radius; } + public Circle(int x, int y) { center.setPoint(x, y); radius = 1; } + + public double getRadius() { return radius; } + public void setRadius(double radius) { this.radius = radius; } + + public double area() { return Math.PI * radius * radius; } + public String toString() { return String.format("Circle of radius %.2f at point %s", radius, center.toString()); } +} diff --git a/T3Q2-4/src/Cylinder.java b/T3Q2-4/src/Cylinder.java new file mode 100644 index 0000000..85c66b5 --- /dev/null +++ b/T3Q2-4/src/Cylinder.java @@ -0,0 +1,17 @@ +public class Cylinder extends Circle { + private double height; + + public Cylinder() { height = 1; } + public Cylinder(double height) { this.height = height;} + public Cylinder(double radius, double height) { super(radius); this.height = height; } + public Cylinder(int x, int y, double radius) { super(x, y, radius); height = 1; } + public Cylinder(int x, int y, double radius, double height) { super(x, y, radius); this.height = height; } + + public double getHeight() { return height; } + public void setHeight(double height) { this.height = height; } + + public double area() { return 2 * (super.area() + Math.PI * super.getRadius() * height); } + public double volume() { return super.area() * height; }; + + public String toString() { return String.format("Cylinder of height %.2f, radius %.2f at point %s", height, getRadius(), center.toString() ); } +} diff --git a/T3Q2-4/src/Point.java b/T3Q2-4/src/Point.java new file mode 100644 index 0000000..780e3e8 --- /dev/null +++ b/T3Q2-4/src/Point.java @@ -0,0 +1,13 @@ +public class Point { + protected int x, y; + + public Point() { x = 0; y = 0; } + public Point(int x, int y) { this.x = x; this.y = y; } + + public void setPoint(int x, int y) { this.x = x; this.y = y; } + + public int getX() { return x; } + public int getY() { return y; } + + public String toString() { return String.format("[%d,%d]", x, y); } +} \ No newline at end of file diff --git a/T3Q2-4/src/Test.java b/T3Q2-4/src/Test.java new file mode 100644 index 0000000..f481306 --- /dev/null +++ b/T3Q2-4/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + + public static void main(String[] args) { + Circle testCircle1 = new Circle(); + Circle testCircle2 = new Circle(2, 4, 35); + Cylinder testCylinder1 = new Cylinder(); + Cylinder testCylinder2 = new Cylinder(3, 3, 8, 20); + + System.out.printf("Area of Circle 1 = %.2f\n", testCircle1.area()); + System.out.printf("Area of Circle 2 = %.2f\n", testCircle2.area()); + System.out.printf("Surface Area of Cylinder 1 = %.2f\n", testCylinder1.area()); + System.out.printf("Surface Area of Cylinder 2 = %.2f\n", testCylinder2.area()); + System.out.printf("Volume of Cylinder 1 = %.2f\n", testCylinder1.volume()); + System.out.printf("Volume of Cylinder 2 = %.2f\n", testCylinder2.volume()); + + System.out.println("Description of Circle 1 = " + testCircle1); + System.out.println("Description of Circle 2 = " + testCircle2); + System.out.println("Description of Cylinder 1 = " + testCylinder1); + System.out.println("Description of Cylinder 2 = " + testCylinder2); + } +}