Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) throws IOException {
  9.  
  10. NbaDraft nbaDraft1 = new NbaDraft(); // Create object of class w/ methods to help create draft
  11.  
  12. ArrayList<String> nbaTeams = new ArrayList<>(); // create ArrayList to hold all 30 NBA teams
  13. nbaTeams.addAll(nbaDraft1.addNbaTeams()); // call method to add teams in alphabetical order
  14.  
  15. ArrayList<String> draftOrder = new ArrayList<>(); // create ArrayList to hold teams in the order they'll pick
  16. draftOrder.addAll(nbaDraft1.createDraftOrder(nbaTeams)); // create order from team list, add to order list
  17. nbaDraft1.printDraftOrder(draftOrder); // print draft order
  18.  
  19. ArrayList<String> draftPlayers = new ArrayList<>(); // create ArrayList to hold players the teams pick
  20. draftPlayers.addAll(nbaDraft1.draftPlayers(draftOrder)); // prompt for players, add to list
  21.  
  22. ArrayList<String> draft1 = new ArrayList<>();
  23. draft1.addAll(nbaDraft1.createDraft(draftOrder, draftPlayers));
  24. // create ArrayList to hold draft info (teams in proper order associated w/ players they drafted)
  25. // nbaDraft1.printDraft(draft1); // print draft (Un-comment code in order to print draft instead of
  26. // reading from text file to be created below)
  27.  
  28. CreateDraftFile draft = new CreateDraftFile();
  29. // create object of class w/ methods to write and read file that'll hold your particular draft
  30. draft.writeToFile(draft1); // call method to write your draft to a text file
  31. System.out.println();
  32. draft.readDraftFile(); // call method read and print the info on file
  33. }
  34. }
  35.  
  36. public class NbaDraft {
  37.  
  38. private ArrayList<String> list;
  39. private ArrayList<String> order;
  40. private ArrayList<String> picks;
  41. private ArrayList<String> draft;
  42. private String rounds;
  43. private int numRounds;
  44.  
  45. public NbaDraft() {
  46. list = new ArrayList<>();
  47. order = new ArrayList<>();
  48. picks = new ArrayList<>();
  49. draft = new ArrayList<>();
  50. numRounds = 0;
  51. }
  52.  
  53. public ArrayList<String> addNbaTeams(){
  54.  
  55. String teams = ("Atlanta Hawks,Boston Celtics,Brooklyn Nets,Charlotte Hornets,Chicago Bulls," +
  56. "Cleveland Cavaliers,Dallas Mavericks,Denver Nuggets,Detroit Pistons,Golden State Warriors" +
  57. ",Houston Rockets,Indiana Pacers,Los Angeles Clippers,Los Angeles Lakers,Memphis Grizzlies" +
  58. ",Miami Heat,Milwaukee Bucks,Minnesota Timberwolves,New Orleans Pelicans,New York Knicks," +
  59. "Oklahoma City Thunder,Orlando Magic,Philadelphia 76ers,Phoenix Suns,Portland Trailblazers" +
  60. ",Sacramento Kings,San Antonio Spurs,Toronto Raptors,Utah Jazz,Washington Wizards");
  61.  
  62. String[] data = teams.split(",");
  63.  
  64. list.addAll(Arrays.asList(data));
  65.  
  66. return list;
  67. }
  68.  
  69. public ArrayList<String> createDraftOrder(ArrayList<String> teams){
  70. while (numRounds == 0) {
  71. try {
  72. rounds = (JOptionPane.showInputDialog("How many rounds will the draft have? (1-5)"));
  73. numRounds = Integer.parseInt(rounds);
  74. } catch (NumberFormatException e) {
  75. JOptionPane.showMessageDialog(null, "Wrong data type, please enter an integer");
  76. }
  77. }
  78.  
  79. Collections.shuffle(teams);
  80. order.addAll(teams);
  81.  
  82. for (int i = 0; i < (numRounds - 1); ++i) {
  83. Collections.reverse(teams);
  84. order.addAll(teams);
  85. }
  86.  
  87. return order;
  88. }
  89.  
  90. public void printDraftOrder(ArrayList<String> list){
  91.  
  92. if(numRounds == 0){
  93. System.out.println("Draft cancelled!");
  94. }
  95. if(numRounds>=1) {
  96. System.out.println("nRound 1: n");
  97. for (int x = 0; x < 30; ++x) {
  98. System.out.println((x + 1) + ". " + list.get(x));
  99. }
  100. }
  101. if(numRounds >= 2) {
  102. System.out.println("nRound 2: n");
  103. for (int x = 30; x < 60; ++x) {
  104. System.out.println((x + 1) + ". " + list.get(x));
  105. }
  106. }
  107.  
  108. if(numRounds >= 3) {
  109. System.out.println("nRound 3: n");
  110. for (int x = 60; x < 90; ++x) {
  111. System.out.println((x + 1) + ". " + list.get(x));
  112. }
  113. }
  114.  
  115. if(numRounds >= 4) {
  116. System.out.println("nRound 4: n");
  117. for (int x = 90; x < 120; ++x) {
  118. System.out.println((x + 1) + ". " + list.get(x));
  119. }
  120. }
  121.  
  122. if(numRounds == 5) {
  123. System.out.println("nRound 5: n");
  124. for (int x = 120; x < 150; ++x) {
  125. System.out.println((x + 1) + ". " + list.get(x));
  126. }
  127. }
  128. }
  129.  
  130. public ArrayList<String> draftPlayers(ArrayList<String> players){
  131.  
  132. for (int i=0; i<players.size(); ++i){
  133. String pick = JOptionPane.showInputDialog("Pick #" + (i+1) + ": nEnter your draft selection for the " + players.get(i));
  134. if(pick != null){
  135. picks.add(pick);
  136. }
  137. else {
  138. break;
  139. }
  140. }
  141. return picks;
  142. }
  143.  
  144. public ArrayList<String> createDraft(ArrayList<String> order, ArrayList<String> picks) {
  145. for (int x=0; x<picks.size(); ++x) {
  146. if((order.get(x)!= null) && (picks.get(x)!= null)) {
  147. draft.add((x + 1) + ". " + order.get(x) + ": " + picks.get(x));
  148. }
  149. }
  150. return draft;
  151. }
  152.  
  153. public void printDraft(ArrayList<String> draft){
  154.  
  155. if (numRounds == 0) {
  156. System.out.println("Draft cancelled!");
  157. }
  158.  
  159. if (numRounds >= 1) {
  160. System.out.println("nRound 1: n");
  161. for (int x = 0; x < 30; ++x) {
  162. System.out.println(draft.get(x));
  163. }
  164. }
  165.  
  166. if (numRounds >= 2) {
  167. System.out.println("nRound 2: n");
  168. for (int x = 30; x < 60; ++x) {
  169. System.out.println(draft.get(x));
  170. }
  171. }
  172.  
  173. if (numRounds >= 3) {
  174. System.out.println("nRound 3: n");
  175. for (int x = 60; x < 90; ++x) {
  176. System.out.println(draft.get(x));
  177. }
  178. }
  179.  
  180. if (numRounds >= 4) {
  181. System.out.println("nRound 4: n");
  182. for (int x = 90; x < 120; ++x) {
  183. System.out.println(draft.get(x));
  184. }
  185. }
  186.  
  187. if (numRounds == 5) {
  188. System.out.println("nRound 5: n");
  189. for (int x = 120; x < 150; ++x) {
  190. System.out.println(draft.get(x));
  191. }
  192. }
  193. }
  194. }
  195.  
  196. public class CreateDraftFile {
  197. private Writer draftFile;
  198. private InputStream inDraft;
  199. private BufferedReader draft;
  200. private File file;
  201.  
  202. public CreateDraftFile() {
  203. try {
  204.  
  205.  
  206. String fileName = JOptionPane.showInputDialog("Input file name: ");
  207. String filePath = JOptionPane.showInputDialog("Input file path (aside from file name, end with \\): ");
  208. file = new File(filePath + fileName + ".txt");
  209. draftFile = new BufferedWriter(new FileWriter(file));
  210. inDraft = new FileInputStream(file);
  211. draft = new BufferedReader(new InputStreamReader(inDraft));
  212. }
  213. catch (IOException e){
  214. e.getMessage();
  215. }
  216. }
  217.  
  218. public void writeToFile(ArrayList<String> draftInfo) throws IOException {
  219. try {
  220. String separator = System.getProperty("line.separator");
  221. for (int i=0; i<draftInfo.size(); ++i) {
  222. draftFile.write(draftInfo.get(i) + separator);
  223. }
  224. }
  225. catch (IOException e){
  226. e.getMessage();
  227. }
  228. draftFile.close();
  229. }
  230.  
  231. public void readDraftFile() throws IOException {
  232. String s;
  233. while((s = draft.readLine())!= null){
  234. System.out.println(s);
  235. }
  236. draft.close();
  237. }
  238.  
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement