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

public enum Level {
INFO,
DEBUG,
WARNING,
ERROR
}
69 changes: 69 additions & 0 deletions task01/src/com/example/task01/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.task01;
import java.util.HashMap;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Date;

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

public Logger(String name) {
this.name = name;
loggers.put(name,this);
}
public String getName(){
return name;
}

public static Logger getLogger(String name){
if (!loggers.containsKey(name))
throw new IllegalArgumentException("There is no logger with that name");
return loggers.get(name);
}
public void setLevel(Level level){
this.level = level;
}
public Level getLevel(){
return level;
}

public void info(String message){
log(Level.INFO, message);
}
public void info(String format, Object... args){
log(Level.INFO, format, args);
}
public void debug(String message){
log(Level.DEBUG, message);
}
public void debug(String format, Object... args){
log(Level.DEBUG, format, args);
}
public void error(String message){
log(Level.ERROR, message);
}
public void error(String format, Object... args){
log(Level.ERROR, format, args);
}
public void warning(String message){
log(Level.WARNING, message);
}
public void warning(String format, Object... args){
log(Level.WARNING, format, args);
}
public void log(Level level, String message){
if (this.level == level){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String date = dateFormat.format(new Date());
String time = timeFormat.format(new Date());
System.out.println(String.format("[%s] %s %s %s - %s", level, date, time, name, message));;
}
}

public void log(Level level, String format, Object... args) {
log(level, String.format(format, args));
}
}
2 changes: 1 addition & 1 deletion task02/src/com/example/task02/Bill.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ private static class BillItem {
this.amount = amount;
}
}
}
}
20 changes: 20 additions & 0 deletions task02/src/com/example/task02/DiscountBill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.task02;

public class DiscountBill extends Bill
{
private final double discount;
public DiscountBill(double discount){
this.discount = discount;
}
public double getDiscount() {
return discount;
}
public double getDiscountAmount() {
return super.getPrice() * discount / 100;
}

@Override
public long getPrice() {
return Math.round(super.getPrice() * (100 - discount) / 100.0);
}
}
2 changes: 1 addition & 1 deletion task02/src/com/example/task02/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public long getPrice() {
public String toString() {
return String.format("[%s:%d]", name, price);
}
}
}
23 changes: 13 additions & 10 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package com.example.task02;

public class Task02Main {

private static final Item ITEM1 = new Item("Товар 1", 10);
private static final Item ITEM2 = new Item("Товар 2", 20);
private static final Item ITEM3 = new Item("Товар 3", 30);
private static final Item ITEM4 = new Item("Товар 4", 40);
private static final Item ITEM5 = new Item("Товар 5", 50);
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);
bill.add(ITEM2, 5);
bill.add(ITEM3, 2);
System.out.println("Базовый счет:\n\n" + bill);

DiscountBill discountBill = new DiscountBill(10);
discountBill.add(ITEM1, 10);
discountBill.add(ITEM2, 5);
discountBill.add(ITEM3, 2);
System.out.println("\nСчет со скидкой:\n" + discountBill);
System.out.println("\nРазмер скидки: " + discountBill.getDiscount() + "%");
System.out.println("Абсолютное значение скидки: " + discountBill.getDiscountAmount());
System.out.println("Итоговая стоимость: " + discountBill.getPrice());
}
}
}
30 changes: 30 additions & 0 deletions task03/src/com/example/task03/Hours.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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;
}
}
11 changes: 8 additions & 3 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 / 1000f);
}

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

@Override
public long toHours() {
return this.amount / 3600 / 1000 ;
}
}
21 changes: 11 additions & 10 deletions task03/src/com/example/task03/Minutes.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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 * 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);
}
}
}
8 changes: 6 additions & 2 deletions task03/src/com/example/task03/Seconds.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ 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();
}
42 changes: 41 additions & 1 deletion task03/src/com/example/task03/TimeUnitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,47 @@ public static Milliseconds toMillis(Seconds seconds) {
* @param millis интервал в миллисекундах
* @return интервал в секундах
*/
public static Milliseconds toMillis(Minutes minutes) {
return new Milliseconds(minutes.toMillis());
}

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

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 milliseconds){
return new Minutes(milliseconds.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 milliseconds){
return new Hours(milliseconds.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;

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

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

public class FileHandler implements MessageHandler {
@Override
public void log(String message) {
try (FileWriter writer = new FileWriter("log.txt", true)) {
writer.write(message + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
8 changes: 8 additions & 0 deletions task04/src/com/example/task04/Level.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.task04;

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