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
5 changes: 5 additions & 0 deletions src/main/java/AuthorOfBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AuthorOfBook extends ValueGetter<Book, java.lang.String>{
public java.lang.String get(Book book){
return book.getAuthor();
}
}
2 changes: 0 additions & 2 deletions src/main/java/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import java.util.Optional;
import java.util.Comparator;
import java.util.List;
import java.lang.String;
import java.util.LinkedList;
import java.util.Collection;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/Book.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public class Book {
private String author;
private String title;
private Integer pages;
private static Integer pages;

public Book() {
}
Expand All @@ -20,7 +20,10 @@ public String getTitle() {
return title;
}

public int getPages() {
public Integer getPages() {
return pages;
}
public static Integer getPages(Object o) {
return pages;
}

Expand Down
32 changes: 14 additions & 18 deletions src/main/java/BookRandomizer.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import java.util.Random;

public class BookRandomizer implements ItemRandomizer<Book> {
private static final String[] AUTHORS = {"Jane Austen", "Harper Lee", "Scott Fitzgerald", "Gabriel Garcia", "Truman Capote"};
private static final String[] TITLES = {"Pride and Prejudice", "To Kill a Mockingbird", "The Great Gatsby", "One Hundred Years of Solitude", "In Cold Blood"};
private static final Integer MIN_PAGES = 300;
private static final Integer MAX_PAGES = 1500;
private static final Random RANDOM = new Random();
private final String[] authors = {"Jane Austen", "Harper Lee", "Scott Fitzgerald", "Gabriel Garcia", "Truman Capote"};
private final String[] titles = {"Pride and Prejudice", "To Kill a Mockingbird", "The Great Gatsby", "One Hundred Years of Solitude", "In Cold Blood"};
private final Integer minPages = 300;
private final Integer maxPages = 1500;
private final Random random = new Random();

@Override
public Book generate(){
String author = getRandomAuthor();
String title = getRandomTitle();
Integer pages = getRandomPages();

return new Book.Builder()
.setAuthor(author)
.setTitle(title)
.setPages(pages)
.build();
return Book.createInstance(author,title,pages);
}

private static String getRandomAuthor() {
int index = RANDOM.nextInt(AUTHORS.length);
return AUTHORS[index];
private String getRandomAuthor() {
int index = random.nextInt(authors.length);
return authors[index];
}

private static String getRandomTitle() {
int index = RANDOM.nextInt(TITLES.length);
return TITLES[index];
private String getRandomTitle() {
int index = random.nextInt(titles.length);
return titles[index];
}

private static Integer getRandomPages() {
return MIN_PAGES + RANDOM.nextInt(MAX_PAGES - MIN_PAGES + 1);
private Integer getRandomPages() {
return minPages + random.nextInt(maxPages - minPages + 1);
}
}
7 changes: 6 additions & 1 deletion src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
public class Car {
private Integer power;
private static Integer power;
private String model;
private Integer yearOfProduction;

public Car() {
}


public Integer getPower() {
return power;
}
public static Integer getPower(Object o) {
return power;
}

public String getModel() {
return model;
Expand All @@ -28,6 +32,7 @@ public String toString() {
'}';
}


public static class BuildCAr{
private Integer power;
private String model;
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/CarRandomizer.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import java.util.Random;

public class CarRandomizer implements ItemRandomizer<Car>{
private static final String[] MODELS = {"Ford Bronco", "Haval Jolion", "Audi A3", "Mercedes-Benz V-Class", "Lexus LS"};
private static final Integer MIN_POWER = 100;
private static final Integer MAX_POWER = 400;
private static final Integer MIN_YEAR = 2000;
private static final Integer MAX_YEAR = 2025;
private static final Random RANDOM = new Random();
private final String[] models = {"Ford Bronco", "Haval Jolion", "Audi A3", "Mercedes-Benz V-Class", "Lexus LS"};
private final Integer minPower = 100;
private final Integer maxPower = 400;
private final Integer minYear = 2000;
private final Integer maxYear = 2025;
private final Random random = new Random();

@Override
public Car generate() {
Integer power = getRandomPower();
String model = getRandomModel();
Integer year = getRandomYear();

return new Car.BuildCAr()
.setPower(power)
.setModel(model)
.setYearOfProduction(year)
.build();
return Car.createInstance(power,model,year);
}

private static Integer getRandomPower() {
return MIN_POWER + RANDOM.nextInt(MAX_POWER - MIN_POWER + 1);
private Integer getRandomPower() {
return minPower + random.nextInt(maxPower - minPower + 1);
}

private static String getRandomModel() {
int index = RANDOM.nextInt(MODELS.length);
return MODELS[index];
private String getRandomModel() {
int index = random.nextInt(models.length);
return models[index];
}

private static Integer getRandomYear() {
return MIN_YEAR + RANDOM.nextInt(MAX_YEAR - MIN_YEAR + 1);
private Integer getRandomYear() {
return minYear + random.nextInt(maxYear - minYear + 1);
}
}
4 changes: 2 additions & 2 deletions src/main/java/CheckHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public static Boolean carCheck(Integer power, String model, Integer year) {
}

public static Boolean bookCheck(String author, String title, Integer pages) {
return pages != null && !author.isEmpty() && !title.isEmpty() & pages > 0;
return pages != null && !author.isEmpty() && !title.isEmpty() && pages > 0;
}

public static Boolean rootVegetableCheck(String type, String colour, Integer weight) {
return weight != null && !type.isEmpty() && !colour.isEmpty() & weight > 0;
return weight != null && !type.isEmpty() && !colour.isEmpty() && weight > 0;
}
}
5 changes: 5 additions & 0 deletions src/main/java/ColourOfRV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ColourOfRV extends ValueGetter<RootVegetable, java.lang.String>{
public java.lang.String get(RootVegetable rv){
return rv.getColour();
}
}
18 changes: 18 additions & 0 deletions src/main/java/DataType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class DataType {
public static enum datatype{
CAR(Car.class),
BOOK(Book.class),
ROOTVEGETABLE(RootVegetable.class);
private final Class<?> clazz;

datatype(Class<?> clazz) {
this.clazz = clazz;
}
public Class<?> getClazz() {
return clazz;
}
public String getClassName() {
return clazz.getSimpleName();
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/ExitAction.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
public class ExitAction implements MenuAction{
@Override
public void execute() {
public MenuAction execute() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

хочу обсудить

System.out.println("Выход из программы...");
System.exit(0);

}
return null;
}
}
29 changes: 9 additions & 20 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import java.util.Arrays;
import java.util.Scanner;


public class Main {
public static enum datatype{
CAR,
BOOK,
ROOTVEGETABLE
}
public static void main(String[] args) {
//Storage storage = Storage.getInstance();

Menu print_array_menu=new Menu();
print_array_menu.prelude="Выберите тип данных";
print_array_menu.addAction("1", "Автомобиль",new PrintArrayAction<Car>(datatype.CAR));
print_array_menu.addAction("2", "Книга",new PrintArrayAction<Book>(datatype.BOOK));
print_array_menu.addAction("3", "Корнепллод",new PrintArrayAction<RootVegetable>(datatype.ROOTVEGETABLE));
print_array_menu.addAction("0", "Отмена", null);

Menu menu = new Menu();
menu.reopen_after_submenu_closes=true;
menu.addAction("1", "Заполнение массива объектов вручную", new ManuallyFillArrayAction());
menu.addAction("2", "Создание массива объектов с рандомными значениями", new RandomFillArrayAction());
menu.addAction("3", "Вывод массива объектов на экран", print_array_menu);
menu.addAction("4", "Сортировка массива", null);//TODO
menu.addAction("5", "Выход", null);
menu.addAction("1", "Заполнение массива объектов из файла", new ReadFileArrayAction());
menu.addAction("2", "Заполнение массива объектов вручную", new ManuallyFillArrayAction());
menu.addAction("3", "Заполнение массива объектов рандомными значениями", new RandomFillArrayAction());
menu.addAction("4", "Вывод массива объектов на экран", new PrintType());
menu.addAction("5", "Сортировка", new SortAction());
menu.addAction("7", "Поиск элемента в массиве",new SearchHelper());
menu.addAction("0", "Выход", new ExitAction());

menu.execute();

}
}
63 changes: 61 additions & 2 deletions src/main/java/ManuallyFillArrayAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
public class ManuallyFillArrayAction implements MenuAction{
import java.io.File;
import java.util.Scanner;

public class ManuallyFillArrayAction implements MenuAction {

@Override
public void execute(){
public MenuAction execute() {
Scanner scanner = new Scanner(System.in);
boolean step = true;
while (step) {
System.out.println("Выберите тип данных для массива:");
System.out.println("1. Автомобиль");
System.out.println("2. Книга");
System.out.println("3. Корнепллод");
System.out.println("0. Вернуться в главное меню");
String choice = scanner.next();
switch (choice) {
case "1":
fillArray(scanner, DataType.datatype.CAR, new FillCarManually());
step = false;
break;
case "2":
fillArray(scanner, DataType.datatype.BOOK, new FillBookManually());
step = false;
break;
case "3":
fillArray(scanner, DataType.datatype.ROOTVEGETABLE, new FillRootVegetableManually());
step = false;
break;
case "0":
step = false;
break;
}
}
return null;
}

private <T> void fillArray(Scanner scanner, DataType.datatype datatype, Fill<T> filler) {
Storage<T> storage = Storage.<T>getInstance(datatype);
T[] array = filler.fill();
int start_index = findFirst(storage, array.length);
Object[] objects = storage.getObjects();
System.arraycopy(array, 0, objects, start_index, array.length);
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;
// Найти первое свободное место в массиве
while (startIndex < objects.length && objects[startIndex] != null) {
startIndex++;
}
// Если места нет увеличиваем размер массива
if (startIndex >= objects.length) {
storage.expandArray(size);
objects = storage.getObjects();
}
return startIndex;
}
}
46 changes: 3 additions & 43 deletions src/main/java/Menu.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Menu implements MenuAction{
Expand All @@ -26,31 +25,8 @@ public Menu() {
public void addAction(String key, String name, MenuAction action){
choices.put(key, new Pair<String, MenuAction>(name, action));
}

/*
public void display() {
while (true) {
System.out.println("Выберите действие:");
System.out.println("1. Заполнение массива объектов вручную");
System.out.println("2. Создание массива объектов с рандомными значениями");
System.out.println("3. Вывод массива объектов на экран");
System.out.println("4. Сортировка массива");
System.out.println("0. Выход");

int choice = getIntInput();
MenuAction action = actions.get(choice);
if (action != null) {
action.execute();
} else if (choice == 0) {
actions.get(0).execute();
} else {
System.out.println("Неверный выбор. Попробуйте снова.");
}
System.out.println();
}
}*/

public void execute(){
public MenuAction execute(){
String input;
do{
for (String k : choices.keySet()){
Expand All @@ -68,26 +44,10 @@ public void execute(){
System.out.println(wrong);
}
if (next.b==null){
return;
return null;
}
next.b.execute();
}while(reopen_after_submenu_closes);
return null;
}
/*
private int getIntInput() {
Scanner scanner = new Scanner(System.in);
int number;
while (true) {
System.out.print("Введите целое число: ");
if (scanner.hasNextInt()) {
number = scanner.nextInt();
break;
} else {
System.out.println("Ошибка: Введено не целое число. Попробуйте снова.");
scanner.next(); // Очистить некорректный ввод
}
}
return number;

}*/
}
2 changes: 1 addition & 1 deletion src/main/java/MenuAction.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public interface MenuAction {
void execute();
MenuAction execute();
}
Loading