Monty374

Player

Nov 15th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package carnival;
  2.  
  3. /**
  4. *
  5. * @author Monty
  6. */
  7. import java.text.NumberFormat;
  8.  
  9. public class Player {
  10. private double spendingMoney;
  11. private String prizesWon;
  12.  
  13.  
  14. /**
  15. * constructor
  16. * pre: none
  17. * post: A Player object created. Spending money given to player.
  18. * Prize won set to to none.
  19. */
  20. public Player(double money) {
  21. spendingMoney = money;
  22. prizesWon = "";
  23. }
  24.  
  25.  
  26. /**
  27. * Player pays for and then plays a game.
  28. * pre: none
  29. * post: Player's spending money decreased by cost of game.
  30. * The player has a new prize added to existing prizes.
  31. */
  32. public String play(GameBooth game) {
  33. String newPrize;
  34.  
  35. if (game.getCost() > spendingMoney) {
  36. return("Sorry, not enough money to play.");
  37. } else {
  38. spendingMoney -= game.getCost(); //pay for game
  39. newPrize = game.start(); //play the game and get prize
  40. if (prizesWon.length() > 0)
  41. prizesWon = newPrize + ", " + prizesWon; //add new prize to existing prizes
  42. else
  43. prizesWon = newPrize; // Add new prize -- first time only.
  44. game.prizesAwarded();
  45. return("prize won: " + newPrize); //display the new prize won
  46. }
  47. }
  48. /**
  49. * Returns the list of prizes won.
  50. * pre: none
  51. * post: The list of prizes is returned.
  52. */
  53. public String showPrizes() {
  54. return(prizesWon);
  55. }
  56.  
  57.  
  58. public String toString() {
  59. String moneyAndPrizes;
  60. NumberFormat money = NumberFormat.getCurrencyInstance();
  61. moneyAndPrizes = "Remaining spending money is " + money.format(spendingMoney);
  62. moneyAndPrizes += " and prizes won so far are: " + prizesWon;
  63. return (moneyAndPrizes);
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment