Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
- // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
- import java.util.*;
- public class Register {
- private Scanner keyboard = new Scanner(System.in);
- private List<Event> eventList = new ArrayList<>();
- private List<Participant> participantList = new ArrayList<>();
- private int participantNumber = 99;
- private String readCommand() {
- 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");
- return inputStrings("Skriv ditt val här: ");
- }
- private boolean useCommand(String command) {
- switch (command) {
- case "1":
- case "Add event":
- addEvent();
- break;
- case "2":
- case "Add participant":
- addParticipant();
- break;
- case "3":
- case "Remove participant":
- removeParticipant();
- break;
- case "4":
- case "Add result":
- addResult();
- break;
- case "5":
- case "Participant":
- viewParticipant();
- break;
- case "7":
- System.out.println(eventList);
- System.out.println(participantList);
- break;
- case "Exit":
- System.out.println("Program closing, goodbye!");
- return false;
- default:
- if (command.startsWith("Message")) {
- message(command);
- } else if (eventExistsStartMenu(command)) {
- viewEvent(command);
- }else{
- System.out.println("Error, wrong command");
- }
- }
- return true;
- }
- private void addEvent() {
- String eventNameTemp = inputStrings("Event name: ");
- while(eventNameTemp.trim().isEmpty() || eventNameTemp.isEmpty()){
- System.out.println("Error: name can't be empty");
- eventNameTemp = inputStrings("Event name: ");
- }
- boolean eventAlreadyExists = false;
- for (Event e : eventList){
- if (eventNameTemp.equals(e.getEventName())){
- eventAlreadyExists = true;
- break;
- }
- }
- if(eventAlreadyExists){
- System.out.println("Error: "+ eventNameTemp + " has already been added");
- }else{
- int attemptsAllowedTemp = inputInts("Attempts allowed: ");
- while (attemptsAllowedTemp <= 0){
- System.out.println("Error: too low, must allow at least one attempt");
- attemptsAllowedTemp = inputInts("Attempts allowed: ");
- }
- System.out.println(eventNameTemp + " with " + attemptsAllowedTemp + " attempts allowed created");
- eventList.add(new Event(eventNameTemp,attemptsAllowedTemp));
- }
- }
- private void addParticipant() {
- String firstName = inputStrings("First name: ");
- while (firstName.trim().isEmpty()) {
- System.out.println("Error: name can't be empty!");
- firstName = inputStrings("First name: ");
- }
- String lastName = inputStrings("Last name: ");
- while (lastName.trim().isEmpty()) {
- System.out.println("Error: name can't be empty!");
- lastName = inputStrings("Last name: ");
- }
- String team = inputStrings("Team: ");
- while (team.trim().isEmpty()) {
- System.out.println("Error: name can't be emtpy!");
- team = inputStrings("Team: ");
- }
- participantNumber++;
- participantList.add(new Participant(firstName, lastName, team, participantNumber));
- System.out.println(participantList.get(participantList.size() - 1) + " added");
- }
- private void removeParticipant() {
- int participantToDelete = inputInts("Number: ");
- if(!removeParticipantFromList(participantToDelete)){
- System.out.println("Error: no participant with number " + participantToDelete + " exists");
- }
- }
- private void addResult(){
- int participantNumber = inputInts("Number: ");
- if(!participantExists(participantNumber)){
- System.out.println("Error: no participant with number " + participantNumber + " found!");
- return;
- }
- Participant p = addResultParticipant(participantNumber);
- String eventName = inputStrings("Event: ");
- if(!eventExists(eventName)){
- System.out.println("Error: no event called \"" + eventName + "\" found!");
- return;
- }
- Event e = findEvent(eventName);
- if(p.countTries(participantNumber, eventName) < e.getAttemptsAllowed()){
- double result = getResult(inputDoubles("Results for " + getName(participantNumber) + " from " + p.getTeam() + " in " + eventName + ": "), participantNumber, p, eventName);
- p.setResult(participantNumber, new Result(result, findEvent(eventName)));
- }else{
- System.out.println("Error: " + getName(participantNumber) + " from " + p.getTeam() + " has already made " + e.getAttemptsAllowed() + " attempts in " + eventName);
- }
- }
- private double getResult(double result, int startNumber, Participant o, String eventName){
- while (result < 0) {
- System.out.println("Error: must be greater than or equal to zero!");
- result = inputDoubles("Results for " + getName(startNumber) + " from " + o.getTeam() + " in " + eventName + ": ");
- }
- return result;
- }
- private void viewParticipant(){
- int inputViewParticipant = inputInts("Number: ");
- if(!participantExists(inputViewParticipant)) {
- System.out.println("Error: no participant with number " + inputViewParticipant + " found!");
- return;
- }
- Participant p = findParticipant(inputViewParticipant);
- for (Event e : eventList) {
- if(p.didParticipate(inputViewParticipant, e.getEventName())) {
- System.out.print("Results for " + getName(inputViewParticipant) + " in " + e.getEventName() + ": ");
- List <Result> results = p.getResultPerEvent(inputViewParticipant, e.getEventName());
- for (Result r : results){
- System.out.print(r.getResult() + ", ");
- }
- System.out.println(" ");
- }
- }
- }
- private Participant findParticipant(int participantNumber){
- for (Participant p : participantList){
- if(p.getNumber() == participantNumber){
- return p;
- }
- }
- return null;
- }
- private List<Participant> createBestResultList(String eventName){
- List<Participant> bestResultList = new ArrayList<>();
- for (Event e : eventList) {
- if (eventName.equals(e.getEventName())) {
- for (Participant p : participantList) {
- for (Result r : p.getResultList()) {
- if (r.getEventName().equals(eventName) && !bestResultList.contains(p)) {
- bestResultList.add(p);
- }
- }
- }
- }
- }
- return bestResultList;
- }
- private void viewEvent(String eventName) {
- List<Participant> bestResultList = new ArrayList<>();
- bestResultList = createBestResultList(eventName);
- sortAlphabetically(bestResultList);
- bubbleSortList(bestResultList);
- Participant[] bestResultsArray = convertListToArray(bestResultList);
- int[] placements = new int[bestResultList.size()];
- if (placements.length != 0)
- placements[0] = 1;
- for (int i = 1; i < placements.length; i++){
- for (Participant p : bestResultsArray){
- placements[i] = bestResultsArray[i].getResult().getResult() == bestResultsArray[i-1].getResult().getResult() ? placements[i - 1] : i+1;
- }
- }
- System.out.println("Results for " + eventName);
- for (int i = 0; i < bestResultsArray.length; i++) {
- System.out.print(placements[i] + ": ");
- System.out.println(bestResultsArray[i].getResult().getResult() + " " + bestResultsArray[i].getFullName() + " " + bestResultsArray[i].getTeam());
- }
- }
- private List<Participant> sortAlphabetically(List<Participant> listToSort){
- Collections.sort(listToSort, new Comparator<Participant>() {
- public int compare(Participant p1, Participant p2) {
- return p1.getFullName().compareTo(p1.getFullName());
- }
- });
- return listToSort;
- }
- private List<Participant> bubbleSortList(List<Participant> listToSort){
- int n = listToSort.size();
- Participant temp;
- for (Participant p : listToSort) {
- for (int i = 0; i < n; i++) {
- for (int j = 1; j < (n - i); j++) {
- if (listToSort.get(j - 1).getResult().getResult() < listToSort.get(j).getResult().getResult()){
- temp = listToSort.get(j - 1);
- listToSort.set(j - 1, listToSort.get(j));
- listToSort.set(j, temp);
- }
- }
- }
- }
- return listToSort;
- }
- private Participant [] convertListToArray (List finishedList){
- Participant[] resultArray = new Participant[finishedList.size()];
- finishedList.toArray(resultArray);
- return resultArray;
- }
- private boolean removeParticipantFromList (int participantNumber){
- for (Participant p : participantList){
- if(p.getNumber() == participantNumber){
- System.out.println(p.getFullName() + " from " + p.getTeam() + " with number " + participantNumber + " removed");
- participantList.remove(p);
- return true;
- }
- }
- return false;
- }
- private boolean participantExists (int participantNumber){
- for (Participant o : participantList){
- if(o.getNumber() == participantNumber){
- return true;
- }
- }
- return false;
- }
- private boolean eventExists(String eventName){
- boolean exists = false;
- for (Event e : eventList){
- if(eventName.equals(e.getEventName())) {
- exists = true;
- break;
- }else{
- exists = false;
- }
- }
- return exists;
- }
- private Participant addResultParticipant(int participantNumber) {
- Participant o = null;
- for (Participant p : participantList) {
- if (participantNumber != p.getNumber()) {
- o = null;
- } else {
- o = p;
- return o;
- }
- }
- return o;
- }
- private Event findEvent(String eventName){
- Event w = null;
- for (Event e : eventList){
- if(!eventName.equals(e.getEventName())){
- w = null;
- }else{
- w = e;
- return w;
- }
- }
- return w;
- }
- private boolean eventExistsStartMenu(String command){
- for (Event e : eventList){
- if(command.equals(e.getEventName())){
- return true;
- }
- }
- return false;
- }
- private void message(String command) {
- String cutMessage = command.substring(8, Math.min(command.length(), 62));
- if (cutMessage.length() % 2 == 0) {
- int paddingNeededPerSide = (58 - cutMessage.length()) / 2;
- String paddingLeftSide = String.format("%" + paddingNeededPerSide + "s", "");
- String paddingRightSide = String.format("%-" + paddingNeededPerSide + "s", "");
- System.out.println("############################################################");
- System.out.println("# #");
- System.out.println("#" + paddingLeftSide + cutMessage.toUpperCase() + paddingRightSide + "#");
- System.out.println("# #");
- System.out.println("############################################################");
- } else {
- int paddingNeededPerSide = (58 - cutMessage.length()) / 2;
- String paddingLeftSide = String.format("%" + paddingNeededPerSide + "s", "");
- String paddingRightSide = String.format("%-" + paddingNeededPerSide + "s", "");
- System.out.println("############################################################");
- System.out.println("# #");
- System.out.println("#" + paddingLeftSide + cutMessage.toUpperCase() + paddingRightSide + " #");
- System.out.println("# #");
- System.out.println("############################################################");
- }
- }
- private String getName(int number) {
- String fullName = null;
- for (Participant p : participantList) {
- if (p.getNumber() == number) {
- fullName = p.getFirstName() + " " + p.getLastName();
- return fullName;
- }
- }
- return fullName;
- }
- private String inputStrings(String prompt) {
- System.out.print(prompt);
- String s = keyboard.nextLine().toLowerCase().trim();
- if(s.isEmpty()){
- s = " ";
- }
- String sNormalized = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
- return sNormalized;
- }
- private int inputInts(String prompt) {
- System.out.print(prompt);
- int i = keyboard.nextInt();
- keyboard.nextLine();
- return i;
- }
- private double inputDoubles(String prompt) {
- System.out.print(prompt);
- double d = keyboard.nextDouble();
- keyboard.nextLine();
- return d;
- }
- private void run() {
- boolean running;
- do {
- String command = readCommand();
- running = useCommand(command);
- } while (running);
- }
- public static void main(String[] args) {
- new Register().run();
- }
- }
- NY KLASSS RESULT
- // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
- // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
- public class Result {
- private double result;
- private Event e;
- public Result(double result, Event e) {
- this.e = e;
- this.result = result;
- }
- public double getResult() {
- return result;
- }
- public void setResult(double result) {
- this.result = result;
- }
- public Event getEvent() {
- return e;
- }
- public void setEvent(Event e){
- this.e = e;
- }
- public String getEventName(){
- return e.getEventName();
- }
- public String toString() {
- return (" " + e + " " + result + " ");
- }
- }
- NY KLASS PARTICIPANT
- // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
- // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
- import java.util.ArrayList;
- import java.util.List;
- public class Participant {
- private String firstName;
- private String lastName;
- private String fullName;
- private String team;
- private int number;
- private List<Result> resultList = new ArrayList<>();
- public Participant(String firstName, String lastName, String team, int number) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.team = team;
- this.number = number;
- }
- public Participant() {
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getFullName() {
- return firstName + " " + lastName;
- }
- public void setFullName() {
- this.fullName = fullName;
- }
- public String getTeam() {
- return team;
- }
- public void setTeam(String team) {
- this.team = team;
- }
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- public Result getResult(){
- for (Result r : resultList){
- return r;
- }
- return null;
- }
- public List<Result> getResultList(){
- return resultList;
- }
- public void setResult(int participant, Result r){
- if(participant== number){
- resultList.add(r);
- bubbleSortList(resultList);
- }
- }
- public int countTries(int number, String eventName){
- int tries = 0;
- for(Result r : resultList){
- if(r.getEventName().equals(eventName) && getNumber() == number)
- tries++;
- }
- return tries;
- }
- public List<Result> getResultPerEvent (int viewParticipant, String eventName){
- List<Result> participantResults = new ArrayList<>();
- for(Result r : resultList){
- if(getNumber() == viewParticipant && r.getEventName().equals(eventName)){
- participantResults.add(r);
- }
- }
- return participantResults;
- }
- public boolean didParticipate (int number, String eventName){
- for (Result r : resultList){
- if(getNumber()==number && r.getEventName().equals(eventName)){
- return true;
- }
- }
- return false;
- }
- public List<Result> bubbleSortList(List<Result> listToSort){
- int n = resultList.size();
- Result temp;
- for (Result r : listToSort){
- for(int i = 0 ; i < n; i++){
- for ( int j = 1; j < (n - i); j++){
- if(listToSort.get(j-1).getResult() < listToSort.get(j).getResult()){
- temp = listToSort.get(j - 1);
- listToSort.set(j -1, listToSort.get(j));
- listToSort.set(j, temp);
- }
- }
- }
- }
- return listToSort;
- }
- public String toString() {
- return (firstName + " " + lastName + " from " + team + " with number " + number + " result " + resultList);
- }
- }
- NY KLASS EVENT
- // Felix Vejdegren || Jack Hällström || Douglas Hammarstam
- // feve5814@student.su.se || jaha4972@student.su.se || doha6991@student.su.se
- public class Event {
- private String eventName;
- private int attemptsAllowed;
- public Event(String eventName, int attemptsAllowed) {
- this.eventName = eventName;
- this.attemptsAllowed = attemptsAllowed;
- }
- public String getEventName() {
- return eventName;
- }
- public void setEventName(String eventName) {
- this.eventName = eventName;
- }
- public int getAttemptsAllowed() {
- return attemptsAllowed;
- }
- public void setAttemptsAllowed(int attemptsAllowed) {
- this.attemptsAllowed = attemptsAllowed;
- }
- public String toString() {
- return (eventName + " " + attemptsAllowed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement