Dev-san

Untitled

Oct 23rd, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package laboratoriski2;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Student {
  6.     private String firstName;
  7.     private String lastName;
  8.     private String city;
  9.     private int age;
  10.     private long index;
  11.     private Contact[] contacts;
  12.     private int numberContacts;
  13.  
  14.     public Student(String firstName, String lastName, String city, int age, long index) {
  15.         super();
  16.         this.firstName = firstName;
  17.         this.lastName = lastName;
  18.         this.city = city;
  19.         this.age = age;
  20.         this.index = index;
  21.  
  22.         contacts = new Contact[10];
  23.         numberContacts = 0;
  24.     }
  25.  
  26.     public void addEmailContact(String date, String email) {
  27.         Contact c = new EmailContact(date, email);
  28.         if (numberContacts >= contacts.length)
  29.             makeContactListBigger();
  30.         contacts[numberContacts++] = c;
  31.  
  32.     }
  33.  
  34.     public void addPhoneContact(String date, String phone) {
  35.         Contact c = new PhoneContact(date, phone);
  36.         if (numberContacts >= contacts.length)
  37.             makeContactListBigger();
  38.         contacts[numberContacts++] = c;
  39.     }
  40.  
  41.     private void makeContactListBigger() {
  42.         Contact[] newList = new Contact[contacts.length + 10];
  43.         for (int i = 0; i < contacts.length; i++) {
  44.             newList[i] = contacts[i];
  45.         }
  46.         contacts = newList;
  47.  
  48.     }
  49.  
  50.     public Contact[] getEmailContacts() {
  51.         ArrayList<Contact> ret = new ArrayList<>();
  52.         for (int i = 0; i < numberContacts; i++) {
  53.             if (contacts[i].getType() == "Email")
  54.                 ret.add(contacts[i]);
  55.         }
  56.         Contact[] c = (Contact[]) ret.toArray();
  57.         return c;
  58.     }
  59.  
  60.     public Contact[] getPhoneContacts() {
  61.         ArrayList<Contact> ret = new ArrayList<>();
  62.         for (int i = 0; i < numberContacts; i++) {
  63.             if (contacts[i].getType() == "Phone")
  64.                 ret.add(contacts[i]);
  65.         }
  66.         Contact[] c = (Contact[]) ret.toArray();
  67.         return c;
  68.     }
  69.  
  70.     public String getFullName() {
  71.         return firstName + " " + lastName;
  72.     }
  73.  
  74.     public String getCity() {
  75.         return city;
  76.     }
  77.  
  78.     public long getIndex() {
  79.         return index;
  80.     }
  81.  
  82.     public Contact getLatestContact() {
  83.         if (contacts[0] == null)
  84.             return null;
  85.         Contact latest = contacts[0];
  86.         for (int i = 1; i < contacts.length; i++) {
  87.             if (contacts[i].isNewerThan(latest))
  88.                 latest = contacts[i];
  89.         }
  90.         return latest;
  91.     }
  92.  
  93.     public int getAge() {
  94.         return age;
  95.     }
  96.  
  97.     @Override
  98.     public String toString() {
  99.         Contact tel[] = getPhoneContacts();
  100.         Contact mail[] = getEmailContacts();
  101.         String str =
  102.                 "{" + navodnici("ime")  + ":" + navodnici(firstName)        + ", "
  103.                 + navodnici("prezime")  + ":" + navodnici(lastName)     + ", "
  104.                 + navodnici("vozrast")  + ":" + Integer.toString(age)   + ", "
  105.                 + navodnici("grad")     + ":" + navodnici(city)     + ", "
  106.                 + navodnici("indeks")   + ":" + Integer.toString(index)     + ", "
  107.                 + navodnici("telefonskiKontakti") + ":["
  108.                 ;
  109.         for (int i = 0; i < tel.length; i++)
  110.         {
  111.             str += navodnici(((PhoneContact)tel[i]).getPhone());
  112.             if (i < tel.length-1)
  113.                 str += ", ";
  114.         }
  115.         str += "], " + navodnici("emailKontakti") + ":[";
  116.         for (int i = 0; i < mail.length; i++)
  117.         {
  118.             str += navodnici(((EmailContact)mail[i]).getEmail());
  119.             if (i < mail.length-1)
  120.                 str += ", ";
  121.         }
  122.         str += "]}";
  123.         return str;
  124.     }
  125.  
  126.     public int getNumberOfContacts() {
  127.         return numberContacts;
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment