programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Paul Clapham
• Devaka Cooray
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Knute Snortum
• Liutauras Vilda
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Piet Souris
Bartenders:
• salvin francis
• Carey Brown
• Frits Walraven

# The KISS principle

Ranch Hand
Posts: 123
2
Started reading Real world software development and I practice the KISS principle.
Could someone correct my process of thought, please?

Scenario:

There is an all plates same price restaurant. Price per plate is 4\$:
There are 6 different types of plates.

The owner wants to make the customers to buy different plates of food. He offers 7.5% discount for 2 different plates, 9% discount for 3 different plates, 12% discount for 4 different types of plates, 15% for 5 different plates of food and 20% for 6 plates of food.

Ex:
If a group of 6 buys the same dish: total = 4 * 6
If a group of 3 buys 2 different plates and 1 is the same as: total = ((100 - 7.5) * 2) / 100 + 4

2 - Buy 2 different plates
3 - Buy 3 different plates
4 - Buy 4 different plates
5 - Buy 5 different plates
6 - Buy 6 different plates

customerChoices: 1 * first plate + 2 * third plate + 3 * fifth plate + 3 * forth plate + 6 * second plate
customerChoicesWithDiscount = ((100 - 9) * 6) / 100 + 4) + ((100 - 9) * 9) / 100 + 4)
total = customerChoicesWithDiscount

1. My thought process was to firstly build functionality to check if the plate added and the price are correct. Validation.

2. Have a list where I add the plate.

3. Count the occurrence of each plate

this is where I stoped as I thought I might overcomplicate as per KISS principle.

Saloon Keeper
Posts: 3900
154
hi Lg,

If I write down the frequencies in a List. I getL

That list has size 5, so we're in the 15% discount. First part of the price is therefore: 5 * 4 * 0.85.
Now, subtract 1 from every frequency, and remove zeroes. The list will then become: {1, 2, 2, 5}, thus entering the 12% discount rate. Second part of the price will then be: 4 * 4 * .88.

Et cetera.

Lg Long
Ranch Hand
Posts: 123
2
hi Piet,

Piet wrote:
If I write down the frequencies in a List. I getL

The pattern makes sense. But syntactically, I am afraid I don't get it. When I try to use "List.of()"

Piet wrote:
That list has size 5, so we're in the 15% discount. First part of the price is therefore: 5 * 4 * 0.85.
Now, subtract 1 from every frequency, and remove zeroes. The list will then become: {1, 2, 2, 5}, thus entering the 12% discount rate. Second part of the price will then be: 4 * 4 * .88.

This is what I understand from what you say:
1. Customer adds to customer1ShopBascketList products.
2. A mechanism loops through customer1ShopBascketList
3. As soon as it finds a pattern 1,2,3,4,5 applies 15% discount. Then goes to find other patterns and to apply discount.
4. Customer Check out.

Now how about KISS: for a problem of this type how keep it simple should look like?

Thank you for taking interest.

Piet Souris
Saloon Keeper
Posts: 3900
154
hi Lg,

I notice that this assignment can be understood in another way than I described: it might be the case that if you have 20 plates in total, that contain 6 different plates, that all 20 will have a 20% discount. If there are only 3 different plates among the 20, then all 20 will have a 9% discount. Interpreting it in this way certainly would simplify the total price calculation. How do you interprete the assignment?

Since all plates have the same price, I only need to know the frequencies. The plates they represent are irrelevant. In your example you have the frequencies 1, 2, 3, 3, 5, five frequencies in total. I now split these frequencies in the following frequencies:

1, 1, 1, 1, 1   -  price 5 * 4 * (1 - .15)
1, 1, 1, 1   -  price 4 * 4 * (1 - .12)
1, 1, 1   -  price 3 * 4 * (1 - .09)
1   -  price 1 * 4 * (1 - .00)
1   -  price 1 * 4 * (1 - .00)

(my interpretation). Should the above mentioned interpretation be valid, then the total price would simply be (1 + 2 + 3 + 3 + 5) * 4 * (1- .15).

I need a List<Integer> containing the frequencies. A very convenient way to do so is via the method 'List.of(...)'. But the list created that way is completely immutable, and as you see, I do want to chamge that List. So I create an ArrayList with a Constructor that takes another Collection. So I now have a mutable ArrayList, created with the convenience of List.of().

Sheriff
Posts: 7062
184
List.of() was introduced in Java 9, so you need at least that version to use it.

Lg Long
Ranch Hand
Posts: 123
2

Knute Snortum wrote:List.of() was introduced in Java 9, so you need at least that version to use it.

Thank you. At the moment I am using Java 8.
That's why the confusion.

Lg Long
Ranch Hand
Posts: 123
2
Will come back on this post!

Lg Long
Ranch Hand
Posts: 123
2
After reading on how to solve a problem, I understand that if i can't articulate in English what I want to do, then for sure I can't write a program.

The way I would articulate this algorithm is:

1. Method to add items to a list

2.  Have a getTotalPrice() method.

3. Have a total() method where I am doing validation for different types of discounts

4. I would have methods to check for different sets of items

I think I am stuck on building the sets and pushing the sets to the .

So in my solution:
1. loop through the list - > I guess I could use the iterator() method
2. build sets of unique items - > I am confused how to build this mechanism
3. calculates total minus discount - > this is my total() method
4. pushes the set to the cartTotal
5. start again

What CodeRanch thinks think?

Piet Souris
Saloon Keeper
Posts: 3900
154

First of all: why don't you have a class 'FoodshopCard'or 'FoodOrderingCard', that has methods like 'addFoodItem' with the quantity? You could have a Factory method in the Farmshop class.

And in point 4. I see this method:
Where are the 'itemQuantity' and 'farmShopItem' coming from?

Enough for now.

If your FarmShopItem has overridden the hashCode and the equals method, then if you have a list of FarmShopItems, you can put all items in a Set, that allows only different keys. The size of this Set tells you how many different Items there are.

Lg Long
Ranch Hand
Posts: 123
2

Where are the 'itemQuantity' and 'farmShopItem' coming from?

under,

1. Method to add items to a list

find a simplified class having just fields, assuming constructor, getter setter, equal and hashed, to string involved

'itemQuantity - that is an issue I was hoping to be picked up and provided with a solution.

At the moment, there are 2 ways of dealing with quantity:

1. have a field in FarmShopItem

2. create new instances of the same object.

Question:
In this scenario why should I bother having that field in? I won't have memory problems. I can easily add as many instances of a product and calculate the frequency of each. That would be my quantity.

There are 2 classes at the moment: Model -  FarmShopItem; Controller: FarmShop.

I could have a factory to deal with my total() method.
That is the end goal. Calculate the total of an order.
To calculate the total of an order in this scenario, methods for:
1. calculating the sequences of similar objects by name in farmShopCart list
2. split in sets of 5, 4, 3 etc return set
4. add new set-total to finalTotal
5. repeat step 2 and 3
6. stop

I know point 1. How do I push to sets. Do I need to make a list of sets? How do I apply discount to specific sets? I might be able to sort out how to add the total, but it's far away so I don't need to worry about it.

Marshal
Posts: 68936
275
Don't use bytes. Use ints.

Knute Snortum
Sheriff
Posts: 7062
184

Lg Long wrote:
I could have a factory to deal with my total() method.
That is the end goal. Calculate the total of an order.
To calculate the total of an order in this scenario, methods for:
1. calculating the sequences of similar objects by name in farmShopCart list
2. split in sets of 5, 4, 3 etc return set
4. add new set-total to finalTotal
5. repeat step 2 and 3
6. stop

I know point 1. How do I push to sets. Do I need to make a list of sets? How do I apply discount to specific sets? I might be able to sort out how to add the total, but it's far away so I don't need to worry about it.

I'm not sure what data structure you use to hold sequences, but assuming they're Strings, what about a Map of Lists?  (or Sets)

Lg Long
Ranch Hand
Posts: 123
2

Don't use bytes. Use ints.

Why not use bytes?

I would not expect quantity > 127, therefore the usage. Of course I could make a validation rule to limit the user.

Campbell Ritchie
Marshal
Posts: 68936
275
• 2
Because ints are designed for counting and arithmetic. Work out how much the memory difference would cost between 1,000,000 ints and 1,000,000 bytes.

Campbell Ritchie
Marshal
Posts: 68936
275
• 1
I have done a little mental arithmetic and found out that the memory saving is a lot more than I thought at first. If you are in the States, the excess memory consumption would cost something in the region of 2¢.

Piet Souris
Saloon Keeper
Posts: 3900
154
@Lg,

in my replies at the beginning of this topic I gave two different interpretations of how to determine the discount. A couple of replies ago you wrote:

Lg wrote:
1. loop through the list - > I guess I could use the iterator() method
2. build sets of unique items - > I am confused how to build this mechanism
3. calculates total minus discount - > this is my total() method
4. pushes the set to the cartTotal
5. start again

Which of my two interpretations are you using (or maybe a third one) ?

A FoodShopOrdering class seems handy, like Knute proposes. As far as I m concerned, it could have a method 'addPlate(FoodItem plate), maybe with a quantity. An internal List would be fine, if you send this Card to the FoodShop to calculate the price, let the Foodshop deal with quantities and discounts.

Another thought: how to make sure that the only plates involved are the plates that te Foodshop sells?

Lg Long
Ranch Hand
Posts: 123
2

Another thought: how to make sure that the only plates involved are the plates that te Foodshop sells?

I know how to do that, I wouldn't worry about that at the moment.

I feel that I am moving further away from my goal. Maybe a fix would be to write unit tests and be like, 'ok, how do I make these pass?'

Which of my two interpretations are you using (or maybe a third one) ?

I wouldn't really want to use your interpretations unless I would understand that my algorithm is not good.

Campbell Ritchie
Marshal
Posts: 68936
275

Lg Long wrote:. . . moving further away from my goal. Maybe a fix . . .

That makes it look as if you were guessing about your coding. You can make 1,000,000 guesses and there is a vey high chance that some of them will turn out to be correct. Or you can plan your application and get it to work first time.
What is you plan for working out how many plates/dinners/orders you have got?

Lg Long
Ranch Hand
Posts: 123
2
@Knute

I'm not sure what data structure you use to hold sequences, but assuming they're Strings, what about a Map of Lists?  (or Sets)

Ok, i feel like we start to get somewhere with this question. Why do I need a Map of ?

1. I have a List(CartList) where the products are added. easy to do.
//this where the confusion starts
2. I have a mechanism that loops through the CartList and groups items per requirements. The mechanism returns the sets

So I should build a mechanism that loops through CartList for plate5, plate4, plate3, plate2, plate1. If all found push to a set called fiveDifferentProducts. If 4 found push to a set called fourDifferentProducts etc

Would those sets need to be in a Map? why can't I have different sets for individual discounts and add the total at the end?

What do you think of my post at 20/04/2020 13:46:26 ?

Campbell Ritchie
Marshal
Posts: 68936
275
• 2
This would appear to be from the 13:46:26 post you asked about:-

Lg Long wrote:. . . if i can't articulate in English what I want to do, then for sure I can't write a program.

That's right.

The way I would articulate this algorithm is:

1. Method  . . .

At that point you stopped writing English and started writing computerspeak. So you are now leaving the realms of articulating anything. “Method to add items to a list” contains three words with their ordinary English meanings: “to” (twice) and “a”. Yes, I am exaggerating a bit, but you have gone straight into thinking about your implementation before you worked out what you are going to do. Something like, “Record the whole of my order,” would have been ordinary English.
As you said correctly on Monday, you need to articulate what you are doing in ordinary English before you have any chance of actually doing it. And make sure you aren't writing any code until you have finished articulating what you want to do.

Knute Snortum
Sheriff
Posts: 7062
184

Lg Long wrote:Ok, i feel like we start to get somewhere with this question. Why do I need a Map of ?

I probably put the cart before the horse here.  You should first describe in English what you want to do without referring to any programming jargon.

1. I have a List(CartList) where the products are added. easy to do.
//this where the confusion starts
2. I have a mechanism that loops through the CartList and groups items per requirements. The mechanism returns the sets

So I should build a mechanism that loops through CartList for plate5, plate4, plate3, plate2, plate1. If all found push to a set called fiveDifferentProducts. If 4 found push to a set called fourDifferentProducts etc

This shows what Campbell was speaking of.  You have a lot of computer-speak here but not a clear description of what you want to do.  I can tell you that using different variable names for different sizes of Sets is a bad design, but I can't tell you what is a good one until I know what you're trying to do.

Knute Snortum
Sheriff
Posts: 7062
184
• 3

What do you think of my post at 20/04/2020 13:46:26 ?

Just a note about how to refer to a previous post without quoting: use a link like Campbell did.  You can do this by clicking on the "page" icon near the top of the post, next to the number of seconds/minutes/hours/days.  (I'm not sure of the color.  Orange, maybe?).  If you click on this you will get the URL of your post in the "address bar" of your browser.  Copy this a paste into your new post.  Alternatively, you can right-click the icon and select "Copy link address" or something like that depending on your browser.

Lg Long
Ranch Hand
Posts: 123
2

articulate what you are doing in ordinary English

Goal: return Payment Total minus discount, for shopping cart

// making abstraction of any other methods to add or validate product entrance in the cart for now. Consider the cart has multiple instances of each product

My algorithm:

// separating the Cart List in to Sets
1. Loop over the List
- if product searched for, matching product in Cart List, push to Set
- if product trying to be pushed, already in set, push to new set
- return sets (list of sets? Is this why Map of sets was suggested)

//apply discount to Sets. I think this could be broken in different steps?
2. Loop over each Set produced (could be: Loop over Map of sets)
- if the number of different items = 6, apply 20% to item total
- if the number of different items = 5, apply 15% to item total
- if the number of different items = 4, apply 12% to item total
- if the number of different items = 3, apply 9% to item total
- if the number of different items = 2, apply 7.5% to item total
- if the number of different items = 1, don't apply discount
- return sum of sets, after discount for each set

3. Print total without discount
4. Print total with discount

Would this be clear to read in english?

Knute Snortum
Sheriff
Posts: 7062
184

Lg Long wrote:// separating the Cart List in to Sets
1. Loop over the List
- if product searched for, matching product in Cart List, push to Set
- if product trying to be pushed, already in set, push to new set
- return sets (list of sets? Is this why Map of sets was suggested)

You're still talking about Sets and Lists and pushing and returning.  These are all programming jargon.  But I still don't know why you want to separate cart items like this or why the product searched is selected.

Lg Long
Ranch Hand
Posts: 123
2

But I still don't know why you want to separate cart items like this or why the product searched is selected.

The only way I can think of calculating the total price is to have a mechanism that finds patterns, applies discount and adds that to the total. Repeat.

I'll try again.

1. Check(previously had loop, but new post added 2 min after my post) the products added to the Cart
- if 6 different products found in the ShopingCart apply 20% discount to their total price
- add discounted totals 'to Pay'
- remove the 6 discounted items from the initial Cart

- if 5 different products found in the ShopingCart apply 15% discount to their total price
- add discounted totals 'to Pay'
- remove the 5 discounted items from the initial Cart
and so on..

Is this more clear?

Campbell Ritchie
Marshal
Posts: 68936
275

Lg Long wrote:. . .
Goal: return Payment Total minus discount, for shopping

Stage 1: Good

. . . 1. Loop over the List . . .

Back to computerspeak.

Sorry to tell you again what Knute has already said.

Would this be clear to read in english?

Good again You have realised there is a problem, and knowing there is a problem is the first stage towards solving it.

Get yourself a periodic table of the elements, and find element 14. It's called silicon, and it is a major constituent of a computer. It is also very harmful (just at the moment), and needs to be inactivated, which you do by preventing any electrons from running through it.
Go up one and you will find element 6, called carbon. It is the active ingredient of a pencil, which you need, along with paper (containing carbon and usually devoid of silicon) and a rubber (also containing carbon and usually devoid of silicon); you need all three. Get the biggest rubber you can because you will use it a lot
Write down a shopping list for your dinners. Make sure you don't write counts against any of the dishes.
Count the dishes and write down what you do for counting.
Work out for each dinner what the discount will be.
Calculate the prices and add them up.
Now you have decomposed your goal into four stages. If that doesn't make it really clear, subdivide each stage into sub‑stages. It will read a bit like the following:-That might not help you calculate discounts, and it might appear unrealistic to do that, but that shows you the process you need to follow.

The “unrealistic” part is of course, expecting to find a timetable at a bus stop, or thinking that buses' arrival times bear any resemblance to what the timetable says.

Piet Souris
Saloon Keeper
Posts: 3900
154

Knute Snortum wrote:
You're still talking about Sets and Lists and pushing and returning.  These are all programming jargon.  But I still don't know why you want to separate cart items like this or why the product searched is selected.

I think that OP adopts the algorithm that I described in my first reply. I asked for that a few replies ago, but OP replied (rightly) he is not concerned now about how exactly the total price is to be calculated. But now we're back at the implementation, so to see.

My last remark:

For me it boils down to:

1) there is a FoodShop selling different FoodItems.
2) a Customer can fill in a FoodOrderingCard, with the FoodItems he wants.
3) the FoodStore calculates the total price.

