+ 2
Help Java Coders, Discount Problem
Hello i just wanna know what is the code for this problem, my teacher sucks at teaching java , i'm here hoping to answer the problem with you guys 🙏🏻❤️ Problem: Create a Program which will accept the amount of 3 ITEMS a customer bought. The program must compute for the TOTAL PURCHASE and DISCOUNTED AMOUNT of the total purchase. The program must also trap for the INVALID AMOUNT of purchased item. Here are the discounts: Less than 500 = No discounts 500-799 = 20% 800-1000 = 30% above 1000 = 50% Thank you so much guys 🥺
5 ответов
+ 1
I know you're asking for a full solution now but solving it at least partly yourself would be more rewarding. Doing it yourself would also give you a backup plan if the complete solution isn't given to you in time. You could share it in Sololearn's Code Playground too.
The problem likely sounds too hard right now so here are some smaller iterations of the problem/solution and you could step through them up to the more complete solution:
1. Write a Java program that has a main function that compiles and runs but does nothing else. You could find and copy this from an example online if your teacher didn't explain this anywhere. There should be a Hello World Java program and you could just remove the print statement.
2. Write a Java program that reads an integer from standard input(System.in) using a Scanner. If Scanner isn't defined, find the appropriate import statement.
3. Write a Java program that reads an integer and if it is under 0, gets the user to type a new number.
4. Write a Java program that reads 3 integers with a minimum of 0 and prints out their total.
5. Write a Java program that reads 3 whole numbers and prints 20% of their total.
6. Write a Java program that reads 3 whole numbers and processes the discounts according to the original problem description.
+ 2
Here is a small starting point. I don't want to give a very complete solution and ruin the potential for you to learn from this.
int[] itemCounts = new int[3];
// FIXME: read the counts from the user. You could use a new Scanner(System.in); and its nextInt() method to read integers.
// Validate that they're at least 0 each.
// You can't buy -1 or -2 of any item.
// Calculate the discounts.
double discountTotal = 0;
for (int itemCount: itemCounts)
{
if (itemCount > 500 && itemCount <= 799)
discountTotal += 0.2 * (itemCount - 500);
// handle the other cases.
// This isn't absolutely clear from the question but I would assume the following.
// The math for other intervals should likely work like income tax so that 801 items is discounted at 0.2 * (800 - 500) + 0.3 * (itemCount - 800).
// Similar math should be used for the other one.
}
System.out.println("Discount Total: " + discountTotal);
One thing I wasn't sure about from the question was the price per item. I implemented assuming each 1 item costed $1. Some details are left out that you might not care about. For example, I'm not rounding to nearest cent.
+ 1
hi Sir Josh, thank you so much for spoon feeding! i think i got the problem right now. THANK YOU ❤️
https://code.sololearn.com/cvQRDRaIvB3m/?ref=app
0
i was looking for the full answer to fully understand the logic but still thank you so much Sir Josh for letting me having an insight on this problem ❤️ also it depends on the user of how much the price they will input, so it's not pre defined value 🙏🏻
0
Excellent. Let us know how well it goes when you submit.