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
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.

2 changes: 1 addition & 1 deletion .idea/modules.xml

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
15 changes: 9 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.codedifferently</groupId>
<artifactId>untitled</artifactId>
<groupId>com.kennedy</groupId>
<artifactId>java-receipt-generator</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>25</maven.compiler.source>
<maven.compiler.target>25</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
</project>

27 changes: 27 additions & 0 deletions src/main/java/CodeGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class CodeGenerator {

public static String buildReceiptCode(String name, int visitId) {


String cleaned = (name == null) ? "GUEST" : name.trim(); // trim
String upper = cleaned.toUpperCase(); // toUpperCase

String key;

if (upper.length() >= 3) { // length

key = upper.substring(0, 3); // substring
} else {

key = (upper + "XXX").substring(0, 3);
}

char marker = !upper.isEmpty() ? upper.charAt(0) : 'G'; // charAt

return key + "-" + marker + "-" + visitId;



}
}

6 changes: 6 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {

public static void main(String[] args) {
ReceiptApp.run();
}
}
72 changes: 72 additions & 0 deletions src/main/java/PriceGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.Random;

public class PriceGenerator {

public static int generateVisitId(Random random) {
return 1000 + random.nextInt(9000); // 1000–9999
}

public static double[] generateItemPrices(Random random) {


double[] items = new double[3];
for (int i = 0; i < items.length; i++) {


// $2.50 to $35.00
double raw = 2.50 + (32.50 * random.nextDouble());
items[i] = roundMoney(raw);
}
return items;
}

public static double generateTaxRate(Random random) {
// 4% to 9%
return 0.04 + (0.05 * random.nextDouble());
}

public static double subtotal(double[] items) {
double sum = 0;

for (double p : items) sum += p;

return roundMoney(sum);
}

// Lucky visit bonus (if last digit is 7 => 7% off)
public static double luckyVisitBonusDiscount(int visitId, double subtotal) {
if (visitId % 10 == 7) {
return roundMoney(subtotal * 0.07);
}
return 0.0;
}

// Mystery fee or discount
public static double mysteryAdjustment(Random random, double subtotal) {
boolean fee = random.nextBoolean();

double percent = 0.02 + (0.04 * random.nextDouble()); // 2%..6%

double amount = roundMoney(subtotal * percent);

return fee ? amount : -amount;
}

// VIP coupon randomly works + case-insensitive check
public static double couponDiscount(Random random, String couponCode, double subtotal) {
if (couponCode != null && couponCode.trim().equalsIgnoreCase("VIP")) {
boolean works = random.nextInt(10) < 6;// 60%
if (works) {
return roundMoney(subtotal * 0.15);
}
}
return 0.0;
}

public static double roundMoney(double value) {
return Math.round(value * 100.0) / 100.0;



}
}
21 changes: 21 additions & 0 deletions src/main/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# RANDOM RECEIPT GENERATOR
## Developer : Kenny Edelin

### Main.java
In this class, you will see my main method that allows my receipt to RUN.

### Receipt.java
In this class, you will see sections for User Inout, as well as Random Data generations. User Input, consist of my three question inquiring about Name, Budget and Vip code.

### PriceGenerator.java
In this class, you will see mostly random data generators. Generating item prices, as well as random discounts, mystery adjustments, tax rates and rounded totals. Also variable and output types that I need.

### CodeGenerator.java
This class consist of if/else statements that build the receipt code. These if/else statements are based on the length of user input. There are also return statements to specify what my output should be.

### ReceiptGenerator.java
This class has all contents that the receipt includes, which in return calls for a lot of print statements. Also, I tried my best to separate each section on the receipt with hyphens.

### CLOSING
Thank you for using and analyzing my Random Receipt Generator with me!
!
61 changes: 61 additions & 0 deletions src/main/java/ReceiptApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Random;
import java.util.Scanner;

