Initial commit 8.1
parent
67280ac2cd
commit
90a7308e46
@ -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<String> 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<String> 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<Lecturer> lecturers = new ArrayList<>();
|
||||
private List<Date> 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<Lecturer> getLecturers() {
|
||||
return lecturers;
|
||||
}
|
||||
|
||||
public List<Date> 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)
|
||||
//}
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue