• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java/Android dividing items on a receipt to handle tax/discounts

 
Kevin Kahl
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a processes that essentially creates a receipt. It records a price at the minimum. It can also include a discount and a tax.

I store this information in a database and display it in a ListView. I need to be able to give a subtotal after discounts, and a total after tax in an itemized summary, like on a receipt. However, I cannot figure out how I can dynamically track the different discount/tax rates. I'm thinking maybe I need a custom class that describes a line item. But I'm still left with how to divide those objects so they can be processed. Some sort of array or list? Help!


Price - Discount - Tax

1.55 - 10% - null
14.99 - 10% - 5.5%
12.00 - 10% - 5.5%
3.05 - null - 5.5%
3.33 - null - 0.5%
3.33 - null - 0.5%
4.50 - null - null
49.99 - null - 5.5%
18.99 - 25% - 5.5%
14.00 - 5% - null

etc...

Sub: 125.73

total of all 10% prices and then apply discount
total all 25% prices and then apply discount
total all 5% prices and then apply discount
....

total of all 10% prices after discount and then apply tax
total all 25% prices after discount and then apply tax
total all 5% prices after discount and then apply tax
total all prices with no discount, and then apply tax..

you get the picture. the point is, the layers have to be broken down to be totaled appropriately and then discounted/taxed appropriately.
 
Campbell Ritchie
Sheriff
Posts: 51348
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you do it on paper? You will find that there are several ways to do that. But I think you need to get rid of those nulls first. A null is dangerous, and Java® cannot handle them the way SQL might. Did you mean to write 0 instead?
 
Kevin Kahl
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do mean zeros. I did put null there as to avoid confusion. When I think about it on paper. I think about each line item being a sticky note. I have all these piles that are like categories. So I look to see what the first is, and let's say for example, it has a 5% discount. So I put that one into a 5% pile. The next one has a different discount, so I put that into a different pile. And I continue to sort them by discount. After the discounts are applied. I break them down again by the tax rate, and then apply the taxes. In psuedocode: ok java, give me a space for a list so I can put all of x% discounts there. And another for y% discount, z%...an unknown/undeclared amount of discounts. After that, break off the ones that are b% tax rate into this list, and t% into this list and then compute the taxes on them all.


Campbell Ritchie wrote:How would you do it on paper? You will find that there are several ways to do that. But I think you need to get rid of those nulls first. A null is dangerous, and Java® cannot handle them the way SQL might. Did you mean to write 0 instead?
 
Campbell Ritchie
Sheriff
Posts: 51348
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Kahl wrote:I do mean zeros. I did put null there as to avoid confusion.
Unfortunately, that tactic didn't succeed.
When I think about it on paper. . . . to sort them by discount. . . .
That is obviously one way to do it, but you may find computer arithmetic faster than sorting, so it may be quicker to iterate your bill.

Do you have a line object? Maybe you would call it item instead.
 
Kevin Kahl
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Kevin Kahl wrote:I do mean zeros. I did put null there as to avoid confusion.
Unfortunately, that tactic didn't succeed.
When I think about it on paper. . . . to sort them by discount. . . .
That is obviously one way to do it, but you may find computer arithmetic faster than sorting, so it may be quicker to iterate your bill.

Do you have a line object? Maybe you would call it item instead.


I don't have any objects at this point because in its present form, it only supports one discount and one tax rate making the computing quite easy. Ok, so iteration. Iterating item objects? How do I keep track of the amounts and their rates? I do not know how many different tax rates and discounts the user will use, so I do not see how to define n amount of buckets to hold them.
 
Campbell Ritchie
Sheriff
Posts: 51348
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® is an object‑oriented language. If you have something, your first thought should be to create an object to model it. Your objects record the price, discount and tax rate, and they calculate the total bill for you. You create another object, maybe a List, to hold all those Items and iterate that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic