Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.
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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Antufiev Semen*

| Number | Solution | Short description
| --- | --- | --- |
| HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/master/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |

| HW1 | [Solution_1](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW2_1 | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_2/pyramid_printer) | The app that reads input arguments and prints pyramid|
| HW2_2 | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_2/traffic_light) | The app that reads input arguments and prints color of traffic light now|
| HW2_3 | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_2/random_chars_table) | The app that reads input arguments and prints table with random charset|
| HW3 | [Immutable_class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_3) | Example of immutable class
| HW4_1 | [Custon_anotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_4/custom_annotation) | Custom annotation and handler
| HW4_2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_4/singleton) | Example of singleton class
| HW4_3 | [Custom_file_reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_4/custom_file_reader) | Work with file readers
[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

[codingbat](https://codingbat.com/done?user=alexantufiev@gmail.com&tag=8698341536)
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ repositories {
}

dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'
}

test {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/homework_1/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
public class Main {

public static void main(String[] args) {
System.out.println("Hello homework!");
}
for (String arg : args) {
if (arg.equals("error")) {
System.out.println("\u001B[41m" + "Alarm!" + "\u001B[0m");
break;
}

System.out.println(arg + " : " + arg.length() + " letters");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
System.out.println(arg + " : " + arg.length() + " letters");
System.out.println(arg + ": " + arg.length() + " letters");

}
}
}
11 changes: 11 additions & 0 deletions src/main/java/homework_2/pyramid_printer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package homework_2.pyramid_printer;

public class Main {

public static void main(String[] args) {
PyramidPrinter pyramidPrinter = new PyramidPrinter();
pyramidPrinter.run();
}


}
30 changes: 30 additions & 0 deletions src/main/java/homework_2/pyramid_printer/PyramidPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package homework_2.pyramid_printer;

import java.util.Scanner;
import java.util.regex.Pattern;

public class PyramidPrinter {
Copy link
Owner

@NikolaevArtem NikolaevArtem Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


public static final String ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter";

public void run() {
Scanner scanner = new Scanner(System.in);
String sizeAsString = scanner.nextLine();

if (isNumber(sizeAsString)) {
System.out.println(ERROR_MESSAGE);
return;
}
Comment on lines +14 to +17
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If number, then error?

int size = Integer.parseInt(sizeAsString);
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we're printing x...

}
System.out.println();
}
}

private boolean isNumber(String str) {
return !Pattern.matches( "[0-9]+", str);
}
}
9 changes: 9 additions & 0 deletions src/main/java/homework_2/random_chars_table/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package homework_2.random_chars_table;

public class Main {

public static void main(String[] args) {
RandomCharsTable randomCharsTable = new RandomCharsTable();
randomCharsTable.run();
}
}
56 changes: 56 additions & 0 deletions src/main/java/homework_2/random_chars_table/RandomCharsTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package homework_2.random_chars_table;

import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;

public class RandomCharsTable {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

public static final String ERROR_MESSAGE = "Passed parameters should match the format [positive integer] [positive integer] [even|odd]";
public void run() {
Scanner scanner = new Scanner(System.in);
String[] params = new String[3];
for (int i = 0; i < params.length; i++) {
params[i] = scanner.next();
}
if (isNumber(params[0]) || isNumber(params[1])) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite strange

System.out.println(ERROR_MESSAGE);
return;
}
int rows = Integer.parseInt(params[0]);
int columns = Integer.parseInt(params[1]);
if (rows == 0 || columns == 0) {
return;
}
Comment on lines +21 to +23
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not expected behaviour, according to chat

if (rows < 0 || columns < 0) {
System.out.println(ERROR_MESSAGE);
return;
}
String strategy = params[2];
int strategyMod;
if (strategy.equals("even")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to a new separate method (getResult, for example)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because your code is too complex and difficult to read

strategyMod = 0;
} else if (strategy.equals("odd")){
strategyMod = 1;
} else {
System.out.println(ERROR_MESSAGE);
return;
}
StringBuilder result = new StringBuilder(strategy + " letters - ");
for (int i = 0; i < rows; i++) {
System.out.print("| ");
for (int j = 0; j < columns; j++) {
char letter = (char) (65 + new Random().nextInt(26));
System.out.print(letter + " | ");
if (letter % 2 == strategyMod) {
result.append(letter).append(", ");
}
}
System.out.println();
}
System.out.println(result.deleteCharAt(result.length() - 2));
}

private boolean isNumber(String str) {
return !Pattern.matches( "[0-9]+", str);
}
}
11 changes: 11 additions & 0 deletions src/main/java/homework_2/traffic_light/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package homework_2.traffic_light;

public class Main {

public static void main(String[] args) {
TrafficLight trafficLight = new TrafficLight();
trafficLight.run();
}


}
44 changes: 44 additions & 0 deletions src/main/java/homework_2/traffic_light/TrafficLight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package homework_2.traffic_light;

import java.time.Instant;
import java.util.Scanner;
import java.util.regex.Pattern;

