diff --git a/.DS_Store b/.DS_Store index a70ee6b..d6f6f33 100644 Binary files a/.DS_Store and b/.DS_Store differ 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/java-receipt-generator.iml b/.idea/java-receipt-generator.iml index d6ebd48..f76df2b 100644 --- a/.idea/java-receipt-generator.iml +++ b/.idea/java-receipt-generator.iml @@ -2,7 +2,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index b28c0fb..f2d5b8c 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/RECEIPTREADME.md b/RECEIPTREADME.md new file mode 100644 index 0000000..6b5f90d --- /dev/null +++ b/RECEIPTREADME.md @@ -0,0 +1,66 @@ +# 🧾 Mystery Receipt Generator + +## Overview + +This Java program that generates **Mystery receipt** based on user input and randomly generated values. +It will then compare +the user's budget to the subtotal of the items on the receipt and inform the user of whether their transaction was **successful** or **not**. + +Each time the program runs, the receipt is **slightly different**. + +--- + +## How it works + +The program takes input for the user's: + * name + * budget + * A discount code. + +A receipt is then generated containing: + + * The name of the store + * A receipt code + * 3 item prices + * Subtotal + * Price adjustments containing any price modifiers, such as tax and discount if applicable + * The final total +--- +Sample Input & Output +---------------------------------------------------------------- +Name: Jayden +Budget: 200.00 +Discount: employee + +Jayden's Emporium +ID Code: 191591 +Receipt Code: JAY-191591 +Item 1: \$72.65 +Item 2: \$103.84 +Item 3: \$121.23 +Subtotal: \$297.72 + +---Price Adjustments--- +Sales tax: 6% + +Checking for coupon code... +Coupon code "employee" accepted! +Discount: 30% + +Final Total: $220.92 + +Transaction failed! +Insufficient Funds. User is short by $20.92 dollars... +Cancelling transaction. +--- +## Java Concepts Used +The following Java concepts are used in this project: + +* Use of Variables +* Creation of objects +* Organization of multiple classes and methods within them +* Collecting user input using `Scanner` +* Generating random values using the `Random` class +* The `Math` class and its functions +* Manipulating and validating text using the `String` class +* (`if / else`) Conditionals \ No newline at end of file diff --git a/out/production/java-receipt-generator/main/java/org/codedifferently/MysteryReceiptGenerator b/out/production/java-receipt-generator/main/java/org/codedifferently/MysteryReceiptGenerator new file mode 100644 index 0000000..017d7d3 --- /dev/null +++ b/out/production/java-receipt-generator/main/java/org/codedifferently/MysteryReceiptGenerator @@ -0,0 +1 @@ +import java.util.*; diff --git a/src/main/java/org/codedifferently/CouponValidator.java b/src/main/java/org/codedifferently/CouponValidator.java new file mode 100644 index 0000000..72290e0 --- /dev/null +++ b/src/main/java/org/codedifferently/CouponValidator.java @@ -0,0 +1,38 @@ +package org.codedifferently; + +import java.util.Random; + +public class CouponValidator { + // Checks the coupon code to see if a discount should be applied. + public static double validateCoupon(String coupon, Random random) { + System.out.println("\nChecking for coupon code..."); + if ((coupon).equalsIgnoreCase("friend")) { + // Applies a 15% discount if the "friend" coupon code is used. + System.out.println("Coupon code " + "\"" + coupon + "\" accepted!"); + return 0.15; + } else if ((coupon).equalsIgnoreCase("employee")) { + // Applies a 30% discount if the coupon code "employee" is used. + System.out.println("Coupon code " + "\"" + coupon + "\" accepted!"); + return 0.30; + } else if (coupon.equalsIgnoreCase("chance")) { + /* Starts "chance time" which will roll a die. If the result is even, + a 40% discount will be applied. If it is odd, all items will become 5% + more expensive. + */ + System.out.println("Coupon code " + "\"" + coupon + "\" accepted!\nWelcome to Chance Time!\nRolling the dice (1-6)..."); + int diceRoll = (random.nextInt(1, 7)); + System.out.println("Rolled " + diceRoll + "."); + if (diceRoll % 2 == 0) { + System.out.println("YOU'VE GOTTEN LUCKY!!!"); + return 0.40; + } else { + System.out.println("Better luck next time..."); + return -0.05; + } + } else { + // The coupon code provided does not exist. The discount rate will be 0%. + System.out.println("Coupon code " + "\"" + coupon + "\" not found!"); + return 0.0; + } + } +} diff --git a/src/main/java/org/codedifferently/Main.java b/src/main/java/org/codedifferently/Main.java index 8a571aa..0bedb53 100644 --- a/src/main/java/org/codedifferently/Main.java +++ b/src/main/java/org/codedifferently/Main.java @@ -1,17 +1,27 @@ package org.codedifferently; +import java.util.*; //TIP To Run code, press or // click the icon in the gutter. 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!")); + Scanner sc = new Scanner(System.in); - 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); - } + // Collects input for the user's name and stores it in a variable + System.out.print("Enter your name: "); + String name = sc.nextLine().trim(); + + // Collects input for the user's budget and stores it in a variable + System.out.print("Enter your budget: "); + double budget = sc.nextDouble(); + sc.nextLine(); + + // Collects input for the user's coupon and stores it in a variable + System.out.print("Enter a coupon code: "); + String coupon = sc.nextLine().trim(); + + sc.close(); + // Prints the receipt out to the console. + MysteryReceiptPrinter.printReceipt(name, budget, coupon); } } diff --git a/src/main/java/org/codedifferently/MysteryReceiptCalculator.java b/src/main/java/org/codedifferently/MysteryReceiptCalculator.java new file mode 100644 index 0000000..80d4a42 --- /dev/null +++ b/src/main/java/org/codedifferently/MysteryReceiptCalculator.java @@ -0,0 +1,15 @@ +package org.codedifferently; + +public class MysteryReceiptCalculator { + // Calculates the sum of the three prices given as arguments. + public static double calculateSubtotal(double price1, double price2, double price3) { + return price1 + price2 + price3; + } + + // Calculates the final price with the tax rate and discount rate applied. + public static double calculateFinalTotal(double subtotal, double tax, double discount) { + double finalTotal = Math.ceil((subtotal + (subtotal * tax)) * 100) / 100; + finalTotal = Math.ceil((finalTotal - (finalTotal * discount)) * 100) / 100; + return finalTotal; + } +} diff --git a/src/main/java/org/codedifferently/MysteryReceiptPrinter.java b/src/main/java/org/codedifferently/MysteryReceiptPrinter.java new file mode 100644 index 0000000..83d90f8 --- /dev/null +++ b/src/main/java/org/codedifferently/MysteryReceiptPrinter.java @@ -0,0 +1,60 @@ +package org.codedifferently; +import java.util.*; + +class MysteryReceiptPrinter { + static String storeName = "Jayden's Emporium"; + + // Prints the receipt out to the console. + public static void printReceipt(String name, double budget, String coupon) { + // Creates a random object that will be used for any random elements. + Random random = new Random(); + + // Stores a random visitor ID in a variable. + int visitorID = RandomDataGenerator.generateVisitorID(random); + + // Stores a random receipt code in a variable. + String receiptCode = RandomDataGenerator.generateReceiptCode(name, visitorID); + + // Stores three random prices in three different variables. + double price1 = RandomDataGenerator.generatePrice(random); + double price2 = RandomDataGenerator.generatePrice(random); + double price3 = RandomDataGenerator.generatePrice(random); + + // Stores a random tax rate in a variable. + double tax = RandomDataGenerator.generateTax(random); + + // Stores the subtotal in a variable. + double subtotal = MysteryReceiptCalculator.calculateSubtotal(price1, price2, price3); + + // Prints the receipt to the console + System.out.println("\n" + MysteryReceiptPrinter.storeName); + System.out.println("----------------------------------------------------------------"); + System.out.println("ID Code: " + visitorID); + System.out.println("Receipt Code: " + receiptCode); + System.out.printf("Item 1: $%.2f%n", price1); + System.out.printf("Item 2: $%.2f%n", price2); + System.out.printf("Item 3: $%.2f%n", price3); + System.out.printf("Subtotal: $%.2f%n", subtotal); + System.out.println("\n---Price Adjustments---"); + System.out.println("Sales tax: " + ((int)(tax * 100)) + "%"); + + // Stores the discount rate in a variable. + double discount = CouponValidator.validateCoupon(coupon, random); + + // Stores the final total in a variable. + double finalTotal = MysteryReceiptCalculator.calculateFinalTotal(subtotal, tax, discount); + + // Prints the discount only if one exists. + if (discount != 0.0) { + System.out.println("Discount: " + (int)(discount * 100) + "%"); + } + System.out.printf("\nFinal Total: $%.2f%n", finalTotal); + + // Checks to see if the user has enough money to complete the transaction. + if (finalTotal > budget) { + System.out.printf("\nTransaction failed!\nInsufficient Funds. User is short by $%.2f dollars...\nCancelling transaction.", Math.abs(budget - finalTotal)); + } else { + System.out.println("\nTransaction succeeded!\nThank you for shopping at Jayden's Emporium!"); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/codedifferently/RandomDataGenerator.java b/src/main/java/org/codedifferently/RandomDataGenerator.java new file mode 100644 index 0000000..4934ebc --- /dev/null +++ b/src/main/java/org/codedifferently/RandomDataGenerator.java @@ -0,0 +1,24 @@ +package org.codedifferently; +import java.util.Random; + +public class RandomDataGenerator { + // Generates a random receipt code for the user. + public static String generateReceiptCode(String name, int visitorID) { + return (name.substring(0, 3)).toUpperCase() + "-" + visitorID; + } + + // Generates a random visitor ID for the user. + public static int generateVisitorID(Random random) { + return random.nextInt(1000, 1000000); + } + + // Generates a random item price. + public static double generatePrice(Random random) { + return ((double) random.nextInt(1000, 20000) / 100); + } + + // Generates a random sales tax rate for the store. + public static double generateTax(Random random) { + return ((double) (Math.round(random.nextDouble(1.5, 7.5))) / 100); + } +}