This week you'll build a class that simulates an ORCA Card, which is used to pay for bus, train, and ferry trips in Pierce and King counties. When using the real card, you add funds to it and can then swipe the card to charge rides on the various services. In addition to keeping track of the balance and paying for rides, "our" ORCA card will also keep track of things like the number of trips that have been taken and the amount of tax that has been collected. This assignment will give you some additional practice with fields, methods, constructors, conditionals, and output.
For full credit, your class should contain all of the methods described below. They should have exactly the same name as shown, take the correct inputs, and return the correct information. (I will run a program that creates instances of your class and tests them, and if your names or other details differ, my testing code won't compile.) The assignment is less specific about the instance variables (fields) you'll need you'll have to figure out what you need to store in order to implement the methods below.
1.Start by downloading the OrcaCard BlueJ project. (If you don't start with this project, you won't be able to submit via BlueJ.) The project contains an OrcaCard class, but there's currently not much in the class.
2.Your class needs to be able to keep track of how much money is currently stored on the card. This balance should be set by the constructor, which should take a single input, a double, specifying the initial balance.
3.Your class should have a topUp method that takes a single input (the amount to add to the current balance) and adjusts the balance but doesn't return anything. (It should verify that the specified amount is positive before adjusting the balance.)
4.We'll simulate the process of "swiping" the card via the buyTrip method. It should take a double as a parameter (the cost of the trip), but we're required to pay tax on the cost of the trip as well. Thus, in the body of your buyTrip method you'll need to calculate how much we owe in tax, and decrease the balance by the cost of the trip plus tax. (Assume the tax rate is 6.9%, but use a constant at the top of your class to hold this, so that we can change it easily in the future.) The buyTrip method should be void. You should also keep a separate running total of the amount of tax collected, so that we can report it to the IRS when necessary.
For full credit, you should use a conditional in buyTrip that checks whether you can afford the trip. If not, print an error message but don't adjust the balance on the card or the tax collected. If there are sufficient funds for the trip, do the bookkeeping described in the paragraph above and print a success message that includes the remaining balance on the card.
5.Report it to the IRS you say? We'll need a getTax method. It doesn't take any inputs, but should return the total amount of tax collected during "swipes" of the card.
6.We'll also add a getAverageTripCost method. It doesn't need any inputs, but should return the average cost of the trips paid for by this card. Ignore the tax we pay on the trips when computing the average. (Hint: You already keep track of the total cost of the trips, which will come in handy here, but you'll need to keep some additional information as well.)
7.Finally, write a void printSummary method that prints information about the ORCA card object. Feel free to personalize this as you see fit, but the string should contain at least the card's current balance and the number of trips taken.
8.For full credit, your code should contain comments. There should be a Javadoc-style (/** */) comment at the top of the class containing your name and a sentence or two explaining what it's about, and Javadoc-style comments above each of the methods in your class. You should add a brief comment (using //) for each of the instance variables and constants you use.
Some sample interactions with an OrcaCard in BlueJ's codepad are shown below:
> OrcaCard oc = new OrcaCard(20.50);
> oc.printSummary();
$20.5 left after 0 trip(s).
> oc.buyTrip(10.00);
Success: Ticket purchased. $9.85 remaining.
> oc.getTax()
0.65 (double)
> oc.buyTrip(5);
Success: Ticket purchased. $4.5249999999999995 remaining.
> oc.getAverageTripCost()
7.5 (double)
> oc.getTax()
0.9750000000000001 (double)
> oc.printSummary();
$4.5249999999999995 left after 2 trip(s).
> oc.buyTrip(4.50);
Fail: You can't afford this trip.
> oc.topUp(1.00);
> oc.buyTrip(4.50);
Success: Ticket purchased. $0.7324999999999995 remaining.
> oc.printSummary();
$0.7324999999999995 left after 3 trip(s).
Looking for additional challenges? Add code to buyTrip so that it also prints out a simulated ticket, showing the cost, the amount paid in tax, and the remaining balance on the card. You could add a cheatIRS method that moves the amount you've collected as tax over to the balance of the card. Look into ways to tidy up the dollar amounts so that they always have two digits after the decimal point. In my output, I printed trip(s) so that it sounded ok whether there had been one trip or more. It would look even better if you added some code that looked at the number of trips and either used trip or trips as appropriate.