Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package carnival;
- /**
- *
- * @author Monty
- */
- import java.text.NumberFormat;
- public class Player {
- private double spendingMoney;
- private String prizesWon;
- /**
- * constructor
- * pre: none
- * post: A Player object created. Spending money given to player.
- * Prize won set to to none.
- */
- public Player(double money) {
- spendingMoney = money;
- prizesWon = "";
- }
- /**
- * Player pays for and then plays a game.
- * pre: none
- * post: Player's spending money decreased by cost of game.
- * The player has a new prize added to existing prizes.
- */
- public String play(GameBooth game) {
- String newPrize;
- if (game.getCost() > spendingMoney) {
- return("Sorry, not enough money to play.");
- } else {
- spendingMoney -= game.getCost(); //pay for game
- newPrize = game.start(); //play the game and get prize
- if (prizesWon.length() > 0)
- prizesWon = newPrize + ", " + prizesWon; //add new prize to existing prizes
- else
- prizesWon = newPrize; // Add new prize -- first time only.
- game.prizesAwarded();
- return("prize won: " + newPrize); //display the new prize won
- }
- }
- /**
- * Returns the list of prizes won.
- * pre: none
- * post: The list of prizes is returned.
- */
- public String showPrizes() {
- return(prizesWon);
- }
- public String toString() {
- String moneyAndPrizes;
- NumberFormat money = NumberFormat.getCurrencyInstance();
- moneyAndPrizes = "Remaining spending money is " + money.format(spendingMoney);
- moneyAndPrizes += " and prizes won so far are: " + prizesWon;
- return (moneyAndPrizes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment