From 90a7308e46d3ecf26abec94d581f4f0c796ec5de Mon Sep 17 00:00:00 2001 From: Lisa Date: Fri, 31 May 2019 15:10:14 +0200 Subject: [PATCH] Initial commit 8.1 --- omp.dat | Bin 0 -> 190 bytes src/main/java/_8/Date.java | 106 ++++++++++++++++ src/main/java/_8/Lecture.java | 141 ++++++++++++++++++++++ src/main/java/_8/Lecturer.java | 51 ++++++++ src/main/java/provided/_8/LectureOMP.java | 22 ++++ 5 files changed, 320 insertions(+) create mode 100644 omp.dat create mode 100644 src/main/java/_8/Date.java create mode 100644 src/main/java/_8/Lecture.java create mode 100644 src/main/java/_8/Lecturer.java create mode 100644 src/main/java/provided/_8/LectureOMP.java diff --git a/omp.dat b/omp.dat new file mode 100644 index 0000000000000000000000000000000000000000..983b75253351c22440c7fba1f3ba393c08237984 GIT binary patch literal 190 zcmYj{F%E)25Jg831+}-`BY;-6XrTqcWKXcb2&2oc8HUE|dJ#9Vv3U9a{d`!GyJv?S z@~VE|Q#=zoA!I>yW8bkEhG3KH)Z|)K`WCJI4G>l34N~njs(qFp0bmZvnNXOT9^%4V z93egROrwx-u+~lQ(bdRZ_|5=UpOA527f4`n(W(e7g3Z;vCegxZ2G~}U{Fz23W9e;b Fi(d+AHah?S literal 0 HcmV?d00001 diff --git a/src/main/java/_8/Date.java b/src/main/java/_8/Date.java new file mode 100644 index 0000000..693dfc7 --- /dev/null +++ b/src/main/java/_8/Date.java @@ -0,0 +1,106 @@ +package _8; + +import java.io.DataInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class Date { + + private int year; + private int month; + private int day; + private int hour; + private String lectureHall = ""; + private List topics = new ArrayList<>(); + + public Date() { + super(); + } + + public Date(int year, int month, int day, int hour, String lectureHall) { + super(); + this.year = year; + this.month = month; + this.day = day; + this.hour = hour; + this.lectureHall = lectureHall; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(int month) { + this.month = month; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + public int getHour() { + return hour; + } + + public void setHour(int hour) { + this.hour = hour; + } + + public String getLectureHall() { + return lectureHall; + } + + public void setLectureHall(String lectureHall) { + this.lectureHall = lectureHall; + } + + public List getTopics() { + return topics; + } + + public void addTopics(String topic) { topics.add(topic); } + + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + result.append(year); + result.append("-"); + result.append(month); + result.append("-"); + result.append(day); + result.append(" "); + result.append(hour); + result.append(":00, "); + result.append(lectureHall); + result.append(" "); + result.append(topics); + return result.toString(); + } + + public void load(DataInputStream dis) throws IOException { + this.setYear(dis.readInt()); + this.setMonth(dis.readInt()); + this.setDay(dis.readInt()); + this.setHour(dis.readInt()); + this.setLectureHall(dis.readUTF()); + + int numberOfTopics = dis.readInt(); + for (int i = 0; i < numberOfTopics; i++) { + this.addTopics(dis.readUTF()); + } + } + +} diff --git a/src/main/java/_8/Lecture.java b/src/main/java/_8/Lecture.java new file mode 100644 index 0000000..fc0ca85 --- /dev/null +++ b/src/main/java/_8/Lecture.java @@ -0,0 +1,141 @@ +package _8; + +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +public class Lecture { + + private String number = ""; + private String title = ""; + private String shortTitle = ""; + private String semester = ""; + private List lecturers = new ArrayList<>(); + private List schedule = new ArrayList<>(); + + public Lecture(String number, String title, String shortTitle, String semester) { + super(); + this.number = number; + this.title = title; + this.shortTitle = shortTitle; + this.semester = semester; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getShortTitle() { + return shortTitle; + } + + public void setShortTitle(String shortTitle) { + this.shortTitle = shortTitle; + } + + public String getSemester() { + return semester; + } + + public void setSemester(String semester) { + this.semester = semester; + } + + public List getLecturers() { + return lecturers; + } + + public List getSchedule() { + return schedule; + } + + public void addLecturers(Lecturer lecturer) { + this.lecturers.add(lecturer); + } + + public void addSchedule(Date date) { + this.schedule.add(date); + } + + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + result.append(number); + result.append(": "); + result.append(title); + result.append(" ("); + result.append(shortTitle); + result.append("), "); + result.append(semester); + result.append("\n\t"); + for (int i = 0; i < lecturers.size(); i++) { + if (i > 0) { + result.append(", "); + } + result.append(lecturers.get(i)); + } + for (Date date : schedule) { + result.append("\n\t- "); + result.append(date); + } + result.append("\n"); + return result.toString(); + } + + public static Lecture load(String filename) throws IOException { + Lecture result = null; + InputStream in = null; + try { + in = new FileInputStream(filename); + result = load(in); + } finally { + if (in != null) { + in.close(); + } + } + return result; + } + + public static Lecture load(InputStream in) throws IOException { + DataInputStream inDataStream = new DataInputStream(in); + Lecture result = new Lecture(inDataStream.readUTF(), inDataStream.readUTF(), inDataStream.readUTF(), inDataStream.readUTF()); + + int numberOfLectuerers = inDataStream.readInt(); + for (int i = 0; i < numberOfLectuerers; i++) { + Lecturer lecturer = new Lecturer(); + lecturer.load(inDataStream); + result.addLecturers(lecturer); + } + + int numberOfDates = inDataStream.readInt(); + for (int k = 0; k < numberOfDates; k++) { + Date date = new Date(); + date.load(inDataStream); + result.addSchedule(date); + } + return result; + } + + //public static void saveText(String filename, Lecture data) throws IOException { + //TODO: implement this (task 2) + //} + + //public static Lecture loadText(String filename) throws IOException { + //TODO: implement this (task 2) + //} +} diff --git a/src/main/java/_8/Lecturer.java b/src/main/java/_8/Lecturer.java new file mode 100644 index 0000000..f97d3cb --- /dev/null +++ b/src/main/java/_8/Lecturer.java @@ -0,0 +1,51 @@ +package _8; + +import java.io.DataInputStream; +import java.io.IOException; + +public class Lecturer { + + private String firstName = ""; + private String lastName = ""; + + public Lecturer() { + super(); + } + + public Lecturer(String firstName, String lastName) { + super(); + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + result.append(firstName); + result.append(" "); + result.append(lastName); + return result.toString(); + } + + public void load(DataInputStream dis) throws IOException { + this.setFirstName(dis.readUTF()); + this.setLastName(dis.readUTF()); + } + +} diff --git a/src/main/java/provided/_8/LectureOMP.java b/src/main/java/provided/_8/LectureOMP.java new file mode 100644 index 0000000..a7637b7 --- /dev/null +++ b/src/main/java/provided/_8/LectureOMP.java @@ -0,0 +1,22 @@ +package provided._8; + +import _8.Lecture; +import java.io.IOException; + +public class LectureOMP { + + public static void main(String[] args) { + try { + // task 1 + Lecture omp = Lecture.load("omp.dat"); + System.out.println(omp); + // task 2 + //Lecture.saveText("omp.txt", omp); + //omp = Lecture.loadText("omp.txt"); + //System.out.println(omp); + } catch (IOException e) { + e.printStackTrace(); + } + } + +}