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/java-receipt-generator.iml b/.idea/java-receipt-generator.iml index d6ebd48..63cead1 100644 --- a/.idea/java-receipt-generator.iml +++ b/.idea/java-receipt-generator.iml @@ -2,7 +2,10 @@ - + + + + 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/README.md b/src/README.md new file mode 100644 index 0000000..26b1aff --- /dev/null +++ b/src/README.md @@ -0,0 +1,45 @@ +# 🧾 Walmart Receipt Generator + +## Overview +This project uses the Random, String, and Math classes to generate a randomized receipt that simulates the experience of checking out at Walmart. + +--- + +## Main Class + +The Main class serves as the central hub of the project. It calls methods from the UserInput class to gather the user’s first name, last name, coupon code, and budget. Using this information, the program generates randomized +tax rates and discount rates from MathCalc and StringLogic to calculate the final total. + +--- + +## MathCalc Class + +The MathCalc class handles all math-related calculations using the Math class. It includes a method called generateRandomNumber() that generates a random number within a specified lower and upper bound. This custom random number logic is also used in the generateCouponDiscount() and generateTaxRate() methods, where percentage values are converted to decimals before being returned. + +The class also calculates the final purchase total using the calculateFinalTotal() method, which takes the subtotal, tax rate, and coupon discount as parameters. Lastly, the calculateRemainingBudget() method determines how much budget remains after the purchase. + +--- + +## StringLogic Class + +The StringLogic class contains all string-related operations using the String class. It generates a receipt code by combining the first three letters of the user’s first and last name with a randomly generated number. This class also validates coupon codes by checking whether the input starts with the letter c and contains a hyphen. + +--- + +## UserInput Class + +All user input is handled within the UserInput class. Each method prompts the user for input and stores it as either a String for the first name, last name, and coupon code, or a double for the budget. These values are then passed back to the Main class for processing and calculation. + +--- + +## Sample Output + +Below are screenshots showcasing example outputs generated by the program. + +![image](src/main/images/walmart-receipt1.png) + +Here we have my name for the input Name, as well as a budget, and the correct coupon code. + +![image](src/main/images/walmart-receipt2.png) + +In this image, we display the final result of the receipt code, an incorrect coupon was implemented here. \ No newline at end of file diff --git a/src/main/images/walmart-receipt1.png b/src/main/images/walmart-receipt1.png new file mode 100644 index 0000000..f32ea00 Binary files /dev/null and b/src/main/images/walmart-receipt1.png differ diff --git a/src/main/images/walmart-receipt2.png b/src/main/images/walmart-receipt2.png new file mode 100644 index 0000000..2adc67a Binary files /dev/null and b/src/main/images/walmart-receipt2.png differ diff --git a/src/main/java/org.codedifferently/Main.java b/src/main/java/org.codedifferently/Main.java new file mode 100644 index 0000000..5681e98 --- /dev/null +++ b/src/main/java/org.codedifferently/Main.java @@ -0,0 +1,80 @@ +package org.codedifferently; + +public class Main { + + static void main() + { + //Gather all User Info from UserInput class methods + UserInput userInput = new UserInput(); + String firstName = userInput.promptUserName(true); + String lastName = userInput.promptUserName(false); + double budget = userInput.promptUserBudget(); + String couponCode = userInput.promptCouponCode(); + + //Print Walmart receipt with dashes for formatting + System.out.println("-----------------------------------------------"); + System.out.println("Welcome to Walmart!"); + + //Calculate visitID + MathCalc mathCalc = new MathCalc(); + int visitID = mathCalc.generateRandomNumber(1000,9999); + System.out.println("Visit ID: " + visitID); + + //then calculate receiptCode + StringLogic stringLogic = new StringLogic(); + String receiptCode = stringLogic.generateReceiptCode(firstName, lastName); + System.out.println("Receipt Code: " + receiptCode); + + //generate random item prices + double item1Price = mathCalc.generateRandomNumber(5,75); + double item2Price = mathCalc.generateRandomNumber(10,85); + double item3Price = mathCalc.generateRandomNumber(15, 105); + System.out.println("Item 1: $" + item1Price); + System.out.println("Item 2: $" + item2Price); + System.out.println("Item 3: $" + item3Price); + + //generate coupon rate if valid coupon is applied + double couponRate = (stringLogic.checkValidCoupon(couponCode)) + ? mathCalc.generateCouponDiscount() + : 0; + + //calculate total subtotal of all three items + double subTotal = item1Price + item2Price + item3Price; + + System.out.println("Subtotal: $" + subTotal); + System.out.println("-----------------------------------------------"); + + //then calculate a random taxRate + double taxRate = mathCalc.generateTaxRate(); + System.out.println("Tax Rate: " + (int)(taxRate * 100) + "%"); + + //apply coupons ßfor random %ß off as well, if invalid code do not apply discount. + if(couponRate > 0) { + System.out.println("Valid coupon entered!"); + System.out.println("Applied " + (couponRate * 100) + "% discount"); + } + else { + System.out.println("Invalid coupon code! No discount applied!"); + System.out.println("Make sure your coupons start with c-"); + } + + //calculate final total based on taxes and coupons + double finalTotal = mathCalc.calculateFinalTotal(subTotal, taxRate, couponRate); + System.out.println("Final total: $" + finalTotal); + + //get and return budget remaining for user to see. if no budget left, then notify them + double budgetRemaining = mathCalc.calculateRemainingBudget(budget, finalTotal); + if (budgetRemaining < 0) + { + System.out.println("Looks like the items are past your budget! \n" + + "You'll have to pay this amount to continue: \n$" + + mathCalc.calculateAbsoluteValue(budgetRemaining)); + } + else { + System.out.println("Budget Remaining: $" + budgetRemaining); + } + + System.out.println("Thank you for shopping at Walmart!"); + System.out.println("------------------------------------------------"); + } +} diff --git a/src/main/java/org.codedifferently/MathCalc.java b/src/main/java/org.codedifferently/MathCalc.java new file mode 100644 index 0000000..3548b6e --- /dev/null +++ b/src/main/java/org.codedifferently/MathCalc.java @@ -0,0 +1,37 @@ +package org.codedifferently; + +import java.util.Random; + +public class MathCalc { + + //helper class to generate a random number given a lowerBound and upperBound + public int generateRandomNumber(int lowerBound, int upperBound) { + Random random = new Random(); + return random.nextInt((upperBound - lowerBound) + 1) + lowerBound; + } + + //generates taxRate by picking a random number and converting the % to decimal. + public double generateTaxRate() { + return (generateRandomNumber(1,20)) * 0.01; + } + + //calculates the final total by adding taxes and subtracting discounts. + public double calculateFinalTotal(double subTotal, double taxRate, double discountRate) { + return Math.round(subTotal + (subTotal * taxRate) - (subTotal * discountRate)); + } + + //Calculates remaining budget by subtracting the two arguments and rounding. + public double calculateRemainingBudget(double budget, double finalTotal) { + return Math.round(budget - finalTotal); + } + + //Calculates the absolute value. + public double calculateAbsoluteValue(double num) { + return Math.abs(num); + } + + //Generates couponDiscount by generating a random number, then converting the % to decimal. + public double generateCouponDiscount() { + return generateRandomNumber(1,50) * 0.01; + } +} diff --git a/src/main/java/org.codedifferently/StringLogic.java b/src/main/java/org.codedifferently/StringLogic.java new file mode 100644 index 0000000..9fb099c --- /dev/null +++ b/src/main/java/org.codedifferently/StringLogic.java @@ -0,0 +1,25 @@ +package org.codedifferently; + +public class StringLogic { + + //generates a receipt code after receiving the first and lastName. takes the first 3 letters + //of both to include with a randomly generated number + public String generateReceiptCode(String firstName, String lastName) { + + String firstNameTruncate = (firstName.length() > 3) ? firstName.substring(0,3) : firstName; + String lastNameTruncate = (lastName.length() > 3) ? lastName.substring(0,3) : lastName; + String nameTruncated = firstNameTruncate + lastNameTruncate; + + MathCalc matchCalc = new MathCalc(); + int randomNum = matchCalc.generateRandomNumber(10,100); + return nameTruncated + randomNum; + } + + //validates coupon code, returns true if it starts with a c and contains a - + public boolean checkValidCoupon(String couponCode) { + if (couponCode.startsWith(("c")) && couponCode.contains("-")) { + return true; + } + return false; + } +} diff --git a/src/main/java/org.codedifferently/UserInput.java b/src/main/java/org.codedifferently/UserInput.java new file mode 100644 index 0000000..5008c66 --- /dev/null +++ b/src/main/java/org.codedifferently/UserInput.java @@ -0,0 +1,30 @@ +package org.codedifferently; + +import java.util.Scanner; + +public class UserInput { + + //Prompts for username, via scanner. if argument is false then it prompts for lastName. + public String promptUserName(boolean isFirstName) { + if(isFirstName) System.out.println("Welcome to the Budget Calculator!"); + System.out.println((isFirstName) ? "What is your first name?" : "What is your last name?"); + Scanner scan = new Scanner(System.in); + return scan.next(); + } + + //Prompts for user budget, via scanner. + public double promptUserBudget() { + System.out.println("Okay.., what's your budget lookin' like today?:"); + Scanner scan = new Scanner(System.in); + + return scan.nextDouble(); + } + + //Prompts for a coupon code, via scanner. + public String promptCouponCode() { + System.out.println("Alrighty.. put in your coupon code:"); + Scanner scan = new Scanner(System.in); + return scan.next(); + } + +} diff --git a/src/main/java/org/codedifferently/Main.java b/src/main/java/org/codedifferently/Main.java deleted file mode 100644 index 8a571aa..0000000 --- a/src/main/java/org/codedifferently/Main.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.codedifferently; - -//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!")); - - 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); - } - } -}