public class TrafficLight {

public static final String ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter";
public static final String DAY_OVER = "The day is over";

public void run() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in other places: i would recommend getting rid of multiple if/else statements and separate logic to other methods

Scanner scanner = new Scanner(System.in);
String secondsAsString = scanner.next();
if (isNumber(secondsAsString)) {
System.out.println(ERROR_MESSAGE);
return;
}
long seconds = Long.parseLong(secondsAsString);
if (seconds < 0) {
System.out.println(ERROR_MESSAGE);
return;
}
if (seconds > 86399) {
System.out.println(DAY_OVER);
return;
}
Instant instant = Instant.ofEpochSecond(seconds);
String fullTime = instant.toString();
int size = fullTime.length();
int onlySeconds = Integer.parseInt(fullTime.substring(size - 3, size - 1));
if (onlySeconds >= 0 && onlySeconds < 35) {
System.out.println("Green");
} else if (onlySeconds >= 40 && onlySeconds < 55) {
System.out.println("Red");
} else {
System.out.println("Yellow");
}
}

private boolean isNumber(String str) {
return !Pattern.matches( "[0-9]+", str);
}
}
80 changes: 80 additions & 0 deletions src/main/java/homework_3/ImmutableClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package homework_3;


/*
The class must be declared as final
Data members in the class must be declared as private
Data members in the class must be declared as final
A parameterized constructor should initialize all the fields performing a deep copy
Deep Copy of objects should be performed in the getter methods
No setters
*/


import java.util.Objects;

public final class ImmutableClass {

private final int age;

private final int high;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new mutable class field

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it's a requirement


private final int weight;

private final String name;

public ImmutableClass(int age, int high, int weight, String name) {
this.age = age;
this.high = high;
this.weight = weight;
this.name = name;
}

public ImmutableClass(int weight, int high) {
this.high = high;
this.weight = weight;
this.age = 10;
this.name = "Some person";
}

public int getAge() {
return age;
}

public int getHigh() {
return high;
}

public int getWeight() {
return weight;
}

public String getName() {
return name;
}

public ImmutableClass changeWeightAndHigh(int weight, int high) {
return new ImmutableClass(weight, high);
}

public ImmutableClass changeName(String name) {
return new ImmutableClass(this.age, this.high, this.weight, name);
}

public ImmutableClass changeAge(int age) {
return new ImmutableClass(age, this.high, this.weight, this.name);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ImmutableClass)) return false;
ImmutableClass that = (ImmutableClass) o;
return age == that.age && high == that.high && weight == that.weight && Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(age, high, weight, name);
}
}
72 changes: 72 additions & 0 deletions src/main/java/homework_4/custom_annotation/DefaulValueImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package homework_4.custom_annotation;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class DefaulValueImpl {

public static void insetDefaultValue(Object object) throws IllegalAccessException, InvocationTargetException {
setDefaultValueToFields(object, object.getClass());
setDefaultValueToMethods(object, object.getClass());


}

private static void setDefaultValueToMethods(Object object, Class<?> clazz) throws InvocationTargetException, IllegalAccessException {
List<Method> collectPrivateMethods = Arrays.stream(clazz.getDeclaredMethods())
.filter((o) -> o.isAnnotationPresent(DefaultValue.class))
.collect(Collectors.toList());
setDefaultValueMethods(collectPrivateMethods, clazz, object);
}

private static void setDefaultValueToFields(Object object, Class<?> clazz) throws IllegalAccessException {
List<Field> collectPublic = Arrays.stream(clazz.getFields())
.filter((o) -> o.isAnnotationPresent(DefaultValue.class))
.collect(Collectors.toList());
getDefaultFields(collectPublic, clazz, object);

List<Field> collectPrivate = Arrays.stream(clazz.getDeclaredFields())
.filter((o) -> o.isAnnotationPresent(DefaultValue.class))
.collect(Collectors.toList());
getDefaultFields(collectPrivate, clazz, object);

}

private static void setDefaultValueMethods(List<Method> collectMethods, Class<?> clazz, Object object) throws InvocationTargetException, IllegalAccessException {
for (Method method : collectMethods) {
DefaultValue annotation = method.getAnnotation(DefaultValue.class);
String value = annotation.value();
Class<?> type = method.getParameterTypes()[0];
method.setAccessible(true);
if (type == Integer.class || type == int.class) {
method.invoke(object, Integer.parseInt(value));
} else if (type == Double.class || type == double.class) {
method.invoke(object, Double.parseDouble(value));
} else {
method.invoke(object, value);
}
}
}

private static void getDefaultFields(List<Field> collect, Class<?> clazz, Object object) throws IllegalAccessException {
for (Field field : collect) {
DefaultValue annotation = field.getAnnotation(DefaultValue.class);
String value = annotation.value();
Class<?> type = field.getType();
field.setAccessible(true);
if (type == Integer.class || type == int.class) {
if (field.get(object) == null || field.get(object).equals(0))
field.set(object, Integer.parseInt(value));
} else if (type == Double.class || type == double.class) {
if (field.get(object) == null || field.get(object).equals(0.0))
field.set(object, Double.parseDouble(value));
} else {
if (field.get(object) == null || field.get(object).equals("")) field.set(object, value);
}
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/homework_4/custom_annotation/DefaultValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package homework_4.custom_annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface DefaultValue {
String value();

}
Loading