diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..cf2fc7a8 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..4e3671ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +#files to ignore +*.class +OutputFile.txt \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..5b7023e7 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..712ab9d9 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..51ec1063 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 00000000..797acea5 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/InputFile.txt b/InputFile.txt new file mode 100644 index 00000000..55a38320 --- /dev/null +++ b/InputFile.txt @@ -0,0 +1,8 @@ +2.15,3.00 +4.00,6.00 +3.00,6.00 +3.00,10.00 +4.25,7.46 +5.69,3 +7.81,8.00 +10.00,50.63 \ No newline at end of file diff --git a/OutputFile.txt b/OutputFile.txt new file mode 100644 index 00000000..86871ff2 --- /dev/null +++ b/OutputFile.txt @@ -0,0 +1,8 @@ +3 Quarters, 1 Dime +2 Dollars +8 Nickels, 260 Pennies +1 Five Dollar Bill, 2 Dollars +3 Dollars, 2 Dimes +ERROR +1 Dime, 1 Nickel, 4 Pennies +4 Ten Dollar Bills, 2 Quarters, 1 Dime, 3 Pennies diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..0c30c570 --- /dev/null +++ b/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.example + CashRegister + 1.0-SNAPSHOT + + + 16 + 16 + + + \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 00000000..fca07cd0 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/java/CashRegister.java b/src/main/java/CashRegister.java new file mode 100644 index 00000000..e1299497 --- /dev/null +++ b/src/main/java/CashRegister.java @@ -0,0 +1,306 @@ +import jdk.swing.interop.DispatcherWrapper; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.*; + + +public class CashRegister { + Denomination tenDollar = new Denomination("Ten Dollar Bill", 10.00); + Denomination fiveDollar = new Denomination("Five Dollar Bill", 5.00); + Denomination dollar = new Denomination("One Dollar Bill", 1.00); + Denomination quarter = new Denomination("Quarter", 0.25); + // Oh wow, dimes! + Denomination dime = new Denomination("Dime", 0.10); + Denomination nickel = new Denomination("Nickel", 0.05); + Denomination penny = new Denomination("Penny", 0.01); + + List denominations = new ArrayList<>(); + + public CashRegister() { + this.tenDollar = tenDollar; + this.fiveDollar = fiveDollar; + this.dollar = dollar; + this.quarter = quarter; + this.dime = dime; + this.nickel = nickel; + this.penny = penny; + this.denominations = denominations; + + this.denominations.add(tenDollar); + this.denominations.add(fiveDollar); + this.denominations.add(dollar); + this.denominations.add(quarter); + this.denominations.add(dime); + this.denominations.add(nickel); + this.denominations.add(penny); + } + + // Main method + public static void main(String[] args) throws IOException { + CashRegister cr = new CashRegister(); + cr.fileReader(); + + + } + + // Method for reading from file and writing to file + public void fileReader() throws IOException { + + File file = new File("InputFile.txt"); + Scanner fileScanner = new Scanner(file); + FileWriter output = new FileWriter("OutputFile.txt"); + PrintWriter printWriter = new PrintWriter(output); + + + try { + while (fileScanner.hasNextLine()) { + String line = fileScanner.nextLine(); + double pp = Double.parseDouble(line.split(",")[0]); + double ch = Double.parseDouble(line.split(",")[1]); + double cashBack = pp - ch; + if (ch < pp) { + printWriter.println("ERROR"); + + } else if (ch == pp) { + printWriter.println("ZERO"); + + } else if(cashBack % 3 == 0){ + printWriter.println(this.randomCashBack(pp, ch)); + } else { printWriter.println(this.getCashChange(pp, ch)); + } + } + } catch (Exception e) { + System.out.println("error"); + } + output.close(); + } + + // Helper method for finding random coin + public Denomination getRandomCoin() { + int coin = (int) Math.floor(Math.random() * this.denominations.size()); + return this.denominations.get(coin); + } + + private String getCashChange(double price, double cash) { + + int tenDollars = 0; + int fiveDollars = 0; + int dollar = 0; + int quarters = 0; + int dimes = 0; + int nickels = 0; + int pennies = 0; + + + double cashBack = cash - price; + StringBuilder change = new StringBuilder(); + + while (cashBack > 0.01d) { + if (cashBack >= 10.0d) { + tenDollars++; + cashBack -= 10.0d; + } else if (cashBack >= 5.0d) { + fiveDollars++; + cashBack -= 5.0d; + } else if (cashBack >= 1.0d) { + dollar++; + cashBack -= 1.0d; + } else if (cashBack >= 0.25d) { + quarters++; + cashBack -= 0.25d; + } else if (cashBack >= 0.1d) { + dimes++; + cashBack -= 0.1d; + } else if (cashBack >= 0.05d) { + nickels++; + cashBack -= 0.05d; + } else { + pennies++; + cashBack -= 0.01d; + } + } + if (tenDollars == 1){ + change.append(tenDollars + " Ten Dollar Bill, "); + } else if (tenDollars > 1){ + change.append(tenDollars + " Ten Dollar Bills, "); + } + if(fiveDollars == 1){ + change.append(fiveDollars + " Five Dollar Bill, "); + } else if(fiveDollars > 1){ + change.append(fiveDollars + " Five Dollar Bills, "); + } + if(dollar == 1){ + change.append(dollar + " Dollar, "); + } else if(dollar > 1){ + change.append(dollar + " Dollars, "); + } + if(quarters == 1){ + change.append(quarters + " Quarter, "); + } else if (quarters > 1){ + change.append(quarters + " Quarters, "); + } + if(dimes == 1){ + change.append(dimes + " Dime, "); + } else if (dimes > 1){ + change.append(dimes + " Dimes, "); + } + if(nickels == 1){ + change.append(nickels + " Nickel, "); + } else if (nickels > 1){ + change.append(nickels + " Nickels"); + } + if(pennies == 1){ + change.append(pennies + " Penny, "); + } else if (pennies > 1){ + change.append(pennies + " Pennies, "); + } + + change.delete(change.length() - 2, change.length()); + + return change.toString(); + } + //Method for finding random cash back + //Now with dimes! + public String randomCashBack(double price, double cash){ + + //doubles to track counts of coins selected + double tenDollars = 0; + double fiveDollars = 0; + double dollars = 0; + double quarters = 0; + double dimes = 0; + double nickels = 0; + double pennies = 0; + // variables to store data about random selection + Denomination random; + double maxRandoms; + double chooseRandoms; + + // Arraylist to initially store selected coins as strings. + // Can contain duplicates and has no order + List stringList = new ArrayList(); + + // Calculate cashBack required + double cashBack = cash - price; + //initialize stringbuilder to hold output + StringBuilder change = new StringBuilder(); + + // Main engine + // Selects denomination randomly then selects amount of chosen denomination randomly + // While loop runs until no more change is required + while (cashBack > 0) { + random = this.getRandomCoin(); + // Gives us maximum number of randoms + maxRandoms = Math.floor(cashBack / random.getValue()); + chooseRandoms = Math.floor(Math.random() * (maxRandoms + 1)); + + + if(chooseRandoms > 0){ + cashBack -= chooseRandoms * random.getValue(); + // Round to 2 decimal places + cashBack = Math.round(cashBack * 100.0) / 100.0; + // Switch to make sure the amount of coins returned is tracked + switch (random.getTitle()) { + case "Ten Dollar Bill": + tenDollars += chooseRandoms; + break; + case "Five Dollar Bill": + fiveDollars += chooseRandoms; + break; + case "One Dollar Bill": + dollars += chooseRandoms; + break; + case "Quarter": + quarters += chooseRandoms; + break; + case "Dime": + dimes += chooseRandoms; + break; + case "Nickel": + nickels += chooseRandoms; + break; + case "Penny": + pennies += chooseRandoms; + break; + default: + System.out.println("Something went wrong with the random switch"); + break; + } + // Each time a denomination is selected, a string of that denomination is added + // to the unordered ArrayList (with duplicates) + stringList.add(random.getTitle()); + } + } + + // Duplicates are removed and a high-to-low order is established + // by creating a new ArrayList and iterating through this.denominations + // Strings are added if they are contained in the original list 'stringList' + List finalList = new ArrayList<>(); + + for(int i = 0; i < this.denominations.size(); i++){ + String coin = this.denominations.get(i).getTitle(); + if(stringList.contains(coin)) { + finalList.add(coin); + } + } + // Switch used for the StringBuilder + // Takes two helper methods singleOrPlural and builderHelper that format output + // and reduce repetition, respectively. + for(int i = 0; i < finalList.size(); i++) { + switch (finalList.get(i)) { + case "Ten Dollar Bill": + change.append(builderHelper(finalList.get(i), tenDollars)); + break; + case "Five Dollar Bill": + change.append(builderHelper(finalList.get(i), fiveDollars)); + break; + case "One Dollar Bill": + change.append(builderHelper(finalList.get(i), dollars)); + break; + case "Quarter": + change.append(builderHelper(finalList.get(i), quarters)); + break; + case "Dime": + change.append(builderHelper(finalList.get(i), dimes)); + break; + case "Nickel": + change.append(builderHelper(finalList.get(i), nickels)); + break; + case "Penny": + change.append(builderHelper(finalList.get(i), pennies)); + break; + default: + System.out.println("Something went wrong with the string builder switch"); + break; + } + } + // Takes off last comma and space from the StringBuilder + change.setLength(change.length() - 2); + return change.toString(); + } + + // Helper to decide whether denomination should be printed plural or singular + public String singleOrPlural(String title, double amount) { + if (amount == 1) { + return title; + } else { + // We hardcoded because we only have one case + if (title.equals("Penny")) { + return "Pennies"; + } else { + return title + "s"; + } + } + } + // Reduces repetition in the String Builder switch + public String builderHelper(String coinString, double coin){ + return (int)coin + " " + this.singleOrPlural(coinString, coin) + ", "; + } +} + + + + + + diff --git a/src/main/java/Denomination.java b/src/main/java/Denomination.java new file mode 100644 index 00000000..29ef0244 --- /dev/null +++ b/src/main/java/Denomination.java @@ -0,0 +1,19 @@ +public class Denomination { + + String title; + double value; + // Denomination Constructor + public Denomination(String title, double value){ + this.title = title; + this.value = value; + } + + public String getTitle(){ + return this.title; + } + + public double getValue(){ + return this.value; + } + +} diff --git a/target/.DS_Store b/target/.DS_Store new file mode 100644 index 00000000..ee5ef6f1 Binary files /dev/null and b/target/.DS_Store differ diff --git a/target/classes/CashRegister.class b/target/classes/CashRegister.class new file mode 100644 index 00000000..2541f780 Binary files /dev/null and b/target/classes/CashRegister.class differ diff --git a/target/classes/Denomination.class b/target/classes/Denomination.class new file mode 100644 index 00000000..ce1dfd07 Binary files /dev/null and b/target/classes/Denomination.class differ