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
Binary file added .DS_Store
Binary file not shown.
Binary file added task01/.DS_Store
Binary file not shown.
Binary file added task01/src/.DS_Store
Binary file not shown.
Binary file added task01/src/com/.DS_Store
Binary file not shown.
Binary file added task01/src/com/example/.DS_Store
Binary file not shown.
129 changes: 129 additions & 0 deletions task01/src/com/example/task01/Logger.java
Original file line number Diff line number Diff line change
@@ -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<String, Logger> 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);
}
}
Binary file added task02/.DS_Store
Binary file not shown.
Binary file added task02/src/.DS_Store
Binary file not shown.
Binary file added task02/src/com/.DS_Store
Binary file not shown.
Binary file added task02/src/com/example/.DS_Store
Binary file not shown.
25 changes: 25 additions & 0 deletions task02/src/com/example/task02/DiscountBill.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Binary file added task03/.DS_Store
Binary file not shown.
Binary file added task03/src/.DS_Store
Binary file not shown.
Binary file added task03/src/com/.DS_Store
Binary file not shown.
26 changes: 26 additions & 0 deletions task03/src/com/example/task03/Hours.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
9 changes: 7 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,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));
}

}
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 * 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);
}

}
7 changes: 6 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,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);
}

}
2 changes: 2 additions & 0 deletions task03/src/com/example/task03/TimeUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface TimeUnit {
*/
long toMinutes();

long toHours();

}
8 changes: 8 additions & 0 deletions task03/src/com/example/task03/TimeUnitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

// из секунд в минуты

}
7 changes: 7 additions & 0 deletions task04/src/com/example/task04/ConsoleHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.task04;

public class ConsoleHandler implements MessageHandler{
public void handle(String message){
System.out.println(message);
}
}
20 changes: 20 additions & 0 deletions task04/src/com/example/task04/FileHandler.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Loading