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/src/main/java/org/codedifferently/Main.java b/src/main/java/org/codedifferently/Main.java index 8a571aa..d772b63 100644 --- a/src/main/java/org/codedifferently/Main.java +++ b/src/main/java/org/codedifferently/Main.java @@ -1,17 +1,23 @@ package org.codedifferently; -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.*; //Imports a variety of classes 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!")); + public static void main(String[] args) { - 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); - } + Scanner input = new Scanner(System.in); //Scanner object named "input" + + //This chunk of code prompts the user to enter their information + System.out.print("Enter your name "); + String customerName = input.nextLine(); + System.out.print("Enter your budget (numbers only) "); + String customerBudget = input.nextLine(); + System.out.print("Enter a coupon code "); + String couponCode = input.nextLine().toUpperCase(); + System.out.println(); + + Receipt customerReceipt = new Receipt(); //Creates a new receipt object used to call the receipt methods + customerReceipt.displayReceipt(customerName,Double.parseDouble(customerBudget),couponCode); //calls the displayReceipt method to display all the receipt information + input.close(); } + } diff --git a/src/main/java/org/codedifferently/README.md b/src/main/java/org/codedifferently/README.md new file mode 100644 index 0000000..5a1549d --- /dev/null +++ b/src/main/java/org/codedifferently/README.md @@ -0,0 +1,49 @@ +# How my project works +Once the program is executed, the user is prompted to enter in 3 things: +- their name +- their budget amount +- a coupon code if they have one + +Once the user enters in those 3 things then the program displays the following output: +- The name of the store they just shopped at (I set the store name to be Walmart) +- Their visit ID and receipt code +- The subtotal, tax, discounts, and total bill amounts +- How much of their budget is remaining or how much the user is short + +For the creativity requirement, I made two methods in the receipt class: +- The generateWelcomeMessage method cycles between changing the store name and a welcome message based on a random number generator +- The generateReceiptTagline method cycles between displaying two receipt tagline messages based on a random number generator + + +# Sample Output + +The user enters the following info: +- Name: Bryant Ferguson +- Budget: 333 +- Coupon Code: (the user left it blank as they didn't have a code to enter) + +Here is what the output would look like: +- Welcome to Walmart +- Visit ID: 2825 +- Receipt Code bf2300 +- Your Item prices are: 58.0, 72.0, 17.0 +- Subtotal: 147.0 +- Tax: 9.0 +- You have a $0.0 discount off your bill +- Your final total is 156.0 +- You have $177.0 left in your budget +- Have a WONDERFUL day! + +# Java Concepts Used +The following concepts were used: +- Math methods such as Math.round +- String methods such as substring, length, trim, and toLowerCase +- Switch and if/else statements +- Random class methods such as nextDouble and nextInt +- Object creation using the new keyword +- Collecting user input using the Scanner class methods such as nextLine +- Cycling between using print and println methods based on how I wanted things formatted +- Organize logic across multiple classes + + + diff --git a/src/main/java/org/codedifferently/Receipt.java b/src/main/java/org/codedifferently/Receipt.java new file mode 100644 index 0000000..c9a5eb9 --- /dev/null +++ b/src/main/java/org/codedifferently/Receipt.java @@ -0,0 +1,94 @@ +package org.codedifferently; +import java.util.Random; //Imports the random class + +public class Receipt { + + Random random = new Random(); //Creates a Random object named "random" + ReceiptCalculator calculator = new ReceiptCalculator(); //Creates a ReceiptCalculator object named "calculator" + + //Returns a random number ranging from 1000 to 9999 + public int generateVistId(){ + return random.nextInt(1000,10_000); + } + + //Returns the receipt code consisting of the user's name and a random number ranging from 1000 to 9999 + public String generateReceiptCode(String customerName) { + customerName = customerName.trim().toLowerCase(); + int index = customerName.indexOf(" "); + String firstName; + String lastName; + + if (index == -1) { + firstName = customerName; + lastName = ""; + } else { + firstName = customerName.substring(0, index); + lastName = customerName.substring(index + 1); + } + + if (firstName.length() > 3) { + firstName = firstName.substring(0, 3); + } + + if (lastName.length() > 3) { + lastName = lastName.substring(0, 3); + } + + return firstName + lastName + random.nextInt(1000, 10_000); + } + + //Prints out a Welcome message for the receipt based on the value returned from the random number generator + public void generateWelcomeMessage(){ + int value = random.nextInt(1, 7); + if (value % 2 != 0) { + System.out.println("Welcome to Walmart"); + } else { + System.out.println("Welcome to Target"); + } + } + + //Prints out a closing message for the receipt based on the value returned from the random number generator + public void generateReceiptTagline() { + int value = random.nextInt(1, 7); + if (value % 2 == 0) { + System.out.println("Have a WONDERFUL day!"); + } else { + System.out.println("Have a FANTASTIC day!"); + } + } + + //Prints out all the receipt information based on the values entered in by the user + public void displayReceipt(String customerName, double customerBudget, String couponCode){ + double item1 = calculator.generateItemPrice(); + double item2 = calculator.generateItemPrice(); + double item3 = calculator.generateItemPrice(); + double subTotal = calculator.calcBillSubtotal(item1, item2, item3); + double tax = calculator.calcTaxAmount(subTotal); + double discount = calculator.getDiscountAmt(couponCode); + double finalTotal = calculator.calcFinalTotal(couponCode, item1, item2, item3, customerBudget, tax); + + generateWelcomeMessage(); + System.out.println("---------------------------------"); + System.out.println("Customer Name: " + customerName); + System.out.println("Your budget is $" + customerBudget); + System.out.println("Visit ID: " + generateVistId()); + System.out.println("---------------------------------"); + System.out.println("Receipt Code " + generateReceiptCode(customerName)); + System.out.println("Your Item prices are: $" + item1 + ", $" + item2 + ", $" + item3); + System.out.println("Subtotal: $" + subTotal); + System.out.println("Tax: $" + tax); + System.out.println("You have a $" + discount + " discount off your bill"); + System.out.println("---------------------------------"); + System.out.println("Your final total is $" + finalTotal); + + if (customerBudget >= finalTotal){ + System.out.println("You have $" + (customerBudget - finalTotal) + " left in your budget" ); + } + else{ + System.out.println("You are $" + (finalTotal - customerBudget) + " dollars short"); + } + + generateReceiptTagline(); + } + +} //ends Receipt class diff --git a/src/main/java/org/codedifferently/ReceiptCalculator.java b/src/main/java/org/codedifferently/ReceiptCalculator.java new file mode 100644 index 0000000..509dc7d --- /dev/null +++ b/src/main/java/org/codedifferently/ReceiptCalculator.java @@ -0,0 +1,65 @@ +package org.codedifferently; + +import java.util.Random; //imports the Random class + +public class ReceiptCalculator { + + Random random = new Random(); //Creates a Random object named "random" + double discount; + + //Returns a random value ranging from 0 to 100 + public int generateItemPrice() { + return random.nextInt(0, 101); + } + + //Returns the raw total of the bill before tax and discounts are applied + public double calcBillSubtotal(double firstItemPrice, double secondItemPrice, double thirdItemPrice) { + return firstItemPrice + secondItemPrice + thirdItemPrice; + } + + //Takes in the bill amount and returns the tax rate amount + public double calcTaxAmount(double billAmt) { + return Math.round(billAmt * (random.nextDouble(0.0, 8.25) / 100)); + } + + //Checks if the user enters a valid coupon code + public boolean isValidCoupon(String couponCode) { + switch (couponCode) { + case "ABC123", "123ABC", "CODE_DIFFERENTLY": + return true; + default: + return false; + } + } + + //Returns the discount that the user receives based on the coupon code they enter in + public double getDiscountAmt(String couponCode) { + switch (couponCode) { + case "ABC123": + return discount = 10.00; + case "123ABC": + return discount = 20.00; + case "CODE_DIFFERENTLY": + return discount = 50.00; + default: + return 0.0; + } + } + + //Returns the final bill amount including tax and any discounts applied + public double calcFinalTotal(String couponCode, double firstItemPrice, double secondItemPrice, double thirdItemPrice, double budget, double tax) { + double subTotal = calcBillSubtotal(firstItemPrice, secondItemPrice, thirdItemPrice); + double finalTotal; + + if (isValidCoupon(couponCode)) { + finalTotal = Math.round((subTotal + tax) - discount); + if (finalTotal < 0) { + return 0; + } + } + + finalTotal = Math.round((subTotal + tax) - discount); + return finalTotal; + } + +} //ends ReceiptCalculator class