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:
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.
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.
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.
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
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.
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.
TransitCalculator.
I had diffrent idea for making it work.
I used additionaly Scanner.nextInt() so it is kinda interactive in cmd.
It may be a bit long, but for now I am moving on to another projects and won’t be changing it.
//I am not native, if (I made any mistake) {sorry;}
My gist: https://gist.github.com/362a87cb4cce6ac28ffaa1a8d4e262bc. I put some print statements along the way to check my work. BTW, the Codecademy solution code is wrong! It is not using the correct calculation for the 30-day fare, which is very similar to calculating the 7-day fare.
Actually, the problem is oversimplified - if you have 37 days and a bunch of rides, for example, you would want to use one 30-day, and one 7-day card, not two 30-day cards. And if the rides are not spread out evenly over the days, it can get pretty complicated - I didn’t even attempt that…
For this project, I ventured in a far different path from the guides provided. My aim was to find the best combination of plans to maximize savings, e.g. 1 7-day pass + 3 single-use pass, instead of purchasing 2 7-day passes. Enjoy!
I attempted to find the best combination of ticket plans! Check out my code that I’ve shared in the forum too!
I got quite tired after optimizing this though so I didn’t add in for different age groups or cities. This can be done easily by changing the options array or adding arrays to be referenced to within the code.
Using scanner class is really cool! Opening up to user interface means that the code is friendly to non-coders as well, I think it’s a great idea to include
Great work considering you only spent 2 days learning Java. Some thoughts:
I’d recommend you to have the habit of adding more comments, perhaps some detailed ones to improve readability for yourself (if you revisit many years later) and others (perhaps such as your future manager).
You can try to improve beyond this project! Personally, I felt that one major issue of the task given was the inflexibility to combine multiple plans. E.g. If one was travelling for 37 days, having two 30-day pass would be overpriced and unnecessary, but a 30-day pass and a 7-day pass combined would be perfect. Various combinations give rise to different prices. By calculating for each combination, the code can then truly suggest the cheapest combination plan for the customer. I have actually tried accounting for such combinations, but there’s no model answer to doing it. Feel free to reference my solution I shared previously
I think the exercise needs to be reworded to address whether we should be finding the best combination or just the best plan with the assumption you can ONLY bulk buy a singular plan. Writing the code with the intention of also finding the best combination raises a lot more if statements and edge cases that need to be addressed.
I have only slightly glanced at lucastan’s code but it seems to address the more complicated problems that arises when looking at optimizing all the plans. Congrulations!