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

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

4 changes: 3 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.

3 changes: 2 additions & 1 deletion .idea/misc.xml

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

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
66 changes: 66 additions & 0 deletions RECEIPTREADME.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import java.util.*;
38 changes: 38 additions & 0 deletions src/main/java/org/codedifferently/CouponValidator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
26 changes: 18 additions & 8 deletions src/main/java/org/codedifferently/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package org.codedifferently;
import java.util.*;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
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!"));
Scanner sc = new Scanner(System.in);

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);
}
// 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);
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/codedifferently/MysteryReceiptCalculator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/codedifferently/MysteryReceiptPrinter.java
Original file line number Diff line number Diff line change
@@ -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!");
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/codedifferently/RandomDataGenerator.java
Original file line number Diff line number Diff line change
@@ -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);
}
}