revert 1a39b1d1eda5635b4e8d1d9b0985cdbc48f42c14
revert Merge pull request 'init/2.0.0' (#20) from init/2.0.0 into main Reviewed-on: #20
This commit is contained in:
parent
1a39b1d1ed
commit
ef54b15d2a
148
learn.txt
148
learn.txt
@ -1,148 +0,0 @@
|
|||||||
Inheritance
|
|
||||||
|
|
||||||
In the preceding sections, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
|
|
||||||
|
|
||||||
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
|
|
||||||
|
|
||||||
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
|
|
||||||
|
|
||||||
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.
|
|
||||||
|
|
||||||
The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
|
|
||||||
|
|
||||||
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
|
|
||||||
|
|
||||||
The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.
|
|
||||||
|
|
||||||
At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.
|
|
||||||
|
|
||||||
|
|
||||||
An Example of Inheritance
|
|
||||||
|
|
||||||
Here is the sample code for a possible implementation of a Bicycle class that was presented in the Classes and Objects section:
|
|
||||||
|
|
||||||
public class Bicycle {
|
|
||||||
|
|
||||||
// the Bicycle class has three fields
|
|
||||||
public int cadence;
|
|
||||||
public int gear;
|
|
||||||
public int speed;
|
|
||||||
|
|
||||||
// the Bicycle class has one constructor
|
|
||||||
public Bicycle(int startCadence, int startSpeed, int startGear) {
|
|
||||||
gear = startGear;
|
|
||||||
cadence = startCadence;
|
|
||||||
speed = startSpeed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the Bicycle class has four methods
|
|
||||||
public void setCadence(int newValue) {
|
|
||||||
cadence = newValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setGear(int newValue) {
|
|
||||||
gear = newValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void applyBrake(int decrement) {
|
|
||||||
speed -= decrement;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void speedUp(int increment) {
|
|
||||||
speed += increment;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:
|
|
||||||
|
|
||||||
public class MountainBike extends Bicycle {
|
|
||||||
|
|
||||||
// the MountainBike subclass adds one field
|
|
||||||
public int seatHeight;
|
|
||||||
|
|
||||||
// the MountainBike subclass has one constructor
|
|
||||||
public MountainBike(int startHeight,
|
|
||||||
int startCadence,
|
|
||||||
int startSpeed,
|
|
||||||
int startGear) {
|
|
||||||
super(startCadence, startSpeed, startGear);
|
|
||||||
seatHeight = startHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the MountainBike subclass adds one method
|
|
||||||
public void setHeight(int newValue) {
|
|
||||||
seatHeight = newValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. However, you did not have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug.
|
|
||||||
|
|
||||||
|
|
||||||
What You Can Do in a Subclass
|
|
||||||
|
|
||||||
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
|
|
||||||
|
|
||||||
The inherited fields can be used directly, just like any other fields.
|
|
||||||
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
|
|
||||||
You can declare new fields in the subclass that are not in the superclass.
|
|
||||||
The inherited methods can be used directly as they are.
|
|
||||||
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
|
|
||||||
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
|
|
||||||
You can declare new methods in the subclass that are not in the superclass.
|
|
||||||
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
|
|
||||||
The following sections in this lesson will expand on these topics.
|
|
||||||
|
|
||||||
|
|
||||||
Private Members in a Superclass
|
|
||||||
|
|
||||||
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
|
|
||||||
|
|
||||||
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
|
|
||||||
|
|
||||||
|
|
||||||
Casting Objects
|
|
||||||
|
|
||||||
We have seen that an object is of the data type of the class from which it was instantiated. For example, if we write
|
|
||||||
|
|
||||||
public MountainBike myBike = new MountainBike();
|
|
||||||
|
|
||||||
then myBike is of type MountainBike.
|
|
||||||
|
|
||||||
MountainBike is descended from Bicycle and Object. Therefore, a MountainBike is a Bicycle and is also an Object, and it can be used wherever Bicycle or Object objects are called for.
|
|
||||||
|
|
||||||
The reverse is not necessarily true: a Bicycle may be a MountainBike, but it is not necessarily. Similarly, an Object may be a Bicycle or a MountainBike, but it is not necessarily.
|
|
||||||
|
|
||||||
Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write
|
|
||||||
|
|
||||||
Object obj = new MountainBike();
|
|
||||||
|
|
||||||
then obj is both an Object and a MountainBike (until such time as obj is assigned another object that is not a MountainBike). This is called implicit casting.
|
|
||||||
|
|
||||||
If, on the other hand, we write
|
|
||||||
|
|
||||||
MountainBike myBike = obj;
|
|
||||||
|
|
||||||
we would get a compile-time error because obj is not known to the compiler to be a MountainBike. However, we can tell the compiler that we promise to assign a MountainBike to obj by explicit casting:
|
|
||||||
|
|
||||||
MountainBike myBike = (MountainBike)obj;
|
|
||||||
|
|
||||||
This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume that obj is a MountainBike. If obj is not a MountainBike at runtime, an exception will be thrown.
|
|
||||||
|
|
||||||
Note: You can make a logical test as to the type of a particular object using the instanceof operator. This can save you from a runtime error owing to an improper cast. For example:
|
|
||||||
|
|
||||||
if (obj instanceof MountainBike) {
|
|
||||||
MountainBike myBike = (MountainBike)obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
Here the instanceof operator verifies that obj refers to a MountainBike so that we can make the cast with knowledge that there will be no runtime exception thrown.
|
|
||||||
|
|
||||||
|
|
||||||
Multiple Inheritance of State, Implementation, and Type
|
|
||||||
|
|
||||||
One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot. In addition, you can instantiate a class to create an object, which you cannot do with interfaces. As explained in the section What Is an Object?, an object stores its state in fields, which are defined in classes. One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. For example, suppose that you are able to define a new class that extends multiple classes. When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. What if methods or constructors from different superclasses instantiate the same field? Which method or constructor will take precedence? Because interfaces do not contain fields, you do not have to worry about problems that result from multiple inheritance of state.
|
|
||||||
|
|
||||||
Multiple inheritance of implementation is the ability to inherit method definitions from multiple classes. Problems arise with this type of multiple inheritance, such as name conflicts and ambiguity. When compilers of programming languages that support this type of multiple inheritance encounter superclasses that contain methods with the same name, they sometimes cannot determine which member or method to access or invoke. In addition, a programmer can unwittingly introduce a name conflict by adding a new method to a superclass. Default methods introduce one form of multiple inheritance of implementation. A class can implement more than one interface, which can contain default methods that have the same name. The Java compiler provides some rules to determine which default method a particular class uses.
|
|
||||||
|
|
||||||
The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements. This means that if a variable is declared to be the type of an interface, then its value can reference any object that is instantiated from any class that implements the interface. This is discussed in the section Using an Interface as a Type.
|
|
||||||
|
|
||||||
As with multiple inheritance of implementation, a class can inherit different implementations of a method defined (as default or static) in the interfaces that it extends. In this case, the compiler or the user must decide which one to use.
|
|
||||||
85
todo/src/main/java/ru/kamask/pet/Task.java
Normal file
85
todo/src/main/java/ru/kamask/pet/Task.java
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package ru.kamask.pet;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
class Task {
|
||||||
|
static int taskCount = 0;
|
||||||
|
|
||||||
|
enum TaskIdGenerator {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
private int currentId = 1;
|
||||||
|
|
||||||
|
int nextId() {
|
||||||
|
return currentId++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String title;
|
||||||
|
private String description;
|
||||||
|
private TaskStatus status;
|
||||||
|
private LocalDate createdAt;
|
||||||
|
|
||||||
|
Task(String title, String description) {
|
||||||
|
id = TaskIdGenerator.INSTANCE.nextId();
|
||||||
|
status = TaskStatus.NEW;
|
||||||
|
createdAt = LocalDate.now();
|
||||||
|
taskCount++;
|
||||||
|
|
||||||
|
this.title = title;
|
||||||
|
this.description = description;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void printTotalTasksCreated() {
|
||||||
|
System.out.println("Колличество дел: " + taskCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isCompleted() {
|
||||||
|
return status == TaskStatus.COMPLETED;
|
||||||
|
}
|
||||||
|
|
||||||
|
void markCompleted() {
|
||||||
|
status = TaskStatus.COMPLETED;
|
||||||
|
}
|
||||||
|
|
||||||
|
void markInProgress() {
|
||||||
|
status = TaskStatus.IN_PROGRESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void markCanceled() {
|
||||||
|
status = TaskStatus.CANCELLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskStatus getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TaskPrinter {
|
||||||
|
static void print(Task task) {
|
||||||
|
String template = """
|
||||||
|
|
||||||
|
──────────────────────────────
|
||||||
|
Номер: %-3d
|
||||||
|
Дата создания: %s
|
||||||
|
Статус: %s
|
||||||
|
Название: %-20s
|
||||||
|
------------------------------
|
||||||
|
%s
|
||||||
|
|
||||||
|
""";
|
||||||
|
;
|
||||||
|
String stringCompleted = task.status.getColorCode() + task.status.getDescription() + "\u001B[0m";
|
||||||
|
System.out.printf(template, task.id, task.createdAt, stringCompleted, task.title, task.description);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
41
todo/src/main/java/ru/kamask/pet/TaskStatus.java
Normal file
41
todo/src/main/java/ru/kamask/pet/TaskStatus.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package ru.kamask.pet;
|
||||||
|
|
||||||
|
public enum TaskStatus {
|
||||||
|
NEW("новое") {
|
||||||
|
@Override
|
||||||
|
String getColorCode() {
|
||||||
|
return "\u001B[34m";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
IN_PROGRESS("в работе") {
|
||||||
|
@Override
|
||||||
|
String getColorCode() {
|
||||||
|
return "\u001B[35m";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
COMPLETED("сделано") {
|
||||||
|
@Override
|
||||||
|
String getColorCode() {
|
||||||
|
return "\u001B[32m";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
CANCELLED("отменено") {
|
||||||
|
@Override
|
||||||
|
String getColorCode() {
|
||||||
|
return "\u001B[31m";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
abstract String getColorCode();
|
||||||
|
|
||||||
|
private final String description;
|
||||||
|
|
||||||
|
TaskStatus(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
250
todo/src/main/java/ru/kamask/pet/TodoApp.java
Normal file
250
todo/src/main/java/ru/kamask/pet/TodoApp.java
Normal file
@ -0,0 +1,250 @@
|
|||||||
|
package ru.kamask.pet;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class TodoApp {
|
||||||
|
|
||||||
|
private static TaskManager taskManager;
|
||||||
|
private static Task[] tasks = new Task[10];
|
||||||
|
private static int tasksCounter = 0;
|
||||||
|
private static Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
TodoApp app = new TodoApp();
|
||||||
|
taskManager = app.new TaskManager();
|
||||||
|
|
||||||
|
app.run();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void run() {
|
||||||
|
while (true) {
|
||||||
|
Task.printTotalTasksCreated();
|
||||||
|
String menu = """
|
||||||
|
|
||||||
|
┌──────────────────────────┐
|
||||||
|
| [1] Добавить дело |
|
||||||
|
| [2] Список дел |
|
||||||
|
| |
|
||||||
|
| [0] Выйти из программы |
|
||||||
|
└──────────────────────────┘
|
||||||
|
|
||||||
|
Введите номер пункта меню:""";
|
||||||
|
|
||||||
|
switch (requestIntFromInput(menu, new int[] { 0, 1, 2 })) {
|
||||||
|
case 1 -> displayCreateTask();
|
||||||
|
case 2 -> displayTasks();
|
||||||
|
case 0 -> {
|
||||||
|
scanner.close();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void displayCreateTask() {
|
||||||
|
|
||||||
|
if (Task.taskCount >= 9) {
|
||||||
|
System.out.println("\nОшибка: Достигнут лимит в 10 дел.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String title;
|
||||||
|
String description;
|
||||||
|
|
||||||
|
do {
|
||||||
|
System.out.print("\nНапишите название дела (3–20 символов): ");
|
||||||
|
title = scanner.nextLine().trim();
|
||||||
|
if (title.length() >= 3 && title.length() <= 20)
|
||||||
|
break;
|
||||||
|
|
||||||
|
System.out.print("\nОшибка: Название должно содержать от 3 до 20 символов.\nПопробуйте снова: ");
|
||||||
|
} while (true);
|
||||||
|
|
||||||
|
System.out.print("\nНaпишите описание дела: ");
|
||||||
|
description = scanner.nextLine().trim();
|
||||||
|
|
||||||
|
taskManager.addTask(new Task(title, description));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void displayTask(Task task) {
|
||||||
|
Task.TaskPrinter.print(task);
|
||||||
|
|
||||||
|
String firstOption = switch (task.getStatus()) {
|
||||||
|
case NEW, CANCELLED -> "Начать";
|
||||||
|
case IN_PROGRESS -> "Выполнено";
|
||||||
|
case COMPLETED -> "Доделать";
|
||||||
|
};
|
||||||
|
|
||||||
|
int input = requestIntFromInput("[1]" + firstOption + " [2]Отменить [0]Главное меню\n\nВвод:",
|
||||||
|
new int[] { 0, 1, 2 });
|
||||||
|
|
||||||
|
switch (input) {
|
||||||
|
case 0:
|
||||||
|
return;
|
||||||
|
case 1: {
|
||||||
|
switch (task.getStatus()) {
|
||||||
|
case NEW, CANCELLED, COMPLETED -> taskManager.markInProgress(task.getId());
|
||||||
|
case IN_PROGRESS -> taskManager.markCompleted(task.getId());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
taskManager.markCanceled(task.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
displayTask(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void displayTasks() {
|
||||||
|
|
||||||
|
if (tasksCounter == 0) {
|
||||||
|
int input = requestIntFromInput("""
|
||||||
|
|
||||||
|
Список дел пуст.
|
||||||
|
|
||||||
|
[1] Добавить дело
|
||||||
|
[0] Выйти из программы
|
||||||
|
|
||||||
|
Введите номер пункта:""", new int[] { 0, 1 });
|
||||||
|
|
||||||
|
if (input == 1) {
|
||||||
|
displayCreateTask();
|
||||||
|
} else
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("""
|
||||||
|
Список дел:
|
||||||
|
|
||||||
|
""");
|
||||||
|
|
||||||
|
taskManager.printTasks();
|
||||||
|
|
||||||
|
int[] variantsInput = new int[tasksCounter + 1];
|
||||||
|
for (int i = 0; i < tasksCounter; i++)
|
||||||
|
variantsInput[i] = tasks[i].getId();
|
||||||
|
variantsInput[tasksCounter] = 0;
|
||||||
|
|
||||||
|
int input = requestIntFromInput("""
|
||||||
|
|
||||||
|
Введите номер дела или 0 для возврата в меню:""", variantsInput);
|
||||||
|
|
||||||
|
if (input == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
displayTask(getTaskById(input));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Task getTaskById(int id) {
|
||||||
|
for (int i = 0; i < Task.taskCount; i++) {
|
||||||
|
if (tasks[i].getId() == id)
|
||||||
|
return tasks[i];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int requestIntFromInput(String template, int[] allowedInts) {
|
||||||
|
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)
|
||||||
|
return input;
|
||||||
|
System.out.print("Ошибка: укажите номер выбранного пункта.\nПовторите ввод:");
|
||||||
|
} while (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TaskAction {
|
||||||
|
default void execute(Task task) {
|
||||||
|
};
|
||||||
|
|
||||||
|
default void start(Task task) {
|
||||||
|
};
|
||||||
|
|
||||||
|
default void cancel(Task task) {
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TaskManager {
|
||||||
|
void addTask(Task task) {
|
||||||
|
tasks[tasksCounter++] = task;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean markCompleted(int id) {
|
||||||
|
for (int i = 0; i < tasksCounter; i++)
|
||||||
|
if (tasks[i].getId() == id) {
|
||||||
|
TaskAction action = new TaskAction() {
|
||||||
|
@Override
|
||||||
|
public void execute(Task task) {
|
||||||
|
task.markCompleted();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
action.execute(tasks[i]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean markInProgress(int id) {
|
||||||
|
for (int i = 0; i < tasksCounter; i++)
|
||||||
|
if (tasks[i].getId() == id) {
|
||||||
|
TaskAction action = new TaskAction() {
|
||||||
|
@Override
|
||||||
|
public void start(Task task) {
|
||||||
|
task.markInProgress();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
action.start(tasks[i]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean markCanceled(int id) {
|
||||||
|
for (int i = 0; i < tasksCounter; i++)
|
||||||
|
if (tasks[i].getId() == id) {
|
||||||
|
TaskAction action = new TaskAction() {
|
||||||
|
@Override
|
||||||
|
public void cancel(Task task) {
|
||||||
|
task.markCanceled();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
action.cancel(tasks[i]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printTasks() {
|
||||||
|
class ShortTaskPrinter {
|
||||||
|
void print(Task task) {
|
||||||
|
int id = task.getId();
|
||||||
|
String title = task.getTitle();
|
||||||
|
TaskStatus status = task.getStatus();
|
||||||
|
|
||||||
|
String stringCompleted = status.getColorCode() + status.getDescription() + "\u001B[0m";
|
||||||
|
System.out.printf("%-3d | %-20s | %s\n", id, title, stringCompleted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ShortTaskPrinter printer = new ShortTaskPrinter();
|
||||||
|
for (int i = 0; i < Task.taskCount; i++)
|
||||||
|
printer.print(tasks[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,18 +0,0 @@
|
|||||||
package ru.kamask.pet.todo;
|
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.SimpleTask;
|
|
||||||
|
|
||||||
public class TodoApp {
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void main(String[] args) {
|
|
||||||
var task = new SimpleTask("TodoApp");
|
|
||||||
System.out.println(task);
|
|
||||||
|
|
||||||
task.markAsCompleted();
|
|
||||||
|
|
||||||
var taskData = task.data();
|
|
||||||
System.out.println(taskData);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.model;
|
|
||||||
|
|
||||||
public interface DataBuilder<T> {
|
|
||||||
T data();
|
|
||||||
}
|
|
||||||
@ -1,35 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.model;
|
|
||||||
|
|
||||||
public class SimpleTask extends Task implements DataBuilder<SimpleTask.Data> {
|
|
||||||
private boolean done = false;
|
|
||||||
|
|
||||||
public SimpleTask(String title) {
|
|
||||||
super(title);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void markAsCompleted() {
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isCompleted() {
|
|
||||||
return isDone();
|
|
||||||
}
|
|
||||||
|
|
||||||
void done() {
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isDone() {
|
|
||||||
return done;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Data data() {
|
|
||||||
return new Data(id, title, done);
|
|
||||||
}
|
|
||||||
|
|
||||||
record Data(int id, String title, boolean done) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
package ru.kamask.pet.todo.model;
|
|
||||||
|
|
||||||
public abstract class Task {
|
|
||||||
private static int nextId = 1;
|
|
||||||
|
|
||||||
protected int id;
|
|
||||||
protected String title;
|
|
||||||
|
|
||||||
Task(String title) {
|
|
||||||
this.id = nextId++;
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return String.format("Задача: id - %d, title: \"%s\"", id, title);
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract public void markAsCompleted();
|
|
||||||
|
|
||||||
abstract public boolean isCompleted();
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user