Advertisement
Guest User

Untitled

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