Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laboratoriski2;
- import java.util.ArrayList;
- public class Student {
- private String firstName;
- private String lastName;
- private String city;
- private int age;
- private long index;
- private Contact[] contacts;
- private int numberContacts;
- public Student(String firstName, String lastName, String city, int age, long index) {
- super();
- this.firstName = firstName;
- this.lastName = lastName;
- this.city = city;
- this.age = age;
- this.index = index;
- contacts = new Contact[10];
- numberContacts = 0;
- }
- public void addEmailContact(String date, String email) {
- Contact c = new EmailContact(date, email);
- if (numberContacts >= contacts.length)
- makeContactListBigger();
- contacts[numberContacts++] = c;
- }
- public void addPhoneContact(String date, String phone) {
- Contact c = new PhoneContact(date, phone);
- if (numberContacts >= contacts.length)
- makeContactListBigger();
- contacts[numberContacts++] = c;
- }
- private void makeContactListBigger() {
- Contact[] newList = new Contact[contacts.length + 10];
- for (int i = 0; i < contacts.length; i++) {
- newList[i] = contacts[i];
- }
- contacts = newList;
- }
- public Contact[] getEmailContacts() {
- ArrayList<Contact> ret = new ArrayList<>();
- for (int i = 0; i < numberContacts; i++) {
- if (contacts[i].getType() == "Email")
- ret.add(contacts[i]);
- }
- Contact[] c = (Contact[]) ret.toArray();
- return c;
- }
- public Contact[] getPhoneContacts() {
- ArrayList<Contact> ret = new ArrayList<>();
- for (int i = 0; i < numberContacts; i++) {
- if (contacts[i].getType() == "Phone")
- ret.add(contacts[i]);
- }
- Contact[] c = (Contact[]) ret.toArray();
- return c;
- }
- public String getFullName() {
- return firstName + " " + lastName;
- }
- public String getCity() {
- return city;
- }
- public long getIndex() {
- return index;
- }
- public Contact getLatestContact() {
- if (contacts[0] == null)
- return null;
- Contact latest = contacts[0];
- for (int i = 1; i < contacts.length; i++) {
- if (contacts[i].isNewerThan(latest))
- latest = contacts[i];
- }
- return latest;
- }
- public int getAge() {
- return age;
- }
- @Override
- public String toString() {
- Contact tel[] = getPhoneContacts();
- Contact mail[] = getEmailContacts();
- String str =
- "{" + navodnici("ime") + ":" + navodnici(firstName) + ", "
- + navodnici("prezime") + ":" + navodnici(lastName) + ", "
- + navodnici("vozrast") + ":" + Integer.toString(age) + ", "
- + navodnici("grad") + ":" + navodnici(city) + ", "
- + navodnici("indeks") + ":" + Integer.toString(index) + ", "
- + navodnici("telefonskiKontakti") + ":["
- ;
- for (int i = 0; i < tel.length; i++)
- {
- str += navodnici(((PhoneContact)tel[i]).getPhone());
- if (i < tel.length-1)
- str += ", ";
- }
- str += "], " + navodnici("emailKontakti") + ":[";
- for (int i = 0; i < mail.length; i++)
- {
- str += navodnici(((EmailContact)mail[i]).getEmail());
- if (i < mail.length-1)
- str += ", ";
- }
- str += "]}";
- return str;
- }
- public int getNumberOfContacts() {
- return numberContacts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment