Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- public class Idrott {
- private static Scanner scan;
- private static String input;
- private static Event[] events;
- private static ArrayList<Participant> participants;
- private static ArrayList<Team> teams;
- private static int currentStartNumber;
- private static int eventIndex;
- public static void main(String[] args) {
- boolean isMessage;
- events = new Event[1];
- participants = new ArrayList<Participant>();
- teams = new ArrayList<Team>();
- scan = new Scanner(System.in);
- currentStartNumber = 100;
- eventIndex = 0;
- while (true) {
- //Skriv ut menu
- printMenu();
- // Läs från användaren
- input = scan.nextLine().trim().toLowerCase();
- //kolla om är "message"
- isMessage = false;
- if (input.length() >= 7) {
- if (input.substring(0, 7).equals("message")) {
- isMessage = true;
- }
- }
- if (input.equals("add event")) {
- addEvent(scan);
- } else if (input.equals("add participant")) {
- addParticipant(scan);
- } else if (input.equals("remove participant")) {
- removeParticipant(scan);
- } else if (input.equals("add result")) {
- addResult(scan);
- } else if (input.equals("participant")) {
- participantResultList(scan);
- } else if (input.equals("teams")) {
- teamResults();
- } else if (isMessage) {
- message(input.substring(8));
- } else if (input.equals("reinitialize")) {
- reinitialize();
- } else if (input.equals("exit")) {
- System.exit(1);
- } else {
- if (isEvent(input)) {
- eventResults(input);
- } else {
- System.out.println("Did not recognize command");
- }
- }
- }
- }
- /**
- * Skriver ut meny så att användaren kan navigera registret
- */
- private static void printMenu() {
- System.out.println("\nVälkommen.");
- }
- /**
- * Lägg till ny gren
- */
- private static void addEvent(Scanner scan) {
- Event newEvent;
- String eventName;
- Event existingEvent;
- boolean nameExsits;
- int maxAttempts;
- String bb;
- boolean correctInput;
- // Ask and read event name
- System.out.print("Event name: ");
- eventName = scan.nextLine();
- eventName = toProperCase(eventName.trim());
- //kolla om namnet är tomt
- while (eventName.equals("")) {
- System.out.println("Name can't be empy!");
- System.out.print("Event name: ");
- eventName = scan.nextLine();
- eventName = toProperCase(eventName.trim());
- }
- //kolla namndubletter
- for (int i = 0; i < events.length; i++) {
- if (events[i] != null) {
- existingEvent = events[i];
- //jämför namn med befintliga grenar
- if (existingEvent.getName().equals(eventName)) {
- System.out.println(eventName + "has already been added");
- return; //avsluta metod
- }
- }
- }
- // Ask and read max attempts
- System.out.print("Attempts allowed:" );
- maxAttempts = scan.nextInt();
- scan.nextLine(); // read empty line break
- // print error if max attempts <= 0
- while (maxAttempts <= 0) {
- System.out.println("Number of attempts allowed cannot be zero or less");
- System.out.print("Attempts allowed:" );
- maxAttempts = scan.nextInt();
- }
- // Ask and read bigger better
- System.out.print("Bigger better? (Y/N): ");
- bb = scan.nextLine();
- bb = bb.trim().toLowerCase();
- // Check if biggerBetter is acceptable input
- if (!(bb.equals("yes") || bb.equals("y") || bb.equals("no") || bb.equals("n"))) {
- //ask again
- while (!(bb.equals("yes") || bb.equals("y") || bb.equals("no") || bb.equals("n"))) {
- System.out.println("Only yes/no answer allowed.");
- System.out.print("Bigger better? (Y/N): ");
- bb = scan.nextLine();
- bb = bb.trim().toLowerCase();
- }
- }
- // create new event
- newEvent = new Event(eventName, maxAttempts, bb);
- //Store event in list
- events[eventIndex] = newEvent;
- eventIndex++;
- //kolla om vi behöver förlänga array med events
- if (eventIndex + 1 > events.length) {
- events = doubleArrayLength(events);
- }
- System.out.println(newEvent + " added.\n");
- }
- /**
- * Lägg till deltagare
- */
- private static void addParticipant(Scanner scan) {
- Participant p;
- String fName;
- String lName;
- String teamName;
- boolean foundTeam = false;
- //Läs in förnamn
- System.out.print("First name: ");
- fName = scan.nextLine();
- fName = toProperCase(fName.trim());
- //Läs in efternamn
- System.out.print("Last name: ");
- lName = scan.nextLine();
- lName = toProperCase(lName.trim());
- //Läs in team namn
- System.out.print("Team: ");
- teamName = scan.nextLine();
- teamName = toProperCase(teamName.trim());
- //Skapa participant objekt och lägg till i lista
- p = new Participant(fName, lName, teamName, currentStartNumber);
- participants.add(p);
- //Kolla ifall laget redan finns
- for(Team t : teams) {
- if (t.toString().equals(teamName)) {
- foundTeam = true;
- }
- }
- //Lägg till lag om det inte fanns
- if (!foundTeam) {
- teams.add(new Team(teamName));
- }
- System.out.println(fName + " " + lName + " from " + teamName + " with number " + currentStartNumber + " added.");
- currentStartNumber++;
- }
- /**
- * Ta bort deltagare
- */
- private static void removeParticipant(Scanner scan) {
- int pNum;
- String teamName;
- int teamCount = 0;
- boolean notFound = true;
- System.out.print("Participant number: ");
- pNum = Integer.parseInt(scan.nextLine());
- //Kolla om startnummer finns
- for(Participant par : participants) {
- if (par.getStartNumber() == pNum) {
- teamName = par.getTeam();
- //ta bort deltagare med startnummer pNum
- participants.remove(par);
- notFound = false;
- //Kolla om det finns någon kvar i laget teamName
- for(Participant par2 : participants) {
- if (par2.getTeam() == teamName) {
- teamCount++;
- }
- }
- //ta bort laget om inga deltagare hittades
- if (teamCount == 0) {
- for(Team t : teams) {
- if (t.toString() == teamName) {
- teams.remove(t);
- }
- }
- }
- }
- }
- if (notFound) {
- System.out.println("No participant with number " + pNum + " found");
- }
- }
- /**
- * Lägg till resultat
- */
- private static void addResult(Scanner scan) {
- int pNum;
- String eventName;
- double result;
- Participant p = null;
- Event e = null;
- int maxAttempts;
- int attempts;
- boolean notFound;
- System.out.print("Number: ");
- pNum = Integer.parseInt(scan.nextLine());
- //Kolla om startnummer finns
- notFound = true;
- for(Participant par : participants) {
- if (par.getStartNumber() == pNum) {
- p = par;
- notFound = false;
- }
- }
- if (notFound) {
- System.out.println("No participant with number " + pNum + " found");
- return;
- }
- System.out.print("Event: ");
- eventName = scan.nextLine();
- //Kolla om gren finns
- notFound = true;
- for (int i = 0; i < eventIndex; i++) {
- if (events[i].getName().equals(toProperCase(eventName))) {
- e = events[i];
- notFound = false;
- }
- }
- if (notFound) {
- System.out.println("No event \"" + eventName + "\" found");
- return;
- }
- //Kontrollera antalet försök
- maxAttempts = e.getMaxAttempts();
- attempts = p.getAttempts(e.getName());
- if (attempts == maxAttempts) {
- System.out.println("Too many attempts!");
- return;
- }
- //Läs in resultat och kolla att det är >= 0
- result = -1;
- while (result < 0 ) {
- System.out.print("Results for " + p.getName() + " from " + p.getTeam() + " in " + eventName + ": ");
- result = Double.parseDouble(scan.nextLine());
- if (result < 0) {
- System.out.println("Must be greater than or equal to zero!");
- }
- }
- //Lägg till resultat
- for (int i = 0; i < eventIndex; i++) {
- if (events[i].getName().equals(toProperCase(eventName))) {
- events[i].addResult(result, p.getName(), p.getTeam());
- }
- }
- for(Participant par : participants) {
- if (par.getStartNumber() == pNum) {
- par.addResult(result, e.getName());
- }
- }
- System.out.println("Added result " + result + " for " + p.getName() + " in event " + e.getName());
- }
- private static void participantResultList(Scanner scan) {
- int pNum;
- boolean notFound;
- int pIndex = 0;
- System.out.print("Participant number: ");
- pNum = Integer.parseInt(scan.nextLine());
- //Kolla om startnummer finns
- notFound = true;
- for(Participant par : participants) {
- if (par.getStartNumber() == pNum) {
- pIndex = participants.indexOf(par);
- notFound = false;
- }
- }
- if (notFound) {
- System.out.println("No participant with number " + pNum + " found");
- return;
- }
- participants.get(pIndex).printResults();
- }
- //TODO
- private static void teamResults() {
- int[][] temp = new int[0][0];
- int[][] total = new int[0][0];
- boolean newTeam;
- int lastRow;
- //Loopa igenom grenar
- for (int i = 0; i < events.length; i++) {
- if (events[i] != null) {
- temp = events[i].getTeamResults();
- //Loopa igenom resultat för gren events[i]
- for (int j = 0; j < temp.length; j++) {
- newTeam = true;
- //Loopa igenom total
- for (int k = 0; k < total.length; k++) {
- //Kolla om samma lag
- if (total[k][3] == temp[j][3]) {
- total[k][0] += temp[j][0];
- total[k][1] += temp[j][1];
- total[k][2] += temp[j][2];
- newTeam = false;
- break;
- }
- }
- //om laget inte fanns i total-matrisen; lägg till ny rad med lagets resultat
- if (newTeam) {
- total = incrementMatrixRowLength(total);
- lastRow = total.length-1;
- total[lastRow][0] = temp[j][0];
- total[lastRow][1] = temp[j][1];
- total[lastRow][2] = temp[j][2];
- total[lastRow][3] = temp[j][3];
- }
- }
- }
- }
- //Sortera
- int n = total.length;
- for (int i = 1; i < n; i++) {
- int j = i;
- 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] ) ) ) {
- int[] t = total[j];
- total[j] = total[j-1];
- total[j-1] = t;
- j--;
- }
- }
- //Skriv ut skiten
- System.out.println("1st \t\t 2nd \t\t 3rd \t\t Team name\n");
- System.out.print("****************************************************************************");
- for (int i = 0; i < total.length; i++) {
- System.out.print("\n" + total[i][0] + " \t\t " + total[i][1] + " \t\t " + total[i][2] + " \t\t ");
- //Hitta team namn
- for (Participant p : participants) {
- if (p.getTeam().hashCode() == total[i][3]) {
- System.out.print(p.getTeam());
- break;
- }
- }
- }
- }
- private static void message(String input) {
- final int MSG_LENGTH = 56;
- //Skriv ut 60 hashtags
- for (int i = 0; i < MSG_LENGTH + 4; i++) {
- System.out.print("#");
- }
- //ny rad
- System.out.println();
- // # #
- for (int i = 0; i < MSG_LENGTH + 4; i++) {
- if (i == 0 || i == MSG_LENGTH + 3) {
- System.out.print("#");
- } else {
- System.out.print(" ");
- }
- }
- // ny rad
- System.out.println();
- // skriv ut meddelandet
- System.out.print("# ");
- //fixa meddelandet
- input = input.toUpperCase();
- if (input.length() > MSG_LENGTH) {
- input = input.substring(0, MSG_LENGTH);
- }
- System.out.print(input);
- for (int i = input.length()+2; i < MSG_LENGTH + 4; i++) {
- if (i == MSG_LENGTH + 3) {
- System.out.print("#\n");
- } else {
- System.out.print(" ");
- }
- }
- // # #
- for (int i = 0; i < MSG_LENGTH + 4; i++) {
- if (i == 0 || i == MSG_LENGTH + 3) {
- System.out.print("#");
- } else {
- System.out.print(" ");
- }
- }
- //ny rad
- System.out.println();
- //Skriv ut 60 hashtags
- for (int i = 0; i < MSG_LENGTH + 4; i++) {
- System.out.print("#");
- }
- }
- private static void reinitialize() {
- events = new Event[1];
- participants = new ArrayList<Participant>();
- teams = new ArrayList<Team>();
- currentStartNumber = 100;
- eventIndex = 0;
- message("ALL DATA HAS BEEN REMOVED");
- }
- /**
- * kolla om det är en gren
- */
- private static boolean isEvent(String s) {
- for (int i = 0; i < events.length; i++) {
- if (events[i] != null) {
- if (events[i].getName().equals(toProperCase(s))) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * skriv ut grenresultatlista
- */
- private static void eventResults(String s) {
- Event e = null;
- for (int i = 0; i < events.length; i++) {
- if (events[i] != null) {
- if (events[i].getName().equals(toProperCase(s))) {
- e = events[i];
- }
- }
- }
- e.printResults();
- }
- /**
- * Lägg till 1 rad och bevara data (för int matris)
- */
- private static int[][] incrementMatrixRowLength(int[][] a) {
- int[][] newArray;
- //skapa ny array med dubbel längd
- newArray = new int[a.length+1][4];
- //för över all data till ny array
- for(int i = 0; i < a.length; i++) {
- for(int j = 0; j < a[0].length; j++) {
- newArray[i][j] = a[i][j];
- }
- }
- return newArray;
- }
- /**
- * Dubbla arraylängd och bevara data (för Participant array)
- */
- private static Event[] doubleArrayLength(Event[] a) {
- Event[] newArray;
- //skapa ny array med dubbel längd
- newArray = new Event[a.length*2];
- //för över all data till ny array
- for(int i = 0; i < a.length; i++) {
- if (a[i] != null)
- newArray[i] = a[i];
- }
- return newArray;
- }
- /**
- * Gör första bokstav till versal (funkar endast för första ordet i strängen
- */
- private static String toProperCase(String s) {
- String firstLetter;
- s = s.trim();
- firstLetter = s.substring(0, 1).toUpperCase();
- return firstLetter + s.substring(1).toLowerCase();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement