Advertisement
Guest User

Untitled

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