feature/add/array_input_logic #13
@ -6,41 +6,47 @@ public class Task {
|
|||||||
private String description;
|
private String description;
|
||||||
private boolean completed = false;
|
private boolean completed = false;
|
||||||
|
|
||||||
Task(int id, String title, String description){
|
Task(int id, String title, String description) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printInfo(){
|
public void printInfo() {
|
||||||
String stringCompleted = completed ? "[х]" : "[ ]";
|
String stringCompleted = completed ? "[х]" : "[ ]";
|
||||||
System.out.printf("%-3d | %s | %-20s\n", id, stringCompleted, title);
|
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printInfo(boolean full){
|
public void printInfo(boolean full) {
|
||||||
if (!full){
|
if (!full) {
|
||||||
printInfo();
|
printInfo();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String template = """
|
String template = """
|
||||||
|
|
||||||
|
──────────────────────────────
|
||||||
Номер: %-3d
|
Номер: %-3d
|
||||||
Статус: %s
|
Статус: %s
|
||||||
Название: %-20s
|
Название: %-20s
|
||||||
------------------------------
|
------------------------------
|
||||||
%s
|
%s
|
||||||
|
|
||||||
""";;
|
""";
|
||||||
String stringCompleted = completed ? "в процессе" : "завершена";
|
;
|
||||||
|
String stringCompleted = completed ? "выполнено" : "не выполнено";
|
||||||
System.out.printf(template, id, stringCompleted, title, description);
|
System.out.printf(template, id, stringCompleted, title, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void markAsCompleted(){
|
public void toggleCompleted() {
|
||||||
completed = true;
|
completed = !completed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId(){
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getCompleted() {
|
||||||
|
return completed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,123 +10,139 @@ public class TodoApp {
|
|||||||
private static Scanner scanner = new Scanner(System.in);
|
private static Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
mainMenu();
|
displayMainMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void mainMenu(){
|
public static int requestIntFromInput(String template, int[] allowedInts) {
|
||||||
String menu = """
|
do {
|
||||||
|
System.out.print(template);
|
||||||
|
int input;
|
||||||
|
do {
|
||||||
|
if (scanner.hasNextInt()) {
|
||||||
|
input = scanner.nextInt();
|
||||||
|
scanner.nextLine();
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
scanner.next();
|
||||||
|
System.out.print("Ошибка: используйте цифры.\nПовторите ввод:");
|
||||||
|
}
|
||||||
|
} while (true);
|
||||||
|
|
||||||
Введите номер пункта меню
|
for (int i : allowedInts)
|
||||||
-------------------------
|
if (i == input)
|
||||||
[1] Добавить дело
|
return input;
|
||||||
[2] Список дел
|
System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:");
|
||||||
|
} while (true);
|
||||||
[0] Выйти из программы
|
}
|
||||||
|
|
||||||
""";
|
|
||||||
|
|
||||||
|
public static void displayMainMenu() {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
String menu = """
|
||||||
|
|
||||||
System.err.printf(menu);
|
┌──────────────────────────┐
|
||||||
switch (scanner.nextLine().trim()) {
|
| [1] Добавить дело |
|
||||||
|
| [2] Список дел |
|
||||||
|
| |
|
||||||
|
| [0] Выйти из программы |
|
||||||
|
└──────────────────────────┘
|
||||||
|
|
||||||
case "1" -> createTask();
|
Введите номер пункта меню:""";
|
||||||
|
|
||||||
case "2" -> listTasks();
|
switch (requestIntFromInput(menu, new int[] { 0, 1, 2 })) {
|
||||||
|
case 1 -> displayCreateTask();
|
||||||
case "0" -> {
|
case 2 -> displayTasks();
|
||||||
|
case 0 -> {
|
||||||
scanner.close();
|
scanner.close();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
default -> {
|
|
||||||
System.out.println("Неизвестная команда. Используйте цифры.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createTask() {
|
private static void displayCreateTask() {
|
||||||
|
|
||||||
if (tasksCounter >= 9) {
|
if (tasksCounter >= 9) {
|
||||||
System.out.println("Ошибка: Достигнут лимит в 10 дел.");
|
System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int id = nextTaskId++;
|
int id = nextTaskId++;
|
||||||
|
|
||||||
String title;
|
String title;
|
||||||
System.out.println("Нпишите название дела (3–20 символов)");
|
String description;
|
||||||
while (true) {
|
|
||||||
|
do {
|
||||||
|
System.out.print("\nНпишите название дела (3–20 символов): ");
|
||||||
title = scanner.nextLine().trim();
|
title = scanner.nextLine().trim();
|
||||||
if (title.length() >= 3 && title.length() <= 20)
|
if (title.length() >= 3 && title.length() <= 20)
|
||||||
break;
|
break;
|
||||||
System.out.println("Ошибка: Название должно содержать от 3 до 20 символов. Попробуйте снова.");
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Нпишите описание дела");
|
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\nПопробуйте снова: ");
|
||||||
String description = scanner.nextLine().trim();
|
} while (true);
|
||||||
|
|
||||||
|
System.out.print("\nНпишите описание дела: ");
|
||||||
|
description = scanner.nextLine().trim();
|
||||||
|
|
||||||
var task = new Task(id, title, description);
|
var task = new Task(id, title, description);
|
||||||
tasks[tasksCounter++] = task;
|
tasks[tasksCounter++] = task;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void listTasks() {
|
private static void displayTasks() {
|
||||||
|
|
||||||
if(tasksCounter < 1){
|
if (tasksCounter < 1) {
|
||||||
System.out.println("""
|
int input = requestIntFromInput("""
|
||||||
|
|
||||||
Список дел пуст.
|
Список дел пуст.
|
||||||
|
|
||||||
[1] Добавить дело
|
[1] Добавить дело
|
||||||
[0] Выйти из программы
|
[0] Выйти из программы
|
||||||
|
|
||||||
""");
|
Введите номер пункта:""", new int[] { 0, 1 });
|
||||||
|
|
||||||
while (true) {
|
|
||||||
String input = scanner.nextLine().trim();
|
|
||||||
if(input.equals("1")) createTask();
|
|
||||||
if(input.equals("0")) System.exit(0);
|
|
||||||
System.out.println("Неизвестная команда. Используйте цифры.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (input == 1) {
|
||||||
|
displayCreateTask();
|
||||||
|
displayTasks();
|
||||||
|
} else
|
||||||
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("\nСписок дел:\n");
|
int[] tasksIDs = new int[tasksCounter + 1];
|
||||||
|
System.out.println("""
|
||||||
|
Список дел:
|
||||||
|
|
||||||
|
""");
|
||||||
for (int i = 0; i < tasksCounter; i++) {
|
for (int i = 0; i < tasksCounter; i++) {
|
||||||
tasks[i].printInfo();
|
tasks[i].printInfo();
|
||||||
|
tasksIDs[i] = tasks[i].getId();
|
||||||
}
|
}
|
||||||
|
tasksIDs[tasksCounter] = 0;
|
||||||
|
int input = requestIntFromInput("""
|
||||||
|
|
||||||
System.out.println("Введите номер дела, или 0 для возврата в главное меню:\n");
|
Введите номер дела или 0 для возврата в меню:""", tasksIDs);
|
||||||
|
|
||||||
while (true) {
|
if (input == 0)
|
||||||
String input = scanner.nextLine().trim();
|
return;
|
||||||
if(input.equals("0")) return;
|
|
||||||
|
|
||||||
if(input.matches("\\d+")){
|
displayTask(getTaskById(input));
|
||||||
/*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Неизвестная команда. Используйте цифры.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task getTaskById(int id) {
|
public static Task getTaskById(int id) {
|
||||||
for (int i = 0; i < tasksCounter; i++) {
|
for (int i = 0; i < tasksCounter; i++) {
|
||||||
if(tasks[i].getId() == id) return tasks[i];
|
if (tasks[i].getId() == id)
|
||||||
|
return tasks[i];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void taskMenu(Task task){
|
public static void displayTask(Task task) {
|
||||||
|
task.printInfo(true);
|
||||||
|
String firstOption = task.getCompleted() ? "[1]Не выполнено" : "[1]Выполнено";
|
||||||
|
int input = requestIntFromInput(firstOption + " [0]Главное меню\n\nВвод:",
|
||||||
|
new int[] { 0, 1 });
|
||||||
|
if (input == 1) {
|
||||||
|
task.toggleCompleted();
|
||||||
|
displayTask(task);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void requestInput() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user