Guest User

Untitled

a guest
Feb 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.48 KB | None | 0 0
  1. package model;
  2.  
  3. /**
  4. * Data model for our StockTicker game.
  5. *
  6. * This is a really simple model, with no data validation or business rules.
  7. *
  8. * @author Jim Parry
  9. */
  10. public class Game {
  11.  
  12. /** The allowed stock actions */
  13. public enum Action {
  14.  
  15. UP, DOWN, DIV
  16. };
  17. /** Is logging enabled? */
  18. public static boolean LOGGING = true;
  19. /** Has the game started? */
  20. private boolean started;
  21. /** Is the game over? */
  22. private boolean over;
  23. /** First stock on the game board */
  24. private Stock stock1 = null;
  25. /** Second stock on the game board */
  26. private Stock stock2 = null;
  27. /** Third stock on the game board */
  28. private Stock stock3 = null;
  29. /** Our (at this point) only player */
  30. private Player player = null;
  31.  
  32. //-------------------------------------------------------------
  33. // C o n s t r u c t o r s
  34. //-------------------------------------------------------------
  35. /**
  36. * Default constructor.
  37. * Initialize all fields.
  38. */
  39. public Game() {
  40. this.started = false;
  41. this.over = false;
  42. this.stock1 = null;
  43. this.stock2 = null;
  44. this.stock3 = null;
  45. this.player = null;
  46. }
  47.  
  48. /**
  49. * Convenience constructor.
  50. * Establish initial fields as specified.
  51. *
  52. * @param stock1 First stock
  53. * @param stock2 Second stock
  54. * @param stock3 Third stock
  55. * @param player Our only player
  56. */
  57. public Game(Stock stock1, Stock stock2, Stock stock3, Player player) {
  58. this();
  59. setStock1(stock1);
  60. setStock2(stock2);
  61. setStock3(stock3);
  62. setPlayer(player);
  63. }
  64.  
  65. //-------------------------------------------------------------
  66. // A c c e s s o r s
  67. //-------------------------------------------------------------
  68. /**
  69. * Has the game started yet?
  70. * @return the started
  71. */
  72. public boolean isStarted() {
  73. return started;
  74. }
  75.  
  76. /**
  77. * Is the game over yet?
  78. * @return the over
  79. */
  80. public boolean isOver() {
  81. return over;
  82. }
  83.  
  84. /**
  85. * What is the first stock?
  86. * @return the stock1
  87. */
  88. public Stock getStock1() {
  89. return stock1;
  90. }
  91.  
  92. /**
  93. * WHat is the second stock?
  94. * @return the stock2
  95. */
  96. public Stock getStock2() {
  97. return stock2;
  98. }
  99.  
  100. /**
  101. * What is the third stock?
  102. * @return the stock3
  103. */
  104. public Stock getStock3() {
  105. return stock3;
  106. }
  107.  
  108. /**
  109. * WHo is playing the game?
  110. * @return the player
  111. */
  112. public Player getPlayer() {
  113. return player;
  114. }
  115.  
  116. //-------------------------------------------------------------
  117. // M u t a t o r s
  118. //-------------------------------------------------------------
  119. /**
  120. * Specify whether the game has started.
  121. *
  122. * Business rule: cannot be set true if the game is over
  123. *
  124. * @param started true if started, false otherwise
  125. */
  126. public void setStarted(boolean started) {
  127. if (started == over) {
  128. return;
  129. }
  130. this.started = started;
  131. }
  132.  
  133. /**
  134. * Specify whether the game is over
  135. *
  136. * Business rule: can only be set to true if the game has started
  137. *
  138. * @param true if over, false otherwise
  139. */
  140. public void setOver(boolean over) {
  141. if (over != started) {
  142. return;
  143. }
  144. this.over = over;
  145. }
  146.  
  147. /**
  148. * Set the first stock on the board
  149. * @param stock1 the stock1 to set
  150. */
  151. public void setStock1(Stock stock1) {
  152. if (stock1 != null) {
  153. this.stock1 = stock1;
  154. }
  155. }
  156.  
  157. /**
  158. * Set the second stock on the board
  159. * @param stock2 the stock2 to set
  160. */
  161. public void setStock2(Stock stock2) {
  162. if (stock2 != null) {
  163. if (stock2 != stock1) {
  164. this.stock2 = stock2;
  165. }
  166. }
  167. }
  168.  
  169. /**
  170. * Set the third stock on the board
  171. * @param stock3 the stock3 to set
  172. */
  173. public void setStock3(Stock stock3) {
  174. if (stock3 != null) {
  175. if (stock3 != stock1) {
  176. if (stock3 != stock2) {
  177. this.stock3 = stock3;
  178. }
  179. }
  180. }
  181. }
  182.  
  183. /**
  184. * Set the player
  185. * @param player the player to set
  186. */
  187. public void setPlayer(Player player) {
  188. if (player != null) {
  189. if (player.getName() != "?") {
  190. this.player = player;
  191. }
  192. }
  193. }
  194.  
  195. //-------------------------------------------------------------
  196. // W o r k m e t h o d s
  197. //-------------------------------------------------------------
  198. /**
  199. * Calculate the net worth of a player.
  200. *
  201. * @param playerName The name of the player whose net worth we want.
  202. * @return His/her net worth
  203. */
  204. public int netWorth(String playerName) {
  205. // make sure it is our player
  206. if (!playerName.equals(player.getName())) {
  207. return 0;
  208. }
  209.  
  210. int worth = 0;
  211. worth = player.getCash();
  212. worth += player.getHolding1() * stock1.getValue();
  213. worth += player.getHolding2() * stock2.getValue();
  214. worth += player.getHolding3() * stock3.getValue();
  215.  
  216. return worth;
  217. }
  218.  
  219. /**
  220. * Handle a stock action.
  221. *
  222. * For now, this method acts using stock ordinals, which will indicate whether
  223. * stock 1, 2, or 3 is the one to act on. Eventually, this will be a lookup code,
  224. * so we don't have direct access to the Stock objects.
  225. *
  226. * Business rules:
  227. * 1) if a stock splits (its value reaches or exceeds 200), then the stock is
  228. * deemed to have split; double the holdings in that stock for all players
  229. * holding some, and reset the stock value to “par”
  230. * 2) if a stock gets delisted (its value drops to or below 0), then the
  231. * stock is deemed to have been delisted; zero the holdings in that stock
  232. * for all players holding some, and reset the stock value to “par”
  233. * 3) if a dividend request is received, and if the stock's value is above
  234. * par, then pay dividends for that stock to any player holding some; the
  235. * amount of the “dividend” is determined by the action amount times
  236. * the player's holdings of that stock
  237. * 4) log all actions
  238. * 5) ignore any actions if the game has not started or if the game is over
  239.  
  240. *
  241. * @param whichStock The ordinal of the stock to act on, in the range 1-3.
  242. * @param action The action to take with this stock
  243. * @param amount The amount the stock is go up or down by, or the dividend it is to pay
  244. */
  245. public void act(int whichStock, Action action, int amount) {
  246. Stock stock01 = stock1;
  247. //test if should be 2 or 3
  248. Stock stock02 = stock2;
  249. Stock stock03 = stock3;
  250.  
  251. switch (action) {
  252. case UP:
  253. //handle a stock split
  254. if (stock01.getValue() + amount >= 200) {
  255. player.setHolding1(player.getHolding1() * 2);
  256. stock01.setValue(100);
  257.  
  258. } else if (stock02.getValue() + amount >= 200) {
  259. player.setHolding2(player.getHolding2() * 2);
  260. stock02.setValue(100);
  261.  
  262. } else if (stock03.getValue() + amount >= 200) {
  263. player.setHolding3(player.getHolding3() * 2);
  264. stock03.setValue(100);
  265.  
  266. } else {
  267. switch (whichStock) {
  268. case 1:
  269. stock01.setValue(stock01.getValue() + amount);
  270. break;
  271. case 2:
  272. stock02.setValue(stock02.getValue() + amount);
  273. break;
  274. case 3:
  275. stock03.setValue(stock03.getValue() + amount);
  276. break;
  277. }
  278. }
  279. break;
  280.  
  281. case DOWN:
  282. //statement goes here
  283. //handle delisting
  284. if (stock01.getValue() - amount <= 0) {
  285. player.setHolding1(0);
  286. stock01.setValue(100);
  287.  
  288. } else if (stock02.getValue() - amount <= 0) {
  289. player.setHolding2(0);
  290. stock02.setValue(100);
  291.  
  292. } else if (stock03.getValue() - amount <= 0) {
  293. player.setHolding3(0);
  294. stock03.setValue(100);
  295.  
  296. } else {
  297. switch (whichStock) {
  298. case 1:
  299. stock01.setValue(stock01.getValue() - amount);
  300. break;
  301. case 2:
  302. stock02.setValue(stock02.getValue() - amount);
  303. break;
  304. case 3:
  305. stock03.setValue(stock03.getValue() - amount);
  306. break;
  307. }
  308. }
  309. break;
  310.  
  311. case DIV:
  312. switch (whichStock) {
  313. case 1:
  314. if (stock01.getValue() > 100) {
  315. player.setCash(player.getCash() + (player.getHolding1() * amount));
  316. break;
  317. } else {
  318. break;
  319. }
  320. case 2:
  321. if (stock02.getValue() > 100) {
  322. player.setCash(player.getCash() + (player.getHolding2() * amount));
  323. break;
  324. } else {
  325. break;
  326. }
  327. case 3:
  328. if (stock03.getValue() > 100) {
  329. player.setCash(player.getCash() + (player.getHolding3() * amount));
  330. break;
  331. } else {
  332. break;
  333. }
  334. }
  335. break;
  336. }
  337. }
  338.  
  339. /**
  340. * Start the game!
  341. * Just a convenience method.
  342. */
  343. public void start() {
  344. if (started != true) {
  345. return;
  346. }
  347. }
  348.  
  349. /**
  350. * Stop the game!
  351. * Just a convenience method.
  352. */
  353. public void stop() {
  354. if (over != true) {
  355. return;
  356. }
  357. }
  358.  
  359. /**
  360. * Produce a nice report of the game state.
  361. */
  362. public void report() {
  363. System.out.println("Stock1's value = " + stock1.getValue());
  364. System.out.println("Stock2's value = " + stock2.getValue());
  365. System.out.println("Stock3's value = " + stock3.getValue());
  366. System.out.println("Player: " + player.getName() + "Holdings: "
  367. + player.getHolding1() + "," + player.getHolding2() + ","
  368. + player.getHolding3() + "Net Worth: " + netWorth(player.getName()));
  369. System.out.println("Game status: " + (started));
  370. }
  371.  
  372. /**
  373. * Log a message to the console, if logging is enabled
  374. *
  375. * @param message The message to log
  376. */
  377. public void log(String message) {
  378. if (LOGGING) {
  379. System.out.println("Game log messages");
  380. } else {
  381. return;
  382. }
  383. }
  384.  
  385. //-------------------------------------------------------------
  386. // U t i l i t y m e t h o d s
  387. //-------------------------------------------------------------
  388. /**
  389. * Return a text representation for the game.
  390. * @return Text representation
  391. */
  392. public String toString() {
  393. String result = "StockTicker! ";
  394. result += "Underway: " + isStarted() + " ";
  395. result += "Over: " + isOver() + "\n";
  396. result += "Stocks: \n";
  397. result += getStock1() + "\n";
  398. result += getStock2() + "\n";
  399. result += getStock3() + "\n";
  400. result += getPlayer();
  401. return result;
  402. }
  403. // No equals method - this is meant to be a singleton (only one of them), which
  404. // we will tackle later.
  405. }
Add Comment
Please, Sign In to add comment