| 1 | package net.mandaria.tippytipper.services; |
| 2 | |
| 3 | import java.text.DecimalFormat; |
| 4 | import java.text.NumberFormat; |
| 5 | |
| 6 | public class TipCalculatorService |
| 7 | { |
| 8 | private String billEntry = ""; |
| 9 | private double billAmount = 0; |
| 10 | private double billAmountBeforeTax = 0; |
| 11 | private double taxAmount = 0; |
| 12 | private double tipAmount = 0; |
| 13 | private double tipAmountBeforeRounded = 0; |
| 14 | private double totalAmount = 0; |
| 15 | private double totalAmountBeforeRounded = 0; |
| 16 | private double tipPercentage = 0.15; |
| 17 | private double taxPercentage = 0; |
| 18 | private double splitBillAmount = 0; |
| 19 | private double splitTaxAmount = 0; |
| 20 | private double splitTipAmount = 0; |
| 21 | private double splittotalAmount = 0; |
| 22 | private double splitAdjustment = 0; |
| 23 | private int numberOfPeople = 2; |
| 24 | NumberFormat nf = NumberFormat.getCurrencyInstance(); |
| 25 | |
| 26 | public TipCalculatorService() |
| 27 | { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | public String getBillAmount() |
| 32 | { |
| 33 | return nf.format(billAmount); |
| 34 | } |
| 35 | |
| 36 | public String getTipAmount() |
| 37 | { |
| 38 | return nf.format(tipAmount); |
| 39 | } |
| 40 | |
| 41 | public String getTotalAmount() |
| 42 | { |
| 43 | return nf.format(totalAmount); |
| 44 | } |
| 45 | |
| 46 | public String getTaxAmount() |
| 47 | { |
| 48 | return nf.format(taxAmount); |
| 49 | } |
| 50 | |
| 51 | public String getSplitBillAmount() |
| 52 | { |
| 53 | return nf.format(splitBillAmount); |
| 54 | } |
| 55 | |
| 56 | public String getSplitTaxAmount() |
| 57 | { |
| 58 | return nf.format(splitTaxAmount); |
| 59 | } |
| 60 | |
| 61 | public String getSplitTipAmount() |
| 62 | { |
| 63 | return nf.format(splitTipAmount); |
| 64 | } |
| 65 | |
| 66 | public String getSplitAdjustment() |
| 67 | { |
| 68 | return nf.format(splitAdjustment); |
| 69 | } |
| 70 | |
| 71 | public String getSplitTotalAmount() |
| 72 | { |
| 73 | return nf.format(splittotalAmount); |
| 74 | } |
| 75 | |
| 76 | public String getTipPercentage() |
| 77 | { |
| 78 | double percent = tipPercentage * 100; |
| 79 | DecimalFormat dec = new DecimalFormat("0.0"); |
| 80 | return dec.format(percent) + "%"; |
| 81 | } |
| 82 | |
| 83 | public String getTaxPercentage() |
| 84 | { |
| 85 | double percent = taxPercentage * 100; |
| 86 | DecimalFormat dec = new DecimalFormat("0.0##"); |
| 87 | return dec.format(percent) + "%"; |
| 88 | } |
| 89 | |
| 90 | public double getTipPercentageAsDouble() |
| 91 | { |
| 92 | double percent = tipPercentage * 100; |
| 93 | return percent; |
| 94 | } |
| 95 | |
| 96 | public int getNumberOfPeople() |
| 97 | { |
| 98 | return numberOfPeople; |
| 99 | } |
| 100 | |
| 101 | public int getTipPercentageRounded() |
| 102 | { |
| 103 | int percent = (int)Math.round(tipPercentage * 100); |
| 104 | return percent; |
| 105 | } |
| 106 | |
| 107 | public void setTipPercentage(double percent) |
| 108 | { |
| 109 | tipPercentage = percent; |
| 110 | } |
| 111 | |
| 112 | public void setTaxPercentage(double percent) |
| 113 | { |
| 114 | taxPercentage = percent; |
| 115 | } |
| 116 | |
| 117 | public void refreshBillAmount() |
| 118 | { |
| 119 | double amount; |
| 120 | try |
| 121 | { |
| 122 | amount = Double.valueOf(billEntry); |
| 123 | } |
| 124 | catch(Exception ex) |
| 125 | { |
| 126 | amount = 0; |
| 127 | } |
| 128 | amount = amount / 100; |
| 129 | billAmount = amount; |
| 130 | billAmountBeforeTax = billAmount; |
| 131 | } |
| 132 | |
| 133 | public void appendNumberToBillAmount(String number) |
| 134 | { |
| 135 | if(billEntry.length() < 15) |
| 136 | { |
| 137 | billEntry = billEntry + "" + number; |
| 138 | } |
| 139 | double amount = Double.valueOf(billEntry); |
| 140 | amount = amount / 100; |
| 141 | billAmount = amount; |
| 142 | billAmountBeforeTax = billAmount; |
| 143 | } |
| 144 | |
| 145 | public void removeEndNumberFromBillAmount() |
| 146 | { |
| 147 | if(billEntry.length() > 1) |
| 148 | { |
| 149 | billEntry = billEntry.substring(0, billEntry.length() - 1); |
| 150 | double amount = Double.valueOf(billEntry); |
| 151 | amount = amount / 100; |
| 152 | billAmount = amount; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | billEntry = ""; |
| 157 | billAmount = 0; |
| 158 | } |
| 159 | billAmountBeforeTax = billAmount; |
| 160 | } |
| 161 | |
| 162 | public void clearBillAmount() |
| 163 | { |
| 164 | billEntry = ""; |
| 165 | billAmount = 0; |
| 166 | billAmountBeforeTax = billAmount; |
| 167 | } |
| 168 | |
| 169 | public void calculateTip() |
| 170 | { |
| 171 | if(taxPercentage > 0) |
| 172 | { |
| 173 | billAmount = billAmountBeforeTax; |
| 174 | double bill_amount = billAmount / (1 + taxPercentage); |
| 175 | double bill_amount_rounded = Math.round(bill_amount * 100) / 100.0; // round bill to nearest penny |
| 176 | billAmount = bill_amount_rounded; |
| 177 | |
| 178 | double tax_amount = bill_amount * taxPercentage; // Note: must calculate tax off of non-rounded bill amount to avoid off by 1 cent errors |
| 179 | tax_amount = Math.round(tax_amount * 100) / 100.0; // round tax to nearest penny |
| 180 | taxAmount = tax_amount; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | taxAmount = 0; |
| 185 | } |
| 186 | |
| 187 | double tip_amount = billAmount * tipPercentage; |
| 188 | tip_amount = Math.round(tip_amount * 100) / 100.0; // round tip to nearest penny |
| 189 | double total_amount = billAmount + taxAmount + tip_amount; |
| 190 | |
| 191 | tipAmount = tip_amount; |
| 192 | totalAmount = total_amount; |
| 193 | totalAmountBeforeRounded = totalAmount; |
| 194 | tipAmountBeforeRounded = tipAmount; |
| 195 | } |
| 196 | |
| 197 | public void calculateTip(double percent) |
| 198 | { |
| 199 | setTipPercentage(percent); |
| 200 | calculateTip(); |
| 201 | } |
| 202 | |
| 203 | public void calculateTip(double percent, double taxPercent) |
| 204 | { |
| 205 | setTaxPercentage(taxPercent); |
| 206 | calculateTip(percent); |
| 207 | } |
| 208 | |
| 209 | public void roundUp(boolean roundTip) |
| 210 | { |
| 211 | if(roundTip) |
| 212 | { |
| 213 | tipAmount = tipAmountBeforeRounded; |
| 214 | tipAmount = Math.ceil(tipAmount); |
| 215 | totalAmount = billAmount + taxAmount + tipAmount; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | totalAmount = totalAmountBeforeRounded; |
| 220 | totalAmount = Math.ceil(totalAmount); |
| 221 | tipAmount = totalAmount - billAmount - taxAmount; |
| 222 | } |
| 223 | tipPercentage = ((totalAmount - taxAmount)/billAmount) - 1; |
| 224 | } |
| 225 | |
| 226 | public void roundDown(boolean roundTip) |
| 227 | { |
| 228 | if(roundTip) |
| 229 | { |
| 230 | tipAmount = tipAmountBeforeRounded; |
| 231 | tipAmount = Math.floor(tipAmount); |
| 232 | totalAmount = billAmount + taxAmount + tipAmount; |
| 233 | tipPercentage = ((totalAmount - taxAmount)/billAmount) - 1; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | if(Math.floor(totalAmountBeforeRounded) >= (billAmount + taxAmount)) |
| 238 | { |
| 239 | totalAmount = totalAmountBeforeRounded; |
| 240 | totalAmount = Math.floor(totalAmount); |
| 241 | tipAmount = totalAmount - billAmount - taxAmount; |
| 242 | tipPercentage = ((totalAmount - taxAmount)/billAmount) - 1; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public void splitBill(int people) |
| 248 | { |
| 249 | if(people < 1) |
| 250 | throw new IllegalArgumentException("Number of people cannot be below one."); |
| 251 | numberOfPeople = people; |
| 252 | |
| 253 | // int splitTotalInteger = (int)(totalAmount *100.0); |
| 254 | // int splitTotalRemainder = (int)SplitTotalInteger % numberOfPeople; |
| 255 | // double Adjustment = 0; |
| 256 | // if(splitTotalRemainder != 0) |
| 257 | // Adjustment = -(double)SplitTotalRemainder + (double)numberOfPeople; |
| 258 | // Adjustment = (Math.round(Adjustment/numberOfPeople)/100.0); |
| 259 | |
| 260 | |
| 261 | // int splitBillInteger = (int)(billAmount * 100.0); |
| 262 | // int splitBillRemainder = (int)splitBillInteger % numberOfPeople; |
| 263 | // if(splitBillRemainder != 0) |
| 264 | // splitBillAmount = (double)splitBillInteger - (double)splitBillRemainder + (double)numberOfPeople; |
| 265 | // else |
| 266 | // splitBillAmount = (double)splitBillInteger; |
| 267 | //splitBillAmount = splitBillAmount / 100.0; |
| 268 | //splitBillAmount = splitBillAmount / numberOfPeople; |
| 269 | |
| 270 | splitBillAmount = Math.floor((billAmount / numberOfPeople) * 100.0)/100.0; |
| 271 | |
| 272 | |
| 273 | // calculate tip TODO: Can be refactored with CalculateTip() function into a generic function for both places |
| 274 | double tax_amount = 0; |
| 275 | if(taxPercentage > 0) |
| 276 | { |
| 277 | //tax_amount = splitBillAmount * taxPercentage; |
| 278 | //tax_amount = Math.floor(tax_amount * 100) / 100.0; // round tax to nearest penny |
| 279 | //tax_amount = tax_amount + Adjustment; |
| 280 | |
| 281 | tax_amount = Math.floor((taxAmount / numberOfPeople) * 100.0) / 100.0; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | tax_amount = 0; |
| 286 | } |
| 287 | |
| 288 | //double tip_amount = splitBillAmount * tipPercentage; |
| 289 | double tip_amount = Math.floor((tipAmount / numberOfPeople) * 100.0) / 100.0; // round tip to nearest penny |
| 290 | double total_amount = splitBillAmount + tax_amount + tip_amount; |
| 291 | |
| 292 | // if off by one penny, adjust the amount to reflect this correctly by increasing the tax_amount |
| 293 | double adjustment = 0; |
| 294 | if(total_amount < Math.ceil((totalAmount/numberOfPeople)*100.0)/100.0) |
| 295 | { |
| 296 | adjustment = Math.round((Math.ceil((totalAmount/numberOfPeople)*100.0)/100.0 - total_amount)*100.0)/100.0;//Math.round((totalAmount - (total_amount * numberOfPeople))*100.0)/100.0; |
| 297 | total_amount = total_amount + adjustment; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | splitAdjustment = adjustment; |
| 302 | splitTaxAmount = tax_amount; |
| 303 | splitTipAmount = tip_amount; |
| 304 | splittotalAmount = total_amount; |
| 305 | } |
| 306 | |
| 307 | } |