Advertisement
Filip_Markoski

1.Intro.1 Marathon (Solved)

Oct 17th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. interface IMarathon {
  5.     Athlete bestTime();
  6.     int AthletesFrom(String s);
  7. }
  8.  
  9. class Athlete {
  10.     private String name;
  11.     private String gender;
  12.     private int age;
  13.     private double time;
  14.     private String country;
  15.     public Athlete(String name, String gender, int age, double time, String country) {
  16.         super();
  17.         this.name = name;
  18.         this.gender = gender;
  19.         this.age = age;
  20.         this.time = time;
  21.         this.country = country;
  22.     }
  23.     public String getName() {
  24.         return name;
  25.     }
  26.     public void setName(String name) {
  27.         this.name = name;
  28.     }
  29.     public String getGender() {
  30.         return gender;
  31.     }
  32.     public void setGender(String gender) {
  33.         this.gender = gender;
  34.     }
  35.     public int getAge() {
  36.         return age;
  37.     }
  38.     public void setAge(int age) {
  39.         this.age = age;
  40.     }
  41.     public double getTime() {
  42.         return time;
  43.     }
  44.     public void setTime(double time) {
  45.         this.time = time;
  46.     }
  47.     public String getCountry() {
  48.         return country;
  49.     }
  50.     public void setCountry(String country) {
  51.         this.country = country;
  52.     }
  53.     @Override
  54.     public String toString() {
  55.         return name+"\n"+age+"\n"+country+"\n"+time+"\n";
  56.     }
  57.    
  58. }
  59.  
  60. class Marathon implements IMarathon {
  61.     String hostCountry;
  62.     int year;
  63.     Athlete athletes[];
  64.    
  65.     public Marathon(String hostCountry, int year, Athlete[] athletes) {
  66.         super();
  67.         this.hostCountry = hostCountry;
  68.         this.year = year;
  69.         this.athletes = athletes;
  70.     }
  71.    
  72.     public String getHostCountry() {
  73.         return hostCountry;
  74.     }
  75.     public void setHostCountry(String hostCountry) {
  76.         this.hostCountry = hostCountry;
  77.     }
  78.     public int getYear() {
  79.         return year;
  80.     }
  81.     public void setYear(int year) {
  82.         this.year = year;
  83.     }
  84.     public Athlete[] getAthletes() {
  85.         return athletes;
  86.     }
  87.     public void setAthletes(Athlete[] athletes) {
  88.         this.athletes = athletes;
  89.     }
  90.  
  91.     @Override
  92.     public String toString() {
  93.         StringBuffer sb = new StringBuffer();
  94.         for (Athlete athlete : athletes) {
  95.             sb.append(athlete.toString());
  96.         }
  97.         return hostCountry +"\n"+ year +"\n"+ sb.toString();
  98.     }
  99.  
  100.     @Override
  101.     public Athlete bestTime() {
  102.         double min = 10000000;
  103.         int minIndex = 0;
  104.         for (int i = 0; i < athletes.length; i++) {
  105.             if (athletes[i].getTime() < min) {
  106.                 min = athletes[i].getTime();
  107.                 minIndex = i;
  108.             }
  109.             //System.out.println(athletes[i].getTime() +" "+ min);
  110.         }
  111.         return athletes[minIndex];
  112.     }
  113.  
  114.     @Override
  115.     public int AthletesFrom(String s) {
  116.         int count = 0;
  117.         for (Athlete athlete : athletes) {
  118.             if (athlete.getCountry().equals(s)) {
  119.                 count++;
  120.             }
  121.         }
  122.         return count;
  123.     }
  124.      
  125. }
  126.  
  127. public class Test {
  128.  
  129.     public static void main(String[] args) {
  130.         Scanner input=new Scanner(System.in);
  131.         int n=input.nextInt();
  132.         Athlete[] athletes = new Athlete[n];
  133.        
  134.         String ime;
  135.         String pol;
  136.         int vozrast;
  137.         double vreme;
  138.         String zemja;
  139.        
  140.         input.nextLine();
  141.            
  142.         for(int i=0;i<n;i++)
  143.         {
  144.             ime = input.nextLine();
  145.             pol = input.nextLine();
  146.             vozrast = input.nextInt();
  147.             vreme = input.nextDouble();
  148.             input.nextLine();
  149.             zemja = input.nextLine();
  150.             athletes[i]=new Athlete(ime,pol,vozrast,vreme,zemja);
  151.         }
  152.        
  153.         String mesto;
  154.         int godina;
  155.         String zemjaP;
  156.         mesto = input.nextLine();
  157.         godina = input.nextInt();
  158.         input.nextLine();
  159.        
  160.         Marathon m1 = new Marathon(mesto, godina, athletes);
  161.         System.out.print(m1.toString());
  162.        
  163.         zemjaP = input.nextLine();
  164.         System.out.println("Prvo mesto: " + m1.bestTime().toString());
  165.         System.out.println("Ima vkupno " + m1.AthletesFrom(zemjaP) + " atleticar/i od " + zemjaP);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement