0% found this document useful (0 votes)
7 views4 pages

Task

Program to manage tasks.

Uploaded by

dothhe182087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Task

Program to manage tasks.

Uploaded by

dothhe182087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package stack;

import java.util.Scanner;
import static stack.TaskManagement.showMenu;
public class Main {
public static void main(String[] args) throws Exception {
TaskManagement taskManagement = new TaskManagement();
Scanner sc = new Scanner(System.in);
while (true) {
showMenu();
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1 -> taskManagement.addTaskOption();
case 2 -> taskManagement.deleteTaskOption();
case 3 -> taskManagement.showOp();
case 4 -> {
return;
}
default -> throw new AssertionError();
}
}
}
}

package stack;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class TaskManagement {


private final List<Task> tasks = new ArrayList<>();
private int nextID = 1;
private static final Scanner sc = new Scanner(System.in);

private class Task {


private final int id;
private final int taskTypeID;
private final String requirementName;
private final Date date;
private final double planFrom;
private final double planTo;
private final String assignee;
private final String reviewer;

public Task(int id, int taskTypeID, String requirementName, Date date,


double planFrom, double planTo, String assignee, String reviewer) {
this.id = id;
this.taskTypeID = taskTypeID;
this.requirementName = requirementName;
this.date = date;
this.planFrom = planFrom;
this.planTo = planTo;
this.assignee = assignee;
this.reviewer = reviewer;
}

@Override
public String toString() {
return "Task{" + "id=" + id + ", taskTypeID=" + taskTypeID
+ ", requirementName=" + requirementName + ", date=" + date
+ ", planFrom=" + planFrom + ", planTo=" + planTo
+ ", assignee=" + assignee + ", reviewer=" + reviewer + '}';
}

private String getTaskTypeName() {


return switch (taskTypeID) {
case 1 -> "Code";
case 2 -> "Test";
case 3 -> "Design";
case 4 -> "Review";
default -> "Unknown";
};
}

public int getId() {


return id;
}
}

public static void showMenu() {


System.out.println("====== Task Program ======");
System.out.println("1. Add Task");
System.out.println("2. Delete Task");
System.out.println("3. Display Task");
System.out.println("4. Exit");
System.out.print("Select an option: ");
}

public int addTask(String requirementName, int taskTypeID, String dateStr,


double planFrom, double planTo, String assignee, String reviewer) throws Exception
{
Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateStr);
Task newTask = new Task(nextID++, taskTypeID, requirementName, date,
planFrom, planTo, assignee, reviewer);
tasks.add(newTask);
return newTask.id;
}

public void deleteTask(int id) throws Exception {


Task task = findTaskById(id);
if (task == null) {
throw new Exception("Task ID not found.");
}
tasks.remove(task);
}

public Task findTaskById(int id) {


return tasks.stream().filter(t -> t.getId() ==
id).findFirst().orElse(null);
}
public List<Task> getDataTasks() {
tasks.sort(Comparator.comparingInt(Task::getId));
return tasks;
}

public void addTaskOption() throws Exception {


System.out.println("------ Add Task ------");
System.out.print("Requirement Name: ");
String requirementName = sc.nextLine();
System.out.print("Task Type: ");
int taskTypeID = sc.nextInt();
sc.nextLine();
System.out.print("Date: ");
String date = sc.nextLine();
System.out.print("From: ");
double planFrom = Double.parseDouble(sc.nextLine());
System.out.print("To: ");
double planTo = Double.parseDouble(sc.nextLine());
System.out.print("Assignee: ");
String assignee = sc.nextLine();
System.out.print("Reviewer: ");
String reviewer = sc.nextLine();

if (planFrom < 8 || planTo > 17.5 || planFrom >= planTo) {


System.out.println("Error, only between 8.0 - 17.5");
return;
}
if (taskTypeID < 1 || taskTypeID > 4) {
System.out.println("Task type is not valid.");
return;
}
if (!isValidDate(date)) {
System.out.println("Định dạng ngày không hợp lệ.");
return;
}

int id = addTask(requirementName, taskTypeID, date, planFrom, planTo,


assignee, reviewer);
System.out.println("Task added with ID: " + id);
}

public static boolean isValidDate(String dateStr) {


SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
sdf.setLenient(false);
try {
sdf.parse(dateStr);
return true;
} catch (ParseException e) {
return false;
}
}

public void deleteTaskOption() throws Exception {


System.out.println("----- Delete -----");
System.out.print("ID: ");
int id = sc.nextInt();
sc.nextLine();
deleteTask(id);
System.out.println("Task deleted successfully.");
}

public void showOp() {


System.out.println("----- Task List -----");
List<Task> tasks = getDataTasks();
if (tasks.isEmpty()) {
System.out.println("No tasks available.");
} else {
System.out.printf("%-5s %-20s %-15s %-15s %-15s %-15s %-15s%n",
"ID", "Name", "Task Type", "Date", "Time",
"Assignee", "Reviewer");
for (Task task : tasks) {
System.out.printf("%-5d %-20s %-15s %-15s %-15s %-15s %-15s%n",
task.id,
task.requirementName,
task.getTaskTypeName(),
new SimpleDateFormat("dd-MM-
yyyy").format(task.date),
task.planFrom + " - " + task.planTo,
task.assignee,
task.reviewer);
}
}
}
}

You might also like