(求助)一道难题
有点长Add the following instance methods to the Person class:
• buyPass(TicketBooth booth) - This method simulates the person buying a ride pass from the given booth object. It
should modify the person and the booth in the appropriate way,
and then return a boolean indicating whether or not the
transaction was successful. The transaction is successful
ONLY if the booth has a pass available AND the person has
enough money for the pass.
• buyTickets(int number, TicketBooth booth) - This method simulates the person attempting to buy the specified number of
tickets from the given booth object. It should modify the person
and the booth in the appropriate way. If the booth does not
have the specified number of tickets available, then the
number of tickets to be sold should be reduced to the number
of remaining tickets available. Then, if the person has enough money to buy the resulting number of tickets (i.e., either the requested amount or the reduced amount) then the purchase is made. If the person does not have enough money to buy the tickets, then the number of tickets sold should be further reduced to the number of tickets that the person can afford. The method must return
an integer representing the number of tickets actually sold. Make sure that the booth maintains a proper total of all money paid to it.
• allowedToRide(Ride aRide) - This method determines whether or not the person is allowed to ride on the given ride. A person can get on a ride if he/she meets the ride's minimum height requirements AND the person either has a pass OR the required number of tickets for that ride. The method MUST return a boolean and MUST NOT have any System.out.println code within it.
Also, it MUST be written efficiently ... you will loose marks for unnecessary code ... make use of any methods you already have.
• getOn(Ride aRide) - This method simulates the person getting on the given ride. A person can get on a ride ONLY if he/she is allowed to (i.e., make sure you use the method you just wrote).
Make sure that the Person AND the Ride object are updated accordingly (no tickets are used if the person has a pass). The method MUST NOT have any System.out.println code within it and it should return true if the person was able to get on the ride and false otherwise.
Make sure to now test your code with the following test case in a class called FairTester:
class FairTester {
public static void main(String args[]) {
Ride coaster, ferris, merryGo, tosser;
Person billy, donna, fredy, harry, larry;
TicketBooth booth;
// Make some rides on which the people can ride, specify name, #tickets &height req
coaster = new Ride("Roller Coaster", 6, 4.25f);
ferris = new Ride("Ferris Wheel", 5, 3.1f);
merryGo = new Ride("Merry-Go-Round", 2, 0);
tosser = new Ride("Tummy Tosser", 7, 4.9f);
// Make a booth, specifiying the number of available passes and tickets
booth = new TicketBooth(4, 100);
// Make some people by specifying their height and their money amounts
billy = new Person(4.9f, 10.00f);
donna = new Person(3.0f, 5.00f);
fredy = new Person(6.0f, 0.00f);
harry = new Person(4.8f, 78.50f);
larry = new Person(4.0f, 50.00f);
System.out.println(booth);
System.out.println("\nBilly is a " + billy);
System.out.println("Billy just bought " + billy.buyTickets(20, booth)+ " tickets.");
System.out.println("Billy attempting to go on the " + coaster);
System.out.println("Billy got on: " + billy.getOn(coaster));
System.out.println("Billy attempting to go on the " + tosser);
System.out.println("Billy got on: " + billy.getOn(tosser));
System.out.println("Billy is now a " + billy + "\n");
System.out.println("Donna is a " + donna);
System.out.println("Donna is trying to buy a pass...was she successful: "
+ donna.buyPass(booth));
System.out.println("Donna just bought " + donna.buyTickets(6, booth)+ " tickets.");
System.out.println("Donna is attempting to go on the " + ferris);
System.out.println("Donna got on: " + donna.getOn(ferris));
System.out.println("Donna is attempting to go on the " + merryGo);
System.out.println("Donna got on: " + donna.getOn(merryGo));
System.out.println("Donna is now a " + donna + "\n");
System.out.println("Fredy is a " + fredy);
System.out.println("Fredy just bought " + fredy.buyTickets(5, booth)+ " tickets.");
System.out.println("Fredy is attempting to go on the " + merryGo);
System.out.println("Fredy got on: " + fredy.getOn(merryGo));
System.out.println("Fredy is now a " + fredy + "\n");
System.out.println("Harry is a " + harry);
System.out.println("Harry just bought " + harry.buyTickets(10, booth)+ " tickets.");
System.out.println("Harry is trying to buy a pass...was he successful: "
+ harry.buyPass(booth));
System.out.println("Harry is attempting to go on the " + coaster);
System.out.println("Harry got on: " + harry.getOn(coaster));
System.out.println("Harry is attempting to go on the " + tosser);
System.out.println("Harry got on: " + harry.getOn(tosser));
System.out.println("Harry is attempting to go on the " + coaster);
System.out.println("Harry got on: " + harry.getOn(coaster));
System.out.println("Harry is now a " + harry + "\n");
System.out.println("Larry is a " + larry);
System.out.println("Larry just bought " + larry.buyTickets(15, booth)+ " tickets.");
System.out.println("Larry is attempting to go on the " + tosser);
System.out.println("Larry got on: " + larry.getOn(tosser));
System.out.println("Larry is attempting to go on the " + coaster);
System.out.println("Larry got on: " + larry.getOn(coaster));
System.out.println("Larry is attempting to go on the " + merryGo);
System.out.println("Larry got on: " + larry.getOn(merryGo));
System.out.println("Larry is now a " + larry + "\n");
System.out.println("Ticket Booth made $" + booth.moneyMade);
System.out.println(booth);
System.out.println(coaster + " and had " + coaster.numberOfRiders + " riders.");
System.out.println(ferris + " and had " + ferris.numberOfRiders + " riders.");
System.out.println(merryGo + " and had " + merryGo.numberOfRiders + " riders.");
System.out.println(tosser + " and had " + tosser.numberOfRiders + " riders.");
}
}
Here is the output to expect (pay attention to the bold values to make sure that your code is correct):
Ticket booth with 4 passes and 100 tickets
Billy is a 4.9' person with $10.00 and 0 tickets
Billy just bought 20 tickets.
Billy attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Billy got on: true
Billy attempting to go on the Tummy Tosser requiring 7 tickets with a height restriction of 4.9'
Billy got on: true
Billy is now a 4.9' person with $0.00 and 7 tickets
Donna is a 3.0' person with $5.00 and 0 tickets
Donna is trying to buy a pass...was she successful: false
Donna just bought 6 tickets.
Donna is attempting to go on the Ferris Wheel requiring 5 tickets with a height restriction of 3.1'
Donna got on: false
Donna is attempting to go on the Merry-Go-Round requiring 2 tickets with a height restriction of 0.0'
Donna got on: true
Donna is now a 3.0' person with $2.00 and 4 tickets
Fredy is a 6.0' person with $0.00 and 0 tickets
Fredy just bought 0 tickets.
Fredy is attempting to go on the Merry-Go-Round requiring 2 tickets with a height restriction of 0.0'
Fredy got on: false
Fredy is now a 6.0' person with $0.00 and 0 tickets
Harry is a 4.8' person with $78.50 and 0 tickets
Harry just bought 10 tickets.
Harry is trying to buy a pass...was he successful: true
Harry is attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Harry got on: true
Harry is attempting to go on the Tummy Tosser requiring 7 tickets with a height restriction of 4.9'
Harry got on: false
Harry is attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Harry got on: true
Harry is now a 4.8' person with $57.00 and a pass
Larry is a 4.0' person with $50.00 and 0 tickets
Larry just bought 15 tickets.
Larry is attempting to go on the Tummy Tosser requiring 7 tickets with a height restriction of 4.9'
Larry got on: false
Larry is attempting to go on the Roller Coaster requiring 6 tickets with a height restriction of 4.25'
Larry got on: false
Larry is attempting to go on the Merry-Go-Round requiring 2 tickets with a height restriction of 0.0'
Larry got on: true
Larry is now a 4.0' person with $42.50 and 13 tickets
Ticket Booth made $42.0
Ticket booth with 3 passes and 49 tickets
Roller Coaster requiring 6 tickets with a height restriction of 4.25' and had 3 riders.
Ferris Wheel requiring 5 tickets with a height restriction of 3.1' and had 0 riders.
Merry-Go-Round requiring 2 tickets with a height restriction of 0.0' and had 2 riders.
Tummy Tosser requiring 7 tickets with a height restriction of 4.9' and had 1 riders.