Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .idea/java-receipt-generator.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added src/main/images/walmart-receipt1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/images/walmart-receipt2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions src/main/java/org.codedifferently/Main.java
Original file line number Diff line number Diff line change
@@ -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("------------------------------------------------");
}
}
37 changes: 37 additions & 0 deletions src/main/java/org.codedifferently/MathCalc.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/org.codedifferently/StringLogic.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 30 additions & 0 deletions src/main/java/org.codedifferently/UserInput.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
17 changes: 0 additions & 17 deletions src/main/java/org/codedifferently/Main.java

This file was deleted.