Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.51 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. public class Idrott {
  4.  
  5. private static Scanner scan;
  6. private static String input;
  7. private static Event[] events;
  8. private static ArrayList<Participant> participants;
  9. private static ArrayList<Team> teams;
  10. private static int currentStartNumber;
  11. private static int eventIndex;
  12.  
  13. public static void main(String[] args) {
  14.  
  15. boolean isMessage;
  16.  
  17. events = new Event[1];
  18. participants = new ArrayList<Participant>();
  19. teams = new ArrayList<Team>();
  20. scan = new Scanner(System.in);
  21. currentStartNumber = 100;
  22. eventIndex = 0;
  23.  
  24. while (true) {
  25.  
  26. //Skriv ut menu
  27. printMenu();
  28.  
  29. // Läs från användaren
  30. input = scan.nextLine().trim().toLowerCase();
  31.  
  32. //kolla om är "message"
  33. isMessage = false;
  34. if (input.length() >= 7) {
  35. if (input.substring(0, 7).equals("message")) {
  36. isMessage = true;
  37. }
  38. }
  39.  
  40. if (input.equals("add event")) {
  41. addEvent(scan);
  42. } else if (input.equals("add participant")) {
  43. addParticipant(scan);
  44. } else if (input.equals("remove participant")) {
  45. removeParticipant(scan);
  46. } else if (input.equals("add result")) {
  47. addResult(scan);
  48. } else if (input.equals("participant")) {
  49. participantResultList(scan);
  50. } else if (input.equals("teams")) {
  51. teamResults();
  52. } else if (isMessage) {
  53. message(input.substring(8));
  54. } else if (input.equals("reinitialize")) {
  55. reinitialize();
  56. } else if (input.equals("exit")) {
  57. System.exit(1);
  58. } else {
  59. if (isEvent(input)) {
  60. eventResults(input);
  61. } else {
  62. System.out.println("Did not recognize command");
  63. }
  64.  
  65. }
  66.  
  67. }
  68. }
  69.  
  70. /**
  71. * Skriver ut meny så att användaren kan navigera registret
  72. */
  73. private static void printMenu() {
  74. System.out.println("\nVälkommen.");
  75. }
  76.  
  77. /**
  78. * Lägg till ny gren
  79. */
  80. private static void addEvent(Scanner scan) {
  81. Event newEvent;
  82. String eventName;
  83. Event existingEvent;
  84. boolean nameExsits;
  85. int maxAttempts;
  86. String bb;
  87. boolean correctInput;
  88.  
  89. // Ask and read event name
  90. System.out.print("Event name: ");
  91. eventName = scan.nextLine();
  92. eventName = toProperCase(eventName.trim());
  93.  
  94. //kolla om namnet är tomt
  95. while (eventName.equals("")) {
  96. System.out.println("Name can't be empy!");
  97. System.out.print("Event name: ");
  98. eventName = scan.nextLine();
  99. eventName = toProperCase(eventName.trim());
  100. }
  101.  
  102. //kolla namndubletter
  103. for (int i = 0; i < events.length; i++) {
  104. if (events[i] != null) {
  105. existingEvent = events[i];
  106. //jämför namn med befintliga grenar
  107. if (existingEvent.getName().equals(eventName)) {
  108. System.out.println(eventName + "has already been added");
  109. return; //avsluta metod
  110. }
  111. }
  112. }
  113.  
  114. // Ask and read max attempts
  115. System.out.print("Attempts allowed:" );
  116. maxAttempts = scan.nextInt();
  117. scan.nextLine(); // read empty line break
  118.  
  119. // print error if max attempts <= 0
  120. while (maxAttempts <= 0) {
  121. System.out.println("Number of attempts allowed cannot be zero or less");
  122.  
  123. System.out.print("Attempts allowed:" );
  124. maxAttempts = scan.nextInt();
  125. }
  126.  
  127. // Ask and read bigger better
  128. System.out.print("Bigger better? (Y/N): ");
  129. bb = scan.nextLine();
  130. bb = bb.trim().toLowerCase();
  131.  
  132. // Check if biggerBetter is acceptable input
  133. if (!(bb.equals("yes") || bb.equals("y") || bb.equals("no") || bb.equals("n"))) {
  134.  
  135. //ask again
  136. while (!(bb.equals("yes") || bb.equals("y") || bb.equals("no") || bb.equals("n"))) {
  137. System.out.println("Only yes/no answer allowed.");
  138. System.out.print("Bigger better? (Y/N): ");
  139. bb = scan.nextLine();
  140. bb = bb.trim().toLowerCase();
  141. }
  142. }
  143.  
  144. // create new event
  145. newEvent = new Event(eventName, maxAttempts, bb);
  146.  
  147. //Store event in list
  148. events[eventIndex] = newEvent;
  149. eventIndex++;
  150.  
  151. //kolla om vi behöver förlänga array med events
  152. if (eventIndex + 1 > events.length) {
  153. events = doubleArrayLength(events);
  154. }
  155.  
  156. System.out.println(newEvent + " added.\n");
  157. }
  158.  
  159. /**
  160. * Lägg till deltagare
  161. */
  162. private static void addParticipant(Scanner scan) {
  163. Participant p;
  164. String fName;
  165. String lName;
  166. String teamName;
  167. boolean foundTeam = false;
  168.  
  169. //Läs in förnamn
  170. System.out.print("First name: ");
  171. fName = scan.nextLine();
  172. fName = toProperCase(fName.trim());
  173.  
  174. //Läs in efternamn
  175. System.out.print("Last name: ");
  176. lName = scan.nextLine();
  177. lName = toProperCase(lName.trim());
  178.  
  179. //Läs in team namn
  180. System.out.print("Team: ");
  181. teamName = scan.nextLine();
  182. teamName = toProperCase(teamName.trim());
  183.  
  184. //Skapa participant objekt och lägg till i lista
  185. p = new Participant(fName, lName, teamName, currentStartNumber);
  186.  
  187. participants.add(p);
  188.  
  189. //Kolla ifall laget redan finns
  190. for(Team t : teams) {
  191. if (t.toString().equals(teamName)) {
  192. foundTeam = true;
  193. }
  194. }
  195.  
  196. //Lägg till lag om det inte fanns
  197. if (!foundTeam) {
  198. teams.add(new Team(teamName));
  199. }
  200.  
  201. System.out.println(fName + " " + lName + " from " + teamName + " with number " + currentStartNumber + " added.");
  202.  
  203. currentStartNumber++;
  204. }
  205.  
  206. /**
  207. * Ta bort deltagare
  208. */
  209. private static void removeParticipant(Scanner scan) {
  210. int pNum;
  211. String teamName;
  212. int teamCount = 0;
  213. boolean notFound = true;
  214.  
  215. System.out.print("Participant number: ");
  216. pNum = Integer.parseInt(scan.nextLine());
  217.  
  218. //Kolla om startnummer finns
  219. for(Participant par : participants) {
  220. if (par.getStartNumber() == pNum) {
  221.  
  222. teamName = par.getTeam();
  223.  
  224. //ta bort deltagare med startnummer pNum
  225. participants.remove(par);
  226. notFound = false;
  227.  
  228. //Kolla om det finns någon kvar i laget teamName
  229. for(Participant par2 : participants) {
  230. if (par2.getTeam() == teamName) {
  231. teamCount++;
  232. }
  233. }
  234.  
  235. //ta bort laget om inga deltagare hittades
  236. if (teamCount == 0) {
  237. for(Team t : teams) {
  238. if (t.toString() == teamName) {
  239. teams.remove(t);
  240. }
  241. }
  242. }
  243.  
  244. }
  245. }
  246.  
  247. if (notFound) {
  248. System.out.println("No participant with number " + pNum + " found");
  249. }
  250.  
  251. }
  252.  
  253. /**
  254. * Lägg till resultat
  255. */
  256. private static void addResult(Scanner scan) {
  257. int pNum;
  258. String eventName;
  259. double result;
  260. Participant p = null;
  261. Event e = null;
  262. int maxAttempts;
  263. int attempts;
  264. boolean notFound;
  265.  
  266. System.out.print("Number: ");
  267. pNum = Integer.parseInt(scan.nextLine());
  268.  
  269. //Kolla om startnummer finns
  270. notFound = true;
  271. for(Participant par : participants) {
  272. if (par.getStartNumber() == pNum) {
  273. p = par;
  274. notFound = false;
  275. }
  276. }
  277. if (notFound) {
  278. System.out.println("No participant with number " + pNum + " found");
  279. return;
  280. }
  281.  
  282. System.out.print("Event: ");
  283. eventName = scan.nextLine();
  284.  
  285. //Kolla om gren finns
  286. notFound = true;
  287. for (int i = 0; i < eventIndex; i++) {
  288. if (events[i].getName().equals(toProperCase(eventName))) {
  289. e = events[i];
  290. notFound = false;
  291. }
  292. }
  293. if (notFound) {
  294. System.out.println("No event \"" + eventName + "\" found");
  295. return;
  296. }
  297.  
  298. //Kontrollera antalet försök
  299. maxAttempts = e.getMaxAttempts();
  300. attempts = p.getAttempts(e.getName());
  301.  
  302. if (attempts == maxAttempts) {
  303. System.out.println("Too many attempts!");
  304. return;
  305. }
  306.  
  307. //Läs in resultat och kolla att det är >= 0
  308. result = -1;
  309. while (result < 0 ) {
  310. System.out.print("Results for " + p.getName() + " from " + p.getTeam() + " in " + eventName + ": ");
  311. result = Double.parseDouble(scan.nextLine());
  312. if (result < 0) {
  313. System.out.println("Must be greater than or equal to zero!");
  314. }
  315. }
  316.  
  317. //Lägg till resultat
  318. for (int i = 0; i < eventIndex; i++) {
  319. if (events[i].getName().equals(toProperCase(eventName))) {
  320. events[i].addResult(result, p.getName(), p.getTeam());
  321. }
  322. }
  323. for(Participant par : participants) {
  324. if (par.getStartNumber() == pNum) {
  325. par.addResult(result, e.getName());
  326. }
  327. }
  328.  
  329. System.out.println("Added result " + result + " for " + p.getName() + " in event " + e.getName());
  330. }
  331.  
  332.  
  333. private static void participantResultList(Scanner scan) {
  334. int pNum;
  335. boolean notFound;
  336. int pIndex = 0;
  337.  
  338. System.out.print("Participant number: ");
  339. pNum = Integer.parseInt(scan.nextLine());
  340.  
  341. //Kolla om startnummer finns
  342. notFound = true;
  343. for(Participant par : participants) {
  344. if (par.getStartNumber() == pNum) {
  345. pIndex = participants.indexOf(par);
  346. notFound = false;
  347. }
  348. }
  349. if (notFound) {
  350. System.out.println("No participant with number " + pNum + " found");
  351. return;
  352. }
  353.  
  354. participants.get(pIndex).printResults();
  355.  
  356. }
  357.  
  358. //TODO
  359. private static void teamResults() {
  360.  
  361. int[][] temp = new int[0][0];
  362. int[][] total = new int[0][0];
  363.  
  364. boolean newTeam;
  365. int lastRow;
  366.  
  367. //Loopa igenom grenar
  368. for (int i = 0; i < events.length; i++) {
  369. if (events[i] != null) {
  370.  
  371. temp = events[i].getTeamResults();
  372.  
  373. //Loopa igenom resultat för gren events[i]
  374. for (int j = 0; j < temp.length; j++) {
  375.  
  376. newTeam = true;
  377.  
  378. //Loopa igenom total
  379. for (int k = 0; k < total.length; k++) {
  380.  
  381.  
  382. //Kolla om samma lag
  383. if (total[k][3] == temp[j][3]) {
  384.  
  385. total[k][0] += temp[j][0];
  386. total[k][1] += temp[j][1];
  387. total[k][2] += temp[j][2];
  388. newTeam = false;
  389. break;
  390. }
  391.  
  392. }
  393.  
  394. //om laget inte fanns i total-matrisen; lägg till ny rad med lagets resultat
  395. if (newTeam) {
  396. total = incrementMatrixRowLength(total);
  397. lastRow = total.length-1;
  398. total[lastRow][0] = temp[j][0];
  399. total[lastRow][1] = temp[j][1];
  400. total[lastRow][2] = temp[j][2];
  401. total[lastRow][3] = temp[j][3];
  402. }
  403.  
  404. }
  405.  
  406. }
  407. }
  408.  
  409.  
  410. //Sortera
  411. int n = total.length;
  412. for (int i = 1; i < n; i++) {
  413. int j = i;
  414. while (j > 0 && ( (total[j][0] > total[j-1][0]) || (total[j][0] == total[j-1][0] && total[j][1] > total[j-1][1]) || ( total[j][0] == total[j-1][0] && total[j][1] == total[j-1][1] && total[j][3] > total[j-1][3] ) ) ) {
  415.  
  416. int[] t = total[j];
  417. total[j] = total[j-1];
  418. total[j-1] = t;
  419. j--;
  420. }
  421. }
  422.  
  423. //Skriv ut skiten
  424. System.out.println("1st \t\t 2nd \t\t 3rd \t\t Team name\n");
  425. System.out.print("****************************************************************************");
  426. for (int i = 0; i < total.length; i++) {
  427. System.out.print("\n" + total[i][0] + " \t\t " + total[i][1] + " \t\t " + total[i][2] + " \t\t ");
  428.  
  429. //Hitta team namn
  430. for (Participant p : participants) {
  431. if (p.getTeam().hashCode() == total[i][3]) {
  432. System.out.print(p.getTeam());
  433. break;
  434. }
  435. }
  436.  
  437. }
  438.  
  439.  
  440. }
  441.  
  442. private static void message(String input) {
  443. final int MSG_LENGTH = 56;
  444.  
  445. //Skriv ut 60 hashtags
  446. for (int i = 0; i < MSG_LENGTH + 4; i++) {
  447. System.out.print("#");
  448. }
  449.  
  450. //ny rad
  451. System.out.println();
  452.  
  453. // # #
  454. for (int i = 0; i < MSG_LENGTH + 4; i++) {
  455.  
  456. if (i == 0 || i == MSG_LENGTH + 3) {
  457. System.out.print("#");
  458. } else {
  459. System.out.print(" ");
  460. }
  461. }
  462.  
  463. // ny rad
  464. System.out.println();
  465.  
  466. // skriv ut meddelandet
  467. System.out.print("# ");
  468.  
  469. //fixa meddelandet
  470. input = input.toUpperCase();
  471.  
  472. if (input.length() > MSG_LENGTH) {
  473. input = input.substring(0, MSG_LENGTH);
  474. }
  475.  
  476.  
  477. System.out.print(input);
  478.  
  479. for (int i = input.length()+2; i < MSG_LENGTH + 4; i++) {
  480. if (i == MSG_LENGTH + 3) {
  481. System.out.print("#\n");
  482. } else {
  483. System.out.print(" ");
  484. }
  485.  
  486. }
  487.  
  488.  
  489. // # #
  490. for (int i = 0; i < MSG_LENGTH + 4; i++) {
  491.  
  492. if (i == 0 || i == MSG_LENGTH + 3) {
  493. System.out.print("#");
  494. } else {
  495. System.out.print(" ");
  496. }
  497. }
  498.  
  499. //ny rad
  500. System.out.println();
  501.  
  502. //Skriv ut 60 hashtags
  503. for (int i = 0; i < MSG_LENGTH + 4; i++) {
  504. System.out.print("#");
  505. }
  506.  
  507. }
  508.  
  509. private static void reinitialize() {
  510. events = new Event[1];
  511. participants = new ArrayList<Participant>();
  512. teams = new ArrayList<Team>();
  513. currentStartNumber = 100;
  514. eventIndex = 0;
  515.  
  516. message("ALL DATA HAS BEEN REMOVED");
  517. }
  518.  
  519.  
  520. /**
  521. * kolla om det är en gren
  522. */
  523. private static boolean isEvent(String s) {
  524.  
  525. for (int i = 0; i < events.length; i++) {
  526. if (events[i] != null) {
  527. if (events[i].getName().equals(toProperCase(s))) {
  528. return true;
  529. }
  530. }
  531. }
  532.  
  533. return false;
  534. }
  535.  
  536. /**
  537. * skriv ut grenresultatlista
  538. */
  539. private static void eventResults(String s) {
  540.  
  541. Event e = null;
  542.  
  543. for (int i = 0; i < events.length; i++) {
  544. if (events[i] != null) {
  545. if (events[i].getName().equals(toProperCase(s))) {
  546. e = events[i];
  547. }
  548. }
  549. }
  550.  
  551. e.printResults();
  552. }
  553.  
  554.  
  555. /**
  556. * Lägg till 1 rad och bevara data (för int matris)
  557. */
  558. private static int[][] incrementMatrixRowLength(int[][] a) {
  559.  
  560. int[][] newArray;
  561.  
  562. //skapa ny array med dubbel längd
  563. newArray = new int[a.length+1][4];
  564.  
  565. //för över all data till ny array
  566. for(int i = 0; i < a.length; i++) {
  567. for(int j = 0; j < a[0].length; j++) {
  568. newArray[i][j] = a[i][j];
  569. }
  570. }
  571.  
  572. return newArray;
  573.  
  574. }
  575.  
  576. /**
  577. * Dubbla arraylängd och bevara data (för Participant array)
  578. */
  579. private static Event[] doubleArrayLength(Event[] a) {
  580.  
  581. Event[] newArray;
  582.  
  583. //skapa ny array med dubbel längd
  584. newArray = new Event[a.length*2];
  585.  
  586. //för över all data till ny array
  587. for(int i = 0; i < a.length; i++) {
  588. if (a[i] != null)
  589. newArray[i] = a[i];
  590. }
  591.  
  592. return newArray;
  593.  
  594. }
  595.  
  596.  
  597. /**
  598. * Gör första bokstav till versal (funkar endast för första ordet i strängen
  599. */
  600. private static String toProperCase(String s) {
  601. String firstLetter;
  602.  
  603. s = s.trim();
  604.  
  605. firstLetter = s.substring(0, 1).toUpperCase();
  606.  
  607. return firstLetter + s.substring(1).toLowerCase();
  608.  
  609. }
  610.  
  611. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement