-
Notifications
You must be signed in to change notification settings - Fork 0
Home work1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GeorgyMironov2001
wants to merge
4
commits into
main
Choose a base branch
from
HomeWork1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Home work1 #1
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| plugins { | ||
| id 'java' | ||
| } | ||
|
|
||
| group 'org.example' | ||
| version '1.0-SNAPSHOT' | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| // https://mvnrepository.com/artifact/junit/junit | ||
| testImplementation group: 'junit', name: 'junit', version: '4.13.2' | ||
| } | ||
|
|
||
| test { | ||
| useJUnit() | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.sber.mironov.ClientType; | ||
|
|
||
|
|
||
| import ru.sber.mironov.Clients.Client; | ||
| import ru.sber.mironov.Clients.HoldingClient; | ||
| import ru.sber.mironov.Clients.IndividualClient; | ||
| import ru.sber.mironov.Clients.LegalEntityClient; | ||
| import ru.sber.mironov.MyJsonLibrary.MyJsonObject; | ||
|
|
||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
||
| public enum ClientType { | ||
| INDIVIDUAL { | ||
| public Client createClient(MyJsonObject client) { | ||
| return new IndividualClient(client); | ||
| } | ||
| }, | ||
| LEGAL_ENTITY { | ||
| public Client createClient(MyJsonObject client) { | ||
| return new LegalEntityClient(client); | ||
| } | ||
| }, | ||
| HOLDING { | ||
| public Client createClient(MyJsonObject client) { | ||
| return new HoldingClient(client); | ||
| } | ||
| }; | ||
|
|
||
| public abstract Client createClient(MyJsonObject client); | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package ru.sber.mironov.Clients; | ||
|
|
||
| import ru.sber.mironov.ClientType.ClientType; | ||
| import ru.sber.mironov.MyJsonLibrary.MyJsonObject; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| public class Client { | ||
| private final String name; | ||
| private final String inn; | ||
| private final ClientType type; | ||
|
|
||
| public Client(String name, String inn, ClientType type) { | ||
| this.name = name; | ||
| this.inn = inn; | ||
| this.type = type; | ||
| } | ||
|
|
||
| public Client(MyJsonObject client) { | ||
| this.name = (String) client.getString("name"); | ||
| this.inn = (String) client.getString("inn"); | ||
| this.type = ClientType.valueOf(client.getString("ClientType")); | ||
| } | ||
|
|
||
| public String toString() { | ||
| return name.toString() + " " + inn.toString() + " " + type; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getInn() { | ||
| return inn; | ||
| } | ||
|
|
||
| public ClientType getType() { | ||
| return type; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package ru.sber.mironov.Clients; | ||
|
|
||
|
|
||
| import ru.sber.mironov.MyJsonLibrary.MyJsonObject; | ||
|
|
||
| public class HoldingClient extends Client { | ||
| private final double level; | ||
|
|
||
| public HoldingClient(MyJsonObject client) { | ||
| super(client); | ||
| level = Double.parseDouble(client.getString("level")); | ||
| } | ||
|
|
||
| public double getLevel() { | ||
| return level; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/ru/sber/mironov/Clients/IndividualClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.sber.mironov.Clients; | ||
|
|
||
|
|
||
| import ru.sber.mironov.ClientType.ClientType; | ||
| import ru.sber.mironov.MyJsonLibrary.MyJsonObject; | ||
|
|
||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| public class IndividualClient extends Client { | ||
| private final int salary; | ||
|
|
||
| public IndividualClient(String name, String inn, ClientType type, int salary) { | ||
| super(name, inn, type); | ||
| this.salary = salary; | ||
| } | ||
|
|
||
| public IndividualClient(MyJsonObject client) { | ||
| super(client); | ||
| this.salary = Integer.parseInt(client.getString("salary")); | ||
| } | ||
|
|
||
| public int getSalary() { | ||
| return salary; | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/ru/sber/mironov/Clients/LegalEntityClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package ru.sber.mironov.Clients; | ||
|
|
||
|
|
||
| import ru.sber.mironov.ClientType.ClientType; | ||
| import ru.sber.mironov.MyJsonLibrary.MyJsonObject; | ||
|
|
||
| public class LegalEntityClient extends Client { | ||
|
|
||
| private final Boolean realLegal; | ||
|
|
||
| public LegalEntityClient(String name, String inn, ClientType type, Boolean realLegal) { | ||
| super(name, inn, type); | ||
| this.realLegal = realLegal; | ||
| } | ||
|
|
||
| public LegalEntityClient(MyJsonObject client) { | ||
| super(client); | ||
| realLegal = Boolean.parseBoolean(client.getString("realLegsl")); | ||
| } | ||
|
|
||
| public Boolean getRealLegal() { | ||
| return realLegal; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/ru/sber/mironov/ClientsJsonParser/ClientsJsonParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package ru.sber.mironov.ClientsJsonParser; | ||
|
|
||
|
|
||
| import ru.sber.mironov.ClientType.ClientType; | ||
| import ru.sber.mironov.Clients.Client; | ||
| import ru.sber.mironov.MyJsonLibrary.MyJsonArray; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class ClientsJsonParser { | ||
| public static ArrayList<Client> parseClientsJson(String filePath) throws IOException { | ||
| StringBuilder fileData = new StringBuilder(); | ||
|
|
||
| try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { | ||
| char[] buf = new char[1024]; | ||
| int numRead; | ||
| while ((numRead = reader.read(buf)) != -1) { | ||
| String readData = String.valueOf(buf, 0, numRead); | ||
| fileData.append(readData); | ||
| } | ||
| } | ||
| String content = fileData.toString(); | ||
| MyJsonArray clients = new MyJsonArray(content); | ||
|
|
||
| ArrayList<Client> people = new ArrayList<>(); | ||
| for (var o : clients) { | ||
| ClientType ct = ClientType.valueOf(o.getString("ClientType")); | ||
| people.add(ct.createClient(o)); | ||
| } | ||
| return people; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package ru.sber.mironov; | ||
|
|
||
| import ru.sber.mironov.ClientsJsonParser.ClientsJsonParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) throws IOException { | ||
| /*String[] a = {"1", "2", "3"}; | ||
| ArrayList<String> q = new ArrayList<>(List.of(a));*/ | ||
| String fileName = "src/main/resources/Clients.json"; | ||
| System.out.println(ClientsJsonParser.parseClientsJson(fileName)); | ||
|
|
||
| /*String s = "[\n" + | ||
| " {\n" + | ||
| " \"ClientType\": \"INDIVIDUAL\",\n" + | ||
| " \"inn\": \"1688\",\n" + | ||
| " \"name\": \"Alex\",\n" + | ||
| " \"salary\": \"100\"\n" + | ||
| " },\n" + | ||
| " {\n" + | ||
| " \"ClientType\": \"LEGAL_ENTITY\",\n" + | ||
| " \"name\": \"Vadim\",\n" + | ||
| " \"inn\": \"1432\"\n" + | ||
| " },\n" + | ||
| " {\n" + | ||
| " \"ClientType\": \"HOLDING\",\n" + | ||
| " \"name\": \"Gera\",\n" + | ||
| " \"inn\": \"2001\"\n" + | ||
| " }\n" + | ||
| "]"; | ||
| String str = "{ { { {"; | ||
| Pattern p = Pattern.compile("\\{(\\s.+)+?}"); | ||
| Matcher m = p.matcher(s); | ||
| while (m.find()) { | ||
| System.out.println(s.substring(m.start(), m.end())); | ||
| }*/ | ||
| /*var people = ClientsJsonParser.parseClientsJson((args[0])); | ||
| System.out.println(people);*/ | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
46 changes: 46 additions & 0 deletions
46
src/main/java/ru/sber/mironov/MyJsonLibrary/MyJsonArray.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package ru.sber.mironov.MyJsonLibrary; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.function.Consumer; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class MyJsonArray implements Iterable<MyJsonObject> { | ||
| ArrayList<MyJsonObject> jsonObjects; | ||
|
|
||
| public MyJsonArray(String content) { | ||
| ArrayList<MyJsonObject> clients = new ArrayList<>(); | ||
| for (var x : getSubstrings(content)) { | ||
| clients.add(new MyJsonObject(content.substring(x.get(0), x.get(1)))); | ||
| } | ||
| jsonObjects = clients; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<MyJsonObject> iterator() { | ||
| return jsonObjects.iterator(); | ||
| } | ||
|
|
||
| @Override | ||
| public void forEach(Consumer<? super MyJsonObject> action) { | ||
| jsonObjects.forEach(action); | ||
| } | ||
|
|
||
| private ArrayList<ArrayList<Integer>> getSubstrings(String content) { | ||
| ArrayList<ArrayList<Integer>> res = new ArrayList<>(); | ||
| int prev = 0; | ||
| int now = 0; | ||
| for (int i = 0; i < content.length(); i++) { | ||
| if (content.charAt(i) == '{') { | ||
| prev = i; | ||
| } else if (content.charAt(i) == '}') { | ||
| now = i + 1; | ||
| res.add(new ArrayList<>(List.of(prev, now))); | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/ru/sber/mironov/MyJsonLibrary/MyJsonObject.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package ru.sber.mironov.MyJsonLibrary; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class MyJsonObject { | ||
| private final Map<String, String> map; | ||
|
|
||
| public MyJsonObject(Map<String, String> map) { | ||
| this.map = map; | ||
| } | ||
|
|
||
| public MyJsonObject(String str) { | ||
| Map<String, String> newMap = new HashMap<>(); | ||
| String[] strings = str.split(","); | ||
| for (var s : strings) { | ||
| String[] splitString = s.replace("\s", "") | ||
| .replace("{", "") | ||
| .replace("}", "") | ||
| .replace("\"", "") | ||
| .replace(",", "") | ||
| .replaceAll("[ \\f\\n\\r\\t\\v]", "") | ||
| .split(":"); | ||
| newMap.put(splitString[0], splitString[1]); | ||
| } | ||
| map = newMap; | ||
| } | ||
|
|
||
| public String getString(String key) { | ||
| return map.get(key); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [ | ||
| { | ||
| "ClientType": "INDIVIDUAL", | ||
| "inn": "1688", | ||
| "name": "Alex", | ||
| "salary": 100 | ||
| }, | ||
| { | ||
| "ClientType": "LEGAL_ENTITY", | ||
| "name": "Vadim", | ||
| "inn": "1432", | ||
| "realLegal": false | ||
| }, | ||
| { | ||
| "ClientType": "HOLDING", | ||
| "name": "Gera", | ||
| "inn": "2001", | ||
| "level": 34.5 | ||
| } | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Парсить в map