Untitled

 avatar
unknown
plain_text
5 months ago
5.7 kB
15
Indexable
import java.util.PriorityQueue;
import java.util.Scanner;

class Task implements Comparable<Task> {
    String name;
    int priority;
    String status;

    // Constructor
    public Task(String name, int priority) {
        this.name = name;
        this.priority = priority;
        this.status = "Pending";
    }

    // Compare tasks based on priority (lower number means higher priority)
    @Override
    public int compareTo(Task other) {
        return Integer.compare(this.priority, other.priority);
    }

    @Override
    public String toString() {
        return "Task: " + name + ", Priority: " + priority + ", Status: " + status;
    }
}

public class TaskManager {
    private static PriorityQueue<Task> taskQueue = new PriorityQueue<>();
    private static int totalTasks = 0;  // Total tasks added to the queue
    private static int completedTasks = 0;  // Count of completed tasks

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int option;

        // Menu for user interaction
        do {
            System.out.println("\n=== Priority-Based Task Tracker ===");
            System.out.println("1. Add a new task");
            System.out.println("2. View all tasks");
            System.out.println("3. Update task priority");
            System.out.println("4. Mark task as completed");
            System.out.println("5. View scoring and progress");
            System.out.println("6. Exit");
            System.out.print("Enter your choice: ");
            option = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (option) {
                case 1:
                    addTask(scanner);
                    break;
                case 2:
                    viewTasks();
                    break;
                case 3:
                    updateTaskPriority(scanner);
                    break;
                case 4:
                    markTaskAsCompleted(scanner);
                    break;
                case 5:
                    viewProgress();
                    break;
                case 6:
                    System.out.println("Exiting...");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        } while (option != 6);

        scanner.close();
    }

    // Add a new task
    private static void addTask(Scanner scanner) {
        System.out.print("Enter task name: ");
        String name = scanner.nextLine();
        System.out.print("Enter task priority (lower number means higher priority): ");
        int priority = scanner.nextInt();
        taskQueue.offer(new Task(name, priority));
        totalTasks++;  // Increment total tasks
        System.out.println("Task added successfully.");
    }

    // View all tasks in order of priority
    private static void viewTasks() {
        if (taskQueue.isEmpty()) {
            System.out.println("No tasks available.");
        } else {
            System.out.println("\nCurrent Task List:");
            PriorityQueue<Task> tempQueue = new PriorityQueue<>(taskQueue);  // Copy the queue for displaying
            while (!tempQueue.isEmpty()) {
                System.out.println(tempQueue.poll());
            }
        }
    }

    // Update task priority
    private static void updateTaskPriority(Scanner scanner) {
        System.out.print("Enter the name of the task to update: ");
        String taskName = scanner.nextLine();
        System.out.print("Enter new priority: ");
        int newPriority = scanner.nextInt();
        boolean found = false;

        PriorityQueue<Task> tempQueue = new PriorityQueue<>();

        // Update priority
        while (!taskQueue.isEmpty()) {
            Task task = taskQueue.poll();
            if (task.name.equalsIgnoreCase(taskName)) {
                task.priority = newPriority;
                found = true;
            }
            tempQueue.offer(task);
        }

        taskQueue = tempQueue;  // Update the original queue

        if (found) {
            System.out.println("Task priority updated successfully.");
        } else {
            System.out.println("Task not found.");
        }
    }

    // Mark a task as completed
    private static void markTaskAsCompleted(Scanner scanner) {
        System.out.print("Enter the name of the task to mark as completed: ");
        String taskName = scanner.nextLine();
        boolean found = false;

        PriorityQueue<Task> tempQueue = new PriorityQueue<>();

        // Mark task as completed
        while (!taskQueue.isEmpty()) {
            Task task = taskQueue.poll();
            if (task.name.equalsIgnoreCase(taskName)) {
                if (!task.status.equals("Completed")) {
                    task.status = "Completed";
                    completedTasks++;  // Increment completed tasks
                }
                found = true;
            }
            tempQueue.offer(task);
        }

        taskQueue = tempQueue;  // Update the original queue

        if (found) {
            System.out.println("Task marked as completed.");
        } else {
            System.out.println("Task not found.");
        }
    }

    // View scoring and progress
    private static void viewProgress() {
        if (totalTasks == 0) {
            System.out.println("No tasks have been added yet.");
        } else {
            double progress = (double) completedTasks / totalTasks * 100;
            System.out.println("Total tasks: " + totalTasks);
            System.out.println("Completed tasks: " + completedTasks);
            System.out.printf("Progress: %.2f%% completed\n", progress);
        }
    }
}
Editor is loading...
Leave a Comment