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
100 changes: 100 additions & 0 deletions task01/src/com/example/task01/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.example.task01;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.time.LocalDateTime;

public class Logger {
private static final ArrayList<Logger> loggers = new ArrayList<>();
private final String name;
private LogLevel level;

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

public Logger(String name) {
this.name = name;
this.level = LogLevel.DEBUG;
loggers.add(this);
}

public static Logger getLogger(String name) {
for (Logger logger : loggers) {
if (logger.getName().equals(name)) {
return logger;
}
}
Logger newLogger = new Logger(name);
loggers.add(newLogger);
return newLogger;
}

public String getName(){
return name;
}

public LogLevel getLevel(){
return level;
}

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

public void debug(String message) {
log(LogLevel.DEBUG, message);
}

public void debug(String format, Object... args) {
log(LogLevel.DEBUG, format, args);
}

public void info(String message) {
log(LogLevel.INFO, message);
}

public void info(String format, Object... args) {
log(LogLevel.INFO, format, args);
}

public void warning(String message) {
log(LogLevel.WARNING, message);
}

public void warning(String format, Object... args) {
log(LogLevel.WARNING, format, args);
}

public void error(String message) {
log(LogLevel.ERROR, message);
}

public void error(String format, Object... args) {
log(LogLevel.ERROR, format, args);
}

public void log(LogLevel level, String message) {
if (level.ordinal() >= this.level.ordinal()) {
String formattedMessage = formatMessage(level, message);
System.out.println(formattedMessage);
}
}

public void log(LogLevel level, String format, Object... args) {
if (level.ordinal() >= this.level.ordinal()) {
String formattedMessage = formatMessage(level, String.format(format, args));
System.out.println(formattedMessage);
}
}

private String formatMessage(LogLevel level, String message) {
LocalDateTime now = LocalDateTime.now();
String date = now.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
String time = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
return String.format("[%s] %s %s %s - %s", level, date, time, name, message);
}
}
19 changes: 18 additions & 1 deletion task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

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

System.out.println("logger1 == logger2: " + (logger1 == logger2));

logger1.debug("Debug message");
logger1.info("Info message");
logger1.warning("Warning message");
logger1.error("Error message");

logger1.setLevel(Logger.LogLevel.WARNING);

logger1.debug("This debug message should not be printed");
logger1.info("This info message should not be printed");
logger1.warning("This warning message should be printed");
logger1.error("This error message should be printed");

logger1.error("Formatted error: %d + %d = %d", 2, 3, 5);
}
}
}
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 final int discount;

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

public int getDiscount() {
return discount;
}

@Override
public long getPrice() {
return (long)(super.getPrice() - super.getPrice() * (discount / 100.0));
}

public long getAbsoluteDiscountAmount (){
return super.getPrice() - getPrice();
}
}
15 changes: 8 additions & 7 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public class Task02Main {
private static final Item ITEM6 = new Item("Товар 6", 60);

public static void main(String[] args) {
Bill bill = new Bill();
bill.add(ITEM1, 10);
bill.add(ITEM3, 3);
bill.add(ITEM6, 1);
System.out.println(bill);
bill.add(ITEM3, 3);
System.out.println(bill);
DiscountBill discountBill = new DiscountBill(20); // Скидка 10%
discountBill.add(ITEM2,24 );
discountBill.add(ITEM5, 12);
discountBill.add(ITEM6, 10);
System.out.println(discountBill);

System.out.println("Размер скидки: " + discountBill.getDiscount() + "%");
System.out.println("Абсолютное значение скидки: " + discountBill.getAbsoluteDiscountAmount());
}
}
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 * 3600000;
}

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

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

@Override
public long toHours() {
return amount;
}
}
10 changes: 8 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,17 @@ 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 / (1000 * 60f));
}

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

}
18 changes: 10 additions & 8 deletions task03/src/com/example/task03/Minutes.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package com.example.task03;

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

@Override
public long toHours(){
return Math.round(amount / 60f);
}
}
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 / 60f);
}

@Override
public long toHours(){
return Math.round(amount / 3600f);
}
}
1 change: 1 addition & 0 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ private static void printTimeUnit(TimeUnit unit) {
System.out.println(String.format("Milliseconds: %d", unit.toMillis()));
System.out.println(String.format("Seconds: %d", unit.toSeconds()));
System.out.println(String.format("Minutes: %d", unit.toMinutes()));
System.out.println(String.format("Hours: %d", unit.toHours()));
}
}
7 changes: 7 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,11 @@ public interface TimeUnit {
*/
long toMinutes();

/**
* Возвращает продолжительность текущего интервала, пересчитанного в часах.
* При необходимости округлять по обычным правилам округления (число, меньшее 0.5 переходит в 0, большее или равное - в 1)
*
* @return количество часов в текущем интервале
*/
long toHours();
}
40 changes: 40 additions & 0 deletions task03/src/com/example/task03/TimeUnitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ 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,36 @@ 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 Minutes toMinutes(Milliseconds millis) {
return new Minutes(millis.toMinutes());
}

public static Minutes toMinutes(Seconds seconds) {
return new Minutes(seconds.toMinutes());
}

public static Minutes toMinutes(Hours hours) {
return new Minutes(hours.toMinutes());
}

public static Hours toHours(Milliseconds millis) {
return new Hours(millis.toHours());
}

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

public static Hours toHours(Minutes minutes) {
return new Hours(minutes.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;

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

import java.io.FileWriter;
import java.io.IOException;

public class FileHandler implements MessageHandler {
private final String fileName;

public FileHandler(String filePath) {
this.fileName = filePath;
}

@Override
public void handle(String message) {
try (FileWriter writer = new FileWriter(fileName, true)) {
writer.write(message + '\n');
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
Loading