-
Notifications
You must be signed in to change notification settings - Fork 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
fix Parse reader write randomFAR #18
Open
FedorTvor
wants to merge
2
commits into
main
Choose a base branch
from
dev_fa_fixParse
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
Changes from all commits
Commits
Show all changes
2 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 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
This file contains 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 |
---|---|---|
@@ -1,27 +1,14 @@ | ||
import java.util.Map; | ||
|
||
public class ParseBook { | ||
public Book parse(String line) { | ||
String[] parts = line.substring(line.indexOf("{") + 1, line.indexOf("}")).split(","); | ||
String author = null; | ||
String title = null; | ||
Integer pages = null; | ||
private final ParserHelper parserHelper = new ParserHelper(); | ||
|
||
for (String part : parts) { | ||
String[] keyValue = part.split("="); | ||
String key = keyValue[0].trim(); | ||
String value = keyValue[1].trim().replaceAll("'", ""); | ||
public Book parse(String line) { | ||
Map<String, String> keyValueMap = parserHelper.parseKeyValuePairs(line); | ||
String author = keyValueMap.get("author"); | ||
String title = keyValueMap.get("title"); | ||
Integer pages = parserHelper.parseInteger(keyValueMap.get("pages")); | ||
|
||
if (key.contains("author")) { | ||
author = value; | ||
} else if (key.contains("title")) { | ||
title = value; | ||
} else if (key.contains("pages")) { | ||
try { | ||
pages = Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
return new Book.Builder().setAuthor(author).setTitle(title).setPages(pages).build(); | ||
return Book.createInstance(author,title,pages); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,30 +1,14 @@ | ||
import java.util.Map; | ||
|
||
public class ParseCar { | ||
public Car parse(String line) { | ||
String[] parts = line.substring(line.indexOf("{") + 1, line.indexOf("}")).split(","); | ||
Integer power = null; | ||
String model = null; | ||
Integer year = null; | ||
private final ParserHelper parserHelper = new ParserHelper(); | ||
|
||
for (String part : parts) { | ||
String[] keyValue = part.split("="); | ||
String key = keyValue[0].trim(); | ||
String value = keyValue[1].trim().replaceAll("'", ""); | ||
public Car parse(String line) { | ||
Map<String, String> keyValueMap = parserHelper.parseKeyValuePairs(line); | ||
Integer power = parserHelper.parseInteger(keyValueMap.get("power")); | ||
String model = keyValueMap.get("model"); | ||
Integer year = parserHelper.parseInteger(keyValueMap.get("yearOfProduction")); | ||
|
||
if (key.contains("power")) { | ||
try { | ||
power = Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
} | ||
} else if (key.contains("model")) { | ||
model = value; | ||
} else if (key.contains("yearOfProduction")) { | ||
try { | ||
year = Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
return new Car.BuildCAr().setPower(power).setModel(model).setYearOfProduction(year).build(); | ||
return Car.createInstance(power,model,year); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,27 +1,14 @@ | ||
import java.util.Map; | ||
|
||
public class ParseRootVegetable { | ||
public RootVegetable parse(String line) { | ||
String[] parts = line.substring(line.indexOf("{") + 1, line.indexOf("}")).split(","); | ||
String type = null; | ||
String colour = null; | ||
Integer weight = null; | ||
private final ParserHelper parserHelper = new ParserHelper(); | ||
|
||
for (String part : parts) { | ||
String[] keyValue = part.split("="); | ||
String key = keyValue[0].trim(); | ||
String value = keyValue[1].trim().replaceAll("'", ""); | ||
public RootVegetable parse(String line) { | ||
Map<String, String> keyValueMap = parserHelper.parseKeyValuePairs(line); | ||
String type = keyValueMap.get("type"); | ||
String colour = keyValueMap.get("colour"); | ||
Integer weight = parserHelper.parseInteger(keyValueMap.get("weight")); | ||
|
||
if (key.contains("type")) { | ||
type = value; | ||
} else if (key.contains("colour")) { | ||
colour = value; | ||
} else if (key.contains("weight")) { | ||
try { | ||
weight = Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
return new RootVegetable.Builder().setType(type).setColour(colour).setWeight(weight).build(); | ||
return RootVegetable.createInstance(type, colour, weight); | ||
} | ||
} |
This file contains 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,28 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class ParserHelper { | ||
|
||
public Map<String, String> parseKeyValuePairs(String line) { | ||
String content = line.substring(line.indexOf("{") + 1, line.indexOf("}")); | ||
Map<String, String> keyValueMap = new HashMap<>(); | ||
for (String part : content.split(",")) { | ||
String[] keyValue = part.split("="); | ||
if (keyValue.length != 2) { | ||
continue; | ||
} | ||
String key = keyValue[0].trim(); | ||
String value = keyValue[1].trim().replaceAll("'", ""); | ||
keyValueMap.put(key, value); | ||
} | ||
return keyValueMap; | ||
} | ||
|
||
public Integer parseInteger(String value) { | ||
try { | ||
return Integer.parseInt(value); | ||
} catch (NumberFormatException e) { | ||
System.out.println("Ошибка ввода: " + e); | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import java.io.File; | ||
import java.util.Scanner; | ||
|
||
public class RandomFillArrayAction implements MenuAction { | ||
|
@@ -23,57 +24,41 @@ public void execute(){ | |
String choice = scanner.next(); | ||
switch (choice) { | ||
case "1": | ||
do{ | ||
Storage<Car> storage= Storage.<Car>getInstance(Main.datatype.CAR); | ||
int start_index=findFirst(storage,size); | ||
Object[] objects = storage.getObjects(); | ||
ItemRandomizer<Car> carRandomizer = new CarRandomizer(); | ||
|
||
|
||
for (int i = start_index; i < objects.length; i++) { | ||
objects[i] = carRandomizer.generate(); | ||
} | ||
fillArray(scanner,size,Main.datatype.CAR,new CarRandomizer()); | ||
step = false; | ||
}while(false); | ||
break; | ||
case "2": | ||
System.out.println("Выбран Книга"); | ||
do{ | ||
//Book | ||
Storage<Book> storage= Storage.<Book>getInstance(Main.datatype.BOOK); | ||
int start_index=findFirst(storage,size); | ||
Object[] objects = storage.getObjects(); | ||
ItemRandomizer<Book> bookRandomizer = new BookRandomizer(); | ||
|
||
for (int i = start_index; i < objects.length; i++) { | ||
objects[i] = bookRandomizer.generate(); | ||
} | ||
fillArray(scanner,size,Main.datatype.BOOK,new BookRandomizer()); | ||
step = false; | ||
}while(false); | ||
break; | ||
case "3": | ||
System.out.println("Выбран Корнепллод"); | ||
do{ | ||
Storage<RootVegetable> storage= Storage.<RootVegetable>getInstance(Main.datatype.ROOTVEGETABLE); | ||
int start_index=findFirst(storage,size); | ||
Object[] objects = storage.getObjects(); | ||
|
||
ItemRandomizer<RootVegetable> rootVegetableRandomizer = new RootVegetableRandomizer(); | ||
|
||
for (int i = start_index; i < objects.length; i++) { | ||
objects[i] = rootVegetableRandomizer.generate(); | ||
} | ||
fillArray(scanner,size,Main.datatype.ROOTVEGETABLE,new RootVegetableRandomizer()); | ||
step = false; | ||
}while(false); | ||
break; | ||
case "0": | ||
step = false; | ||
break; | ||
} | ||
} | ||
|
||
System.out.println("выход"); | ||
} | ||
|
||
private <T> void fillArray(Scanner scanner, int size, Main.datatype datatype, ItemRandomizer<T> randomizer) { | ||
Storage<T> storage = Storage.<T>getInstance(datatype); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. отлично, ещё один джейн эйрек ) |
||
int start_index = findFirst(storage, size); | ||
Object[] objects = storage.getObjects(); | ||
for (int i = start_index; i < objects.length; i++) { | ||
objects[i] = randomizer.generate(); | ||
} | ||
String className = datatype.getClassName(); | ||
WriterFile writerFile = new WriterFile<>(new File(className + ".txt")); | ||
writerFile.createdFile(); | ||
writerFile.writeText(objects); | ||
} | ||
|
||
private int findFirst(Storage<? extends Object>storage, int size){ | ||
Object[] objects=storage.getObjects(); | ||
int startIndex = 0; | ||
|
This file contains 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
This file contains 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
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.
отступы по всему проекту - 4 символа, здесь явно больше, поправим разом по всему проекту, это я к слову