Erinevus lehekülje "ITI0011:praktikum 6 N8" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
 
113. rida: 113. rida:
 
 
 
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 +
 +
String dateString = "2014-01-05";
 +
Date date;
 +
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 +
try {
 +
date = sdf.parse(dateString);
 +
System.out.println(date);
 +
Calendar c = Calendar.getInstance();
 +
c.setTime(date);
 +
c.add(Calendar.DATE, 20);
 +
System.out.println(c.getTime());
 +
} catch (ParseException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
}
 
}
 
}
  

Viimane redaktsioon: 9. oktoober 2014, kell 06:22

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

<source lang="java"> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.StringTokenizer;

public class Contacts {

public static class Data { public String name; public String name2; public int blah; public int blah2;

@Override public String toString() { //return super.toString(); return name + " " + name2; } }

public static void main(String[] args) {

try { URL url = new URL("http://dijkstra.cs.ttu.ee/~ago/iti0011/praktikum-06-n8-input.txt"); InputStream is = url.openStream(); InputStreamReader isr = new InputStreamReader(is, "utf8");

BufferedReader br = new BufferedReader(isr);

String line = null;


while (true) { line = br.readLine(); if (line == null) break; System.out.println(line); }

} catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }


String line = "123 tere tulemast 434";

Data d = new Data(); int start = 0; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c == ' ') { System.out.println(line.substring(start, i)); start = i + 1; } } System.out.println(line.substring(start));

String[] tokens = line.split(" "); for (String s : tokens) { // for (i = 0; i < tokens.length; i++) String s = tokens[i]; System.out.println(s); }

StringTokenizer st = new StringTokenizer(line); System.out.println(st.nextToken()); System.out.println(st.nextToken()); System.out.println(st.nextToken()); System.out.println(st.nextToken()); if (st.hasMoreTokens()) { System.out.println(st.nextToken()); }


String number = "123a"; try { int a = Integer.valueOf(number); System.out.println(a); } catch (NumberFormatException e) { System.out.println("NaN"); }

//Data d = new Data(); d.name = "tere"; d.name2 = "tere"; d.blah = 12; d.blah2 = 13;

System.out.println(d);


//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String dateString = "2014-01-05"; Date date; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { date = sdf.parse(dateString); System.out.println(date); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, 20); System.out.println(c.getTime()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

} </source>

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.