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.

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: 0 additions & 1 deletion .idea/vcs.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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<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>

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

This file was deleted.

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

//print console output
public class ConsolePrint {
public void printResults(int visitID, String fullReceiptCode, StoreItem item1, StoreItem item2, StoreItem item3, double subTotal, double taxRate, boolean isDiscount, double discountRate, double fee, boolean isValidCoupon, String couponCode, int discountVal, double finalTotal, double budget){

//instantiate object for price calculation methods
PriceCalculations calculatePrice = new PriceCalculations();

System.out.println("--------- Welcome to Alex's Electronics ----------");
System.out.println("Visit ID: " + visitID);
System.out.println("Receipt Code: " + fullReceiptCode);
//System.out.println(item1.getName() + ": $" + item1.getPrice());
System.out.println(item1.getName() + ": $" + Math.round(item1.getPrice() * 100.0) / 100.0);
// System.out.println(item2.getName() + ": $" + item2.getPrice());
System.out.println(item2.getName() + ": $" + Math.round(item2.getPrice() * 100.0) / 100.0);
//System.out.println(item3.getName() + ": $" + item3.getPrice());
System.out.println(item3.getName() + ": $" + Math.round(item3.getPrice() * 100.0) / 100.0);
//System.out.println("Subtotal: $" + subTotal);
System.out.println("Subtotal: $" + Math.round(subTotal * 100.0) / 100.0);
//System.out.println("Sales Tax: " + taxRate +"%");
System.out.println("Sales Tax: " + Math.round(taxRate * 100.0) / 100.0 + "%");


if(isDiscount){
System.out.println("--------- You received a lucky discount! ---------");
//System.out.println("Lucky Discount: " + discountRate + "%");
System.out.println("Lucky Discount: " + (Math.round(discountRate * 100.0)/100.0) + "%");

subTotal = calculatePrice.extraValues(subTotal, true, 0, discountRate);
//System.out.println("New Subtotal: $" + subTotal);
System.out.println("New Subtotal: $" + (Math.round(subTotal*100.0)/100.0));

//System.out.println("You will receive: " + subTotal*(discountRate/100) + " off");

}else{
System.out.println("--------- A small processing fee was added to your order---------");
//System.out.println("Processing Fee: " + "$" + fee);
System.out.println("Processing Fee: " + "$" + (Math.round(fee * 100.0)/100.0));

subTotal = calculatePrice.extraValues(subTotal, false, fee, 0);
//System.out.println("New Subtotal: $" + subTotal);
System.out.println("New Subtotal: $" + (Math.round(subTotal*100.0)/100.0));

}
if(isValidCoupon){
System.out.println("---------- Coupon Code *" + couponCode.toUpperCase() + "* Accepted ----------");
//System.out.println("Discount: " + discountVal + "%");
System.out.println("Discount: " + (Math.round(discountVal * 100.0)/100.0) + "%");

subTotal = calculatePrice.extraValues(subTotal, true, 0, discountVal);
//System.out.println("New Subtotal: $" + subTotal);
System.out.println("New Subtotal: $" + (Math.round(subTotal*100.0)/100.0));

//System.out.println("Your will receive: " + subTotal*(discountVal/100) + " off");
}
System.out.println("Final Total + Tax: $" + Math.round(finalTotal*100.0)/100.0);
if(budget - finalTotal >= 0){
System.out.println("You have $" + Math.round((budget-finalTotal)*100.0)/100.0 + " remaining in your budget");
}else{
System.out.println("You need $" + Math.round(Math.abs(budget-finalTotal)*100.0)/100.0 + " more in your budget to complete this transaction");
}
//whether user can afford with their budget or if they are short

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

public class CouponChecker {

//check if a coupon is valid
public boolean couponCheck(String[] acceptedCoupons, String providedCoupon){
boolean isValidCoupon = false;
for (int i=0; i< acceptedCoupons.length; i++){
if(acceptedCoupons[i].equals(providedCoupon.toUpperCase())){
isValidCoupon = true;
//discountVal = discountVal * (i+1);
return isValidCoupon;

}
//don't get a discount
}
return false;
}


//set discount value of coupon
public int discountRate(boolean isCoupon, String[] acceptedCoupons, String providedCoupon){
int discountVal = 25;
if (isCoupon){
for (int i=0; i< acceptedCoupons.length; i++){
if(acceptedCoupons[i].equals(providedCoupon.toUpperCase())){

discountVal = discountVal * (i+1);
return discountVal;
}
//don't get a discount
}
}
return 0;
}

//apply coupon discount to final pricing
public double applyCoupon(boolean isValidCoupon, double subTotal, int discountVal){
if (isValidCoupon){
subTotal = subTotal - (subTotal*discountVal/100);
return subTotal;
}
else{
return subTotal;
}
}
}
80 changes: 80 additions & 0 deletions src/main/java/org/codedifferently/ReceiptApp/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.codedifferently.ReceiptApp;

import java.util.Scanner;

//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 {
public static void main(String[] args) {

//these are the accepted coupon codes
String[] couponCodes = {"25OFF", "50OFF", "75OFF", "100OFF"};

//get username
System.out.println("Please enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.next();

//get budget
System.out.println("Please enter your budget: ");
double budget = scanner.nextDouble();

//get coupon code
System.out.println("Please enter your coupon code: ");
String couponCode = scanner.next();

//instantiate objects so we can use their methods
RandomGenerations randomItem = new RandomGenerations();
PriceCalculations calculatePrice = new PriceCalculations();
CouponChecker coupon = new CouponChecker();
ConsolePrint output = new ConsolePrint();


//generate random values for discount, fee and rates
boolean isDiscount = randomItem.discount();
double fee = randomItem.fee();
double taxRate = randomItem.tax();
double discountRate = randomItem.tax();

//generate random item IDs
int itemSelectionOne = randomItem.item();
int itemSelectionTwo = randomItem.item();
int itemSelectionThree = randomItem.item();

//generate random items mapped from ID's
StoreItem item1 = new StoreItem(itemSelectionOne, randomItem.prices());
StoreItem item2 = new StoreItem(itemSelectionTwo, randomItem.prices());
StoreItem item3 = new StoreItem(itemSelectionThree, randomItem.prices());

//store item prices in an array to use with subtotal method
double[] itemPrices = {item1.getPrice(), item2.getPrice(), item3.getPrice()};

//calculate sum of item prices
double subTotal = calculatePrice.subtotal(itemPrices);

//calculate price after incorporating fee/lucky discount, tax, etc..
double subFinalTotal = calculatePrice.finalTotal(subTotal, isDiscount, fee, taxRate, discountRate);

//generate random visitID
int visitID = randomItem.visitIDGen();

//generate receiptCode
String fullReceiptCode = randomItem.receiptCode(username, visitID);

//check whether coupon is valid
boolean isValidCoupon = coupon.couponCheck(couponCodes, couponCode);

//if coupon is valid, assigns the discount value
int discountVal = coupon.discountRate(isValidCoupon, couponCodes, couponCode);

//calculate final total after all coupons, taxes, fees, etc. applied
double finalTotal = coupon.applyCoupon(isValidCoupon, subFinalTotal, discountVal);

//moved all print statements and print logic into a class called console print
//method print results takes all of these values we found and prints formatted info to console
output.printResults(visitID, fullReceiptCode, item1, item2,
item3, subTotal, taxRate, isDiscount, discountRate,
fee, isValidCoupon, couponCode, discountVal, finalTotal, budget);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.codedifferently.ReceiptApp;

public class PriceCalculations {

//sum list of doubles
public double subtotal(double[] prices){
double subtotal = 0.0;
for (int i=0; i<prices.length; i++){
subtotal = subtotal + prices[i];
}
return subtotal;
}

//calculate cost of tax
public double tax(double billPrice, double taxPercent){
return billPrice*(taxPercent/100);
}

//get cost after factoring in a fee or discount. cannot be charged a fee and discount at the same time by default
public double extraValues(double subTotal, boolean discount, double fee, double discountPercent){

if (discount){
return subTotal - (subTotal* discountPercent/100) ;
}else{
return subTotal + fee;
}

}

//sum tax and values after factoring in discount, fees and taxes
public double finalTotal(double subtotal, boolean discount, double fee, double taxPercent, double discountPercent){
//round
double subtotalPostFee = extraValues(subtotal, discount, fee, discountPercent);

double taxValue = tax(subtotalPostFee, taxPercent);

//multiply value of math.round by 100.0, then divide by 100.0 to get rounded value at 2 decimals
return Math.round((subtotalPostFee + taxValue) * 100.0) / 100.0;
}

}
Loading