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..d2b5d0f 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/README.md b/README.md index 0111397..986abae 100644 --- a/README.md +++ b/README.md @@ -1,147 +1,52 @@ # coffee-shop-rewards2 -# Coreye’s Coffee Cafe (Triple C’s) +Triple C's -### “Coffee. Code. Consistency.” +## Objective -Welcome to Triple C’s. +Build a Java console application that manages a coffee shop’s menu and a customer loyalty program. You will practice **Object-Oriented Programming (OOP)** by creating custom classes, managing data with loops, and using logic to award free drinks. -We serve: +## Core Requirements -* Strong coffee -* Clean code -* Clear logic +### 1. The `CoffeeItem` Class -You’ve been hired to build the rewards system. +This class represents a drink on the menu. -Don’t let the Bronze-tier customers finesse free drinks. - ---- - -## The Mission - -Customers: - -* Order items -* Earn points -* Unlock tiers -* Try to redeem rewards - -You: - -* Build the system -* Keep it organized -* Make sure the math is correct - -No arrays. -No lists. -Just solid fundamentals. - ---- - -## 📁 Required Files - -``` -Customer.java -Purchase.java -Main.java -``` - ---- - -# Level 1 — “Open the Shop” - -Create a `Customer`. - -Fields: - -* name -* phoneNumber -* points - -Include: - -* Default constructor -* Parameterized constructor -* Getters and setters - -Create multiple customers. -Let them earn points. -Print their totals. - -Nobody starts Gold at Triple C’s. - ---- - -# Level 2 — “Run the Register” - -Create a `Purchase` class. - -Each item must know: - -* itemName -* price -* isDrink - -Create at least **6 menu items**. - -No arrays. -Just individual object variables. - ---- - -## ☕ Ordering System +* **Attributes:** Name and price. +* **Encapsulation:** Use `private` fields with **Getters and Setters**. +* **Constructor:** Initialize the name and price when a new drink is created. +### 2. The `Customer` Class -The menu must: +This class tracks individual shoppers and their progress toward a reward. -* Display at least once -* Allow customers to choose items -* Determine how points are chosen +* **Attributes:** Name, email, and `drinksPurchased` (an integer). +* **Methods:** * Create a method to increment the drink count. +* Create a method to check if the customer is eligible for a **Reward** (e.g., if they have bought 5 drinks, the next one is free). -Update the customer’s points correctly. ---- - -# Level 3 — “Status Matters” - -Customers unlock tiers: - -* Bronze -* Silver -* Gold - -Add redemption: - -Free Drink = 100 points -If they don’t have enough → deny -If they do → subtract points +### 3. The Shop Logic (`Main.java`) -At the end, print: +This is where your program runs. You should: -Name | Tier | Points - -Clean. Clear. Professional. +* **Instantiate Objects:** Create at least three different `CoffeeItem` objects. +* **Control Flow:** Use an `if-else` statement to determine if a customer pays full price or $0 based on their reward status. +* **Loops:** Use a `while` or `for` loop to simulate a "Daily Sales" cycle, allowing multiple customers to buy drinks until the shop closes. --- -# Level 4 — “Make It Feel Like a Real Checkout” - -Triple C’s runs smooth. - -While the customer is ordering: - -* Track their **session total spent** -* Print each item as it’s ordered (like a receipt) - -When they finish ordering, print: +## The Challenge -* Session total -* Points earned during this session -* Updated total customer points +1. **The "Golden Ticket":** Add logic so that if a customer spends over $20 in a single transaction, they get a "bonus" point toward their rewards. +2. **Input Validation:** Use a `Scanner` to take user input. Ensure the program doesn't crash if someone enters a negative number or an invalid menu selection. -Make it feel like a real checkout screen. +## Example Output +```text +Welcome to Triple Cs! +Customer: Alex | Drinks toward reward: 4 +Alex purchased a Latte ($4.50). +CONGRATS! Reward reached. Next drink is on us! diff --git a/src/main/java/org/codedifferently/CoffeeItem.java b/src/main/java/org/codedifferently/CoffeeItem.java new file mode 100644 index 0000000..68d97f2 --- /dev/null +++ b/src/main/java/org/codedifferently/CoffeeItem.java @@ -0,0 +1,24 @@ +package org.codedifferently; + +public class CoffeeItem { + // Represents the name of the item. + private String name; + // Represents the price of the item + private final double price; + + // Constructor + public CoffeeItem(String name, double price) { + this.name = name; + this.price = price; + } + + // Returns the name of the item. + public String getName() { + return name; + } + + // Returns the price of the item. + 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..be6e880 --- /dev/null +++ b/src/main/java/org/codedifferently/Customer.java @@ -0,0 +1,87 @@ +package org.codedifferently; + +public class Customer { + // Represents the customer's name. + private String name; + // Represents the customer's email address. + private String email; + // Represents the total number of drinks that the customer has purchased. + private int drinksPurchased; + // Represents whether the customer has a reward active + private boolean rewardActive; + // Represents the customer's tab. + private double tab; + + // Constructor + public Customer(String name, String email) { + this.name = name; + this.email = email; + this.drinksPurchased = 0; + this.rewardActive = false; + } + + // Returns the current customer's name. + public String getName() { + return name; + } + + // Returns the current customer's email. + public String getEmail() { + return email; + } + + // Sets the current customer's name. + public void setName(String name) { + this.name = name; + } + + // Sets the current customer's email. + public void setEmail(String email) { + this.email = email; + } + + // Increments drinksPurchased by 1. + public void setDrinksPurchased() { + this.drinksPurchased++; + } + + // Sets the amount of drinks that the customer's bought to 0. + public void setDrinksPurchased(int num) { + this.drinksPurchased = 0; + } + + // Returns a boolean representing whether the current customer has a reward active. + public boolean getRewardActive() { + return this.rewardActive; + } + + // Returns the total number of drinks that the current customer has purchased. + public int getDrinksPurchased() { + return this.drinksPurchased; + } + + // Sets whether the current customer has a reward active. + public void setRewardActive(boolean status) { + this.rewardActive = status; + } + + // A method that returns a boolean representing whether the current customer is eligible for a reward. + public boolean isEligible() { + if (this.drinksPurchased == 4) { + setRewardActive(true); + return true; + } else { + return false; + } + } + + // Gets the current customer's tab. + public double getTab() { + return tab; + } + + // Sets the current customer's tab. + public void setTab(double tab) { + this.tab += tab; + } +} diff --git a/src/main/java/org/codedifferently/Main.java b/src/main/java/org/codedifferently/Main.java index 435139b..1c6bde1 100644 --- a/src/main/java/org/codedifferently/Main.java +++ b/src/main/java/org/codedifferently/Main.java @@ -1,17 +1,21 @@ package org.codedifferently; +import java.util.Scanner; + //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!"); + Scanner sc = new Scanner(System.in); + // Creates a customer object for the menu + Customer patron = Welcome.greet(sc); + + // Instantiates 3 new items to be sold on the menu + CoffeeItem cappuccino = new CoffeeItem("Cappuccino", 4.75); + CoffeeItem ICL = new CoffeeItem("Iced Caramel Latte", 5.95); + CoffeeItem coldBrew = new CoffeeItem("Cold Brew Latte", 4.25); - 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); - } + // Displays the menu. + Purchase.displayMenu(patron, cappuccino, ICL, coldBrew, sc); } } \ No newline at end of file diff --git a/src/main/java/org/codedifferently/Purchase.java b/src/main/java/org/codedifferently/Purchase.java new file mode 100644 index 0000000..9e5741b --- /dev/null +++ b/src/main/java/org/codedifferently/Purchase.java @@ -0,0 +1,114 @@ +package org.codedifferently; + +import java.util.Scanner; + +public class Purchase { + + // Method to print the menu to the console. + public static void displayMenu(Customer patron, CoffeeItem item1, CoffeeItem item2, CoffeeItem item3, Scanner sc) { + int input; + + // Builds the menu display + String item1Display = item1.getName() + ": $" + item1.getPrice(); + String item2Display = item2.getName() + ": $" + item2.getPrice(); + String item3Display = item3.getName() + ": $" + item3.getPrice(); + + // Object that we will use to build the menu. + StringBuilder receipt = new StringBuilder("\nReceipt\n"); + + System.out.println("\nAnd what can I get for you today? (Select a number)\n"); + + // Loop to keep displaying the menu while the customer chooses not to checkout. + do { + // Prints the menu. + System.out.println("Menu"); + System.out.println("1. " + item1Display); + System.out.println("2. " + item2Display); + System.out.println("3. " + item3Display); + System.out.println("4. Checkout"); + System.out.print("Selection: "); + + + // Validates the input. + if (sc.hasNextInt()) { + input = sc.nextInt(); + } else { + input = -1; // force default case + } + sc.nextLine(); + + // Represents whether a reward was just redeemed or not. + boolean rewardUsed; + switch (input) { + // The customer buys the first item. + case 1: + rewardUsed = itemTransaction(patron, item1); + if (rewardUsed) { + receipt.append(item1.getName()).append(": $0.00 (REWARD)"); + } else { + receipt.append(item1Display).append("\n"); + } + break; + // The customer buys the second item. + case 2: + rewardUsed = itemTransaction(patron, item2); + if (rewardUsed) { + receipt.append(item2.getName()).append(": $0.00 (REWARD)"); + } else { + receipt.append(item2Display).append("\n"); + } + break; + // The customer buys the 3rd item + case 3: + rewardUsed = itemTransaction(patron, item3); + if (rewardUsed) { + receipt.append(item3.getName()).append(": $0.00 (REWARD)\n"); + } else { + receipt.append(item3Display).append("\n"); + } + break; + case 4: + break; + default: + System.out.println("\nI'm sorry, we don't carry that drink.\n"); + } + } while (input != 4); + double tab = patron.getTab(); + + // Prints the receipt out to the console. + System.out.println(receipt); + System.out.printf("Total: $%.2f%n\n", tab); + + // Gives the customer an additional coupon if the just spent $20 or more. + if (tab > 20.00) { + System.out.println("Congratulations! You've earned a voucher that can be redeemed for any drink at during your next visit!"); + } + System.out.println("Thank you for shopping at Triple C's!"); + } + + // A method that processes a transaction. + public static boolean itemTransaction(Customer patron, CoffeeItem item) { + // If the customer did not activate a reward process the purchase normally. + if (!patron.getRewardActive()) { + System.out.println("\n" + patron.getName() + " purchased a " + item.getName() + ". ($" + item.getPrice() + ")"); + patron.setTab(item.getPrice()); + /* If the customer is eligible for a reward, notify them that there next drink is free and set their + drinksPurchased back to 0 + */ + if (patron.isEligible()) { + System.out.println("CONGRATS! Reward reached. Next drink is on us!\n"); + patron.setDrinksPurchased(0); + // The customer is not eligible for a reward, so increment their drinks purchased by 1. + } else { + patron.setDrinksPurchased(); + System.out.println("Drinks toward reward: " + patron.getDrinksPurchased() + "\n"); + } + return false; + // The customer just redeemed a reward, so they are not charged for a drink. + } else { + System.out.println("\n" + patron.getName() + " redeemed their reward for " + item.getName() + ". ($0.00)\n"); + patron.setRewardActive(false); + } + return true; + } +} diff --git a/src/main/java/org/codedifferently/Welcome.java b/src/main/java/org/codedifferently/Welcome.java new file mode 100644 index 0000000..a81f597 --- /dev/null +++ b/src/main/java/org/codedifferently/Welcome.java @@ -0,0 +1,42 @@ +package org.codedifferently; +import java.util.Scanner; + +public class Welcome { + public static Customer greet(Scanner sc) { + System.out.println("Welcome to Triple Cs!"); + String name, email; + + // Prompts the user for a valid name for the order. Will launch an error if it is invalid. + while (true) { + System.out.print("Can I get a name for the order?: "); + name = sc.nextLine(); + try { + if (!name.matches("[a-zA-Z]+")) { + throw new IllegalArgumentException(); + } + break; + } catch (IllegalArgumentException e) { + System.out.println("\nInvalid input. Please use letters only.\n"); + } + } + + // Prompts the user for a valid email address for the order. Will launch an error if it is invalid. + while (true) { + System.out.print("And your email address?: "); + email = sc.nextLine(); + try { + if (!email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")) { + throw new IllegalArgumentException(); + } + break; + } catch (IllegalArgumentException e) { + System.out.println("\nInvalid email address. Try again.\n"); + } + } + + System.out.println("\nThanks! Reminder that you get a free drink for every 5 drinks that you buy! :)"); + + // Returns an object representing the current customer. + return new Customer(name, email); + } +}