ITI0011:praktikum 6 N8

Allikas: Kursused
Redaktsioon seisuga 8. oktoober 2014, kell 22:30 kasutajalt Ago (arutelu | kaastöö) (→‎Tips)
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 9. oktoober kell 8.00

Praktikumis käsitletavad teemad:

  • git
  • veebist lugemine
  • Stringi parsimine
  • objekti loomine
  • (sortimine)

Kood tuleb üles laadida git'i. Kausta (projekti) nimi peaks olema "prax6".

Koodinäide

tekib tunnis

Tips

  • URL, URL.openStream()
  • InputStreamReader(InputStream, charset), InputStreamReader(url.openStream(), "utf-8")
  • BufferedReader
  • String, String.split
  • StringTokenizer, StringTokenizer.nextToken()
  • Integer.parseInt(String), Double.parseDouble(String)
  • Collections.sort(List, Comparator)
  • Object.toString()

Exercise

You have a database of contacts. The database is stored in a text file. Now you want to write a program that reads the data from the file and converts it into Java objects. You will write different objects for different type of contacts. But in order to store them in one collection, you need to have a common type (e.g. super class).

1) Create objects for different contacts:

  • friend. has fields:
    • birthday
    • firstname
    • lastname
    • phone number
  • relative. has fields:
    • birthday
    • firstname
    • lastname
    • relation (e.g. mother, uncle etc.)
  • work contact. has fields
    • birthday
    • firstname
    • lastname
    • email
    • department

2) Read information from file: http://dijkstra.cs.ttu.ee/~ago/iti0011/praktikum-06-n8-input.txt

The structure of the file (F, R, W indicates contact type: Friend, Relative, Work accordingly):

in case of a friend:

F birthday firstname lastname phonenumber


in case of a relative:

 R birthday firstname lastname relation

in case of a work contact:

W birthday firstname lastname email department

For example the file:

F  1990-02-23 Mati Kaal 1231233
R 1971-05-13 Anu Välba mother
W 1982-01-21 Juur Juurikas juuri@kas.ee PÄRNU

would yield in 3 objects: one friend object, one relative object and one work contact object.

3) (optional) try to sort the objects by birthday (day and month only, if day and month are the same, then use year).

An example: http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property

4) print out contacts.

Contacts should be stored in a list or array (or something else similar). Print out all the information about the contact. In case of a friend, print out "Friend " + firstname, lastname, birthday, phone number etc.