• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applying a discount?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I need to make a program that asks the user how many packages they want to order, and then applies a discount based on the amount of packages input into the program. I think I know how to do this but in my current code I have two errors. Here is my current code



Here are the errors I am getting from this code

[line: 14]
Error: The operator >= is undefined for the argument type(s) java.lang.String, int

[line: 14]
Error: The operator < is undefined for the argument type(s) java.lang.String, int

How should I go about fixing this?


 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
parseInt isn't changing the String userinput into an int, instead it creates a new int using userinput.

So save the result of parseInt into an int variable and then use that variable instead of String userinput for the comparison.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:parseInt isn't changing the String userinput into an int, instead it creates a new int using userinput.

So save the result of parseInt into an int variable and then use that variable instead of String userinput for the comparison.



Okay so here is my new code


It can compile now but after I input the amount of packages I want it does nothing. What should I do now?

Edit- I was able to fix it.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to double post but after I finished my code it stopped working. It can compile but it wont display the output box like its supposed to. I have a feeling it has something to do with the If Then statements but it was able to handle two of them perfectly. Did I put to many of them? How can I fix this? Here is my code

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


where is your discount variable?



you missed the closing bracket of the last else if statement, also you don't have the default else statement.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaypee Hernandez wrote:

where is your discount variable?



you missed the closing bracket of the last else if statement, also you don't have the default else statement.



I don't use a discount variable. I used the *20, *30, and so on as the discount. Also I added in that the bracket and now I am getting the following error

[line: 28]
Error: The local variable finalCost may not have been initialized

I think it is because finalCost is inside the loops and the message is outside of them. But how can I get this to work?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can fix the initialization error in one of two ways.
Add initialization before first if()

or add an else at the end
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use class names like package3. That means nothing, and should start with a Capital Letter anyway. Similary don't use variable names like print2.
Please indent the code correctly; the line after a { should be moved to the right. We have some suggestions here. Also don't write such long lines; I Have edited them all to shorten them, and you can see the right way to do it.

Consider using String#format instead of the multiple + operators. That sort of code should not be in the main method; you should have different methods for input, calculating discount, etc.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Sing wrote:I don't use a discount variable. I used the *20, *30, and so on as the discount...


I think that's Jaypee's point. You're making life difficult for yourself because you don't have one.
Also:
1. 'print2' is a really bad name for that variable.
2. If you reverse the order of your checks, your code will be a lot simpler.
3. If you put that discount calculation in a method, your code will be a lot clearer.

Viz:
Note also that I use doubles everywhere, including literals.

'99' is not a double, it's an int; so if you assign it to a double, Java has to convert it. Now these conversions are trivial; but maybe not if you have a billion of them to do.

They're also "sloppy" programming, so try and remember: If you want a double, add '.0' (or 'd' - but I prefer the first). Ie:

  double cost = 99.0;

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Speaking of discounts. My dad came up with what I reckon is one of the neatest formulas I've ever seen:

  discount = (log₁₀(quantity) * factor)%;

So, say you want your customers to get a 10% discount for every 100 items they buy in a single order, you set the factor to 5. Then they get a 15% discount if they buy a thousand, 20% if they buy 10,000... and so on.

And furthermore, if they buy 322 items, they get a 12.5% discount; and (most important for my dad), if they buy 2 items, they get 1.5% off.

Depending on the factor, you might have to set a "high" threshold for quantity, but the formula lends itself brilliantly to production costing.

Of course, he was dealing with engineers, who had no trouble with it; but I still reckon that "Discounts start at 2" is a catchy phrase.

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also cast that formula to an int, so you get the discount going up in predictable steps. Engineers like things with logs in, so they can work out the discount on their slide rules.

About 15 years ago there was an article in Scientific American about slide rules. It was headed “History of Science”. I still have the slide rule I used at school.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:About 15 years ago there was an article in Scientific American about slide rules. It was headed “History of Science”. I still have the slide rule I used at school.


I hate to say I don't; but I do have my dad's - a beautiful piece of Swiss craftmanship which feels like it's from the '30's.

But the important thing about a slide rule is that it's linear. And it's finite.

So two important things, But many programmers who've never seen one could learn from it.

Winston
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I have ever seen one in actual life - they probably started dying out once pocket calculators started becoming popular in the 80s.

My dad used to keep a pocket calculator in a drawer, and on special occasions he would let my brothers and I use it (we weren't really old enough to need a calculator, as all the Maths we did could be done using pen and paper). I can't say the craftsmanship looked like it was from the 30s, instead it looked like something mass-produced in a factory with no character at all, but when you're a kid you find these things really cool and each time my dad used to let us use it it would make my day!

(BTW, cool formula, unfortunately I can't see Sainsburys somehow adopting it anytime soon - the overwhelming majority of people in the UK wouldn't even know what log is!)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic