Skip to content

Commit ab99600

Browse files
committed
22.11.2016
1 parent ff9e930 commit ab99600

File tree

18 files changed

+849
-0
lines changed

18 files changed

+849
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.javarush.test.level23.lesson06.task01;
2+
3+
/* Как выбрать нужное?
4+
В методе main присвойте объекту Object obj экземпляр класса TEST
5+
Константу TEST и класс TEST менять нельзя.
6+
*/
7+
public class Solution {
8+
public static final String TEST = "test";
9+
10+
public static class TEST {
11+
@Override
12+
public String toString() {
13+
return "test class";
14+
}
15+
}
16+
17+
static Object obj;
18+
19+
public static void main(String[] args) {
20+
obj = new Solution.TEST();
21+
System.out.println(obj);
22+
}
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.javarush.test.level23.lesson06.task02;
2+
3+
/* Рефакторинг
4+
Отрефакторите класс Solution: вынесите все константы в public вложенный(nested) класс Constants.
5+
Запретите наследоваться от Constants.
6+
*/
7+
public class Solution {
8+
9+
public class ServerNotAccessibleException extends Exception {
10+
public ServerNotAccessibleException() {
11+
super(Constants.U_IS_NOT_ACCESSIBLE_NOW);
12+
}
13+
14+
public ServerNotAccessibleException(Throwable cause) {
15+
super(Constants.U_IS_NOT_ACCESSIBLE_NOW, cause);
16+
}
17+
}
18+
19+
public class UnauthorizedUserException extends Exception {
20+
public UnauthorizedUserException() {
21+
super(Constants.U_IS_NOT_AUTHORIZED);
22+
}
23+
24+
public UnauthorizedUserException(Throwable cause) {
25+
super(Constants.U_IS_NOT_AUTHORIZED, cause);
26+
}
27+
}
28+
29+
public class BannedUserException extends Exception {
30+
public BannedUserException() {
31+
super(Constants.U_IS_BANNED);
32+
}
33+
34+
public BannedUserException(Throwable cause) {
35+
super(Constants.U_IS_BANNED, cause);
36+
}
37+
}
38+
39+
public class RestrictionException extends Exception {
40+
public RestrictionException() {
41+
super(Constants.A_IS_DENIED);
42+
}
43+
44+
public RestrictionException(Throwable cause) {
45+
super(Constants.A_IS_DENIED, cause);
46+
}
47+
}
48+
49+
public final static class Constants {
50+
static final String A_IS_DENIED = "Access is denied.";
51+
static final String U_IS_BANNED = "User is banned.";
52+
static final String U_IS_NOT_AUTHORIZED = "User is not authorized.";
53+
static final String U_IS_NOT_ACCESSIBLE_NOW = "Server is not accessible for now.";
54+
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.javarush.test.level23.lesson08.home01;
2+
3+
import com.javarush.test.level23.lesson08.home01.vo.NamedItem;
4+
5+
import java.lang.reflect.ParameterizedType;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public abstract class AbstractDbSelectExecutor<T extends NamedItem> {
10+
11+
public abstract String getQuery();
12+
13+
/**
14+
* It's fake method
15+
*
16+
* @return a list of 5 fake items
17+
*/
18+
public List<T> execute() {
19+
List<T> result = new ArrayList<>();
20+
//assert the query is not null
21+
String query = getQuery();
22+
if (query == null) return result;
23+
24+
try {
25+
//generate 5 fake items
26+
for (int i = 1; i <= 5; i++) {
27+
T newItem = getNewInstanceOfGenericType();
28+
newItem.setId(i);
29+
newItem.setName(newItem.getClass().getSimpleName() + "-" + i);
30+
newItem.setDescription("Got by executing '" + query + "'");
31+
result.add(newItem);
32+
}
33+
} catch (InstantiationException | IllegalAccessException e) {
34+
e.printStackTrace();
35+
}
36+
return result;
37+
}
38+
39+
//reflection
40+
//you have to know that it is possible to create new instance of T (generic type) class by using its default constructor
41+
private T getNewInstanceOfGenericType() throws InstantiationException, IllegalAccessException {
42+
return (T) ((Class) ((ParameterizedType) this.getClass().
43+
getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
44+
}
45+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.javarush.test.level23.lesson08.home01;
2+
3+
import com.javarush.test.level23.lesson08.home01.vo.*;
4+
5+
import java.util.List;
6+
7+
/* Анонимность иногда так приятна!
8+
1. В пакете vo создайте public классы User, Location, Server, Subject, Subscription, которые наследуются от NamedItem
9+
2. В классе Solution для каждого класса создайте свой метод, который возвращает список экземпляров класса.
10+
Например, для класса User это будет - public List<User> getUsers()
11+
Для класса Location это будет - public List<Location> getLocations()
12+
3. Внутри каждого такого метода создайте анонимный класс от AbstractDbSelectExecutor и вызовите его нужный метод.
13+
Подсказка: тело метода должно начинаться так: return new AbstractDbSelectExecutor
14+
15+
4. Пример вывода для User и Location:
16+
Id=5, name='User-5', description=Got by executing 'select * from USER'
17+
Id=1, name='Location-1', description=Got by executing 'select * from LOCATION'
18+
19+
5. Проанализируйте пример вывода и сформируйте правильный query для всех классов.
20+
6. Классы не должны содержать закоментированного кода.
21+
*/
22+
public class Solution {
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
print(solution.getUsers());
26+
print(solution.getLocations());
27+
}
28+
29+
public static void print(List list) {
30+
String format = "Id=%d, name='%s', description=%s";
31+
for (Object obj : list) {
32+
NamedItem item = (NamedItem) obj;
33+
System.out.println(String.format(format, item.getId(), item.getName(), item.getDescription()));
34+
}
35+
}
36+
37+
public List<User> getUsers() {
38+
return new AbstractDbSelectExecutor<User>() {
39+
40+
@Override
41+
public String getQuery()
42+
{
43+
return "select * from USER";
44+
}
45+
}.execute();
46+
47+
}
48+
49+
public List<Location> getLocations() {
50+
return new AbstractDbSelectExecutor<Location>() {
51+
52+
@Override
53+
public String getQuery()
54+
{
55+
return "select * from LOCATION";
56+
}
57+
}.execute();
58+
}
59+
60+
public List<Server> getServers() {
61+
return new AbstractDbSelectExecutor<Server>() {
62+
63+
@Override
64+
public String getQuery()
65+
{
66+
return "select * from SERVER";
67+
}
68+
}.execute();
69+
}
70+
71+
public List<Subject> getSubjects() {
72+
return new AbstractDbSelectExecutor<Subject>() {
73+
74+
@Override
75+
public String getQuery()
76+
{
77+
return "select * from SUBJECT";
78+
}
79+
}.execute();
80+
}
81+
82+
public List<Subscription> getSubscriptions() {
83+
return new AbstractDbSelectExecutor<Subscription>() {
84+
85+
@Override
86+
public String getQuery()
87+
{
88+
return "select * from SUBSCRIPTION";
89+
}
90+
}.execute();
91+
}
92+
93+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.javarush.test.level23.lesson08.home01.vo;
2+
3+
/**
4+
* Created by ukr-sustavov on 22.11.2016.
5+
*/
6+
public class Location extends NamedItem
7+
{
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.javarush.test.level23.lesson08.home01.vo;
2+
3+
public class NamedItem {
4+
private int id;
5+
private String name;
6+
private String description;
7+
8+
public NamedItem() {
9+
}
10+
11+
public int getId() {
12+
return id;
13+
}
14+
15+
public void setId(int id) {
16+
this.id = id;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getDescription() {
28+
return description;
29+
}
30+
31+
public void setDescription(String description) {
32+
this.description = description;
33+
}
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.javarush.test.level23.lesson08.home01.vo;
2+
3+
/**
4+
* Created by ukr-sustavov on 22.11.2016.
5+
*/
6+
public class Server extends NamedItem
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.javarush.test.level23.lesson08.home01.vo;
2+
3+
/**
4+
* Created by ukr-sustavov on 22.11.2016.
5+
*/
6+
public class Subject extends NamedItem
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.javarush.test.level23.lesson08.home01.vo;
2+
3+
/**
4+
* Created by ukr-sustavov on 22.11.2016.
5+
*/
6+
public class Subscription extends NamedItem
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.javarush.test.level23.lesson08.home01.vo;
2+
3+
/**
4+
* Created by ukr-sustavov on 22.11.2016.
5+
*/
6+
public class User extends NamedItem
7+
{
8+
}

0 commit comments

Comments
 (0)