Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions task01/src/com/example/task01/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example.task01;


import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Logger {
private final String name;
private static final Map<String, Logger> loggers = new HashMap<>();

public enum Level{
DEBUG, INFO, WARNING, ERROR
}

private Level currentLevel = Level.DEBUG;

private Logger(String name){
this.name = name;
}

public String getName(){
return name;
}

public static Logger getLogger(String name){
if (!loggers.containsKey(name)){
loggers.put(name, new Logger(name));
}
return loggers.get(name);
}

public void setLevel(Level level){
this.currentLevel = level;
}

public Level getLevel(){
return currentLevel;
}

public void log(Level level, String template, Object... args){
if(level.ordinal() >= currentLevel.ordinal()){
String formattedMessage = MessageFormat.format(template, args);
printLog(level, formattedMessage);
}
}

private void printLog(Level level, String message){
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String date = now.format(dateFormatter);
String time = now.format(timeFormatter);

System.out.println(String.format("[%s] %s %s %s - %s", level, date, time, name, message));
}

public void debug(String template, Object... args){
log(Level.DEBUG, template, args);
}
public void debug(String message){
log(Level.DEBUG, message);
}
public void info(String template, Object... args){
log(Level.INFO, template, args);
}
public void info(String message){
log(Level.INFO, message);
}
public void warning(String template, Object... args){
log(Level.WARNING, template, args);
}
public void warning(String message){
log(Level.WARNING, message);
}
public void error(String template, Object... args){
log(Level.ERROR, template, args);
}
public void error(String message) {
log(Level.ERROR, message);
}
}
4 changes: 4 additions & 0 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class Task01Main {
public static void main(String[] args) {
Logger logger1 = Logger.getLogger("testLogger");
Logger logger2 = Logger.getLogger("testLogger");

logger1.log(Logger.Level.ERROR, "error complete");
System.out.println();
}
}
22 changes: 22 additions & 0 deletions task02/src/com/example/task02/DiscountBill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.task02;

public class DiscountBill extends Bill{
private long discount;

public DiscountBill(long discount){
this.discount = discount;
}

public String getDiscountPercent() {
return discount + "%";
}

public long getDiscountAmount(){
return super.getPrice() * discount / 100;
}

@Override
public long getPrice() {
return super.getPrice() - super.getPrice() * discount / 100;
}
}
8 changes: 8 additions & 0 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ public static void main(String[] args) {
System.out.println(bill);
bill.add(ITEM3, 3);
System.out.println(bill);

DiscountBill discountBill = new DiscountBill(15);
discountBill.add(ITEM1, 12);
discountBill.add(ITEM2, 5);
discountBill.add(ITEM4,5);
System.out.println(discountBill.getDiscountAmount()); // абсолютная разница
System.out.println(discountBill.getDiscountPercent()); // % скидки
System.out.println(discountBill);
}
}
29 changes: 29 additions & 0 deletions task03/src/com/example/task03/Hours.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.task03;

public class Hours implements TimeUnit{
private final long amount;

public Hours(long amount){
this.amount = amount;
}

@Override
public long toMillis(){
return amount * 3600 * 1000;
}

@Override
public long toSeconds(){
return amount * 3600;
}

@Override
public long toMinutes(){
return amount * 60;
}

@Override
public long toHours(){
return amount;
}
}
7 changes: 5 additions & 2 deletions task03/src/com/example/task03/Milliseconds.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ public long toMillis() {

@Override
public long toSeconds() {
return amount / 1000;
return Math.round(amount / 1000f);
}

@Override
public long toMinutes() {
return amount / 1000 * 60;
return Math.round(amount / (float) (1000 * 60));
}

@Override
public long toHours(){ return Math.round(amount / (float) (3600 * 1000)); }
}
19 changes: 11 additions & 8 deletions task03/src/com/example/task03/Minutes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

public class Minutes implements TimeUnit {

private final long amount;

public Minutes(long amount) {
// TODO: реализовать
throw new UnsupportedOperationException();
this.amount = amount;
}

@Override
public long toMillis() {
// TODO: реализовать
throw new UnsupportedOperationException();
return amount * 60 * 1000;
}

@Override
public long toSeconds() {
// TODO: реализовать
throw new UnsupportedOperationException();
return amount * 60;
}

@Override
public long toMinutes() {
// TODO: реализовать
throw new UnsupportedOperationException();
return amount;
}

@Override
public long toHours(){
return Math.round(amount / 60f);
}
}
5 changes: 4 additions & 1 deletion task03/src/com/example/task03/Seconds.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public long toSeconds() {

@Override
public long toMinutes() {
return Math.round(amount / 60);
return Math.round(amount / 60f);
}

@Override
public long toHours(){return Math.round( amount / 3600f); }
}
1 change: 1 addition & 0 deletions task03/src/com/example/task03/TimeUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public interface TimeUnit {
*/
long toMinutes();

long toHours();
}
22 changes: 22 additions & 0 deletions task03/src/com/example/task03/TimeUnitUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.task03;

import javax.jws.Oneway;

/**
* Класс, в котором собраны методы для работы с {@link TimeUnit}
*/
Expand All @@ -15,6 +17,12 @@ public static Milliseconds toMillis(Seconds seconds) {
return new Milliseconds(seconds.toMillis());
}

public static Milliseconds toMillis(Minutes minutes) { return new Milliseconds(minutes.toMillis()); }

public static Milliseconds toMillis(Hours hours) { return new Milliseconds(hours.toMillis()); }



/**
* Конвертирует интервал в миллисекундах в интервал в секундах
*
Expand All @@ -24,4 +32,18 @@ public static Milliseconds toMillis(Seconds seconds) {
public static Seconds toSeconds(Milliseconds millis) {
return new Seconds(millis.toSeconds());
}

public static Seconds toSeconds(Minutes minutes) {
return new Seconds(minutes.toSeconds());
}

public static Seconds toSeconds(Hours hours) {
return new Seconds(hours.toSeconds());
}

public static Hours toHours(Minutes minutes) { return new Hours(minutes.toHours()); }

public static Hours toHours(Seconds seconds) { return new Hours(seconds.toHours()); }

public static Hours toHours(Milliseconds millis){ return new Hours(millis.toHours());}
}
8 changes: 8 additions & 0 deletions task04/src/com/example/task04/ConsoleHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.task04;

public class ConsoleHandler implements MessageHandler{
@Override
public void log(String message) {
System.out.println(message);
}
}
5 changes: 5 additions & 0 deletions task04/src/com/example/task04/ErrorLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.task04;

public enum ErrorLevel {
DEBUG, INFO, WARNING, ERROR
}
24 changes: 24 additions & 0 deletions task04/src/com/example/task04/FileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.task04;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileHandler implements MessageHandler {
private final String Path;

public FileHandler(String path) {
Path = path;
}

@Override
public void log(String message) {
File file = new File(Path);

try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
fileOutputStream.write(message.getBytes());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
Loading