From ec5bd28678bf3d7fff2b391a24b2d8b482e5502c Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Thu, 14 Oct 2021 15:47:27 -0400 Subject: [PATCH 01/11] Initial commit as Java Project --- .classpath | 10 ++++++++++ .project | 17 +++++++++++++++++ .settings/org.eclipse.jdt.core.prefs | 14 ++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..9f9bbb56 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 00000000..a3a2e917 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + CashRegister + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..8aabe8df --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=16 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=16 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=16 From c4882a993593848f4ec8d3449290df094511210b Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Thu, 14 Oct 2021 18:08:32 -0400 Subject: [PATCH 02/11] Added ability to read input file --- Input.txt | 3 +++ bin/.gitignore | 2 ++ src/Reader/MoneyReader.java | 26 ++++++++++++++++++++++++++ src/Register/Register.java | 30 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 Input.txt create mode 100644 bin/.gitignore create mode 100644 src/Reader/MoneyReader.java create mode 100644 src/Register/Register.java diff --git a/Input.txt b/Input.txt new file mode 100644 index 00000000..fba6116f --- /dev/null +++ b/Input.txt @@ -0,0 +1,3 @@ +2.12,3.00 +1.97,2.00 +3.33,5.00 \ No newline at end of file diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 00000000..a03ce606 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/Reader/ +/Register/ diff --git a/src/Reader/MoneyReader.java b/src/Reader/MoneyReader.java new file mode 100644 index 00000000..d68f2140 --- /dev/null +++ b/src/Reader/MoneyReader.java @@ -0,0 +1,26 @@ +package Reader; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + + +public class MoneyReader { + + private BufferedReader in; + + public MoneyReader(String fileName) throws FileNotFoundException { + in = new BufferedReader(new FileReader(fileName)); + } + + public String readPayment() throws IOException { + String line = in.readLine(); + return line; + + } + + public void close() throws IOException { + in.close(); + } +} diff --git a/src/Register/Register.java b/src/Register/Register.java new file mode 100644 index 00000000..67de7831 --- /dev/null +++ b/src/Register/Register.java @@ -0,0 +1,30 @@ +package Register; + +import java.io.IOException; + +import Reader.MoneyReader; + + +public class Register { + + public static void main(String[] args) throws IOException { + // TODO Auto-generated method stub + Register register = new Register(); + register.loadPayments("Input.txt"); + } + + public void loadPayments(String fileName) throws IOException{ + MoneyReader moneyReader = new MoneyReader(fileName); + String line; + + while((line = moneyReader.readPayment()) != null) { + double price = Double.parseDouble(line.split(",")[0]); + double amountGiven = Double.parseDouble(line.split(",")[1]); + + System.out.println(price + "," + amountGiven); + } + + moneyReader.close(); + } + +} From 306f046c96fc235290828ae711846560ac1eba30 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Fri, 15 Oct 2021 23:41:20 -0400 Subject: [PATCH 03/11] Added printing to file, Change class, and logic to output change --- Input.txt | 8 +- Output.txt | 9 ++ bin/.gitignore | 1 + src/Register/Change.java | 141 ++++++++++++++++++++++++++++ src/Register/Register.java | 88 +++++++++++++++-- src/{Reader => io}/MoneyReader.java | 2 +- src/io/MoneyWriter.java | 23 +++++ 7 files changed, 264 insertions(+), 8 deletions(-) create mode 100644 Output.txt create mode 100644 src/Register/Change.java rename src/{Reader => io}/MoneyReader.java (96%) create mode 100644 src/io/MoneyWriter.java diff --git a/Input.txt b/Input.txt index fba6116f..85a20bcf 100644 --- a/Input.txt +++ b/Input.txt @@ -1,3 +1,9 @@ +100,99 +1,1 +2,100 +20,30 2.12,3.00 1.97,2.00 -3.33,5.00 \ No newline at end of file +3.33,5.00 +1.85,2.00 +1.67,2.00 \ No newline at end of file diff --git a/Output.txt b/Output.txt new file mode 100644 index 00000000..5a65de97 --- /dev/null +++ b/Output.txt @@ -0,0 +1,9 @@ +Insufficient Funds +Exact Change Given +1 FIFTIES. 2 TWENTIES. 1 FIVE(S). 3 ONE(S). +1 TEN(S). +3 QUARTER(S). 1 DIME(S). 3 PENNIES. +3 PENNIES. +1 ONE(S). 2 QUARTER(S). 1 DIME(S). 1 NICKEL(S). 2 PENNIES. +1 DIME(S). 1 NICKEL(S). +33 PENNIES. diff --git a/bin/.gitignore b/bin/.gitignore index a03ce606..48e36722 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,2 +1,3 @@ /Reader/ /Register/ +/io/ diff --git a/src/Register/Change.java b/src/Register/Change.java new file mode 100644 index 00000000..6aa1b7f6 --- /dev/null +++ b/src/Register/Change.java @@ -0,0 +1,141 @@ +package Register; + +import java.text.DecimalFormat; + +public class Change { + private int hundreds; + private int fifties; + private int twenties; + private int tens; + private int fives; + private int ones; + private int quarters; + private int dimes; + private int nickels; + private int pennies; + private String cashBack; + private DecimalFormat df; + + public Change(DecimalFormat df){ + this.df = df; + } + + public double hundreds(double change) { + if((int)(change/100) > 0){ + hundreds = (int)(change/100); + change = Double.valueOf(df.format(change - (hundreds * 100))); + } + return change; + } + + public double fifties(double change) { + if((int)(change/50) >0){ + fifties = (int)(change/50); + change = Double.valueOf(df.format(change - (fifties * 50))); + } + return change; + } + + public double twenties(double change) { + if((int)(change/20) >0){ + twenties = (int)(change/20); + change = Double.valueOf(df.format(change - (twenties * 20))); + } + return change; + } + + public double tens(double change) { + if((int)(change/10) >0){ + tens = (int)(change/10); + change = Double.valueOf(df.format(change - (tens * 10))); + } + return change; + } + + public double fives(double change) { + if((int)(change/5) >0){ + fives = (int)(change/5); + change = Double.valueOf(df.format(change - (fives * 5))); + } + return change; + } + + public double ones(double change) { + if(change-1 > 0) { + ones = 0; + while(change >= 1) { + change = Double.valueOf(df.format(change -1)); + ones++; + } + } + return change; + } + + public double quarters(double change) { + if((int)(change/.25) > 0) { + quarters = (int)(change/.25); + change = Double.valueOf(df.format(change - (quarters * .25))); + } + return change; + } + + public double dimes(double change) { + if((int)(change/.10) > 0) { + dimes = (int)(change/.10); + change = Double.valueOf(df.format(change - (dimes * .10))); + } + return change; + } + + public double nickels(double change) { + if((int)(change/.05) > 0) { + nickels = (int)(change/.05); + change = Double.valueOf(df.format(change - (nickels * .05))); + } + return change; + } + + public double pennies(double change) { + if((int)(change/.01) > 0) { + pennies = (int)(change/.01); + change = change - (pennies * .01); + } + return change; + } + + public String printChange() { + cashBack = ""; + if(hundreds != 0){ + cashBack += hundreds + " HUNDREDS(S). "; + } + if(fifties != 0){ + cashBack += fifties + " FIFTIES. "; + } + if(twenties != 0){ + cashBack += twenties + " TWENTIES. "; + } + if(tens != 0){ + cashBack += tens + " TEN(S). "; + } + if(fives != 0){ + cashBack += fives + " FIVE(S). "; + } + if(ones != 0){ + cashBack += ones + " ONE(S). "; + } + if(quarters != 0){ + cashBack += quarters + " QUARTER(S). "; + } + if(dimes != 0){ + cashBack += dimes + " DIME(S). "; + } + if(nickels != 0){ + cashBack += nickels + " NICKEL(S). "; + } + if(pennies != 0){ + cashBack += pennies + " PENNIES. "; + } + + return cashBack; + } +} diff --git a/src/Register/Register.java b/src/Register/Register.java index 67de7831..b398b503 100644 --- a/src/Register/Register.java +++ b/src/Register/Register.java @@ -1,30 +1,106 @@ package Register; import java.io.IOException; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Random; -import Reader.MoneyReader; +import io.MoneyReader; +import io.MoneyWriter; public class Register { - + public static void main(String[] args) throws IOException { - // TODO Auto-generated method stub Register register = new Register(); - register.loadPayments("Input.txt"); + MoneyWriter moneyWriter = new MoneyWriter("Output.txt"); + ArrayList change = new ArrayList(); + + change = register.payments("Input.txt"); + + for (int counter = 0; counter < change.size(); counter++) { + String cashBack = register.outputChange(change.get(counter)); + System.out.println(cashBack); + moneyWriter.writeChange(cashBack); + } + + moneyWriter.close(); } - public void loadPayments(String fileName) throws IOException{ + public ArrayList payments(String fileName) throws IOException{ + ArrayList change = new ArrayList(); MoneyReader moneyReader = new MoneyReader(fileName); String line; while((line = moneyReader.readPayment()) != null) { + DecimalFormat df = new DecimalFormat("#.##"); + double price = Double.parseDouble(line.split(",")[0]); double amountGiven = Double.parseDouble(line.split(",")[1]); - System.out.println(price + "," + amountGiven); + double diff = Double.valueOf(df.format(amountGiven - price)); + + change.add(diff); } moneyReader.close(); + return change; + } + + public String outputChange(double change) throws IOException { + if(change < 0) { + String cashBack = ""; + cashBack= "Insufficient Funds"; + return cashBack; + }else if(change == 0) { + String cashBack = ""; + cashBack = "Exact Change Given"; + return cashBack; + }else { + DecimalFormat df = new DecimalFormat("#.##"); + Change cashBack = new Change(df); + while(change >= 1) { + change = cashBack.hundreds(change); + change = cashBack.fifties(change); + change = cashBack.twenties(change); + change = cashBack.tens(change); + change = cashBack.fives(change); + change = cashBack.ones(change); + } + + if((change != 0) && (((change * 100) % 3) == 0)) { + Random rand = new Random(); + int[] cases = {1, 5, 10, 25}; + while(change >0) { + int i = cases[rand.nextInt(4)]; + switch(i) { + case 1: + change = cashBack.pennies(change); + break; + case 5: + change = cashBack.nickels(change); + break; + case 10: + change = cashBack.dimes(change); + break; + case 25: + change = cashBack.quarters(change); + break; + } + + } + } + else{ + while(change > 0) { + change = cashBack.quarters(change); + change = cashBack.dimes(change); + change = cashBack.nickels(change); + change = cashBack.pennies(change); + } + } + + return cashBack.printChange(); + } } } diff --git a/src/Reader/MoneyReader.java b/src/io/MoneyReader.java similarity index 96% rename from src/Reader/MoneyReader.java rename to src/io/MoneyReader.java index d68f2140..81bd7b7e 100644 --- a/src/Reader/MoneyReader.java +++ b/src/io/MoneyReader.java @@ -1,4 +1,4 @@ -package Reader; +package io; import java.io.BufferedReader; import java.io.FileNotFoundException; diff --git a/src/io/MoneyWriter.java b/src/io/MoneyWriter.java new file mode 100644 index 00000000..a009e392 --- /dev/null +++ b/src/io/MoneyWriter.java @@ -0,0 +1,23 @@ +package io; + +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +public class MoneyWriter { + + private PrintWriter out; + + public MoneyWriter(String fileName) throws IOException{ + out = new PrintWriter(new FileWriter(fileName)); + } + + public void writeChange(String output) { + out.println(output); + } + + public void close() throws IOException { + out.close(); + } +} From 6790634325e78b9389519fb7e41abce45451c995 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sat, 16 Oct 2021 00:42:16 -0400 Subject: [PATCH 04/11] Bug Fix --- Input.txt | 3 ++- Output.txt => Receipt.txt | 5 +++-- bin/.gitignore | 1 - src/Register/Change.java | 6 +++--- src/Register/Register.java | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) rename Output.txt => Receipt.txt (79%) diff --git a/Input.txt b/Input.txt index 85a20bcf..a7a78097 100644 --- a/Input.txt +++ b/Input.txt @@ -6,4 +6,5 @@ 1.97,2.00 3.33,5.00 1.85,2.00 -1.67,2.00 \ No newline at end of file +1.67,2.00 +0.70,1.00 \ No newline at end of file diff --git a/Output.txt b/Receipt.txt similarity index 79% rename from Output.txt rename to Receipt.txt index 5a65de97..a7cb3bc5 100644 --- a/Output.txt +++ b/Receipt.txt @@ -5,5 +5,6 @@ Exact Change Given 3 QUARTER(S). 1 DIME(S). 3 PENNIES. 3 PENNIES. 1 ONE(S). 2 QUARTER(S). 1 DIME(S). 1 NICKEL(S). 2 PENNIES. -1 DIME(S). 1 NICKEL(S). -33 PENNIES. +3 NICKEL(S). +3 DIME(S). 3 PENNIES. +6 NICKEL(S). diff --git a/bin/.gitignore b/bin/.gitignore index 48e36722..1aef84ec 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,3 +1,2 @@ -/Reader/ /Register/ /io/ diff --git a/src/Register/Change.java b/src/Register/Change.java index 6aa1b7f6..65fea898 100644 --- a/src/Register/Change.java +++ b/src/Register/Change.java @@ -73,7 +73,7 @@ public double ones(double change) { public double quarters(double change) { if((int)(change/.25) > 0) { - quarters = (int)(change/.25); + quarters = (int)((change*100)/(.25*100)); change = Double.valueOf(df.format(change - (quarters * .25))); } return change; @@ -81,7 +81,7 @@ public double quarters(double change) { public double dimes(double change) { if((int)(change/.10) > 0) { - dimes = (int)(change/.10); + dimes = (int)((change*100)/(.10*100)); change = Double.valueOf(df.format(change - (dimes * .10))); } return change; @@ -89,7 +89,7 @@ public double dimes(double change) { public double nickels(double change) { if((int)(change/.05) > 0) { - nickels = (int)(change/.05); + nickels = (int)((change*100)/(.05*100)); change = Double.valueOf(df.format(change - (nickels * .05))); } return change; diff --git a/src/Register/Register.java b/src/Register/Register.java index b398b503..4e52b76b 100644 --- a/src/Register/Register.java +++ b/src/Register/Register.java @@ -13,7 +13,7 @@ public class Register { public static void main(String[] args) throws IOException { Register register = new Register(); - MoneyWriter moneyWriter = new MoneyWriter("Output.txt"); + MoneyWriter moneyWriter = new MoneyWriter("Receipt.txt"); ArrayList change = new ArrayList(); change = register.payments("Input.txt"); From de0acd49a81167cdd68fcb75549065f0102212c2 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sat, 16 Oct 2021 14:49:17 -0400 Subject: [PATCH 05/11] Added JavaDoc --- Input.txt | 2 +- Receipt.txt | 6 +-- src/Register/Change.java | 88 ++++++++++++++++++++++++++++++++++++++ src/Register/Register.java | 43 ++++++++++++++++--- src/io/MoneyReader.java | 23 +++++++++- src/io/MoneyWriter.java | 14 ++++++ 6 files changed, 166 insertions(+), 10 deletions(-) diff --git a/Input.txt b/Input.txt index a7a78097..80d6c9ce 100644 --- a/Input.txt +++ b/Input.txt @@ -1,4 +1,4 @@ -100,99 +100 , 99 1,1 2,100 20,30 diff --git a/Receipt.txt b/Receipt.txt index a7cb3bc5..03d50061 100644 --- a/Receipt.txt +++ b/Receipt.txt @@ -5,6 +5,6 @@ Exact Change Given 3 QUARTER(S). 1 DIME(S). 3 PENNIES. 3 PENNIES. 1 ONE(S). 2 QUARTER(S). 1 DIME(S). 1 NICKEL(S). 2 PENNIES. -3 NICKEL(S). -3 DIME(S). 3 PENNIES. -6 NICKEL(S). +1 DIME(S). 1 NICKEL(S). +6 NICKEL(S). 3 PENNIES. +1 QUARTER(S). 5 PENNIES. diff --git a/src/Register/Change.java b/src/Register/Change.java index 65fea898..23051a19 100644 --- a/src/Register/Change.java +++ b/src/Register/Change.java @@ -1,7 +1,15 @@ package Register; +import java.io.IOException; import java.text.DecimalFormat; +/** + * The Change class calculates how much of each bill and coin + * are needed to makeup the change to be given back. + * + * @author chasekeady + * + */ public class Change { private int hundreds; private int fifties; @@ -16,10 +24,23 @@ public class Change { private String cashBack; private DecimalFormat df; + + /** + * Standard constructor for the Change class + * + * @param df - formats doubles in class to 2 decimal places + */ public Change(DecimalFormat df){ this.df = df; } + /** + * Calculates how many hundred dollar bills are needed to make the change + * to be given back. Returns how much change is left after hundred dollar + * bills are accounted for. + * + * @param change - amount needed to be returned + */ public double hundreds(double change) { if((int)(change/100) > 0){ hundreds = (int)(change/100); @@ -28,6 +49,13 @@ public double hundreds(double change) { return change; } + /** + * Calculates how many fifty dollar bills are needed to make the change + * to be given back. Returns how much change is left after fifty dollar + * bills are accounted for. + * + * @param change - amount needed to be returned + */ public double fifties(double change) { if((int)(change/50) >0){ fifties = (int)(change/50); @@ -36,6 +64,13 @@ public double fifties(double change) { return change; } + /** + * Calculates how many twenty dollar bills are needed to make the change + * to be given back. Returns how much change is left after twenty dollar + * bills are accounted for. + * + * @param change - amount needed to be returned + */ public double twenties(double change) { if((int)(change/20) >0){ twenties = (int)(change/20); @@ -44,6 +79,13 @@ public double twenties(double change) { return change; } + /** + * Calculates how many ten dollar bills are needed to make the change + * to be given back. Returns how much change is left after ten dollar + * bills are accounted for. + * + * @param change - amount needed to be returned + */ public double tens(double change) { if((int)(change/10) >0){ tens = (int)(change/10); @@ -52,6 +94,13 @@ public double tens(double change) { return change; } + /** + * Calculates how many five dollar bills are needed to make the change + * to be given back. Returns how much change is left after five dollar + * bills are accounted for. + * + * @param change - amount needed to be returned + */ public double fives(double change) { if((int)(change/5) >0){ fives = (int)(change/5); @@ -60,6 +109,13 @@ public double fives(double change) { return change; } + /** + * Calculates how many one dollar bills are needed to make the change + * to be given back. Returns how much change is left after one dollar + * bills are accounted for. + * + * @param change - amount needed to be returned + */ public double ones(double change) { if(change-1 > 0) { ones = 0; @@ -71,6 +127,13 @@ public double ones(double change) { return change; } + /** + * Calculates how many quarters are needed to make the change + * to be given back. Returns how much change is left after the + * quarters are accounted for. + * + * @param change - amount needed to be returned + */ public double quarters(double change) { if((int)(change/.25) > 0) { quarters = (int)((change*100)/(.25*100)); @@ -79,6 +142,13 @@ public double quarters(double change) { return change; } + /** + * Calculates how many dimes are needed to make the change + * to be given back. Returns how much change is left after the + * dimes are accounted for. + * + * @param change - amount needed to be returned + */ public double dimes(double change) { if((int)(change/.10) > 0) { dimes = (int)((change*100)/(.10*100)); @@ -87,6 +157,13 @@ public double dimes(double change) { return change; } + /** + * Calculates how many nickels are needed to make the change + * to be given back. Returns how much change is left after the + * nickels are accounted for. + * + * @param change - amount needed to be returned + */ public double nickels(double change) { if((int)(change/.05) > 0) { nickels = (int)((change*100)/(.05*100)); @@ -95,6 +172,13 @@ public double nickels(double change) { return change; } + /** + * Calculates how many pennies are needed to make the change + * to be given back. Returns how much change is left after the + * pennies are accounted for. + * + * @param change - amount needed to be returned + */ public double pennies(double change) { if((int)(change/.01) > 0) { pennies = (int)(change/.01); @@ -103,6 +187,10 @@ public double pennies(double change) { return change; } + /** + * Returns a string that contains the bills and coins that makeup + * the change needed to be handed back. + */ public String printChange() { cashBack = ""; if(hundreds != 0){ diff --git a/src/Register/Register.java b/src/Register/Register.java index 4e52b76b..fc529905 100644 --- a/src/Register/Register.java +++ b/src/Register/Register.java @@ -8,9 +8,24 @@ import io.MoneyReader; import io.MoneyWriter; - +/** + * The Register class holds the main method for the CashRegister project. + * The class first loads the input file that contains a price and amount paid, + * then calculates the change that is needed to be given back. Once the change is + * calculated, it is printed to a Receipt.txt file. + * + * @author chasekeady + * + */ public class Register { - + + /** + * The main method for the CashRegister project. This method will loop + * through a list of change and prints the result to Receipt.txt + * + * @param args + * @throws IOException + */ public static void main(String[] args) throws IOException { Register register = new Register(); MoneyWriter moneyWriter = new MoneyWriter("Receipt.txt"); @@ -27,6 +42,14 @@ public static void main(String[] args) throws IOException { moneyWriter.close(); } + /** + * Reads price and amount from Input.txt file, calculates the change + * that is needed to be handed back, and inserts that value into an ArrayList. + * The ArrayList is then returned. + * + * @param fileName - the name of the file the payment are loaded from + * @throws IOException + */ public ArrayList payments(String fileName) throws IOException{ ArrayList change = new ArrayList(); MoneyReader moneyReader = new MoneyReader(fileName); @@ -35,8 +58,8 @@ public ArrayList payments(String fileName) throws IOException{ while((line = moneyReader.readPayment()) != null) { DecimalFormat df = new DecimalFormat("#.##"); - double price = Double.parseDouble(line.split(",")[0]); - double amountGiven = Double.parseDouble(line.split(",")[1]); + double price = Double.parseDouble(line.split(",")[0].trim()); + double amountGiven = Double.parseDouble(line.split(",")[1].trim()); double diff = Double.valueOf(df.format(amountGiven - price)); @@ -47,6 +70,13 @@ public ArrayList payments(String fileName) throws IOException{ return change; } + /** + * Returns a string that contains the bills and coins that makeup + * the change needed to be handed back. + * + * @param change - amount needed to be returned + * @throws IOException + */ public String outputChange(double change) throws IOException { if(change < 0) { String cashBack = ""; @@ -57,6 +87,7 @@ public String outputChange(double change) throws IOException { cashBack = "Exact Change Given"; return cashBack; }else { + //Calculates how many of each bill are needed to makeup the change DecimalFormat df = new DecimalFormat("#.##"); Change cashBack = new Change(df); while(change >= 1) { @@ -68,6 +99,7 @@ public String outputChange(double change) throws IOException { change = cashBack.ones(change); } + //If change in cents is divisible by 3, the change given back will be random if((change != 0) && (((change * 100) % 3) == 0)) { Random rand = new Random(); int[] cases = {1, 5, 10, 25}; @@ -90,7 +122,8 @@ public String outputChange(double change) throws IOException { } } - else{ + //If the change, in cents, is not divisible by 3, the minimum amount of coins will be returned + else { while(change > 0) { change = cashBack.quarters(change); change = cashBack.dimes(change); diff --git a/src/io/MoneyReader.java b/src/io/MoneyReader.java index 81bd7b7e..327d2fd8 100644 --- a/src/io/MoneyReader.java +++ b/src/io/MoneyReader.java @@ -5,21 +5,42 @@ import java.io.FileReader; import java.io.IOException; - +/** + * The MoneyReader class reads in all the prices and payments into the project + * + * @author chasekeady + * + */ public class MoneyReader { private BufferedReader in; + /** + * Standard constructor for creating a MoneyReader + * + * @param fileName - the name of the file that will be read from + * @throws FileNotFoundException + */ public MoneyReader(String fileName) throws FileNotFoundException { in = new BufferedReader(new FileReader(fileName)); } + /** + * Reads in a line from the file + * + * @throws IOException + */ public String readPayment() throws IOException { String line = in.readLine(); return line; } + /** + * Closes the MoneyReader instance + * + * @throws IOException + */ public void close() throws IOException { in.close(); } diff --git a/src/io/MoneyWriter.java b/src/io/MoneyWriter.java index a009e392..f986b355 100644 --- a/src/io/MoneyWriter.java +++ b/src/io/MoneyWriter.java @@ -9,14 +9,28 @@ public class MoneyWriter { private PrintWriter out; + /** + * Standard constructor for creating a MoneyWriter + * + * @param fileName - the name of the new file + * @throws IOException + */ public MoneyWriter(String fileName) throws IOException{ out = new PrintWriter(new FileWriter(fileName)); } + /** + * Writes a line to the new file + */ public void writeChange(String output) { out.println(output); } + /** + * Closes the MoneyWriter instance + * + * @throws IOException + */ public void close() throws IOException { out.close(); } From 53597d11bfa44eaf1156249e1d2b3b813884f487 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sun, 17 Oct 2021 11:29:15 -0400 Subject: [PATCH 06/11] Added Junit test cases for Change class --- .classpath | 1 + bin/.gitignore | 2 +- bin/io/MoneyReader.class | Bin 0 -> 835 bytes bin/io/MoneyWriter.class | Bin 0 -> 762 bytes src/Register/Change.java | 2 +- src/test/ChangeTest.java | 271 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 bin/io/MoneyReader.class create mode 100644 bin/io/MoneyWriter.class create mode 100644 src/test/ChangeTest.java diff --git a/.classpath b/.classpath index 9f9bbb56..c8ffe1c0 100644 --- a/.classpath +++ b/.classpath @@ -6,5 +6,6 @@ + diff --git a/bin/.gitignore b/bin/.gitignore index 1aef84ec..ec88932a 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,2 +1,2 @@ +/test/ /Register/ -/io/ diff --git a/bin/io/MoneyReader.class b/bin/io/MoneyReader.class new file mode 100644 index 0000000000000000000000000000000000000000..9c509b1a3beec8fe3d123b04e86689a1f1df483c GIT binary patch literal 835 zcmZXS(NEJ*7{$-s1|6l2F<@*cqC<4uWP9@k;sZt!lL|)25+1wl4okY$be)m^N)vU7 zB>n;ZQO0v`TO9GFz2Cj}`+et}_V=HkzW^MfV<0C`^1|-hFp$^pr8|_7fr3E!%KhYa zeK)x5p7yWgAQs4Zfk4gC2kbbSjz%()!=&jzVDZoky!fTSMl-pjyU*gt3oZ}ro`CuK z^FV%#y)c*(oCd?&I|7AQ;ZT~$qh!Iria@?;_YABGRI|>zA`Gkv zRI)QgO~jd~U_&vACd$}UtD3-CDwAGP4|Z<}0$Uaquqd$Nc!4~bj{7n?cl*91+6f1) z-*Y2R)#+v-{@_g*m~%Hp22va;U+!3UYE|=z2rNg;`<;6|mI32kZ`%LIX5a~hW{%#R z-ln6BJhku)4bD*V&&b?;Xvxc$4>1uE2AxVE-@PVxfot literal 0 HcmV?d00001 diff --git a/bin/io/MoneyWriter.class b/bin/io/MoneyWriter.class new file mode 100644 index 0000000000000000000000000000000000000000..ff45d11d5c3b8c0dcd1434505330d98fb07ac221 GIT binary patch literal 762 zcmZ9JTTk0S5QWb=0bejlngHQaXbZH>MQYyq0;*J@R;rW&5-2E-j=O|K#*Q4Pm!DOs zB~l~x2lPiF&N>D{U)DQ2XTF&^{&(}|FMxeCO&9_N6}3J`q5N?ct3<{o@&ctx@2l4e zys+Oo>Rn1d5y(X&p3Cht!;<4zg~_yJPhfsug(`VRL2ar?Yflo2`g`?`fb-$IF9(T= z!l8);fyy1{)6soKAb${jkq&YwxG+&<*IK<};>nDDrmu;LK=}^zQ3Y~3kApInHNbLE zLRH^ZNysMS?Ah4$jtzlz7ju{wD7IB74@Xx$8J~K+KoZ@Kd@tyDvC?t&m`^U$kPfvU z+eQzpb53yRU8R)|24*G*EPT@jA6(E??pVT(i(Oa(=0IB+gmjS}546p5eh>{)QO(6G zZG>|YjbdNwCE0xDf4$M2@Pdov_)`T8y$x?U-su2in^B&T1DCf`W(|H<8)GasCMb0$ zD0iD 0) { + if(change-1 >= 0) { ones = 0; while(change >= 1) { change = Double.valueOf(df.format(change -1)); diff --git a/src/test/ChangeTest.java b/src/test/ChangeTest.java new file mode 100644 index 00000000..751952f5 --- /dev/null +++ b/src/test/ChangeTest.java @@ -0,0 +1,271 @@ +package test; + +import static org.junit.Assert.*; +import java.text.DecimalFormat; +import Register.Change; +import org.junit.jupiter.api.Test; + +class ChangeTest { + + @Test + void testHundredsNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.hundreds(100.00); + + assertEquals(0.0,result,0.0); + } + + @Test + void testHundredsRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.hundreds(150.00); + + assertEquals(50.0,result,0.0); + } + + @Test + void testNoHundreds() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.hundreds(99.00); + + assertEquals(99.0,result,0.0); + } + + @Test + void testFiftiesNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.fifties(50.00); + + assertEquals(0.0,result,0.0); + } + + @Test + void testFiftiesRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.fifties(60.00); + + assertEquals(10.0,result,0.0); + } + + @Test + void testNoFifties() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.fifties(49.00); + + assertEquals(49.0,result,0.0); + } + + @Test + void testTwentiesNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.twenties(20.00); + + assertEquals(0.0,result,0.0); + } + + @Test + void testTwentiesRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.twenties(30.00); + + assertEquals(10.0,result,0.0); + } + + @Test + void testNoTwenties() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.twenties(15.00); + + assertEquals(15.0,result,0.0); + } + + @Test + void testTensNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.tens(100.00); + + assertEquals(0.0,result,0.0); + } + + @Test + void testTensRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.tens(25.00); + + assertEquals(5.0,result,0.0); + } + + @Test + void testNoTens() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.tens(9.00); + + assertEquals(9.0,result,0.0); + } + + @Test + void testFivesNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.fives(10.00); + + assertEquals(0.0,result,0.0); + } + + @Test + void testFivesRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.fives(11.00); + + assertEquals(1.0,result,0.0); + } + + @Test + void testNoFives() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.fives(4.00); + + assertEquals(4.0,result,0.0); + } + + @Test + void testOnesNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.ones(1.00); + + assertEquals(0.0,result,0.0); + } + + @Test + void testOnesRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.ones(1.50); + + assertEquals(0.50,result,0.0); + } + + @Test + void testNoOnes() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.ones(0.99); + + assertEquals(0.99,result,0.0); + } + + @Test + void testQuartersNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.quarters(0.50); + + assertEquals(0.0,result,0.0); + } + + @Test + void testQuartersRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.quarters(0.51); + + assertEquals(0.01,result,0.0); + } + + @Test + void testNoQuarters() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.quarters(0.24); + + assertEquals(0.24,result,0.0); + } + + @Test + void testDimesNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.dimes(0.90); + + assertEquals(0.0,result,0.0); + } + + @Test + void testDimesRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.dimes(0.95); + + assertEquals(0.05,result,0.0); + } + + @Test + void testNoDimes() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.dimes(0.09); + + assertEquals(0.09,result,0.0); + } + + @Test + void testNickelsNoRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.nickels(0.15); + + assertEquals(0.0,result,0.0); + } + + @Test + void testNickelsRemainder() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.nickels(0.16); + + assertEquals(0.01,result,0.0); + } + + @Test + void testNoNickels() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.nickels(0.04); + + assertEquals(0.04,result,0.0); + } + + @Test + void testPennies() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.pennies(0.10); + + assertEquals(0.0,result,0.0); + } + + @Test + void testNoPennies() { + DecimalFormat df = new DecimalFormat("#.##"); + Change change = new Change(df); + double result = change.pennies(0.00); + + assertEquals(0.0,result,0.0); + } + +} From d48b410028ba8f36f11e2291931a298f7eaf270f Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sun, 17 Oct 2021 11:32:35 -0400 Subject: [PATCH 07/11] Added try/catch --- Receipt.txt | 4 ++-- src/Register/Register.java | 30 +++++++++++++++++------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Receipt.txt b/Receipt.txt index 03d50061..6b325a44 100644 --- a/Receipt.txt +++ b/Receipt.txt @@ -5,6 +5,6 @@ Exact Change Given 3 QUARTER(S). 1 DIME(S). 3 PENNIES. 3 PENNIES. 1 ONE(S). 2 QUARTER(S). 1 DIME(S). 1 NICKEL(S). 2 PENNIES. -1 DIME(S). 1 NICKEL(S). +15 PENNIES. 6 NICKEL(S). 3 PENNIES. -1 QUARTER(S). 5 PENNIES. +1 QUARTER(S). 1 NICKEL(S). diff --git a/src/Register/Register.java b/src/Register/Register.java index fc529905..a2ca5bd5 100644 --- a/src/Register/Register.java +++ b/src/Register/Register.java @@ -27,19 +27,23 @@ public class Register { * @throws IOException */ public static void main(String[] args) throws IOException { - Register register = new Register(); - MoneyWriter moneyWriter = new MoneyWriter("Receipt.txt"); - ArrayList change = new ArrayList(); - - change = register.payments("Input.txt"); - - for (int counter = 0; counter < change.size(); counter++) { - String cashBack = register.outputChange(change.get(counter)); - System.out.println(cashBack); - moneyWriter.writeChange(cashBack); - } - - moneyWriter.close(); + try { + Register register = new Register(); + MoneyWriter moneyWriter = new MoneyWriter("Receipt.txt"); + ArrayList change = new ArrayList(); + + change = register.payments("Input.txt"); + + for (int counter = 0; counter < change.size(); counter++) { + String cashBack = register.outputChange(change.get(counter)); + System.out.println(cashBack); + moneyWriter.writeChange(cashBack); + } + + moneyWriter.close(); + }catch(Exception e) { + System.out.println(e); + } } /** From 31408e93210ad3e5b81fcc11a1422484cbddf2f0 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sun, 17 Oct 2021 11:41:38 -0400 Subject: [PATCH 08/11] Updated ReadMe file --- README.md | 3 +++ src/test/ChangeTest.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 62a96fc3..fac50848 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ Cash Register ============ +This project is written in Java. It takes in a flat file with a list of prices and payments, and outputs the change, as a String, to a Receipt.txt file. In most cases, the least amount of physical change is returned, however, if the total due in cents is divisible by 3, the application randomly generates the change denominations. Below is the original problems and criteria for the application. + + The Problem ----------- Creative Cash Draw Solutions is a client who wants to provide something different for the cashiers who use their system. The function of the application is to tell the cashier how much change is owed and what denominations should be used. In most cases the app should return the minimum amount of physical change, but the client would like to add a twist. If the total due in cents is divisible by 3, the app should randomly generate the change denominations (but the math still needs to be right :)) diff --git a/src/test/ChangeTest.java b/src/test/ChangeTest.java index 751952f5..dc60e9aa 100644 --- a/src/test/ChangeTest.java +++ b/src/test/ChangeTest.java @@ -5,6 +5,9 @@ import Register.Change; import org.junit.jupiter.api.Test; +/* + * Test class for the Change class + */ class ChangeTest { @Test From 7e2724803698ffcb8e1ae58931a975feb2d94252 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sun, 17 Oct 2021 11:42:42 -0400 Subject: [PATCH 09/11] Updated ReadMe file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fac50848..c131662e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Cash Register ============ -This project is written in Java. It takes in a flat file with a list of prices and payments, and outputs the change, as a String, to a Receipt.txt file. In most cases, the least amount of physical change is returned, however, if the total due in cents is divisible by 3, the application randomly generates the change denominations. Below is the original problems and criteria for the application. +This project is written in Java with a JUnit test class. It takes in a flat file with a list of prices and payments, and outputs the change, as a String, to a Receipt.txt file. In most cases, the least amount of physical change is returned, however, if the total due in cents is divisible by 3, the application randomly generates the change denominations. Below is the original problems and criteria for the application. The Problem From 470bd2fd1da6698511e0bc3c0de9b979419aee69 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sun, 17 Oct 2021 11:44:07 -0400 Subject: [PATCH 10/11] Test --- Receipt.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Receipt.txt b/Receipt.txt index 6b325a44..df3b65c2 100644 --- a/Receipt.txt +++ b/Receipt.txt @@ -5,6 +5,6 @@ Exact Change Given 3 QUARTER(S). 1 DIME(S). 3 PENNIES. 3 PENNIES. 1 ONE(S). 2 QUARTER(S). 1 DIME(S). 1 NICKEL(S). 2 PENNIES. -15 PENNIES. -6 NICKEL(S). 3 PENNIES. -1 QUARTER(S). 1 NICKEL(S). +3 NICKEL(S). +33 PENNIES. +6 NICKEL(S). From 7aae5abfbbdcf7a7f299de80786c130f29279f01 Mon Sep 17 00:00:00 2001 From: CKeady26 Date: Sun, 17 Oct 2021 12:18:43 -0400 Subject: [PATCH 11/11] Removed unused imports and added Test class for Register class --- src/Register/Change.java | 3 +-- src/test/RegisterTest.java | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/test/RegisterTest.java diff --git a/src/Register/Change.java b/src/Register/Change.java index 7bc8c6a5..d7a9c985 100644 --- a/src/Register/Change.java +++ b/src/Register/Change.java @@ -1,6 +1,5 @@ package Register; -import java.io.IOException; import java.text.DecimalFormat; /** @@ -194,7 +193,7 @@ public double pennies(double change) { public String printChange() { cashBack = ""; if(hundreds != 0){ - cashBack += hundreds + " HUNDREDS(S). "; + cashBack += hundreds + " HUNDRED(S). "; } if(fifties != 0){ cashBack += fifties + " FIFTIES. "; diff --git a/src/test/RegisterTest.java b/src/test/RegisterTest.java new file mode 100644 index 00000000..ccecb27c --- /dev/null +++ b/src/test/RegisterTest.java @@ -0,0 +1,43 @@ +package test; + + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import Register.Register; + +/* + * Test class for Register Class + */ +class RegisterTest { + + @Test + void testPayments() throws IOException { + Register register = new Register(); + ArrayList change = register.payments("Input.txt"); + int len = change.size(); + + assertEquals(10,len); + } + + @Test + void testOutPutChangeBills() throws IOException { + Register register = new Register(); + String output = register.outputChange(186.00); + String result = "1 HUNDRED(S). 1 FIFTIES. 1 TWENTIES. 1 TEN(S). 1 FIVE(S). 1 ONE(S). "; + assertTrue(output.equals(result)); + } + + @Test + void testOutPutChangeCents() throws IOException { + Register register = new Register(); + String output = register.outputChange(0.41); + String result = "1 QUARTER(S). 1 DIME(S). 1 NICKEL(S). 1 PENNIES. "; + assertTrue(output.equals(result)); + } + +}