diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 4b151ab..4a5e775 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,14 @@ - + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index f21bb22..1a42885 100644 --- a/pom.xml +++ b/pom.xml @@ -13,5 +13,18 @@ 21 UTF-8 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 21 + 21 + --enable-preview + + + + \ No newline at end of file diff --git a/src/main/java/org/codedifferently/CoffeeItem.java b/src/main/java/org/codedifferently/CoffeeItem.java new file mode 100644 index 0000000..72969f7 --- /dev/null +++ b/src/main/java/org/codedifferently/CoffeeItem.java @@ -0,0 +1,25 @@ +package org.codedifferently; +import java.util.Scanner; +public class CoffeeItem { + private String name; + private double price; + + public CoffeeItem(String name, double price) { + this.name = name; + this.price = price; + + } + public String getName() { + return name; + + + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } +} diff --git a/src/main/java/org/codedifferently/Customer.java b/src/main/java/org/codedifferently/Customer.java new file mode 100644 index 0000000..832adb5 --- /dev/null +++ b/src/main/java/org/codedifferently/Customer.java @@ -0,0 +1,32 @@ +package org.codedifferently; + +import java.util.Scanner; + + +public class Customer { + private String name; + private String email; + private int drinksPurchased; + + public Customer(String name, String email) { + this.name = name; + this.email = email; + this.drinksPurchased = 0; + + } + + public Customer() { + this("Guest", "N/a"); + } + + public String getName() {return name;} + public String getEmail() {return email;} + public int getDrinksPurchased() {return drinksPurchased;} + + public void incrementDrinksPurchased() {drinksPurchased++;} + + public boolean isEligibleForRewards() {return drinksPurchased != 0 && drinksPurchased % 5 == 0;} + + + } + diff --git a/src/main/java/org/codedifferently/Main.java b/src/main/java/org/codedifferently/Main.java index 435139b..cbfc3a9 100644 --- a/src/main/java/org/codedifferently/Main.java +++ b/src/main/java/org/codedifferently/Main.java @@ -1,17 +1,171 @@ package org.codedifferently; +import java.util.Scanner; +import java.util.ArrayList; +import java.util.List; +import java.util.Date; +import java.text.SimpleDateFormat; + //TIP To Run code, press or // click the icon in the gutter. public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); + Scanner scanner = new Scanner(System.in); + + + + CoffeeItem latte = new CoffeeItem("Latte", 4.50); + CoffeeItem cappuccino = new CoffeeItem("Cappuccino", 5.00); + CoffeeItem espresso = new CoffeeItem("Espresso", 3.00); + + + List menu = new ArrayList<>(); + menu.add(latte); + menu.add(cappuccino); + menu.add(espresso); + + System.out.println("Enter customer name: "); + String name = scanner.nextLine(); + System.out.println("Enter customer email: "); + String email = scanner.nextLine(); + + Customer customer = new Customer(name, email); + + + boolean shopOpen = true; + while (shopOpen) { + + + System.out.println("Welcome to Mani G's Cafe"); + System.out.println("Menu"); + for (int i = 0; i < menu.size(); i++) { + System.out.println(i + 1 + ". " + menu.get(i).getName() + "($" + menu.get(i).getPrice() + ")"); + } + + System.out.println("Enter the number of the drink you want to purchase (or 0 to exit):"); + + int choice = -1; + try { + choice = Integer.parseInt(scanner.nextLine()); + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a number."); + continue; + } + if (choice == 0) { + shopOpen = false; + + System.out.println("Shop is closing. Goodbye!"); + continue; + + } + + if (choice < 1 || choice > menu.size()) { + System.out.println("Invalid menu selection."); + continue; + + } + + CoffeeItem selectedDrink = menu.get(choice - 1); + double priceToPay = selectedDrink.getPrice(); + double originalItemPrice = selectedDrink.getPrice(); + + System.out.println("How many of this drink? "); + int qty; + + try { + qty = Integer.parseInt(scanner.nextLine()); + } catch (NumberFormatException e) { + System.out.println("Invalid input. Enter a whole number"); + continue; + } + + if (qty <= 0) { + System.out.println("Quantity must be at least 1"); + continue; + } + + double unitPrice = selectedDrink.getPrice(); + double subtotal = unitPrice * qty; + + boolean rewardsApplied = false; + double discount = 0.0; + + if (customer.isEligibleForRewards()) { + rewardsApplied = true; + discount = unitPrice; + } + + double total = Math.max(0, subtotal - discount); + + boolean bonusPoint = subtotal > 20; + + int paidDrinks = qty - (rewardsApplied ? 1 : 0); + for (int i = 0; i < paidDrinks; i++) { + customer.incrementDrinksPurchased(); + } + + if (bonusPoint) { + customer.incrementDrinksPurchased(); + } + + + System.out.println("\n===========RECEIPT============"); + System.out.println("Customer " + customer.getName()); + System.out.println("Item: " + selectedDrink.getName()); + System.out.println("Unit Price: $%.2f%n"+ unitPrice); + System.out.println("Quantity: " + qty); + System.out.println("Subtotal: $%.2f%n" + subtotal); + + if (rewardsApplied){ + System.out.println("Reward Discount(1 free): -$%.2f%n" + discount); + } + + if (rewardsApplied) { + System.out.println("TOTAL: -$%.2f%n" + total); + + if (bonusPoint) { + System.out.println("Golden Ticket: +1 bonus point (Spent over $20)"); + } + + System.out.println("Drinks toward reward: " + (customer.getDrinksPurchased() % 5)); + System.out.println("====================\n"); + } + + + + + System.out.println("Customer: " + customer.getName() + " | Drinks toward reward: " + (customer.getDrinksPurchased() % 5)); + + boolean rewardApplied = false; + + if (customer.isEligibleForRewards()) { + + System.out.println("CONGRATS! Reward reached. Next drink is on us!"); + priceToPay = 0.0; + rewardApplied = true; + } + + boolean getBonusPoint = false; + if (originalItemPrice > 20) { + System.out.println("Congratulations! You get a bonus point for spending over $20"); + customer.incrementDrinksPurchased(); + getBonusPoint = true; + } + + System.out.println(customer.getName() + " purchased a " + selectedDrink.getName() + "($" + + String.format("%.2f", priceToPay) + ")"); + + customer.incrementDrinksPurchased(); + + + + + + } + + + } } - } -} \ No newline at end of file + + +