Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. //Any necessary imports
  2. import java.util.*;
  3.  
  4. public class StockTrader
  5. {
  6. public static void main(String[] args)
  7. {
  8.  
  9. // declare and initialize a Scanner
  10. Scanner in = new Scanner(System.in);
  11. // get seed value from user and use it to make the random number
  12. // generator
  13. System.out.println("Please enter a seed value [integer]:");
  14. // you may assume the user inputs an int for the seed
  15. int seed = in.nextInt();
  16. in.reset();
  17. Random rng = new Random(seed);
  18.  
  19. // declare and initialize any other needed variables
  20. int pORNG = 100;
  21. int pMCRO = 100;
  22. int pGUUG = 100;
  23. int sORNG = 0;
  24. int sMCRO = 0;
  25. int sGUUG = 0;
  26. int money = 10000;
  27. int day = 1;
  28. int tvalue = sORNG * pORNG + sMCRO * pMCRO + sGUUG * pGUUG + money;
  29.  
  30.  
  31. // begin day loop
  32. while (day < 12)
  33. {
  34. // print daily report
  35. System.out.println("****************************");
  36. System.out.println("Day " + day + " - You Currently Have:");
  37. System.out.println("Money: " + money);
  38. System.out.println("ORNG Shares: " + sORNG);
  39. System.out.println("MCRO Shares: " + sMCRO);
  40. System.out.println("GUUG Shares: " + sGUUG);
  41. System.out.println("Total Value: " + tvalue);
  42. System.out.println();
  43. // begin menu loop
  44.  
  45. boolean done = false;
  46. while(!done)
  47. {
  48. // prompt user for their decision (buy, sell, or end turn)
  49. System.out.println("Would you like to:");
  50. System.out.println("1. Buy");
  51. System.out.println("2. Sell");
  52. System.out.println("3. End your trading");
  53. String userChoice = in.next();
  54.  
  55. while (!userChoice.equals("1") && !userChoice.equals("2") && !userChoice.equals("3"))
  56. {
  57. System.out.println("Invalid input; enter 1, 2, or 3.");
  58. System.out.println("Would you like to:");
  59. System.out.println("1. Buy");
  60. System.out.println("2. Sell");
  61. System.out.println("3. End your trading");
  62. userChoice = in.next();
  63.  
  64. }
  65. // if user chose buy
  66.  
  67. if (userChoice.equals("1"))
  68. {
  69. // Start buy loop
  70. // prompt for which stock to buy
  71. System.out.println("Which stock would you like to buy?");
  72. System.out.println("1. Orange Inc.");
  73. System.out.println("2. Macrosoft");
  74. System.out.println("3. Guugle");
  75. String iBuy = in.next();
  76.  
  77. while (!iBuy.equals("1") && !iBuy.equals("2") && !iBuy.equals("3"))
  78. {
  79. System.out.println("Invalid input; enter 1, 2, or 3.");
  80. System.out.println("Which stock would you like to buy?");
  81. System.out.println("1. Orange Inc.");
  82. System.out.println("2. Macrosoft");
  83. System.out.println("3. Guugle");
  84. iBuy = in.next();
  85.  
  86. }
  87.  
  88. // prompt for how many shares to buy
  89.  
  90. System.out.println("Enter how many shares you would like to buy:");
  91.  
  92. if (in.hasNextInt())
  93. {
  94. int sBuy = in.nextInt();
  95. // update money and shares count
  96. if (iBuy.equals("1"))
  97. {
  98. int nMoney = pORNG * sBuy;
  99.  
  100. while (nMoney > money)
  101. {
  102. System.out.println("You don't have that much money.");
  103. System.out.println("Enter how many shares you would like to buy:");
  104. sBuy = in.nextInt();
  105. nMoney = pORNG * sBuy;
  106. }
  107.  
  108. money = money - nMoney;
  109. sORNG = sORNG + sBuy;
  110.  
  111. System.out.println("You now have " + money + " money.");
  112. }
  113. else if (iBuy.equals("2"))
  114. {
  115. int nMoney = pMCRO * sBuy;
  116.  
  117. if (nMoney > money)
  118. {
  119. System.out.println("You don't have that much money.");
  120. System.out.println("Enter how many shares you would like to buy:");
  121. sBuy = in.nextInt();
  122. }
  123. else
  124. {
  125. money = money - nMoney;
  126. sMCRO = sMCRO + sBuy;
  127. }
  128. System.out.println("You now have " + money + " money.");
  129. }
  130. else if (iBuy.equals("3"))
  131. {
  132. int nMoney = pGUUG * sBuy;
  133.  
  134. if (nMoney > money)
  135. {
  136. System.out.println("You don't have that much money.");
  137. System.out.println("Enter how many shares you would like to buy:");
  138. sBuy = in.nextInt();
  139. }
  140. else
  141. {
  142. money = money - nMoney;
  143. sMCRO = sMCRO + sBuy;
  144. }
  145. System.out.println("You now have " + money + " money.");
  146. }
  147. }
  148. else
  149. {
  150. System.out.println("Please enter an integer.");
  151. }
  152. }
  153.  
  154. // End buy loop
  155. // if user chose sell
  156. int iSell = 0;
  157. if (userChoice.equals("2"))
  158. {
  159. // Start sell loop
  160.  
  161. // prompt for which stock to sell
  162. System.out.println("Which stock would you like to sell?");
  163. System.out.println("1. Orange Inc.");
  164. System.out.println("2. Macrosoft");
  165. System.out.println("3. Guugle");
  166. iSell = in.nextInt();
  167.  
  168. // prompt for how many shares to sell
  169. System.out.println("Enter how many shares you would like to sell:");
  170. int sSell = in.nextInt();
  171. // update money and shares count
  172. if (iSell == 1)
  173. {
  174. int nMoney = pORNG * sSell;
  175. money = money + nMoney;
  176. sORNG = sORNG - sSell;
  177. System.out.println("You now have " + money + " money.");
  178. }
  179. if (iSell == 2)
  180. {
  181. int nMoney = pMCRO * sSell;
  182. money = money + nMoney;
  183. sMCRO = sMCRO - sSell;
  184. }
  185. if (iSell == 3)
  186. {
  187. int nMoney = pGUUG * sSell;
  188. money = money + nMoney;
  189. sGUUG = sGUUG - sSell;
  190. }
  191. else
  192. {
  193. System.err.println("Invalid input; enter 1, 2, or 3.");
  194. System.out.println("Which stock would you like to buy?");
  195. System.out.println("1. Orange Inc.");
  196. System.out.println("2. Macrosoft");
  197. System.out.println("3. Guugle");
  198. iSell = in.nextInt();
  199.  
  200. }
  201. }
  202. // end of loop sell loop
  203. // if user chose to end turn
  204. if (userChoice.equals("3"))
  205. {
  206. done = true;
  207. }
  208. }
  209.  
  210.  
  211. // update data such that menu loop will end
  212.  
  213. // end menu loop
  214.  
  215. // calculate closing prices for Orange Inc., Macrosoft, and Guugle
  216.  
  217. int change = rng.nextInt(2);
  218. int sChange = (int) (Math.random() * 10) +1;
  219.  
  220. if (change == 0)
  221. {
  222. pORNG = pORNG + sChange;
  223.  
  224. //System.out.println("Orange Inc. Increases by " + sChange);
  225. //System.out.println("New price is: " + pORNG);
  226. }
  227. else
  228. {
  229. pORNG = pORNG - sChange;
  230. //System.out.println("Orange Inc. Decreases " + sChange);
  231. //System.out.println("New price is: " + pORNG);
  232. }
  233.  
  234. int change2 = rng.nextInt(2);
  235. int sChange2 = (int) (Math.random() * 10) +1;
  236. // You need to make all the stocks change mang :|
  237. if (change2 == 0) {
  238. pMCRO = pMCRO + sChange2;
  239. //System.out.println("Macrosoft Increases by " + sChange2);
  240. //System.out.println("New price is: " + pMCRO);
  241. }
  242. else
  243. {
  244. pMCRO = pMCRO - sChange2;
  245. //System.out.println("Macrosoft Decreases by " + sChange2);
  246. //System.out.println("New price is: " + pMCRO);
  247. }
  248. int change3 = rng.nextInt(2);
  249. int sChange3 = (int) (Math.random() * 10) +1;
  250. if (change3 == 0)
  251. {
  252. pGUUG = pGUUG + sChange3;
  253. //System.out.println("Guugle Increases by " + sChange3);
  254. //System.out.println("New price is: " + pGUUG);
  255. }
  256. else
  257. {
  258. pGUUG = pGUUG - sChange3;
  259. //System.out.println("Guugle Decreases by " + sChange3);
  260. //System.out.println("New price is: " + pGUUG);
  261. }
  262.  
  263. // print end of day report
  264. System.out.println("Day " + day + " - Closing Prices:");
  265. System.out.println("Money: " + money);
  266. System.out.println("ORNG Price: " + pORNG);
  267. System.out.println("MCRO Price: " + pMCRO);
  268. System.out.println("GUUG Price: " + pGUUG);
  269. System.out.println("");
  270. // end of day loop
  271. day++;
  272. }
  273. // print end-of-game report
  274.  
  275. if (day == 12)
  276. {
  277. System.out.println("*******************");
  278. System.out.println("*******************");
  279. System.out.println("End of Game Summary");
  280. System.out.println("Money: " + money);
  281. System.out.println("ORNG Shares: " + sORNG);
  282. System.out.println("MCRO Shares: " + sMCRO);
  283. System.out.println("GUUG Shares: " + sGUUG);
  284. System.out.println("Total Value: " + tvalue);
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement