diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..73bef707 Binary files /dev/null and b/.DS_Store differ diff --git a/task01/.DS_Store b/task01/.DS_Store new file mode 100644 index 00000000..43167b08 Binary files /dev/null and b/task01/.DS_Store differ diff --git a/task01/src/.DS_Store b/task01/src/.DS_Store new file mode 100644 index 00000000..03e1b353 Binary files /dev/null and b/task01/src/.DS_Store differ diff --git a/task01/src/com/.DS_Store b/task01/src/com/.DS_Store new file mode 100644 index 00000000..f85faa31 Binary files /dev/null and b/task01/src/com/.DS_Store differ diff --git a/task01/src/com/example/.DS_Store b/task01/src/com/example/.DS_Store new file mode 100644 index 00000000..e11ac252 Binary files /dev/null and b/task01/src/com/example/.DS_Store differ diff --git a/task01/src/com/example/task01/Logger.java b/task01/src/com/example/task01/Logger.java new file mode 100644 index 00000000..461ef89b --- /dev/null +++ b/task01/src/com/example/task01/Logger.java @@ -0,0 +1,129 @@ +package com.example.task01; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; + +public class Logger { + // Константы для сравнения "Важности" + public static final int DEBUG = 0; + public static final int INFO = 1; + public static final int WARNING = 2; + public static final int ERROR = 3; + + private final String name; + private int level = INFO; + private static Map storage = new HashMap<>(); + + private Logger(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } + + public static Logger getLogger(String name){ + if (storage.containsKey(name)){ + return storage.get(name); + } + else{ + Logger logger = new Logger(name); + storage.put(name, logger); + return logger; + } + } + + public void setLevel(int newLevel){ + if(newLevel >= DEBUG && newLevel <= ERROR){ + this.level = newLevel; + } + } + + public int getLevel(){ + return this.level; + } + + // Преобразование числового уровня в текстовый + private String getLevelName(int level) { + switch (level) { + case DEBUG: return "DEBUG"; + case INFO: return "INFO"; + case WARNING: return "WARNING"; + case ERROR: return "ERROR"; + default: return "UNKNOWN"; + } + } + + // Форматирование полного сообщения согласно заданию + private String formatLogMessage(int 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); + String levelName = getLevelName(level); + + return String.format("[%s] %s %s %s - %s", levelName, date, time, this.name, message); + } + + // Публичные методы log + public void log(int level, String message) { + if (level >= this.level) { + String formattedMessage = formatLogMessage(level, message); + System.out.println(formattedMessage); + } + } + + public void log(int level, String template, Object... args) { + if (level >= this.level) { + String formattedMessage = formatMessage(template, args); + log(level, formattedMessage); + } + } + + // Вспомогательный метод для форматирования + private String formatMessage(String template, Object... args) { + String result = template; + for (Object arg : args) { + result = result.replaceFirst("\\{\\}", String.valueOf(arg)); + } + return result; + } + + // Простые методы для уровней + public void debug(String message) { + log(DEBUG, message); + } + + public void info(String message) { + log(INFO, message); + } + + public void warning(String message) { + log(WARNING, message); + } + + public void error(String message) { + log(ERROR, message); + } + + // Методы с шаблонами для уровней + public void debug(String template, Object... args) { + log(DEBUG, template, args); + } + + public void info(String template, Object... args) { + log(INFO, template, args); + } + + public void warning(String template, Object... args) { + log(WARNING, template, args); + } + + public void error(String template, Object... args) { + log(ERROR, template, args); + } +} diff --git a/task02/.DS_Store b/task02/.DS_Store new file mode 100644 index 00000000..43167b08 Binary files /dev/null and b/task02/.DS_Store differ diff --git a/task02/src/.DS_Store b/task02/src/.DS_Store new file mode 100644 index 00000000..03e1b353 Binary files /dev/null and b/task02/src/.DS_Store differ diff --git a/task02/src/com/.DS_Store b/task02/src/com/.DS_Store new file mode 100644 index 00000000..f85faa31 Binary files /dev/null and b/task02/src/com/.DS_Store differ diff --git a/task02/src/com/example/.DS_Store b/task02/src/com/example/.DS_Store new file mode 100644 index 00000000..14c6f213 Binary files /dev/null and b/task02/src/com/example/.DS_Store differ diff --git a/task02/src/com/example/task02/DiscountBill.java b/task02/src/com/example/task02/DiscountBill.java new file mode 100644 index 00000000..36bf4882 --- /dev/null +++ b/task02/src/com/example/task02/DiscountBill.java @@ -0,0 +1,25 @@ +package com.example.task02; + +public class DiscountBill extends Bill { + private double discount; + + public DiscountBill(double discount){ + super(); + this.discount = discount; + } + + public long getPrice(){ + long price = super.getPrice(); + return (long)(price * (1 - discount / 100.0)); + } + + public double getDiscountPercent(){ + return discount; + } + + public long getDiscountAmount(){ + long price1 = super.getPrice(); + long price2 = getPrice(); + return price1 - price2; + } +} diff --git a/task03/.DS_Store b/task03/.DS_Store new file mode 100644 index 00000000..43167b08 Binary files /dev/null and b/task03/.DS_Store differ diff --git a/task03/src/.DS_Store b/task03/src/.DS_Store new file mode 100644 index 00000000..03e1b353 Binary files /dev/null and b/task03/src/.DS_Store differ diff --git a/task03/src/com/.DS_Store b/task03/src/com/.DS_Store new file mode 100644 index 00000000..f85faa31 Binary files /dev/null and b/task03/src/com/.DS_Store differ diff --git a/task03/src/com/example/task03/Hours.java b/task03/src/com/example/task03/Hours.java new file mode 100644 index 00000000..fbfb451c --- /dev/null +++ b/task03/src/com/example/task03/Hours.java @@ -0,0 +1,26 @@ +package com.example.task03; + +public class Hours implements TimeUnit{ + private final long amount; + + public Hours(long amount){ + this.amount = amount; + } + + public long toMillis(){ + return amount * 3600000; + } + + public long toSeconds(){ + return amount * 3600; + } + + public long toMinutes(){ + return amount * 60; + } + + public long toHours(){ + return amount; + } + +} diff --git a/task03/src/com/example/task03/Milliseconds.java b/task03/src/com/example/task03/Milliseconds.java index 5115bc7d..5d8857a6 100644 --- a/task03/src/com/example/task03/Milliseconds.java +++ b/task03/src/com/example/task03/Milliseconds.java @@ -18,11 +18,16 @@ public long toMillis() { @Override public long toSeconds() { - return amount / 1000; + return Math.round(amount / 1000.0); } @Override public long toMinutes() { - return amount / 1000 * 60; + return Math.round(amount / 60000.0); } + + public long toHours(){ + return Math.round(amount / (1000.0 * 60 * 60)); + } + } diff --git a/task03/src/com/example/task03/Minutes.java b/task03/src/com/example/task03/Minutes.java index d6fa0594..04048806 100644 --- a/task03/src/com/example/task03/Minutes.java +++ b/task03/src/com/example/task03/Minutes.java @@ -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 * 60000; } @Override public long toSeconds() { - // TODO: реализовать - throw new UnsupportedOperationException(); + return amount * 60; } @Override public long toMinutes() { - // TODO: реализовать - throw new UnsupportedOperationException(); + return amount; + } + + public long toHours(){ + return Math.round(amount / 60); } + } diff --git a/task03/src/com/example/task03/Seconds.java b/task03/src/com/example/task03/Seconds.java index ce6bc213..b2cdb74f 100644 --- a/task03/src/com/example/task03/Seconds.java +++ b/task03/src/com/example/task03/Seconds.java @@ -23,6 +23,11 @@ public long toSeconds() { @Override public long toMinutes() { - return Math.round(amount / 60); + return Math.round(amount / 60.0); } + + public long toHours(){ + return Math.round(amount/3600); + } + } diff --git a/task03/src/com/example/task03/TimeUnit.java b/task03/src/com/example/task03/TimeUnit.java index 3b123fb8..303a88b4 100644 --- a/task03/src/com/example/task03/TimeUnit.java +++ b/task03/src/com/example/task03/TimeUnit.java @@ -28,4 +28,6 @@ public interface TimeUnit { */ long toMinutes(); + long toHours(); + } diff --git a/task03/src/com/example/task03/TimeUnitUtils.java b/task03/src/com/example/task03/TimeUnitUtils.java index 790f8850..be5344ba 100644 --- a/task03/src/com/example/task03/TimeUnitUtils.java +++ b/task03/src/com/example/task03/TimeUnitUtils.java @@ -24,4 +24,12 @@ public static Milliseconds toMillis(Seconds seconds) { public static Seconds toSeconds(Milliseconds millis) { return new Seconds(millis.toSeconds()); } + + // Из миллисекунд в минуты + public static Minutes millToMinutes(Milliseconds millis){ + return new Minutes(millis.toMinutes()); + } + + // из секунд в минуты + } diff --git a/task04/src/com/example/task04/ConsoleHandler.java b/task04/src/com/example/task04/ConsoleHandler.java new file mode 100644 index 00000000..b2113687 --- /dev/null +++ b/task04/src/com/example/task04/ConsoleHandler.java @@ -0,0 +1,7 @@ +package com.example.task04; + +public class ConsoleHandler implements MessageHandler{ + public void handle(String message){ + System.out.println(message); + } +} diff --git a/task04/src/com/example/task04/FileHandler.java b/task04/src/com/example/task04/FileHandler.java new file mode 100644 index 00000000..1b96d284 --- /dev/null +++ b/task04/src/com/example/task04/FileHandler.java @@ -0,0 +1,20 @@ +package com.example.task04; + +import java.io.FileWriter; +import java.io.IOException; + +public class FileHandler implements MessageHandler { + private String fileName; + + public FileHandler(String fileName) { + this.fileName = fileName; + } + + public void handle(String message) { + try (FileWriter writer = new FileWriter(fileName, true)) { + writer.write(message + "\n"); // добавляем перенос строки + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/task04/src/com/example/task04/Logger.java b/task04/src/com/example/task04/Logger.java new file mode 100644 index 00000000..08d65c7c --- /dev/null +++ b/task04/src/com/example/task04/Logger.java @@ -0,0 +1,141 @@ +package com.example.task04; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Logger { + // Константы для сравнения "Важности" + public static final int DEBUG = 0; + public static final int INFO = 1; + public static final int WARNING = 2; + public static final int ERROR = 3; + + private final String name; + private int level = INFO; + private static Map storage = new HashMap<>(); // хранилище логгеров + + private List handlers = new ArrayList<>(); + + private Logger(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } + + public static Logger getLogger(String name){ + if (storage.containsKey(name)){ + return storage.get(name); + } + else{ + Logger logger = new Logger(name); + storage.put(name, logger); + return logger; + } + } + + public void setLevel(int newLevel){ + if(newLevel >= DEBUG && newLevel <= ERROR){ + this.level = newLevel; + } + } + + public int getLevel(){ + return this.level; + } + + // Преобразование числового уровня в текстовый + private String getLevelName(int level) { + switch (level) { + case DEBUG: return "DEBUG"; + case INFO: return "INFO"; + case WARNING: return "WARNING"; + case ERROR: return "ERROR"; + default: return "UNKNOWN"; + } + } + + // Форматирование полного сообщения согласно заданию + private String formatLogMessage(int 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); + String levelName = getLevelName(level); + + return String.format("[%s] %s %s %s - %s", levelName, date, time, this.name, message); + } + + // Публичные методы log + public void log(int level, String message) { + if (level >= this.level) { + String formattedMessage = formatLogMessage(level, message); + + for (MessageHandler handler : handlers){ + handler.handle(formattedMessage); + } + } + } + + public void log(int level, String template, Object... args) { + if (level >= this.level) { + String formattedMessage = formatMessage(template, args); + log(level, formattedMessage); + } + } + + // Вспомогательный метод для форматирования + private String formatMessage(String template, Object... args) { + String result = template; + for (Object arg : args) { + result = result.replaceFirst("\\{\\}", String.valueOf(arg)); + } + return result; + } + + // Простые методы для уровней + public void debug(String message) { + log(DEBUG, message); + } + + public void info(String message) { + log(INFO, message); + } + + public void warning(String message) { + log(WARNING, message); + } + + public void error(String message) { + log(ERROR, message); + } + + // Методы с шаблонами для уровней + public void debug(String template, Object... args) { + log(DEBUG, template, args); + } + + public void info(String template, Object... args) { + log(INFO, template, args); + } + + public void warning(String template, Object... args) { + log(WARNING, template, args); + } + + public void error(String template, Object... args) { + log(ERROR, template, args); + } + + public void addHandler(MessageHandler handler){ + handlers.add(handler); + } + +} diff --git a/task04/src/com/example/task04/MemoryHandler.java b/task04/src/com/example/task04/MemoryHandler.java new file mode 100644 index 00000000..199a704d --- /dev/null +++ b/task04/src/com/example/task04/MemoryHandler.java @@ -0,0 +1,30 @@ +package com.example.task04; + +import java.util.ArrayList; +import java.util.List; + +class MemoryHandler implements MessageHandler { + private List messages = new ArrayList<>(); + private MessageHandler target; + private int maxMessages; + + public MemoryHandler(MessageHandler target, int maxMessages) { + this.target = target; + this.maxMessages = maxMessages; + } + + public void handle(String message) { + messages.add(message); + + if (messages.size() >= maxMessages) { + flush(); // автоматическая отправка при заполнении + } + } + + public void flush() { + for (String message : messages) { + target.handle(message); + } + messages.clear(); // очищаем после отправки + } +} diff --git a/task04/src/com/example/task04/MessageHandler.java b/task04/src/com/example/task04/MessageHandler.java new file mode 100644 index 00000000..b55ac1b8 --- /dev/null +++ b/task04/src/com/example/task04/MessageHandler.java @@ -0,0 +1,5 @@ +package com.example.task04; + +public interface MessageHandler { + void handle(String message); +} diff --git a/task04/src/com/example/task04/RotationFIleHandler.java b/task04/src/com/example/task04/RotationFIleHandler.java new file mode 100644 index 00000000..8d411f16 --- /dev/null +++ b/task04/src/com/example/task04/RotationFIleHandler.java @@ -0,0 +1,35 @@ +package com.example.task04; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +class RotationFileHandler implements MessageHandler { + private String fileName; + private String currentFile; + + public RotationFileHandler(String fileName) { + this.fileName = fileName; + this.currentFile = getCurrentFileName(); + } + + private String getCurrentFileName() { + String hour = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH")); + return fileName.replace(".", "_" + hour + "."); + } + + public void handle(String message) { + String newFile = getCurrentFileName(); + if (!newFile.equals(currentFile)) { + currentFile = newFile; // сменили файл при смене часа + } + + try (PrintWriter writer = new PrintWriter(new FileWriter(currentFile, true))) { + writer.println(message); + } catch (IOException e) { + // игнорируем ошибки + } + } +}