Skip to content

Instantly share code, notes, and snippets.

@PShuvaev
Created April 10, 2015 10:08
Show Gist options
  • Save PShuvaev/7f76ecd2fc59db6ba526 to your computer and use it in GitHub Desktop.
Save PShuvaev/7f76ecd2fc59db6ba526 to your computer and use it in GitHub Desktop.
package com.epam.shuvaev_pavel.java.lesson3.task3;
import java.util.*;
import java.time.*;
import java.time.temporal.*;
import java.util.function.*;
import java.util.stream.*;
import java.time.format.*;
class TrainingCenter {
public static final int StartingAt = 10;
public static final int EndingAt = 18;
public static final int StudyHours = EndingAt-StartingAt;
}
class Student {
private String fullName;
private String curriculum;
private LocalDateTime startDate;
private List<Course> courseList;
public Student(String fullName, String curriculum, LocalDateTime startDate, List<Course> courseList){
setFullName(fullName);
setCurriculum(curriculum);
setStartDate(startDate);
setCourseList(courseList);
}
public String getFullName(){
return fullName;
}
public void setFullName(String fullName){
this.fullName = fullName;
}
public String getCurriculum(){
return curriculum;
}
public void setCurriculum(String curriculum){
this.curriculum = curriculum;
}
public LocalDateTime getStartDate(){
return startDate;
}
public void setStartDate(LocalDateTime startDate){
int hour = startDate.getHour();
if(hour < TrainingCenter.StartingAt || hour > TrainingCenter.EndingAt){
throw new RuntimeException("Start training after school!");
}
this.startDate = startDate;
}
public List<Course> getCourseList(){
return courseList;
}
public void setCourseList(List<Course> courseList){
this.courseList = courseList != null
? courseList
: new ArrayList<Course>();
}
}
class Course {
private String name;
private int duration;
public Course(String name, int duration){
setName(name);
setDuration(duration);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getDuration(){
return duration;
}
public void setDuration(int duration){
this.duration = duration;
}
}
public class StatusOfStudyCourses {
private List<Student> studentList;
private DateTimeFormatter DateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
private StatusOfStudyCourses(List<Student> studentList){
this.studentList = studentList != null ? studentList : new ArrayList<Student>();
}
private int timeLeft(Student student, LocalDateTime currDate){
LocalDateTime startDate = student.getStartDate();
boolean daysIsMatched = startDate.withHour(0).compareTo(currDate.withHour(0)) == 0;
return
// learning time for first day
startDate.getHour() - TrainingCenter.StartingAt
// learning time for period
+ (int)ChronoUnit.DAYS.between(
startDate.plusDays(daysIsMatched ? 0 : 1).withHour(TrainingCenter.StartingAt),
currDate.withHour(TrainingCenter.StartingAt)) * TrainingCenter.StudyHours
// learning time for current day
+ currDate.getHour() - TrainingCenter.StartingAt;
}
private String learningInfo(Student student, LocalDateTime currDate, boolean extendedOutput){
if(student.getStartDate().compareTo(currDate) > 0){
throw new RuntimeException(String.format("Danger! %s is a time traveler!", student.getFullName()));
}
int totalLearningHours = student.getCourseList().stream().mapToInt(x -> x.getDuration()).sum();
LocalDateTime endLearningTime = student.getStartDate().plusHours(totalLearningHours);
int remainingLearningTime = totalLearningHours - timeLeft(student, currDate);
String info = String.format("%s %s(%s) %s%s-",
student.getFullName(),
extendedOutput
? String.format("Рабочее время (с %d до %d) ", TrainingCenter.StartingAt, TrainingCenter.EndingAt)
: "",
student.getCurriculum(),
extendedOutput
? String.format("Длительность программы (%d ч) ", totalLearningHours)
: "",
extendedOutput
? String.format("Дата старта (%s) Дата завершения (%s) ",
DateFormatter.format(student.getStartDate()),
DateFormatter.format(endLearningTime))
: "");
// format day if it is not zero
Function<Integer, String> formatDay = day -> day == 0 ? "" : String.format("%d д ", day);
if(remainingLearningTime > 0){
return String.format("%s Обучение не закончено. До окончания осталось %s%d ч.",
info,
formatDay.apply(remainingLearningTime / TrainingCenter.StudyHours),
remainingLearningTime % TrainingCenter.StudyHours);
} else {
int elapsedHours = (int)ChronoUnit.HOURS.between(endLearningTime, currDate);
return String.format("%s Обучение закончено. После окончания прошло %s%d ч.",
info,
formatDay.apply(elapsedHours / 24), elapsedHours % 24);
}
}
public void print(boolean extendedOutput){
LocalDateTime date = LocalDateTime.now();
for(Student student : studentList){
System.out.println(learningInfo(student, date, extendedOutput));
}
}
public static void main(String[] args){
List<Student> studentList = Arrays.asList(
new Student("Ivanov Ivan", "J2EE Developer", LocalDateTime.of(2015, Month.APRIL, 9, 10, 0),
Arrays.asList(
new Course("Технология Java Servlets", 16),
new Course("Struts Framework", 24))),
new Student("Petrov Petr", "Java Developer", LocalDateTime.of(2015, Month.APRIL, 9, 10, 0),
Arrays.asList(
new Course("Обзор технологий Java", 8),
new Course("Библиотека JFC/Swing", 16),
new Course("Технология JDBC", 16))));
boolean extendedOutput = args.length > 0 && args[0].equals("extendedOutput");
new StatusOfStudyCourses(studentList).print(extendedOutput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment