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
8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

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
28 changes: 17 additions & 11 deletions src/main/java/org/codedifferently/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package org.codedifferently;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import java.util.*; //Imports a variety of classes
public class Main {
static void main() {
//TIP Press <shortcut actionId="ShowIntentionActions"/> 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 <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
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();
}

}
49 changes: 49 additions & 0 deletions src/main/java/org/codedifferently/README.md
Original file line number Diff line number Diff line change
@@ -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



94 changes: 94 additions & 0 deletions src/main/java/org/codedifferently/Receipt.java
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions src/main/java/org/codedifferently/ReceiptCalculator.java
Original file line number Diff line number Diff line change
@@ -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