A
A
Anna1234567892020-12-15 22:23:25
Java
Anna123456789, 2020-12-15 22:23:25

How to implement a task in spring?

An educational institution run by a student can be in the following states: issued, completed, submitted for verification, verified, resubmitted for verification, not completed. Determine the logic of changing states and develop a model of the system.
There is code for the State pattern

package by.bsac.model;

import java.util.StringJoiner;

public class Student {
    private String lastName;
    private String name;
    private Task task;

    public Student(String lastName, String name) {
        this.lastName = lastName;
        this.name = name;
        this.task = null;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public void forChecking() {
        if (task != null){
            //System.out.print(task.getCurrentState().getClass().getSimpleName());
            task.learning();
        }
        else{ System.out.println("Нельзя выдать на проверку, " +
                "так как задание небыло получено студентом");}
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Student.class.getSimpleName() + "[", "]")
                .add("lastName='" + lastName + "'")
                .add("name='" + name + "'")
                .add("task=" + task)
                .toString();
    }
}
package by.bsac.model;

import by.bsac.state.IState;
import by.bsac.state.Issue;

import java.util.StringJoiner;

public class Task {
    private String title;
    private String decryption;
    private Teacher teacher;
    private IState currentState;

    public Task(String title, String decryption, Teacher teacher) {
        this.title = title;
        this.decryption = decryption;
        this.teacher = teacher;
        this.currentState = new Issue();
    }

    public String getTitle() {
        return title;
    }

    public String getDecryption() {
        return decryption;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public IState getCurrentState() {
        return currentState;
    }

    public void setCurrentState(IState currentState) {
        this.currentState = currentState;
    }

    public void learning() {
        currentState.learning(this);
    }

    public void toCancel() {
        currentState.toCancel(this);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Task.class.getSimpleName() + "[", "]")
                .add("title='" + title + "'")
                .add("decryption='" + decryption + "'")
                .add("teacher=" + teacher)
                .add("currentState=" + currentState)
                .toString();
    }
}
package by.bsac.model;

import by.bsac.state.AbstractState;
import by.bsac.state.Issue;
import by.bsac.state.SurveyedForTwoCheck;

import java.util.Objects;
import java.util.StringJoiner;

public class Teacher {
    private String lastName;
    private String name;

    public Teacher(String lastName, String name) {
        this.lastName = lastName;
        this.name = name;
    }

    public void checking(Task task, boolean isDone) {
        if (task.getTeacher().equals(this)) {
            if (isDone) {
                //System.out.println(task.getCurrentState().getClass().getSimpleName());
                task.learning();
            }
            else {
                //System.out.println(task.getCurrentState().getClass().getSimpleName());
                task.toCancel();
                if (!task.getCurrentState().equals(new SurveyedForTwoCheck())){
                    //System.out.println(task.getCurrentState().getClass().getSimpleName());
                    task.learning();
                }
            }
        } else System.out.println("Учитель не может проверить задание, " +
                "так как не назначен на проверку этой работы");
    }

    public Task toIssue(String title, String decryption) {
        System.out.println("Выдано!");
        return new Task(title, decryption, this);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Teacher.class.getSimpleName() + "[", "]")
                .add("lastName='" + lastName + "'")
                .add("name='" + name + "'")
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        return Objects.equals(lastName, teacher.lastName) &&
                Objects.equals(name, teacher.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(lastName, name);
    }
}
package by.bsac.state;

import java.util.Objects;

public abstract class AbstractState implements IState {
    private boolean isRetry;    //флаг о повторной попытке

    public AbstractState() {
        this.isRetry = false;
    }

    public boolean isRetry() {
        return isRetry;
    }

    public void setRetry(boolean retry) {
        isRetry = retry;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AbstractState state = (AbstractState) o;
        return isRetry == state.isRetry;
    }

    @Override
    public int hashCode() {
        return Objects.hash(isRetry);
    }
}
package by.bsac.state;

import by.bsac.model.Task;

public class Done extends AbstractState {
    @Override
    public void learning(Task task) {
        System.out.println("Перевыполнить задание нельзя, так как оно уже выполнено.");
    }

    @Override
    public void toCancel(Task task) {
        System.out.println("Задание уже завершено.");
    }
}
package by.bsac.state;

import by.bsac.model.Task;

public class Issue extends AbstractState {  //Выдано
    public Issue() {
        super();
    }

    @Override
    public void learning(Task task) {
        if (!isRetry()) {
            System.out.println("Сдано на проверку");
            task.setCurrentState(new SurveyedForCheck());
        } else {
            System.out.println("Пересдано на проверку");
            task.setCurrentState(new SurveyedForTwoCheck());
        }
    }

    @Override
    public void toCancel(Task task) {
        System.out.println("Пересдано на проверку");
        AbstractState state = new SurveyedForTwoCheck();
        state.setRetry(true);
        task.setCurrentState(state);
    }

}
package by.bsac.state;

import by.bsac.model.Task;

public interface IState {
    void learning(Task task);
    void toCancel(Task task);
}
package by.bsac.state;

import by.bsac.model.Task;

public class NotDone extends AbstractState {
    @Override
    public void learning(Task task) {
        System.out.println("Выдано на перевыполнение. Перевыполнение возможно только 1 раз.");
        AbstractState state = new Issue();
        state.setRetry(true);
        task.setCurrentState(state);
    }

    @Override
    public void toCancel(Task task) {
        System.out.println("Закончить выполнение задания не возможно, " +
                "так как оно не выполнено.");
    }
}
package by.bsac.state;

import by.bsac.model.Task;

public class SurveyedForCheck extends AbstractState {

    @Override
    public void learning(Task task) {
        System.out.println("Выполнено!");
        task.setCurrentState(new Done());
    }

    @Override
    public void toCancel(Task task) {
        System.out.println("Не выполнено.");
        task.setCurrentState(new NotDone());
    }
}
package by.bsac.state;

import by.bsac.model.Task;

public class SurveyedForTwoCheck extends AbstractState {
    @Override
    public void learning(Task task) {
        System.out.println("Выполнено!");
        AbstractState state = new Done();
        task.setCurrentState(new Done());
    }

    @Override
    public void toCancel(Task task) {
        System.out.println("Не выполнено.");

    }
}
package by.bsac;

import by.bsac.model.Student;
import by.bsac.model.Teacher;

public class Main {
    public static void main(String[] args) {
        Student student = new Student("Дворак", "Владимир");
        Teacher teacher = new Teacher("Василенок", "Павел");
        student.setTask(teacher.toIssue("Заголовок", "Описание задания"));
        student.forChecking();
        teacher.checking(student.getTask(), false);

        student.forChecking();
        teacher.checking(student.getTask(), true);
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question