diff --git a/.idea/encodings.xml b/.idea/encodings.xml index e7cd972..b2320b5 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index b28c0fb..6350505 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,10 +5,11 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 36361c6..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 9c12bdd..2362803 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/6tlCTWq0) # 🧾 Mystery Receipt Generator (Java CLI Project) ## Overview diff --git a/src/main/java/org/codedifferently/Main.java b/src/main/java/org/codedifferently/Main.java index 8a571aa..18e0112 100644 --- a/src/main/java/org/codedifferently/Main.java +++ b/src/main/java/org/codedifferently/Main.java @@ -1,17 +1,38 @@ package org.codedifferently; -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.Scanner; + public class Main { - static void main() { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - IO.println(String.format("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 . - IO.println("i = " + i); - } + public static void main(String[] args) { + Scanner scr = new Scanner(System.in); + + System.out.println("Enter your name: "); + String name = scr.nextLine(); + + System.out.println("Enter your budget: "); + double budget = scr.nextDouble(); + scr.nextLine(); // consume leftover newline + + System.out.println("Enter a coupon code: "); + String couponCode = scr.nextLine(); + + // Create a Receipt object + Receipt receipt = new Receipt(); + receipt.generateItemPrices(); // generates 3 separate items + receipt.calculateSubtotal(); + receipt.applyCoupon(couponCode); + receipt.calculateTax(); + receipt.calculateFinalTotal(); + + // Create a Visit object + Visit visit = new Visit(name); + visit.generateReceiptCode(); + boolean withinBudget = visit.checkBudget(receipt.finalTotal, budget); + + // Print the receipt + System.out.println("\n--- Receipt ---"); + visit.printReceiptCode(); + receipt.printReceiptDetails(); + System.out.println("Within budget: " + withinBudget); } } diff --git a/src/main/java/org/codedifferently/README.md b/src/main/java/org/codedifferently/README.md new file mode 100644 index 0000000..002c905 --- /dev/null +++ b/src/main/java/org/codedifferently/README.md @@ -0,0 +1,80 @@ +Receipt Generator in Java +What It Does + +This program generates a simple receipt for three items. It calculates the subtotal, applies a coupon, adds tax, checks the budget, and gives a receipt code. + +• **How It Works** + +1. Ask the user for their name, budget, and coupon code. + + +2. Generate three random item prices. + + +3. Add the prices to get the subtotal. + + +4. Apply a 50% discount if the coupon code is SAVE50. + + +5. Calculate tax at 8%. + + +6. Calculate the final total. + + +7. Generate a receipt code from the first two letters of the name + random 4-digit number. + + +8. Check if the total is within the budget. + + +9. Print a simple receipt with all the details. + +• **Sample Output** + +Enter your name: +Alice + +Enter your budget: +150 + +Enter a coupon code: +SAVE50 + +--- Receipt --- + +Receipt Code: AL4821 + +Item 1 Price: $42.34 + +Item 2 Price: $57.89 + +Item 3 Price: $33.12 + +Subtotal: $133.35 + +Discounted Total: $66.67 + +Tax: $5.33 + +Final Total: $72.00 + +Within budget: true + + +• **Java Concepts Used** + +Classes and objects + +Methods + +Random numbers + +User input with Scanner + +Variables and data types + +Conditional statements (if) + +Printing output with System.out.println diff --git a/src/main/java/org/codedifferently/Receipt.java b/src/main/java/org/codedifferently/Receipt.java new file mode 100644 index 0000000..e12d701 --- /dev/null +++ b/src/main/java/org/codedifferently/Receipt.java @@ -0,0 +1,59 @@ +package org.codedifferently; + +import java.util.Random; + +public class Receipt { + double item1; + double item2; + double item3; + double subtotal; + double discountedTotal; + double taxAmount; + double finalTotal; + + private final double TAX_RATE = 0.08; // 8% tax + + // Generate 3 separate random item prices + public void generateItemPrices() { + Random random = new Random(); + item1 = 10 + (90 * random.nextDouble()); + item2 = 10 + (90 * random.nextDouble()); + item3 = 10 + (90 * random.nextDouble()); + } + + // Calculate subtotal + public void calculateSubtotal() { + subtotal = item1 + item2 + item3; + } + + // Apply coupon + public void applyCoupon(String couponCode) { + if (couponCode.equalsIgnoreCase("SAVE50")) { + discountedTotal = subtotal * 0.5; + } else { + discountedTotal = subtotal; + } + } + + // Calculate tax + public void calculateTax() { + taxAmount = discountedTotal * TAX_RATE; + } + + // Calculate final total + public void calculateFinalTotal() { + finalTotal = discountedTotal + taxAmount; + } + + // Print receipt details in a simple way + public void printReceiptDetails() { + System.out.println("Item 1 Price: $" + item1); + System.out.println("Item 2 Price: $" + item2); + System.out.println("Item 3 Price: $" + item3); + System.out.println("Subtotal: $" + subtotal); + System.out.println("Discounted Total: $" + discountedTotal); + System.out.println("Tax: $" + taxAmount); + System.out.println("Final Total: $" + finalTotal); + } + +} diff --git a/src/main/java/org/codedifferently/Visit.java b/src/main/java/org/codedifferently/Visit.java new file mode 100644 index 0000000..33ef11b --- /dev/null +++ b/src/main/java/org/codedifferently/Visit.java @@ -0,0 +1,26 @@ +package org.codedifferently; + +public class Visit { + String name; + String receiptCode; + + public Visit(String name) { + this.name = name; + } + + // Generate receipt code: first 2 letters of name + random 4-digit number + public void generateReceiptCode() { + int visitId = (int) (Math.random() * 9000) + 1000; + receiptCode = name.substring(0, Math.min(2, name.length())).toUpperCase() + visitId; + } + + // Check if final total is within the user's budget + public boolean checkBudget(double finalTotal, double budget) { + return finalTotal <= budget; + } + + // Print the receipt code + public void printReceiptCode() { + System.out.println("Receipt Code: " + receiptCode); + } +}