• 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

Incrementing Numbers?

 
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have 3 classes that inherit my Rental Class(hourly, daily, weekly) Each class is to calculate a discount for rentals. For Example:

Hourly rentals: five dollars off all hours rented if rented for three or more hours. That is, renting a SeaDoo
for three hours would be gross($150) - $5 per hour ($15) for a total of $135.
(So I need that discount to increment by 5 every hour)

Daily rentals: one day free with every five days rental, e.g. an eight day umbrella rental would be gross
($120) - one day free ($15) for a total of $105.
(increment by 15 every 5 days)

Weekly rentals: 25% off any additional weeks, e.g. a three week rental would be $800 for the first week
and $600 for each of the next two weeks for a total of $2000

Most of this code is made by my teacher, and all the comments are things we need to do depending on what grade we want, but for right now im trying to get the discounts

Instructions
Here is my assignment and explanations just in case I have made no sense

Company Class


Rental:


Hourly:


Daily:


Weekly:
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're overcomplicating things by thinking in terms in incrementing by a certain amount.

15 + 15 + 15  is just 15 * 3
10 + 10 + 10 + 10 is just 10 * 4

See what I'm getting at? I only skimmed through your instructions but I didn't see anything that required you to do addition rather than multiplication.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ehhh. I mean like the code Below the discount would just be the cost because its one day. But if they rented it for 10 days, I couldnt just say discount = cost because thats just one day discount. So Somehow I need that if statement to look for every fifth number after 5 and my cost to add by itself after every 5th day. Confused right now
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about:
discount = numPeriods / 5 * cost
total = price - discount

Just saying there could be more options, not necessarily valid though. I'm not fully in context.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think that would work. numsPeriod is how long they are renting it for, so numsPeriod could be any number of hours, days, or weeks. ive tried looking at some stuff, but still not any luck
 
Saloon Keeper
Posts: 10687
85
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
Could you normalize hours, days, and weeks, as their equivalent number of hours?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote: So Somehow I need that if statement to look for every fifth number after 5 and my cost to add by itself after every 5th day. Confused right now


No, you don't. You're making this way too complicated. Stop overthinking the problem. Don't try to do everything in the if-statement. Break your problem down into smaller chunks, like how Liutauras showed.

You need to figure out a formula for calculating the discount. Tip: It's just division, multiplication, and subtraction.  

I'll give you a similar example. If a store sold Oreos at $2 for 1 pack but $5 for 3 packs, how much would you pay if you bought 5 packs? 6 packs? 7 packs? 8 packs?

5 packs:  5/3 = 1 remainder 2.  So you pay $5 * 1 + $2 * 2 = $9
6 packs:  6/3 = 2 remainder 0.  So you pay $5 * 2 + $2 * 0 = $10
7 packs:  7/3 = 2 remainder 1.  So you pay $5 * 2 + $2 * 1 = $12
8 packs:  8/3 = 2 remainder 2.  So you pay $5 * 2 + $2 * 2 = $14

 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I was wrong. Liutauras had it right. Now I just need to find out for the 35/hour and weekly discount.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to do that is to write down your calculations for several examples the way I showed before then look for a pattern.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Could you normalize hours, days, and weeks, as their equivalent number of hours?


There's really no need to do that. Each unit has its own rule for calculating a discount, that's why there are three different subclasses for Rental.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have my calculations, butt now I need my parent class to get the values from my three getDiscount methods. The other thing is I know my 3 discount methods return a double. Talking to the instructor there is only suppose to be 4 Parmeters for the Rental class, so I can’t pass in the discount value that way. Do I just create a local variable then?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:I have my calculations, butt now I need my parent class to get the values from my three getDiscount methods. The other thing is I know my 3 discount methods return a double. Talking to the instructor there is only suppose to be 4 Parmeters for the Rental class, so I can’t pass in the discount value that way. Do I just create a local variable then?



Do you understand why you have three subclasses of Rental: Hourly, Daily, and Weekly? Why do you think those classes only define an overridden public double getDiscount() method?

Go back over your notes about inheritance, polymorphism, and overriding methods.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The design of the program is much like what you have here:

You can see what this code does here: https://repl.it/@jlacar/SimplePolymorphism
reply
    Bookmark Topic Watch Topic
  • New Topic