public class ReceiptApp {

public static void run() {
Scanner scanner = new Scanner(System.in);
Random random = new Random();

// User Input
System.out.print("What is your name? ");
String name = scanner.nextLine().trim();

System.out.print("What is your budget? ");
double budget = scanner.nextDouble();
scanner.nextLine();

System.out.print("What is your coupon code: ");
String coupon = scanner.nextLine();

// Random Input
int visitId = PriceGenerator.generateVisitId(random);
double[] items = PriceGenerator.generateItemPrices(random);
double taxRate = PriceGenerator.generateTaxRate(random);

String receiptCode = CodeGenerator.buildReceiptCode(name, visitId);

double subtotal = PriceGenerator.subtotal(items);
double tax = subtotal * taxRate;

double luckyDiscount = PriceGenerator.luckyVisitBonusDiscount(visitId, subtotal);
double mystery = PriceGenerator.mysteryAdjustment(random, subtotal);
double couponDiscount = PriceGenerator.couponDiscount(random, coupon, subtotal);

double total = subtotal + tax + mystery - luckyDiscount - couponDiscount;
total = PriceGenerator.roundMoney(Math.max(0, total));

double remaining = PriceGenerator.roundMoney(budget - total);

ReceiptPrinter.print(
"Ken's Grocery Store",
name,
visitId,
receiptCode,
items,
subtotal,
taxRate,
tax,
mystery,
luckyDiscount,
coupon,
couponDiscount,
total,
budget,
remaining
);

scanner.close();
}
}

94 changes: 94 additions & 0 deletions src/main/java/ReceiptPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
public class ReceiptPrinter {

public static void print(


String storeName,

String customerName,

int visitId,

String receiptCode,

double[] items,
double subtotal,
double taxRate,
double tax,
double mysteryAdjustment,
double luckyBonusDiscount,

String coupon,

double couponDiscount,
double total,
double budget,
double remaining


) {
System.out.println();

System.out.println("----- Welcome to " + storeName + " -----");

System.out.println("Customer: " + customerName);

System.out.println("Visit ID: " + visitId);

System.out.println("Receipt Code: " + receiptCode);

System.out.println("----------------------------------------");

for (int i = 0; i < items.length; i++) {
System.out.printf("Item %d: $%.2f%n", (i + 1), items[i]);
}

System.out.println("----------------------------------------");

System.out.printf("Subtotal: $%.2f%n", subtotal);

System.out.printf("Tax (%.1f%%): $%.2f%n", taxRate * 100.0, tax);

if (mysteryAdjustment > 0) {
System.out.printf("Mystery Fee: +$%.2f%n", mysteryAdjustment);

} else if (mysteryAdjustment < 0) {
System.out.printf("Mystery Discount: -$%.2f%n", Math.abs(mysteryAdjustment));
}

if (luckyBonusDiscount > 0) {
System.out.printf("Lucky Visit Bonus: -$%.2f%n", luckyBonusDiscount);
}

if (couponDiscount > 0) {
System.out.printf("Coupon (%s) Applied: -$%.2f%n", coupon.trim().toUpperCase(), couponDiscount);
} else {
System.out.printf("Coupon (%s): Not Applied%n", coupon.trim());
}

System.out.println("----------------------------------------");

System.out.printf("FINAL TOTAL: $%.2f%n", total);

System.out.printf("Budget: $%.2f%n", budget);

if (remaining >= 0) {
System.out.printf("Budget Remaining: $%.2f%n", remaining);
if (remaining < 5) {
System.out.println("WARNING: Tight budget. You're cutting it close.");
}
} else {
System.out.printf("Short By: $%.2f%n", Math.abs(remaining));

System.out.println("WARNING: You do not have enough budget to complete this order.");

}

System.out.println("----------------------------------------");




System.out.println("Thank you. Come again.");
}
}
17 changes: 0 additions & 17 deletions src/main/java/org/codedifferently/Main.java

This file was deleted.