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.

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
45 changes: 33 additions & 12 deletions src/main/java/org/codedifferently/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
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.Scanner;

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!"));

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);
}
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);

System.out.println("Enter your name: ");
String name = scr.nextLine();

System.out.println("Enter your budget: ");
double budget = scr.nextDouble();
scr.nextLine(); // consume leftover newline

System.out.println("Enter a coupon code: ");
String couponCode = scr.nextLine();

// Create a Receipt object
Receipt receipt = new Receipt();
receipt.generateItemPrices(); // generates 3 separate items
receipt.calculateSubtotal();
receipt.applyCoupon(couponCode);
receipt.calculateTax();
receipt.calculateFinalTotal();

// Create a Visit object
Visit visit = new Visit(name);
visit.generateReceiptCode();
boolean withinBudget = visit.checkBudget(receipt.finalTotal, budget);

// Print the receipt
System.out.println("\n--- Receipt ---");
visit.printReceiptCode();
receipt.printReceiptDetails();
System.out.println("Within budget: " + withinBudget);
}
}
80 changes: 80 additions & 0 deletions src/main/java/org/codedifferently/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Receipt Generator in Java
What It Does

This program generates a simple receipt for three items. It calculates the subtotal, applies a coupon, adds tax, checks the budget, and gives a receipt code.

• **How It Works**

1. Ask the user for their name, budget, and coupon code.


2. Generate three random item prices.


3. Add the prices to get the subtotal.


4. Apply a 50% discount if the coupon code is SAVE50.


5. Calculate tax at 8%.


6. Calculate the final total.


7. Generate a receipt code from the first two letters of the name + random 4-digit number.


8. Check if the total is within the budget.


9. Print a simple receipt with all the details.

• **Sample Output**

Enter your name:
Alice

Enter your budget:
150

Enter a coupon code:
SAVE50

--- Receipt ---

Receipt Code: AL4821

Item 1 Price: $42.34

Item 2 Price: $57.89

Item 3 Price: $33.12

Subtotal: $133.35

Discounted Total: $66.67

Tax: $5.33

Final Total: $72.00

Within budget: true


• **Java Concepts Used**

Classes and objects

Methods

Random numbers

User input with Scanner

Variables and data types

Conditional statements (if)

Printing output with System.out.println
59 changes: 59 additions & 0 deletions src/main/java/org/codedifferently/Receipt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.codedifferently;

import java.util.Random;

public class Receipt {
double item1;
double item2;
double item3;
double subtotal;
double discountedTotal;
double taxAmount;
double finalTotal;

private final double TAX_RATE = 0.08; // 8% tax

// Generate 3 separate random item prices
public void generateItemPrices() {
Random random = new Random();
item1 = 10 + (90 * random.nextDouble());
item2 = 10 + (90 * random.nextDouble());
item3 = 10 + (90 * random.nextDouble());
}

// Calculate subtotal
public void calculateSubtotal() {
subtotal = item1 + item2 + item3;
}

// Apply coupon
public void applyCoupon(String couponCode) {
if (couponCode.equalsIgnoreCase("SAVE50")) {
discountedTotal = subtotal * 0.5;
} else {
discountedTotal = subtotal;
}
}

// Calculate tax
public void calculateTax() {
taxAmount = discountedTotal * TAX_RATE;
}

// Calculate final total
public void calculateFinalTotal() {
finalTotal = discountedTotal + taxAmount;
}

// Print receipt details in a simple way
public void printReceiptDetails() {
System.out.println("Item 1 Price: $" + item1);
System.out.println("Item 2 Price: $" + item2);
System.out.println("Item 3 Price: $" + item3);
System.out.println("Subtotal: $" + subtotal);
System.out.println("Discounted Total: $" + discountedTotal);
System.out.println("Tax: $" + taxAmount);
System.out.println("Final Total: $" + finalTotal);
}

}
26 changes: 26 additions & 0 deletions src/main/java/org/codedifferently/Visit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.codedifferently;

public class Visit {
String name;
String receiptCode;

public Visit(String name) {
this.name = name;
}

// Generate receipt code: first 2 letters of name + random 4-digit number
public void generateReceiptCode() {
int visitId = (int) (Math.random() * 9000) + 1000;
receiptCode = name.substring(0, Math.min(2, name.length())).toUpperCase() + visitId;
}

// Check if final total is within the user's budget
public boolean checkBudget(double finalTotal, double budget) {
return finalTotal <= budget;
}

// Print the receipt code
public void printReceiptCode() {
System.out.println("Receipt Code: " + receiptCode);
}
}