Group#19

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Group #19

 Ahmad Latif 04162113032


 Muzamil Kashif 04162113027

Project Title:
To-Do List System

Submitted to:
Ma’am Madiha Haider Syed
& Ma’am Zahra Abbas
Code:
package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ToDoList extends Application {

private static final String JDBC_URL = "jdbc:mysql://localhost:3306/todo_list_db?


user=<ahmadlatifch>&password=<Ahmad2203?>";
private Connection connection;

private ObservableList<TaskItem> tasks;


private int taskCounter = 1;

public static void main(String[] args) {


launch(args);
}

private void initializeDatabase() {


try {
connection = DriverManager.getConnection(JDBC_URL);
connection.setAutoCommit(true);
createTableIfNotExists();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error connecting to the database: " + e.getMessage());
}
}

private void createTableIfNotExists() {


String createTableSQL = "CREATE TABLE IF NOT EXISTS tasks (" +
"id INT PRIMARY KEY AUTO_INCREMENT," +
"description VARCHAR(255) NOT NULL," +
"timestamp DATETIME NOT NULL," +
"due_date DATE," +
"completed BOOLEAN NOT NULL" +
")";
try (Statement statement = connection.createStatement()) {
statement.execute(createTableSQL);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error creating tasks table: " + e.getMessage());
}
}

private void saveTaskToDatabase(TaskItem task) {


String insertSQL = "INSERT INTO tasks (description, timestamp, due_date, completed)
VALUES (?, ?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(insertSQL,
Statement.RETURN_GENERATED_KEYS)) {
preparedStatement.setString(1, task.getDescription());
preparedStatement.setString(2, task.getTimestamp().toString());
preparedStatement.setString(3, task.getDueDate() != null ? task.getDueDate().toString() :
null);
preparedStatement.setBoolean(4, task.isCompleted());
preparedStatement.executeUpdate();

try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {


if (generatedKeys.next()) {
task.setId(generatedKeys.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error saving task to the database: " + e.getMessage());
}
}

private void updateTaskInDatabase(TaskItem task) {


String updateSQL = "UPDATE tasks SET description = ?, timestamp = ?, due_date = ?,
completed = ? WHERE id = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(updateSQL)) {
preparedStatement.setString(1, task.getDescription());
preparedStatement.setString(2, task.getTimestamp().toString());
preparedStatement.setString(3, task.getDueDate() != null ? task.getDueDate().toString() :
null);
preparedStatement.setBoolean(4, task.isCompleted());
preparedStatement.setInt(5, task.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error updating task in the database: " + e.getMessage());
}
}

private void deleteTaskFromDatabase(TaskItem task) {


String deleteSQL = "DELETE FROM tasks WHERE id = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(deleteSQL)) {
preparedStatement.setInt(1, task.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error deleting task from the database: " + e.getMessage());
}
}

@Override
public void start(Stage primaryStage) {

primaryStage.setTitle("To-Do List");
initializeDatabase();

tasks = FXCollections.observableArrayList();
ListView<TaskItem> todoListView = new ListView<>(tasks);
TextField taskInput = new TextField();
Button addButton = new Button("Add");
Button deleteButton = new Button("Delete");
Button completeButton = new Button("Complete");
Button editButton = new Button("Edit");
DatePicker datePicker = new DatePicker();
Button addDueDateButton = new Button("Add Due Date");

addButton.setOnAction(e -> {
String taskDescription = taskInput.getText().trim();
if (!taskDescription.isEmpty()) {
LocalDateTime timestamp = LocalDateTime.now();
LocalDate dueDate = LocalDate.now();
TaskItem newTask = new TaskItem(taskCounter++, taskDescription, timestamp,
dueDate, false);
tasks.add(newTask);
saveTaskToDatabase(newTask);
taskInput.clear();
}
});

deleteButton.setOnAction(e -> {
int selectedIndex = todoListView.getSelectionModel().getSelectedIndex();
if (selectedIndex != -1) {
TaskItem deletedTask = tasks.remove(selectedIndex);
deleteTaskFromDatabase(deletedTask);
}
});

completeButton.setOnAction(e -> {
int selectedIndex = todoListView.getSelectionModel().getSelectedIndex();
if (selectedIndex != -1) {
TaskItem selectedTask = tasks.get(selectedIndex);
selectedTask.setCompleted(true);
todoListView.refresh();
updateTaskInDatabase(selectedTask);
}
});

editButton.setOnAction(e -> {
int selectedIndex = todoListView.getSelectionModel().getSelectedIndex();
if (selectedIndex != -1) {
TaskItem selectedTask = tasks.get(selectedIndex);
String editedTaskDescription = taskInput.getText().trim();
if (!editedTaskDescription.isEmpty()) {
selectedTask.setDescription(editedTaskDescription);
taskInput.clear();
todoListView.refresh();
updateTaskInDatabase(selectedTask);
}
}
});

addDueDateButton.setOnAction(e -> {
int selectedIndex = todoListView.getSelectionModel().getSelectedIndex();
if (selectedIndex != -1) {
TaskItem selectedTask = tasks.get(selectedIndex);
LocalDate selectedDate = datePicker.getValue();
if (selectedDate != null) {
selectedTask.setDueDate(selectedDate);
datePicker.setValue(null);
todoListView.refresh();
updateTaskInDatabase(selectedTask);
} else {
System.out.println("Please select a due date.");
}
}
});

HBox inputBox = new HBox(taskInput, addButton, deleteButton, completeButton,


editButton);
HBox dueDateBox = new HBox(datePicker, addDueDateButton);
VBox controlsBox = new VBox(inputBox, dueDateBox);
controlsBox.setSpacing(5);
controlsBox.setPadding(new Insets(10));

BorderPane pane = new BorderPane();


pane.setTop(controlsBox);
pane.setCenter(todoListView);

try {
Image backgroundImage = new
Image("file:///Users/ahmadlatif/Documents/Semester5/OOP/Lab/Chapter14/To-Do-List/
images/ali.jpeg");
BackgroundImage background = new BackgroundImage(
backgroundImage, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT,
BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT
);
Background backgroundObject = new Background(background);
pane.setBackground(backgroundObject);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error loading the background image: " + e.getMessage());
}

Scene scene = new Scene(pane, 800, 600);


primaryStage.setTitle("To-Do-List");
primaryStage.setScene(scene);
primaryStage.show();
initializeDatabase();
}
private static class TaskItem {
private int id;
private String description;
private LocalDateTime timestamp;
private LocalDate dueDate;
private boolean completed;

public TaskItem(int id, String description, LocalDateTime timestamp, LocalDate dueDate,


boolean completed) {
this.id = id;
this.description = description;
this.timestamp = timestamp;
this.dueDate = dueDate;
this.completed = completed;
}

public void setId(int id) {


this.id = id;
}

public void setDescription(String description) {


this.description = description;
}

public void setTimestamp(LocalDateTime timestamp) {


this.timestamp = timestamp;
}

public void setDueDate(LocalDate dueDate) {


this.dueDate = dueDate;
}

public void setCompleted(boolean completed) {


this.completed = completed;
}

public int getId() {


return id;
}

public String getDescription() {


return description;
}
public LocalDateTime getTimestamp() {
return timestamp;
}

public LocalDate getDueDate() {


return dueDate;
}

public boolean isCompleted() {


return completed;
}

@Override
public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedTimestamp = timestamp.format(formatter);
String formattedDueDate = (dueDate != null) ? dueDate.toString() : "";
String status = completed ? " (Completed)" : "";
return id + ". " + description + " - " + formattedTimestamp +
" Due Date: " + formattedDueDate + status;
}
}
}
Interface:

How it works?
When we type our task in the first section and press add
button then the task will be added and displayed, and it
will also be saved into our database connected. The time
will be shown with task when it saved and the due date
will be automatically set as the current date.
We can further edit our task, Delete our task, Mark it as
Completed and change the due date. In case of Due Date,
the calendar will open as follow
Uses:
 We can use it in our daily lives to manage our tasks.
 Task Management is easy and compatible to
everyone.
 We can retrieve each task from database.

You might also like