Then work out the details, or write tests that give you a clue what and how to implement.

Lg Long
Ranch Hand
Posts: 123
2
@Campbell, thank you for the effort.

Some dude said that if you can't mentally model, just go home and find another job.
After this exercise, I believe it takes practice.

This is what I tried. I need to think a bit more on it. I also need to read more on the text above the 'stages'.

Then work out the details

@Piet

I know how to do point 1 and 2.

I need to break down point 3. That was my main problem from beginning.

Knute Snortum
Sheriff
Posts: 7062
184
You're getting there.  I'm still confused about (at least!) one thing: When I think of a shopping cart, I don't think of it this way:

* a lemon
* another lemon
* another lemon
* a quart of milk
* another quart of milk

but like this:

3 lemons
2 quarts of milk

Would this be five things or two things?

Lg Long
Ranch Hand
Posts: 123
2

Would this be five things or two things?

I understand, it is the natural way of thinking.

In this scenario there would be a discount of 7.5, as there are 2 unique items, but those unique items are repeated one more time therefore no discount is added to it.

I think it would be 3 things in the back end?
total = 1 lemon + one quarter of milk - ((quarter of milk + lemon) * 0.075) + 2 * lemons + quarter of milk

or could be 4 if we remove the multiplication and we just add one the second remained lemon?

How would you calculate the discount if you would think of it as 2 things?

Knute Snortum
Sheriff
Posts: 7062
184
If I'm understanding you correctly, a more natural way of looking at the situation would to say:

Total = qty of lemons * lemon price + qty of milk quarts * milk price - (lemon price + milk price) * 0.075

That is easily calculated if you have two item in your cart, each with a quantity and a price.

* for each item in the cart
*   add the item's extension (qty * price) to the extension total
*   add the item's price to the price total
* subtract from the extension total the price total * 0.075

Marshal
Posts: 25456
65

Lg Long wrote:How would you calculate the discount if you would think of it as 2 things?

To me the discount would always be calculated based on the total amount, regardless of the number of "things". But perhaps the discount only applies under certain circumstances? I scanned the thread but didn't really find a requirement which described that.

Edit: Okay, there it is in the original post. As I should have expected. And then followed a whole lot of techie discussion about how to calculate the discount. So from a commercial point of view, here's the process:

1. Calculate the gross sale amount (without discount).

2. Find the discount tier which applies.

3. Find the rate for the discount (which may be zero percent).

4. Calculate the discount.

5. Subtract it from the gross sale amount.

It seems that Step 2 is the source of confusion at the moment.

Lg Long
Ranch Hand
Posts: 123
2

2. Find the discount tier which applies.

Hope this makes it more clear.
Scenario 1: 1 whole chicken , 2 carton of eggs, 3 boxes of milk, 4 kg of potatoes, 5 apples, 6 stakes

Group1: 1 whole chicken + 1 carton of eggs + 1 box of milk + 1 kg of potatoes + 1 apple + 1 stake = 6 different items in the basket => total - 20% discount
Group2: 1 carton of eggs + 1 box of milk + 1 kg of potatoes + 1 apple + 1 stake = 5 different items in the basket => total - 15% discount
Group3: 1 box of milk + 1 kg of potatoes + 1 apple + 1 stake = 4 different items in the basket => total - 12% discount
Group4: 1 kg of potatoes + 1 apple + 1 stake = 3 different items in the basket => total - 9% discount
Group5: 1 apple + 1 stake = 2 different items in the basket => total - 7.5% discount
Group6: 1 stake = one item in the basket => total (no discount offered)

Pay = Group1Total + Group2Total + Group3Total + Group4Total + Group5Total + Group6Total

@Knute

3 lemons
2 quarts of milk

Would this be five things or two things?

Thinking of your question, I understand your logic. Essentially they are 2 things, but from a marketing perspective they are 5 things.
Would be interesting to see how I could implement your interpretation as well.
For learning purposes I could develop certain features for this project.
I do need to learn how to work with Date&Time, so it could be interesting to have 'your interpretation' discounted for holiday season.

But for now, I need to focus and understand how I can calculate the Pay

Campbell Ritchie
Marshal
Posts: 68936
275

Lg Long wrote:@Campbell, thank you for the effort.  . . .

That's a pleasure

There are many ways to skin a cat, but I think I would do it differently. If I go to the till at Sainsurys' or Morrisons', I sometimes find there is a “multibuy” offer, but that appears at the end of the till receipt.

