OrcaCard
class with state representing the actual state of a card, and behaviors modeling the kinds of things we expect to do with the card. The most important state information is the balance (dollar amount) remaining on the card, though for this assignment you'll also need to store the number of trips taken. (As you work through the assignment you may decide to store additional information as well.) By the time you're done with the steps below, your class will have a constructor and the five methods (behaviors) shown on the menu below.
OrcaCard
class, but there's currently not much in the class.
double
, specifying the initial balance on the card.
topUp
method that takes a single input (the amount to add to the current balance) and adjusts the balance but doesn't return anything. For full credit, it should verify that the specified amount is positive before adjusting the balance. (If the value is negative your method shouldn't update the balance.) It doesn't need to complain to the user if the input is negative — silently ignoring them is fine — but feel free to insult the user if desired if they try to add a negative amount to the balance.
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 the cost of the 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.)
For full credit, you should use a conditional in buyTrip
that checks whether you can afford the trip (including the tax). If the card doesn't have enough money, print an error message and leave the balance unchanged. If there are sufficient funds for the trip, print a success message that includes the remaining balance on the card.
getAverageTripCost
method. It doesn't need any inputs, but should return the average cost of all of the trips paid for by this card. (That is, the total cost divided by the number of trips.) Ignore the tax we pay on the trips when computing the average. (Hint: You'll need some extra state to support this method, though some of the info you need was probably added to support earlier methods.)
getTax
method. It shouldn't take any inputs, but should return the total amount of tax collected during "swipes" of the card. (Hint: You can use the state you added for the previous method to help you out here as well, or find some other way to keep track of the total amount of tax collected.)
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.
/** */
) 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. Notice that when you work with double
s, the math doesn't always come out precisely. That's to be expected, and the results on your computer might differ a little from what I'm showing below, but the first few significant digits of your results should agree with the values 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).
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.
topUp()
method works as described above.
getAverageTripCost()
method works as described.
getTax()
and printSummary()
methods work properly.