From 89be6eec2da31393085cce61a09e5d3ea00442ca Mon Sep 17 00:00:00 2001 From: "Sean R. Kuhn" <42843938+srk1010@users.noreply.github.com> Date: Wed, 13 Nov 2019 10:29:01 -0500 Subject: [PATCH] Add files via upload Code sample completed. Ready for assessment. --- .../bin/com/kuhn/cashRegister/Currency.class | Bin 0 -> 1282 bytes .../com/kuhn/cashRegister/Denomination.class | Bin 0 -> 848 bytes .../bin/com/kuhn/cashRegister/Till.class | Bin 0 -> 3459 bytes .../cashRegisterApplication/Application.class | Bin 0 -> 2235 bytes .../kuhn/cashRegisterApplication/README.md | 28 +++ .../kuhn/cashRegisterApplication/input.csv | 3 + .../cashRegisterInputReader/InputReader.class | Bin 0 -> 1928 bytes .../OutputWriter.class | Bin 0 -> 863 bytes .../com/kuhn/cashRegisterTest/UnitTest.class | Bin 0 -> 1205 bytes .../src/com/kuhn/cashRegister/Currency.java | 43 ++++ .../com/kuhn/cashRegister/Denomination.java | 32 +++ .../src/com/kuhn/cashRegister/Till.java | 204 ++++++++++++++++++ .../cashRegisterApplication/Application.java | 46 ++++ .../kuhn/cashRegisterApplication/README.md | 28 +++ .../kuhn/cashRegisterApplication/input.csv | 5 + .../cashRegisterInputReader/InputReader.java | 76 +++++++ .../cashRegisterInputReader/OutputWriter.java | 36 ++++ .../com/kuhn/cashRegisterTest/UnitTest.java | 37 ++++ 18 files changed, 538 insertions(+) create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Currency.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Denomination.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Till.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterApplication/Application.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterApplication/README.md create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterApplication/input.csv create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterInputReader/InputReader.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterInputReader/OutputWriter.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterTest/UnitTest.class create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Currency.java create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Denomination.java create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Till.java create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/Application.java create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/README.md create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/input.csv create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/InputReader.java create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/OutputWriter.java create mode 100644 SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterTest/UnitTest.java diff --git a/SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Currency.class b/SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Currency.class new file mode 100644 index 0000000000000000000000000000000000000000..b98eac45aed8081d73d1a7fc791dd1e02bca6fa0 GIT binary patch literal 1282 zcmah{%}*0S9DPGSc3BJT7ARlxsg<@UMZ^y%a={pzG@6L1A)c1)v|VVYy4^*+d+_Yt zMDHFnQAsrN2YB+&@DK2;{$_W<1|eK#=gqwL=FQvr{`<>5fF;~i(8VxewVL@IuP*YI z*{r zM1di`Y75(4V~Aw3WrpZ_tHL!za3%o-s)8PdnbVC_^wH^cPgERUX=_NpkoqJ;DpUHG zbHjCPQ7aU)WfcPqO4Y8qHgBs)k=lLAMSzAWhsy=6k21un_O7IyrNx$zvk4+U@l1yr zAv8slirthOGjttcTRXg=;yl?u=0cEtHyAD?5JQ}yuVf2;*K2NZ=YhG^;0(!9%Q72f z)3GHE7o%?7ZZk}s(BZ*<=4$HFH@Ti6;c@?E}Ie`@{P{qUv_q2_c z=UDuNoy7mo;&B#|8lD)>P zz6X6{GwQ>BqyME3V?G=-lpj8f`*6sJ{PdyX!x4l1@}cU(F}>m=Js^+kBLNJ^le!kb zfIO`RsN0vGi%Rqf=?@gP3$rw?pqEA+Q5e)m5@YB`4g*-mAnqWAhZsT?!|*VI7Z}AW zV!S5q8;m1E$^>1VE{rUxWHEEN7?x3>6S`>R;?C1XTb4 literal 0 HcmV?d00001 diff --git a/SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Denomination.class b/SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegister/Denomination.class new file mode 100644 index 0000000000000000000000000000000000000000..8df731504414e701c0a819765490bee9096d0776 GIT binary patch literal 848 zcmah`OHbQC5dJ2yod+%^X_5j-DIg9>D%BTmATCsukRp(viqzxAtdgaPEjbRqiUUZX zNjO%+7p|@%_W!e*ku|RX~og9YhoV_iPmUftZd?Q{eB`qVOF#3u!ebRIbEU?W&2$WPG&s{8nubiIiEQY1U_;{6h7?m12*IqSm9(}j8EIv3 z3i$yVC%8C&5b{e&LhCeX>$D9fjWbD;O#6_R&a^X`wljV0LuWdj&h(|z()K$mSu)^s zrqb-*d(J)gyXSo8oGbhDe}C{1fc1FUgCd1xq2x&0a3+>$3mNIyZZm47Z8O!j$BM^2 z&=e|%j4`7vZX}{@y9S5MkgcGHGO3iA2#qTQdKTZ_nX}leP&{VDGp2&-R&aM%3CrH1 z;A(E^SI|0>Vbcp2%6uq+*Mka$75^L1i*l5eK*8@rG2Ak%_Q3;Pp`yo1m^(8ggJx=v zF&O8O{+?vWi1!;QOXQqVvtw3TVfmMME`if)t+}hE-@SDonc|1ucPaSNrY*~RlC}}2 z7`;YhNVF-nQ$`|f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Yn$VwGWl`;RbAeyvDi0v|BHhu3Bj z9};&EVLZl1RQQD0K0N;gkJqAS$*`syFKOiI;e^2fU36q@-yMm;I|Y|`OJ z)H;p?Rg6|}&&k?$P#f9cO^1N<}0I3NUpS$78A2d_8JVRG9D~XHXTPy@tm1l2|N<=0)mQL|m zT1`eKl(4Ey{EGEce7V7;8dlZX@bj)}g0;34s9IQP_rJU5;V_*BG_M@PXt zkpu2dc;xgGNzR8z5N;sHP&#!#xsLA!Q>Q;Z<(_0HggS|#+`9@M%SZ&%Lx$Ykl`~3u zE&?H6FzmH%X5B>EL#ligPPmCIEDN`=#*l5bhquA7ZXu6?I^DHkpg>4Z1pHlcHs;ZL zckFY;CG=c>=te^MOE4#AB4%)U|Ft(QVSQE#40xRQk{Xz=zj%BuMHs{e9xzn&-Xe64 z`hOw@9x~KcftSh236J<>rQ|j-T*cl9pQpHqEj&`o))}h*(5XE%v5BV2*rln<44Rvk zAExmq7M`jOc}-eXv@EohfO*dkV-lyJBN4+!syuc0n;@-h9*0TfaYc@FzWxpl6mc|2 z{w%$v41IJP`b2l3|BMK#>oLh2QC#J%+Y8*WJLf3c-E)+_>cleHGIC2>p`v>zqKXP3 z^|XwTDtXjEk74?x24!eK4QuqjPgst;4Lxv9d1_$O{)S@j5}Tt-Y>mh%EwHmd<88Nd tfydno>|JrFQ4O#|woY9d*hZ7=?lm6GC67GC)Vk?38y(G~OVt_d{|1JJxsd<> literal 0 HcmV?d00001 diff --git a/SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterTest/UnitTest.class b/SoftWriters/SeanKuhn_CashRegister/bin/com/kuhn/cashRegisterTest/UnitTest.class new file mode 100644 index 0000000000000000000000000000000000000000..fb990288ee61d0c555318cd1279eb03deb778bc7 GIT binary patch literal 1205 zcmah|YfsZq7=F$+)-g8O#sC?&f*`j+xp@O)KtMt=Faad-H%TxmKhc+V~2}DQ7_5@-pl>;k@D4J7fLR_G= zWV_Z@^<rb~8tfdxJA=qiOsc ziBDWaXqagT57TJjI#mncoaT<*!`ld2>rolb0g&WbK+yRW+2`kID@|mZ2hNb+%-t-0 zMxg ltcfcyVjg#hd6!Z;5cx%ls0N`StKnWSPseEC0dFH%_zeIJ2gLvY literal 0 HcmV?d00001 diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Currency.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Currency.java new file mode 100644 index 00000000..14ddc091 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Currency.java @@ -0,0 +1,43 @@ +package com.kuhn.cashRegister; + +/** + * The Currency class is the middle class of the cashRegister package hierarchy. + * Currency contains the definitions and methods for all usable currency. + * This class can be modified to add support for non US based currency without modifying any other class.. + * + * @author Sean Kuhn + * @Date 10/25/2019 + * + */ +public class Currency { + private Denomination currency[]; + + //Initializes Currency with denominations common in the united states. Excluded $2 bills and half dollar coins. + public Currency() { + currency = new Denomination[] { + new Denomination("hundreds",10000), + new Denomination("fifties",5000), + new Denomination("twenties",2000), + new Denomination("tens",1000), + new Denomination("fives",500), + new Denomination("ones",100), + new Denomination("quarters",25), + new Denomination("dimes",10), + new Denomination("nickel",5), + new Denomination("pennies",1) + }; + } + public Denomination getDenomination(int index) { + return this.currency[index]; + } + public int getCurrencyLength() { + return this.currency.length; + } + public String getCurrencyDenomination(int index) { + return currency[index].getDenomination(); + } + public int getCurrencyValue(int index) { + return currency[index].getValue(); + } + } + diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Denomination.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Denomination.java new file mode 100644 index 00000000..049ad8c3 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Denomination.java @@ -0,0 +1,32 @@ +package com.kuhn.cashRegister; + +/** + * The Denomination class is the bottom class of the cashRegister package hierarchy. + * Denomination defines the methods and values inherent to a denomination object + * + * @author Sean Kuhn + * @Date 10/25/2019 + * + */ +public class Denomination { + private String denomination; + private int value; + + public Denomination(String denomination, int value) { + this.denomination = denomination; + this.value = value; + } + + public void setDenomination(String denomination) { + this.denomination = denomination; + } + public String getDenomination() { + return this.denomination; + } + public void setValue(int value) { + this.value = value; + } + public int getValue() { + return this.value; + } +} diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Till.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Till.java new file mode 100644 index 00000000..2fc3dbcd --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegister/Till.java @@ -0,0 +1,204 @@ +package com.kuhn.cashRegister; +import java.lang.StringBuilder; +import java.util.Random; +/** + * The Till class is the top class of the cashRegister package hierarchy. + * Till contains all money handling methods, as well as all Currency instantiations. + * + * @author Sean Kuhn + * @Date 10/25/2019 + * + */ +public class Till { + private Currency currency; + private int value; + public Till() { + currency = new Currency(); + this.value = 25000; + } + + public Till(double value) { + currency = new Currency(); + this.value = (int) (value * 100); + } + + public void setTillTotal(double value) { + this.value = (int) (value * 100); + } + + public double getTillTotal() { + return this.value; + } + + /** + * The transaction method detects errors and calls get change methods based on change divisibility. + * @param charge The amount to be paid. + * @param payment The actual amount paid. + * @return Returns the string containing change instructions. + * @throws Exception Contains information for relevant errors. + */ + public String transaction(double charge, double payment) throws Exception { + int change = (int) ((payment * 100) - (charge * 100)); + this.setTillTotal(this.getTillTotal() + 100 * payment); + String output = ""; + + if( change > (this.value)) { + throw new Exception("Change ammount is greater than available funds!"); + }else if( ((charge * 100) % 1) > 0){ + throw new Exception("Change ammount includes fractions of a penny!"); + }else if((charge * 100) % 3 == 0){ + output = getRandomChange((int) (change)); + }else if(change <= (this.value)){ + output = getBasicChange((int) (change)); + } + + return output; + } + + /** + * The getBasicChange method calculates the number and type of denominations to return using the fewest bills and coins. + * + * @param change The number of cents to be returned to the purchaser. + * @return Returns a grammatically correct string of change instructions. + */ + public String getBasicChange(int change) { + StringBuilder output = new StringBuilder(); + + for(int i = 0; i < currency.getCurrencyLength(); i++) { + double j = (change / currency.getCurrencyValue(i)); + + if(j >= 1) { + this.setTillTotal(this.getTillTotal() - (Math.floor(j) * currency.getCurrencyValue(i))); + change -= (Math.floor(j) * currency.getCurrencyValue(i)); + output.append((int)Math.floor(j)); + output.append(grammar(i,j)); + + if(i < currency.getCurrencyLength()-1) { + output.append(", "); + } + } + } + + return output.toString(); + } + + /** + * The getRandomChange method calculates the number and type of denominations to return using a randomized mix of denominations. + * + * @param change The number of cents to be returned to the purchaser. + * @return Returns a grammatically correct string of change instructions. + */ + public String getRandomChange(int change) { + StringBuilder output = new StringBuilder(); + + for(int i = 0; i < currency.getCurrencyLength(); i++) { + double j = (change / currency.getCurrencyValue(i)); + int tempRand = new Random().nextInt((int)Math.floor(j)+1); + + if(i == (currency.getCurrencyLength()-1)) { + tempRand = change; + } + + if(j >= 1 && change <= (this.value) && tempRand > 0) { + this.setTillTotal(this.getTillTotal() - (tempRand * currency.getCurrencyValue(i))); + change -= (tempRand * currency.getCurrencyValue(i)); + output.append(tempRand); + output.append(grammar(i,j)); + + if(i < currency.getCurrencyLength()-1) { + output.append(", "); + } + } + } + + return output.toString(); + } + + /** + * The grammar method takes the denomination index and number of bills or coins and returns the proper plurality of the denomination. + * + * @param i The index of a denomination in currency.getDenomination(). + * @param j The number of bills or coins to consider. Relevant states are essentially <= 1 and > 1. + * @return Returns a string containing the name of a denomination in it's proper plurality." + */ + public String grammar(int i, double j) { + String grammer = ""; + + switch(i) { + case 0: + if((int)Math.floor(j) > 1) { + grammer = " hundreds"; + } else { + grammer = " hundred"; + } + break; + case 1: + if((int)Math.floor(j) > 1) { + grammer = " fifties"; + } else { + grammer = " fifty"; + } + break; + case 2: + if((int)Math.floor(j) > 1) { + grammer = " twenties"; + } else { + grammer =" twenty"; + } + break; + case 3: + if((int)Math.floor(j) > 1) { + grammer = " tens"; + } else { + grammer = " ten"; + } + break; + case 4: + if((int)Math.floor(j) > 1) { + grammer = " fives"; + } else { + grammer = " five"; + } + break; + case 5: + if((int)Math.floor(j) > 1) { + grammer = " dollars"; + } else { + grammer = " dollar"; + } + break; + case 6: + if((int)Math.floor(j) > 1) { + grammer = " quarters"; + } else { + grammer = " quarter"; + } + break; + case 7: + if((int)Math.floor(j) > 1) { + grammer = " dimes"; + } else { + grammer = " dime"; + } + break; + case 8: + if((int)Math.floor(j) > 1) { + grammer = " nickels"; + } else { + grammer = " nickel"; + } + break; + case 9: + if((int)Math.floor(j) > 1) { + grammer = " pennies"; + } else { + grammer = " penny"; + } + break; + default : + break; + } + + return grammer; + } +} diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/Application.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/Application.java new file mode 100644 index 00000000..bf02a1f2 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/Application.java @@ -0,0 +1,46 @@ +package com.kuhn.cashRegisterApplication; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import com.kuhn.cashRegister.*; +import com.kuhn.cashRegisterInputReader.*; + +/** + * The Application class contains the main runnable method for the CashRegister project + * + * @author Sean Kuhn + * @Date 10/27/2019 + * + */ +public class Application { + public static void main(String[] Args) throws Exception { + Till till = new Till(); + BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in)); + + //read in the input and output paths. + System.out.println("Fully qualified Input Path:"); + String inputPath = consoleReader.readLine(); + System.out.println("Fully Qualified Output Path:"); + String outputPath = consoleReader.readLine(); + consoleReader.close(); + + //read in the input file + InputReader inputReader = new InputReader(inputPath); + StringBuilder output = new StringBuilder(); + double[][] transactions = inputReader.getTransactions(); + + //Builds output file from transaction array + for(int i = 0; i < inputReader.getLines(); i++) { + String change = till.transaction(transactions[i][0], transactions[i][1]); + System.out.println(change); + output.append(change); + if(i != (inputReader.getLines()-1)) { + output.append(System.getProperty("line.separator")); + } + + } + + OutputWriter outputWriter = new OutputWriter(output.toString(),outputPath); + outputWriter.writeResults(); + } +} diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/README.md b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/README.md new file mode 100644 index 00000000..0af34cf3 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/README.md @@ -0,0 +1,28 @@ +# Cash Register +--- +The requirement for this program is to create a cash register application that takes transaction data as input, and outputs change instructions. Change should be handled in the lowest number of bills and coins, unless the amount due is divisible by 3. If the amount due is divisible by 3, then the number of each denomination is randomized to give the customer correct change, but in mixed up denominations eg. 80 cents change could return as 1 nickel, 75 pennies. + +This program is divided into three main packages; + + +1. com.kuhn.cashRegister + * the cashRegister package contains a hierarchy of classes used to control the money changing process. + a.Denomination is the lowest class in the cashRegister hierarchy, and contains only two parameters: Denomination and value. + + b.Currency is the middle class in the cashRegister hierarchy, and contains a set of denomination instances. These instances can be modified to add support for non-standard currency types. + + c.Till is the highest class in the cashRegister hierarchy, and contains the opening till value, instantiated currency object, and all money handling methods. + i. The transaction method detects errors and calls get change methods based on change divisibility. + ii. The getBasicChange method calculates the number and type of denominations to return using the fewest bills and coins. + iii. The getRandomChange method calculates the number and type of denominations to return using a randomized mix of denominations. + iv. The grammar method takes the denomination index and number of bills or coins and returns the proper plurality of the denomination. + +2. com.kuhn.cashRegisterInputReader + * the cashRegisterInputReader package contains the classes necessary for reading from and writing to files. + +3. com.kuhn.cashRegisterApplication + * the cashRegisterApplication package contains the runnable class as well as supporting documentation. + +# Limitations +--- +Input files formatted as charge,payment do not specify the amount and type of bills or coins given in the transaction. since this is not present, there is not good way to track individual denominations. If a Till object was initialized with a finite number of each bill and coin, the getChange methods would have to handle shortages. eg. 40 cents in change is due but only 3 dimes are left to give. two nickels are substituted for the "4th" dime. As transactions are completed, the transaction amount is added to the till as a lump sum, making it impossible to track denomination shorting. \ No newline at end of file diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/input.csv b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/input.csv new file mode 100644 index 00000000..a5fa8576 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterApplication/input.csv @@ -0,0 +1,5 @@ +2.12,3 +9.99,10 +1.97,2 +3.33,5 +26.63,50 diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/InputReader.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/InputReader.java new file mode 100644 index 00000000..b3027baa --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/InputReader.java @@ -0,0 +1,76 @@ +package com.kuhn.cashRegisterInputReader; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +/** + * The InputReader class contains methods for locating and parsing .csv files. + * + * @author Sean Kuhn + * @Date 10/31/2019 + * + */ +public class InputReader { + private String path; + + + public InputReader(String path) throws FileNotFoundException { + this.path = path; + + } + + public int getLines() throws IOException { + + int lines = 0; + File file = new File(this.path); + FileReader fileReader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(fileReader); + while(bufferedReader.readLine() != null) lines++; + bufferedReader.close(); + fileReader.close(); + return lines; + } + + /** + * The readFile method reads a csv file into a string array. + * + * @return Returns a string array. + * @throws IOException + */ + public String[] readFile() throws IOException { + + int lines = this.getLines(); + String[] csv = new String[lines]; + File file = new File(this.path); + FileReader fileReader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(fileReader); + + for(int i = 0; i < lines; i++) { + csv[i] = bufferedReader.readLine(); + } + + bufferedReader.close(); + fileReader.close(); + return csv; + } + + /** + * The getTraansactions method takes the string array from readFile, and parses the entire transaction set into a 2d array. + * + * @return Returns a 2d double array. + * @throws IOException + */ + public double[][] getTransactions() throws IOException{ + String[] csv = readFile(); + double[][] transactions = new double[csv.length][2]; + for(int i = 0; i < csv.length; i++) { + String[] line = csv[i].split(","); + transactions[i][0] = Double.valueOf(line[0]); + transactions[i][1] = Double.valueOf(line[1]); + } + return transactions; + } +} diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/OutputWriter.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/OutputWriter.java new file mode 100644 index 00000000..9495a1e2 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterInputReader/OutputWriter.java @@ -0,0 +1,36 @@ +package com.kuhn.cashRegisterInputReader; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + +/** + * The OutputWriter class takes a writes a string to a specified path + * + * @author Sean Kuhn + * @Date 11/03/2019 + * + */ +public class OutputWriter { + private String output; + private String path; + + public OutputWriter(String output, String path) { + this.output = output; + this.path = path; + } + + /** + * The writeResults method writes an entire batch of change instructions to the output path. + * + * @throws IOException + */ + public void writeResults() throws IOException + { + String output = this.output; + + BufferedWriter writer = new BufferedWriter(new FileWriter(this.path)); + writer.write(output); + writer.close(); + } +} diff --git a/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterTest/UnitTest.java b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterTest/UnitTest.java new file mode 100644 index 00000000..d8197ba5 --- /dev/null +++ b/SoftWriters/SeanKuhn_CashRegister/src/com/kuhn/cashRegisterTest/UnitTest.java @@ -0,0 +1,37 @@ +package com.kuhn.cashRegisterTest; + +import org.junit.Test; +import static org.junit.Assert.*; +import com.kuhn.cashRegister.*; + +/** + * This is a sample unit test of getBasicChange + * @author Sean Kuhn + * @Date 11/7/2019 + * + */ +public class UnitTest { + + @Test + public void testGetBasicChange() { + Till tillUnitTest = new Till(); + String result = tillUnitTest.getBasicChange(2337); + + assertEquals("1 twenty, 3 dollars, 1 quarter, 1 dime, 2 pennies", result); + + } + + @Test + public void testTransaction() { + Till tillUnitTest = new Till(); + String result = ""; + try { + result = tillUnitTest.transaction(26.63,50.00); + } catch (Exception e) { + e.printStackTrace(); + } + + assertEquals("1 twenty, 3 dollars, 1 quarter, 1 dime, 2 pennies", result); + + } +}