Advertisement
Vejdis

Register

Jan 12th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.59 KB | None | 0 0
  1. REGISTER MAIN KLASS
  2.  
  3. // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
  4. // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
  5.  
  6. import java.util.*;
  7.  
  8. public class Register {
  9.     private Scanner keyboard = new Scanner(System.in);
  10.  
  11.  
  12.     private List<Event> eventList = new ArrayList<>();
  13.     private List<Participant> participantList = new ArrayList<>();
  14.  
  15.  
  16.  
  17.     private int participantNumber = 99;
  18.  
  19.  
  20.     private String readCommand() {
  21.  
  22.         System.out.println("Vad vill du göra?\n 1: Lägga till gren\n 2: Registrera ny deltagare\n 3: Ta bort deltagare\n 4: Registrera resultat\n 5: Se resultatlista för deltagare\n 6: se resultatlista för specifik gren\n 7: Skicka meddelande\n 8: Avsluta");
  23.         return inputStrings("Skriv ditt val här: ");
  24.     }
  25.  
  26.     private boolean useCommand(String command) {
  27.  
  28.         switch (command) {
  29.             case "1":
  30.             case "Add event":
  31.                 addEvent();
  32.                 break;
  33.             case "2":
  34.             case "Add participant":
  35.                 addParticipant();
  36.                 break;
  37.             case "3":
  38.             case "Remove participant":
  39.                 removeParticipant();
  40.                 break;
  41.             case "4":
  42.             case "Add result":
  43.                 addResult();
  44.                 break;
  45.             case "5":
  46.             case "Participant":
  47.                 viewParticipant();
  48.                 break;
  49.             case "7":
  50.                 System.out.println(eventList);
  51.                 System.out.println(participantList);
  52.             case "Exit":
  53.                 System.out.println("Program closing, goodbye!");
  54.                 return false;
  55.  
  56.             default:
  57.                 if (command.startsWith("Message")) {
  58.                     message(command);
  59.                 } else if (eventExistsStartMenu(command)) {
  60.                     viewEvent(command);
  61.                 }else{
  62.                     System.out.println("Error, wrong command");
  63.                 }
  64.         }
  65.         return true;
  66.     }
  67.  
  68.     private void addEvent() {
  69.  
  70.         String eventNameTemp = inputStrings("Event name: ");
  71.         while(eventNameTemp.trim().isEmpty() || eventNameTemp.isEmpty()){
  72.             System.out.println("Error: name can't be empty");
  73.             eventNameTemp = inputStrings("Event name: ");
  74.         }
  75.         boolean eventAlreadyExists = false;
  76.  
  77.         for (Event e : eventList){
  78.             if (eventNameTemp.equals(e.getEventName())){
  79.                 eventAlreadyExists = true;
  80.                 break;
  81.             }
  82.         }
  83.         if(eventAlreadyExists){
  84.             System.out.println(eventNameTemp + " has already been added");
  85.         }else{
  86.             int attemptsAllowedTemp = inputInts("Attempts allowed: ");
  87.             while (attemptsAllowedTemp <= 0){
  88.                 attemptsAllowedTemp = inputInts("Attempts allowed: ");
  89.             }
  90.             System.out.println(eventNameTemp + " with " + attemptsAllowedTemp + " attempts allowed created");
  91.             eventList.add(new Event(eventNameTemp,attemptsAllowedTemp));
  92.         }
  93.  
  94.     }
  95.  
  96.     private void addParticipant() {
  97.  
  98.         String firstName = inputStrings("First name: ");
  99.         while (firstName.trim().isEmpty()) {
  100.             System.out.println("Error: name can't be empty!");
  101.             firstName = inputStrings("First name: ");
  102.         }
  103.  
  104.  
  105.         String lastName = inputStrings("Last name: ");
  106.         while (lastName.trim().isEmpty()) {
  107.             System.out.println("Error: name can't be empty!");
  108.             lastName = inputStrings("Last name: ");
  109.         }
  110.  
  111.  
  112.         String team = inputStrings("Team: ");
  113.         while (team.trim().isEmpty()) {
  114.             System.out.println("Error: name can't be emtpy!");
  115.             team = inputStrings("Team: ");
  116.         }
  117.  
  118.         participantNumber++;
  119.  
  120.         participantList.add(new Participant(firstName, lastName, team, participantNumber));
  121.  
  122.         System.out.println(participantList.get(participantList.size() - 1) + " added");
  123.  
  124.     }
  125.  
  126.     private void removeParticipant() {
  127.  
  128.         int participantToDelete = inputInts("Number: ");
  129.         if(removeParticipantFromList(participantToDelete)){
  130.             findResultAndRemoveResult(findParticipant(participantToDelete), participantToDelete);
  131.         }else{
  132.             System.out.println("Error: no participant with number " + participantToDelete + " exists");
  133.  
  134.         }
  135.     }
  136.  
  137.     private void addResult(){
  138.  
  139.         int participantNumber = inputInts("Number: ");
  140.         if(!participantExists(participantNumber)){
  141.             System.out.println("Error: no participant with number " + participantNumber + " found!");
  142.             return;
  143.         }
  144.         String eventName = inputStrings("Event: ");
  145.         if(!eventExists(eventName)){
  146.             System.out.println("Error: no event called \"" + eventName + "\" found!");
  147.             return;
  148.         }
  149.         Participant p = addResultParticipant(participantNumber);
  150.         Event e = findEvent(eventName);
  151.         if(eventExists(eventName)&& findAttemptsAllowed(eventName) > countTries(getResultsInEvent(getParticipantResults(participantNumber, eventName), eventName))){
  152.             double t = getResult(inputDoubles("Results for " + getName(participantNumber) + " from " + p.getTeam() + " in " + eventName + ": "), participantNumber, p, eventName);
  153.  
  154.  
  155.             setResult(participantNumber, new Result(t, findEvent(eventName)));
  156.  
  157.             System.out.println("The result for " + getName(participantNumber) + " has been added");
  158.         }else{
  159.             System.out.println("Error: " + getName(participantNumber) + " from " + p.getTeam() + " has already made " + e.getAttemptsAllowed() + " attempts in " + eventName);
  160.         }
  161.  
  162.  
  163.     }
  164.  
  165.  
  166.     private void setResult(int participantNumber, Result r){
  167.         for (Participant p : participantList){
  168.             if(p.getNumber() == participantNumber){
  169.                 p.setResult(participantNumber, r);
  170.             }
  171.  
  172.         }
  173.     }
  174.  
  175.     private void viewParticipant(){
  176.  
  177.         int inputViewParticipant = inputInts("Number: ");
  178.         if(!participantExists(inputViewParticipant)) {
  179.             System.out.println("Error: no participant with number " + inputViewParticipant + " found!");
  180.             return;
  181.         }
  182.         for (Event e : eventList){
  183.             if(didParticipate(inputViewParticipant, e.getEventName())){
  184.                 System.out.print("Results for " + getName(inputViewParticipant) + " in " + e.getEventName() + ": ");
  185.                 List<Result> results = getParticipantResults(inputViewParticipant, e.getEventName());
  186.  
  187.                 for (Result r : resultList){
  188.                     System.out.print(r.getResult() + ", ");
  189.                 }
  190.                 System.out.println(" ");
  191.             }
  192.         }
  193.  
  194.  
  195.     }
  196.  
  197.     private void viewEvent(String eventName){
  198.  
  199.         Result[] resultsArray = convertListToArray(keepBestResults(bubbleSortList(sortAlphabetically(getEventResults(eventName)))));
  200.         int[] placements = new int[keepBestResults(bubbleSortList(sortAlphabetically(getEventResults(eventName)))).size()];
  201.         if(placements.length != 0)
  202.             placements[0] = 1;
  203.  
  204.         for (int i = 1; i < placements.length; i++) {
  205.             placements[i] = resultsArray[i].getResult() == resultsArray[i - 1].getResult() ? placements[i - 1] : i + 1;
  206.         }
  207.  
  208.         for (int i = 0; i < resultsArray.length; i++) {
  209.             System.out.print(placements[i] + ": ");
  210.             System.out.println(resultsArray[i].getResult() + " " + getFullNameFromResults(resultsArray[i]) + " " + getTeamFromResutls(resultsArray[i]));
  211.         }
  212.  
  213.         getFullNameFromResults(resultsArray[0]);
  214.     }
  215.  
  216.     private List<Result> getEventResults(String eventNameInput) {
  217.         List<Result> eventResults = new ArrayList<>();
  218.         for(Result r : resultList){
  219.             if(eventNameInput.equals(r.getEventName(findEvent(eventNameInput)))){
  220.                 eventResults.add(r);
  221.  
  222.             }
  223.         }
  224.         return eventResults;
  225.  
  226.     }
  227.  
  228.     private List<Result> sortAlphabetically(List<Result> listToSort){
  229.  
  230.         Collections.sort(listToSort, new Comparator<Result>() {
  231.             public int compare(Result r1, Result r2) {
  232.                 return r1.getFullName(r1).compareTo(r2.getFullName(r2));
  233.  
  234.             }
  235.         });
  236.  
  237.         return listToSort;
  238.     }
  239.  
  240.     private List<Result> bubbleSortList(List<Result> listToSort){
  241.         int n = listToSort.size();
  242.         Result temp;
  243.  
  244.         for (Result r : listToSort) {
  245.             for (int i = 0; i < n; i++) {
  246.                 for (int j = 1; j < (n - i); j++) {
  247.                     if (listToSort.get(j - 1).getResult() < listToSort.get(j).getResult()) {
  248.                         temp = listToSort.get(j - 1);
  249.                         listToSort.set(j - 1, listToSort.get(j));
  250.                         listToSort.set(j, temp);
  251.                     }
  252.                 }
  253.             }
  254.         }
  255.         return listToSort;
  256.     }
  257.  
  258.     private List<Result> keepBestResults(List<Result> sortedList){
  259.  
  260.         List<Result> finishedList = new ArrayList<>();
  261.  
  262.         for (Result r : sortedList){
  263.             boolean exists = false;
  264.             if(finishedList.isEmpty()){
  265.                 finishedList.add(r);
  266.             }else{
  267.                 for (Result t : finishedList){
  268.                     if(r.getParticipantNumber(r) == t.getParticipantNumber(t)){
  269.                         exists = true;
  270.                     }
  271.                 }
  272.                 if(!exists){
  273.                     finishedList.add(r);
  274.                 }
  275.             }
  276.         }
  277.         return finishedList;
  278.     }
  279.  
  280.     private Result [] convertListToArray (List finishedList){
  281.         Result[] resultArray = new Result[finishedList.size()];
  282.         finishedList.toArray(resultArray);
  283.         return resultArray;
  284.     }
  285.  
  286.     private String getFullNameFromResults(Result t){
  287.         for (Result r : resultList){
  288.             return r.getFullName(r);
  289.         }
  290.         return null;
  291.     }
  292.  
  293.     private String getTeamFromResutls(Result t){
  294.         for (Result r : resultList){
  295.             return r.getTeamName(r);
  296.         }
  297.         return null;
  298.     }
  299.  
  300.     private boolean removeParticipantFromList (int participantNumber){
  301.         for (Participant p : participantList){
  302.             if(p.getNumber() == participantNumber){
  303.                 System.out.println(p.getFullName() + " from " + p.getTeam() + " with number " + participantNumber + " removed");
  304.                 participantList.remove(p);
  305.                 return true;
  306.             }
  307.         }
  308.         return false;
  309.     }
  310.  
  311.     private boolean didParticipate(int viewParticipant, String eventName){
  312.  
  313.         for (Result r : resultList){
  314.             if (viewParticipant == r.getParticipantNumber(findParticipant(viewParticipant)) && r.getEventName(findEvent(eventName)).equals(eventName))
  315.                 return true;
  316.         }
  317.         return false;
  318.     }
  319.  
  320.     private boolean participantExists (int participantNumber){
  321.  
  322.         for (Participant o : participantList){
  323.             if(o.getNumber() == participantNumber){
  324.                 return true;
  325.             }
  326.         }
  327.         return false;
  328.     }
  329.  
  330.     private Participant findParticipant(int participantNumber){
  331.         for (Participant o : participantList){
  332.             if(o.getNumber() == participantNumber){
  333.                 return o;
  334.             }
  335.         }
  336.         return null;
  337.     }
  338.  
  339.     private List<Result> findResultAndRemoveResult(Participant p, int participantNumber){
  340.         List <Result> resultListNew = new ArrayList<>();
  341.         for (Result r : resultList){
  342.             if(participantNumber != r.getParticipantNumber(p)){
  343.                 resultListNew.add(r);
  344.             }
  345.  
  346.         }
  347.         resultList = resultListNew;
  348.         return resultList;
  349.     }
  350.  
  351.     private boolean eventExists(String eventName){
  352.         boolean exists = false;
  353.  
  354.         for (Event e : eventList){
  355.             if(eventName.equals(e.getEventName())) {
  356.                 exists = true;
  357.                 break;
  358.             }else{
  359.                 exists = false;
  360.             }
  361.         }
  362.         return exists;
  363.     }
  364.  
  365.     private Participant addResultParticipant(int participantNumber) {
  366.         Participant o = null;
  367.         for (Participant p : participantList) {
  368.             if (participantNumber != p.getNumber()) {
  369.                 o = null;
  370.             } else {
  371.                 o = p;
  372.                 return o;
  373.             }
  374.         }
  375.         return o;
  376.     }
  377.  
  378.     private Event findEvent(String eventName){
  379.         Event w = null;
  380.         for (Event e : eventList){
  381.             if(!eventName.equals(e.getEventName())){
  382.                 w = null;
  383.             }else{
  384.                 w = e;
  385.                 return w;
  386.             }
  387.         }
  388.         return w;
  389.     }
  390.  
  391.     private int findAttemptsAllowed(String eventName){
  392.         for (Event e : eventList){
  393.             if(e.getEventName().equals(eventName)){
  394.                 return e.getAttemptsAllowed();
  395.             }
  396.         }
  397.         return 0;
  398.     }
  399.  
  400.     private List<Result> getParticipantResults(int viewParticipant, String eventName){
  401.         List <Result> participantResults = new ArrayList<>();
  402.         Participant o = null;
  403.         Event w = null;
  404.         for (Participant p : participantList) {
  405.             if (viewParticipant == p.getNumber()) {
  406.                 o = p;
  407.             }
  408.         }
  409.         for (Event e : eventList){
  410.             if(eventName.equals(e.getEventName())){
  411.                 w=e;
  412.             }
  413.         }
  414.  
  415.         for(Result r : resultList){
  416.             if(r.getParticipantNumber(o) == viewParticipant && r.getEventName(w).equals(eventName)){
  417.                 participantResults.add(r);
  418.             }
  419.         }
  420.         return participantResults;
  421.     }
  422.  
  423.     private List<Result>getResultsInEvent(List<Result> participantResults, String eventName){
  424.         List<Result> participantResultsPerEvent = new ArrayList<>();
  425.  
  426.         Event w = null;
  427.         for (Event e : eventList){
  428.             if(eventName.equals(e.getEventName())){
  429.                 w = e;
  430.             }
  431.         }
  432.  
  433.         for (Result r : participantResults){
  434.             if(r.getEventName(w).equals(eventName)){
  435.                 participantResultsPerEvent.add(r);
  436.             }
  437.         }
  438.         return participantResultsPerEvent;
  439.     }
  440.  
  441.     private int countTries(List<Result> participantResultsPerEvent){
  442.         return participantResultsPerEvent.size();
  443.     }
  444.  
  445.     private double getResult(double result, int startNumber, Participant o, String eventName) {
  446.  
  447.         while (result < 0) {
  448.             System.out.println("Error: must be greater than or equal to zero!");
  449.             result = inputDoubles("Results for " + getName(startNumber) + " from " + o.getTeam() + " in " + eventName + ": ");
  450.         }
  451.  
  452.         return result;
  453.     }
  454.  
  455.     private boolean eventExistsStartMenu(String command){
  456.         for (Event e : eventList){
  457.             if(command.equals(e.getEventName())){
  458.                 return true;
  459.             }
  460.         }
  461.         return false;
  462.     }
  463.  
  464.     private void message(String command) {
  465.  
  466.         String cutMessage = command.substring(8, Math.min(command.length(), 62));
  467.  
  468.         if (cutMessage.length() % 2 == 0) {
  469.             int paddingNeededPerSide = (58 - cutMessage.length()) / 2;
  470.             String paddingLeftSide = String.format("%" + paddingNeededPerSide + "s", "");
  471.             String paddingRightSide = String.format("%-" + paddingNeededPerSide + "s", "");
  472.  
  473.             System.out.println("############################################################");
  474.             System.out.println("#                                                          #");
  475.             System.out.println("#" + paddingLeftSide + cutMessage.toUpperCase() + paddingRightSide + "#");
  476.             System.out.println("#                                                          #");
  477.             System.out.println("############################################################");
  478.         } else {
  479.             int paddingNeededPerSide = (58 - cutMessage.length()) / 2;
  480.             String paddingLeftSide = String.format("%" + paddingNeededPerSide + "s", "");
  481.             String paddingRightSide = String.format("%-" + paddingNeededPerSide + "s", "");
  482.  
  483.             System.out.println("############################################################");
  484.             System.out.println("#                                                          #");
  485.             System.out.println("#" + paddingLeftSide + cutMessage.toUpperCase() + paddingRightSide + " #");
  486.             System.out.println("#                                                          #");
  487.             System.out.println("############################################################");
  488.         }
  489.  
  490.  
  491.     }
  492.  
  493.     private String getName(int number) {
  494.  
  495.         String fullName = null;
  496.  
  497.         for (Participant p : participantList) {
  498.             if (p.getNumber() == number) {
  499.                 fullName = p.getFirstName() + " " + p.getLastName();
  500.                 return fullName;
  501.             }
  502.         }
  503.  
  504.         return fullName;
  505.     }
  506.  
  507.     public String inputStrings(String prompt) {
  508.         System.out.print(prompt);
  509.         String s = keyboard.nextLine().toLowerCase().trim();
  510.  
  511.         if(s.isEmpty()){
  512.             s = " ";
  513.         }
  514.         String sNormalized = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
  515.         return sNormalized;
  516.     }
  517.  
  518.     public int inputInts(String prompt) {
  519.         System.out.print(prompt);
  520.         int i = keyboard.nextInt();
  521.         keyboard.nextLine();
  522.         return i;
  523.     }
  524.  
  525.     private double inputDoubles(String prompt) {
  526.         System.out.print(prompt);
  527.         double d = keyboard.nextDouble();
  528.         keyboard.nextLine();
  529.         return d;
  530.     }
  531.  
  532.     private void run() {
  533.         boolean running;
  534.  
  535.         do {
  536.  
  537.             String command = readCommand();
  538.             running = useCommand(command);
  539.  
  540.         } while (running);
  541.     }
  542.  
  543.     public static void main(String[] args) {
  544.         new Register().run();
  545.  
  546.     }
  547.  
  548. }
  549.  
  550. RESULT KLASS
  551.  
  552. // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
  553. // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
  554.  
  555. public class Result {
  556.  
  557.     private double result;
  558.     //private Participant p;
  559.     private Event e;
  560.  
  561.     public Result(double result, Event e) {
  562.         this.e = e;
  563.         this.result = result;
  564.     }
  565.  
  566.     public double getResult() {
  567.         return result;
  568.     }
  569.  
  570.     public void setResult(double result) {
  571.         this.result = result;
  572.     }
  573.  
  574.     public Event getEvent(Event e) {
  575.         return e;
  576.     }
  577.  
  578.     public void setEvent(Event e){
  579.         this.e = e;
  580.     }
  581.  
  582.     public Participant getParticipant(Participant p){
  583.         return p;
  584.     }
  585.  
  586.  
  587.  
  588.     public int getParticipantNumber(Participant p){
  589.         return p.getNumber();
  590.  
  591.     }
  592.  
  593.     public String getEventName(Event e){
  594.         return e.getEventName();
  595.     }
  596.  
  597.     public String toString() {
  598.         return (" " + e + " " + result + " ");
  599.     }
  600.  
  601.  
  602. }
  603.  
  604. EVENT KLASS
  605.  
  606. // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
  607. // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
  608.  
  609.  
  610. public class Event {
  611.  
  612.     private String eventName;
  613.     private int attemptsAllowed;
  614.  
  615.     public Event(String eventName, int attemptsAllowed) {
  616.         this.eventName = eventName;
  617.         this.attemptsAllowed = attemptsAllowed;
  618.     }
  619.  
  620.     public String getEventName() {
  621.         return eventName;
  622.     }
  623.  
  624.     public void setEventName(String eventName) {
  625.         this.eventName = eventName;
  626.     }
  627.  
  628.     public int getAttemptsAllowed() {
  629.         return attemptsAllowed;
  630.     }
  631.  
  632.     public void setAttemptsAllowed(int attemptsAllowed) {
  633.         this.attemptsAllowed = attemptsAllowed;
  634.     }
  635.  
  636.     public String toString() {
  637.         return (eventName + " " + attemptsAllowed);
  638.     }
  639.  
  640. }
  641.  
  642. // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
  643. // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
  644.  
  645. import java.util.ArrayList;
  646. import java.util.List;
  647.  
  648. public class Participant {
  649.  
  650.     private String firstName;
  651.     private String lastName;
  652.     private String fullName;
  653.     private String team;
  654.     private int number;
  655.     private Result r;
  656.  
  657.  
  658.     public Participant(String firstName, String lastName, String team, int number) {
  659.         this.firstName = firstName;
  660.         this.lastName = lastName;
  661.         this.team = team;
  662.         this.number = number;
  663.     }
  664.  
  665.     public Participant() {
  666.  
  667.     }
  668.  
  669.     public String getFirstName() {
  670.         return firstName;
  671.     }
  672.  
  673.     public void setFirstName(String firstName) {
  674.         this.firstName = firstName;
  675.     }
  676.  
  677.     public String getLastName() {
  678.         return lastName;
  679.     }
  680.  
  681.     public void setLastName(String lastName) {
  682.         this.lastName = lastName;
  683.     }
  684.  
  685.     public String getFullName() {
  686.         return firstName + " " + lastName;
  687.     }
  688.  
  689.     public void setFullName() {
  690.         this.fullName = fullName;
  691.     }
  692.  
  693.     public String getTeam() {
  694.         return team;
  695.     }
  696.  
  697.     public void setTeam(String team) {
  698.         this.team = team;
  699.     }
  700.  
  701.     public int getNumber() {
  702.         return number;
  703.     }
  704.  
  705.     public void setNumber(int number) {
  706.         this.number = number;
  707.     }
  708.  
  709.     public Result getResult(){
  710.         return r;
  711.     }
  712.  
  713.     public void setResult(int participant, Result r){
  714.         if(participant== number){
  715.             this.r = r;
  716.         }
  717.  
  718.  
  719.  
  720.  
  721.     }
  722.  
  723.     public String toString() {
  724.         return (firstName + " " + lastName + " from " + team + " with number " + number);
  725.     }
  726. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement