Advertisement
Guest User

Untitled

a guest
May 26th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.InputMismatchException;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class Game {
  7. private final int stepsInGame;
  8. private int currentStep;
  9.  
  10. private Planet p1;
  11. private Planet p2;
  12. private Planet p3;
  13.  
  14. public Game(int stepsInGame) {
  15. this.stepsInGame = stepsInGame;
  16. }
  17.  
  18. public int getStepsInGame() {
  19. return stepsInGame;
  20. }
  21.  
  22. public int getCurrentStep() {
  23. return currentStep;
  24. }
  25.  
  26. public void pregameSetups(Trader trader) {
  27.  
  28. p1 = new Planet("Galerius");
  29. p2 = new Planet("Harmonia");
  30. p3 = new Planet("Niven");
  31.  
  32. trader.addDestination(p1);
  33. trader.addDestination(p2);
  34. trader.addDestination(p3);
  35.  
  36. p1.addCommodity(new Commodity("coffee beans", 500, 3));
  37. p1.addCommodity(new Commodity("platinum ore", 200, 1));
  38. p1.addCommodity(new Commodity("sugarcane", 120, 10));
  39.  
  40. p2.addCommodity(new Commodity("coffee beans", 360, 3));
  41. p2.addCommodity(new Commodity("platinum ore", 200, 1));
  42. p2.addCommodity(new Commodity("sugarcane", 170, 10));
  43.  
  44. p3.addCommodity(new Commodity("coffee beans", 410, 3));
  45. p3.addCommodity(new Commodity("platinum ore", 500, 1));
  46. p3.addCommodity(new Commodity("sugarcane", 100, 10));
  47.  
  48. Commodity c1 = new Commodity("coffee beans", -1, 3);
  49. Commodity c2 = new Commodity("platinum ore", -1, 1);
  50. Commodity c3 = new Commodity("sugarcane", -1, 10);
  51. List<Commodity> commodities = new ArrayList<Commodity>();
  52. commodities.add(c1);
  53. commodities.add(c2);
  54. commodities.add(c3);
  55.  
  56. trader.setCommodities(commodities);
  57.  
  58.  
  59. }
  60.  
  61. public void start(Trader trader) {
  62. currentStep++;
  63.  
  64. Scanner scanner = new Scanner(System.in);
  65. System.out.println("Which planet would you like to travel to?");
  66. System.out.println("\nYou can travel to:");
  67.  
  68. for (Planet p : trader.getDestinations()) {
  69. if (trader.getLocation() == null) {
  70. System.out.print(p + " ");
  71. }
  72. else if (!p.getName().equals(trader.getLocation().getName())) {
  73. System.out.print(p + " ");
  74. }
  75. }
  76. System.out.println();
  77.  
  78. while (true) {
  79. String input = scanner.nextLine();
  80. if (trader.getLocation() != null && input.equals(trader.getLocation().getName())) {
  81. System.out.println("You are already at " + trader.getLocation());
  82. } else if (input.equals(p1.getName())) {
  83. System.out.println("Traveling to " + p1 + "...");
  84. trader.setLocation(p1);
  85. break;
  86. } else if (input.equals(p2.getName())) {
  87. System.out.println("Traveling to " + p2 + "...");
  88. trader.setLocation(p2);
  89. break;
  90. } else if (input.equals(p3.getName())) {
  91. System.out.println("Traveling to " + p3 + "...");
  92. trader.setLocation(p3);
  93. break;
  94. } else {
  95. System.out.println("Not a valid planet!");
  96. }
  97. }
  98.  
  99. System.out.println("...\n...\n...\n...\nYou have arrived at "
  100. + trader.getLocation());
  101.  
  102. printStatistics(trader);
  103.  
  104. while (true) {
  105. System.out.print("\nWould you like to buy or sell?");
  106. System.out.println(" (buy) (sell) or (done)");
  107. String choice = scanner.nextLine();
  108.  
  109. if (choice.equals("buy")) {
  110. buy(trader, scanner);
  111. } else if (choice.equals("sell")) {
  112. sell(trader, scanner);
  113. } else if (choice.equals("done")) {
  114. break;
  115. } else {
  116. System.out.println("Not a valid entry");
  117. }
  118. }
  119. p1.randomizePrices();
  120. p2.randomizePrices();
  121. p3.randomizePrices();
  122.  
  123. scanner.close();
  124. }
  125.  
  126. public void printStatistics(Trader t) {
  127. System.out.println("\nCurrent stats:");
  128. printTrader(t);
  129. printItems(t);
  130. System.out.println("\n\nPlanet Items:");
  131. printPlanetItems(t);
  132. System.out.println();
  133. }
  134.  
  135. public void printPlanetItems(Trader trader) {
  136. for (Commodity c : trader.getLocation().getCommodities()) {
  137. System.out.printf("Item: %-20sInventory: %-10dPrice: $%.2f\n",
  138. c, c.getInventory(), c.getPrice());
  139. }
  140. }
  141.  
  142. public void printTrader(Trader t) {
  143. System.out.printf("Name: %-15sTotal money: $%-15.2fPlanet: %s\n", t, t.getTotalMoney(), t.getLocation());
  144. }
  145.  
  146.  
  147. public void printItems(Trader trader) {
  148. for (Commodity c : trader.getCommodities()) {
  149. System.out.printf("Item: %-20sInventory: %d\n", c,
  150. c.getInventory());
  151. }
  152. }
  153.  
  154. public void buy(Trader trader, Scanner scanner) {
  155. boolean bought = false;
  156. while (true) {
  157. if (!bought) {
  158. System.out.println("What would you like to buy? (Press c to cancel)");
  159. } else {
  160. System.out.println("Would you like to buy anything else? (Press c to cancel)");
  161. }
  162. String item = scanner.nextLine();
  163.  
  164. if (item.equals("c")) {
  165. return;
  166. } else
  167. loop: {
  168. for (Commodity c : trader.getLocation().getCommodities()) {
  169. if (item.equals(c.getName())) {
  170. while (true) {
  171. try {
  172. System.out
  173. .println("How many " + item + "? (Enter -1 to cancel)");
  174. int quantity = scanner.nextInt();
  175. scanner.nextLine();
  176. if (quantity == -1) {
  177. break loop;
  178. } else if (quantity * c.getPrice() <= trader
  179. .getTotalMoney()
  180. && quantity > 0
  181. && quantity <= c.getInventory()) {
  182. trader.buy(c, quantity);
  183. System.out.println("Bought " + quantity
  184. + " " + item);
  185. printStatistics(trader);
  186. bought = true;
  187. break loop;
  188. } else if (quantity * c.getPrice() > trader.getTotalMoney()) {
  189. System.out.println("Not enough money.");
  190. } else {
  191. System.out.println("Not valid.");
  192. }
  193. } catch (InputMismatchException e) {
  194. System.out
  195. .println("Please enter a number.");
  196. scanner.nextLine();
  197. }
  198. }
  199. }
  200. }
  201. System.out.println("Not a valid commodity.");
  202. }
  203. }
  204. }
  205.  
  206. public void sell(Trader trader, Scanner scanner) {
  207. boolean sold = false;
  208. while (true) {
  209. if (!sold) {
  210. System.out.println("What would you like to sell? (Press c to cancel)");
  211. } else {
  212. System.out.println("Would you like to sell anything else? (Press c to cancel)");
  213. }
  214. String item = scanner.nextLine();
  215.  
  216. if (item.equals("c")) {
  217. return;
  218. } else
  219. loop: {
  220. for (Commodity c : trader.getCommodities()) {
  221. if (item.equals(c.getName())) {
  222. double price = 0;
  223. for (Commodity com : trader.getLocation()
  224. .getCommodities()) {
  225. if (com.getName().equals(c.getName())) {
  226. price = com.getPrice();
  227. }
  228. }
  229. while (true) {
  230. try {
  231. System.out
  232. .println("How many " + item + "? (Enter -1 to cancel)");
  233. int quantity = scanner.nextInt();
  234. scanner.nextLine();
  235. if (quantity == -1) {
  236. break loop;
  237. } else if (quantity > 0
  238. && quantity <= c.getInventory()) {
  239. trader.sell(c, quantity, price);
  240. System.out.println("Sold " + quantity
  241. + " " + item);
  242. printStatistics(trader);
  243. sold = true;
  244. break loop;
  245. } else {
  246. System.out.println("Not valid.");
  247. }
  248. } catch (InputMismatchException e) {
  249. System.out
  250. .println("Please enter a number.");
  251. scanner.nextLine();
  252. }
  253. }
  254.  
  255. }
  256. }
  257. System.out.println("Not a valid commodity.");
  258. }
  259. }
  260. }
  261.  
  262. public boolean gameOver() {
  263. if (currentStep >= stepsInGame) return true;
  264. return false;
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement