Best Fare Calculator Challenge Project (Java)

Congratulations on completing the Best Fare Calculator project!

Compare your project to our solution code and share your project below! Your solution might not look exactly like ours, and that’s okay! The most important thing right now is to get your code working as it should (you can always refactor more later). There are multiple ways to complete these projects and you should exercise your creative abilities in doing so.

This is a safe space for you to ask questions about any sample solution code and share your work with others! Simply reply to this thread to get the conversation started. Feedback is a vital component in getting better with coding and all ability levels are welcome here, so don’t be shy!

About community guidelines: This is a supportive and kind community of people learning and developing their skills. All comments here are expected to keep to our community guidelines


How do I share my own solutions?

  • If you completed the project off-platform, you can upload your project to your own GitHub and share the public link on the relevant project topic.
  • If you completed the project in the Codecademy learning environment, use the share code link at the bottom of your code editor to create a gist, and then share that link here.

Do I really need to get set up on GitHub?
Yes! Both of these sharing methods require you to get set up on GitHub, and trust us, it’s worth your time. Here’s why:

  1. Once you have your project in GitHub, you’ll be able to share proof of your work with potential employers, and link out to it on your CV.
  2. It’s a great opportunity to get your feet wet using a development tool that tech workers use on the job, every day.

Not sure how to get started? We’ve got you covered - read this article for the easiest way to get set up on GitHub.

Best practices for asking questions about the sample solution

  • Be specific! Reference exact line numbers and syntax so others are able to identify the area of the code you have questions about.

My solution https://gist.github.com/115feb564ff4b9079cada588ae6773c4

A little shorter than the sample solution, in getRidePrices() I didn’t create any new variables I did everything inside the array and in getBestFare() I didn’t create a new array I just used the getRidePrices method itself the same way I would use an array. Not sure if that is better or worse way of doing it in the long run but I was pleasantly surprised that it worked.

I didn’t know about any of the Math operations so in unlimited7Price I just used modulus to see if there was any remainder and if there was I added 1 to the number of passes bought. The last difference was I did the conversion from total price to price per ride at the end when I returned the String.

I had a lot of fun figuring this out! Any feedback would be appreciated.

You got something wrong in your solution in unlimited7Price method. If the number of days is below 7, math.ceil would return 0.0.

I have been at this for over 5 hours and so far it does not work. So we attempted the solution’s code and the solution code is incorrect according to the requirements.
" * 20 rides over 19 days should return 4.95

  • 50 rides over 22 days should return 2.64
  • 14 rides over 6 days should return either 2.357142857142857 or 2.36"

the sample code outputs:
Single Ride: 2.75
30 Day Unlimited: 2.54
and last 7 day unlimited: 2.357

This is my solution https://gist.github.com/41b4b5cc27e86247a9496548f4da03cf

I thought this was an interesting program to write. I need to find a better way to return a string. The solution returned the string in a single line, but I think it is too long. I think it is convention for lines to be shorter than 80 characters. I could have definitely worked on reducing number of lines, but it would reduce readability and for a short program like this I don’t think extra lines hurt.

On to the next one :slight_smile: !

Could you post your code? You are looking at unlimited7Price function right? 20 rides over 19 days prints 4.95 correctly. This is because you need a minimum of three 7 day passes to travel for 19 days. Therefore you need to spend 99 dollars. So we calculate what each ride costs, 99 / rides, where rides = 20. Then the average cost per ride using the unlimited7Price is 99/20 = 4.95.