The computer in the till does the counting for me, and finding there are four cans of beans on the belt, gives me the 70p discount. It doesn't see the whole list and count cans of beans, but counts them as each goes past the assistant behind the till. So irrespective of whether I picked the beans off the shelf simultaneously, there is no way to predict that all the beans will go past the till together, nor is there any way to predict whether they will be together or interspersed with other purchases. So let's consider what goes onto the belt.The same sort of thing happens when your large party go out for dinner. Reference. The waiter says, “Are you ready to order?” and you go round the table saying things like,Assuming that “lasagne” and “Lasagne” are both the same thing, the waiter writes this sort of thing on his little pad:-Now he knows to give you 5% off for the two beefs and 10% off for the three lasagne (or whatever the percentage is). So you get a bill like this:-I have no idea where you would find a technique for counting like that, but if you go into the Java™ Tutorials and push ctrl‑F‑“Map” and look at the first hit, you might find something to your advantage. Remember what Piet said on Monday about the equals() and hashCode() methods. Now your only problem is how to remember who wanted the vindaloo and who wanted the mild curry because you will get big trouble if you swap the two.

Why are people going on about the size of sets? The size of a set containing lasagne 4× is one.
Have you considered an enum type for your dinners?

Knute Snortum
Sheriff
Posts: 7062
184

Lg Long wrote:

@Knute

3 lemons
2 quarts of milk

Would this be five things or two things?

Thinking of your question, I understand your logic. Essentially they are 2 things, but from a marketing perspective they are 5 things.
Would be interesting to see how I could implement your interpretation as well.

My algorithm would be like this:

an order has a list of line items
a line item has a price, quantity and discount

remember the number of line items
for each line item in an order
determine discount by number of line items
set the discount of the line item
decrement number of line items
set line items back to order

to total an order
for each line item in order
extension is qty * price - discount
sum extension

I coded this for fun, but I'd like to see how you do it before I share it.

Lg Long
Ranch Hand
Posts: 123
2
@Knute

This is what I tried.

I do get a null pointer exception in line 22.

Campbell Ritchie
Marshal
Posts: 68936
275
Getting better

I would prefer to have an Item class and (I remember this from a databases course where we looked at a medical prescription, and line is the correct wo‍rd) a Line class. The Line contains an Item, a non‑zero (positive) amount and a discount.
You are making life difficult for yourself by iterating the List in nested loops. What's more, what will happen if your item 0 is the same as your item 3? Are you going to count that item into two lines? That is where the Map's counting method which I didn't tell you about on Thursday is so nice. You only need to iterate the List once. Then you can use the counts to create Line objects, and can calculate the discount as you create the |Line objects.

By the way, what are you going to do about the following scenario?

Campbell arrives two minutes after everybody has ordered:-

Yes.

In that case, sorry to inconvenience you, but I would like a steak'n'kidney pie please.

[end quotes]

Your Map will allow you readily to find the Line for a particular Item, and you can consider adding an extra Item to that Line and recalculating your discount.

Lg Long
Ranch Hand
Posts: 123
2

Getting better

Thanks!

1.

Line class

Did a quick google search & a Ranch search. is Line Class concept related to composition, inheritance & polymorphism? webpage
Could I get a real life example with a List Class?

2.

What's more, what will happen if your item 0 is the same as your item 3?

The reason I choose to use that formula is because I wanted to learn how to do it manually. I feel that if I would know how to write it by hand I could learn how to write scripts, in the same time(for other languages).
I was looking at examples here: webpage

3. I think that by using just one data structure and learn the mistakes and the problems created by using it for the solution, would teach why I need to use another data structure or combine data structures to solve the problem?

4.

You only need to iterate the List once. Then you can use the counts to create Line objects, and can calculate the discount as you create the |Line objects.

This means I need to give up on the ArrayList and use a HashMap?

Bonus:

By the way, what are you going to do about the following scenario?

I imagine this line would have an index aka order number, possibly UUID with Date&Time? On what basis would you find the Line? By remembering one particular item that has been added. To me, this goes in 2 directions: human interaction or machine learning.
I understand that you are trying to make me realise I need to use Map?

Knute Snortum
Sheriff
Posts: 7062
184
I haven't tested it, but here's how I'd do the discount processing:

 Wanna see my flashlight? How about this tiny ad? Two software engineers solve most of the world's problems in one K&R sized book https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton