Advertisement
Vejdis

Untitled

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