Who loves creating apps.

Tag: CRUD

a simple yet effective task manager in Java—a project perfectly suited for enriching weekend coding sessions.

Step 1: Define the Task Class

Our journey begins with the creation of the Task class, the cornerstone of our application. This class represents the tasks our users will manage:

public class Task {
    private String description;
    private boolean isCompleted;

    public Task(String description) {
        this.description = description;
        this.isCompleted = false;
    }

    public String getDescription() {
        return description;
    }

    public boolean isCompleted() {
        return isCompleted;
    }

    public void completeTask() {
        isCompleted = true;
    }

    @Override
    public String toString() {
        return (isCompleted ? "[X] " : "[ ] ") + description;
    }
}

Step 2: Task Logic

With our Task class defined, we introduce the TaskManager class to encapsulate our application’s logic, including adding, viewing, completing, and deleting tasks:

import java.util.ArrayList;
import java.util.Scanner;

public class TaskManager {
    private ArrayList<Task> tasks = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        boolean isRunning = true;

        while (isRunning) {
            System.out.println("Task Manager - Choose an action: (1) Add Task (2) View Tasks (3) Complete Task (4) Exit");
            String action = scanner.nextLine();

            switch (action) {
                case "1":
                    addTask();
                    break;
                case "2":
                    viewTasks();
                    break;
                case "3":
                    completeTask();
                    break;
                case "4":
                    isRunning = false;
                    break;
                default:
                    System.out.println("Invalid option. Please choose a valid action.");
            }
        }
    }

    private void addTask() {
        System.out.println("Enter task description:");
        String description = scanner.nextLine();
        tasks.add(new Task(description));
        System.out.println("Task added.");
    }

    private void viewTasks() {
        if (tasks.isEmpty()) {
            System.out.println("No tasks available.");
        } else {
            for (int i = 0; i < tasks.size(); i++) {
                System.out.println((i + 1) + ". " + tasks.get(i));
            }
        }
    }

    private void completeTask() {
        viewTasks();
        System.out.println("Enter the number of the task to complete:");
        int taskNumber = Integer.parseInt(scanner.nextLine());
        if (taskNumber >= 1 && taskNumber <= tasks.size()) {
            tasks.get(taskNumber - 1).completeTask();
            System.out.println("Task completed.");
        } else {
            System.out.println("Invalid task number.");
        }
    }
}

Step 3: Running the Task Manager

To breathe life into our Task Manager, we craft a Main class to serve as the entry point for our application:

public class Main {
    public static void main(String[] args) {
        TaskManager taskManager = new TaskManager();
        taskManager.start();
    }
}

How to Run

  1. Compile the Java Files: Use the javac command in your terminal within the directory containing your .java files.

    javac Task.java TaskManager.java Main.java
  2. Run the Application:

    java Main

Resources for the Java Adventurer

Database Management

At its core, a database is a systematic collection of data that supports the storage, manipulation, and retrieval of information. Databases can be relational (SQL) or non-relational (NoSQL), each serving different needs based on the structure and scalability requirements of your application.

from Sololearn

Best Practices :

  • Data Sanitization : Always sanitize user inputs to prevent SQL injection attacks. This involves escaping potentially harmful characters before they’re processed by the database.
  • Privilege : Operate your database under the principle of least privilege, meaning users and applications should have only the minimum permissions necessary to perform their tasks.

Secure Requests and Authorization

HTTPS : Use HTTPS (Hypertext Transfer Protocol Secure) for all communications between the client and server. HTTPS encrypts data in transit, preventing attackers from intercepting sensitive information.

Authorization Tokens : Implement token-based authorization, such as JWT (JSON Web Tokens), to manage user sessions. Tokens should be securely stored (in HTTP-only cookies) and validated with each request to verify a user’s identity and permissions.

Click here to display content from YouTube.
Learn more in YouTube’s privacy policy.

Safeguarding Your Environment

Application and server configurations play a significant role in security. A misconfigured server or application can serve as an entry point for attackers.

Secure Configuration Practices:

  • Update Regularly: Keep your server software and dependencies up to date to protect against known vulnerabilities.
  • Minimal Exposure: Disable unnecessary services and features on your server to reduce potential attack surfaces.
  • Environment Variables: Store sensitive configuration options such as API keys and database credentials in environment variables, not in your codebase.

A Shield Against Cross-Site Request Forgery

Cross-Site Request Forgery (CSRF) is an attack that tricks the victim into submitting a malicious request. It exploits the trust that a site has in a user’s browser.

How CSRF Protection Works :

  1. Token Generation : The server generates a unique, unpredictable token and sends it to the client’s browser as part of a form or an AJAX request.
  2. Token Validation : When the client submits the form or makes a request, it must include this token. The server then validates the token before processing the request.
  3. Token Invalidation : Tokens are invalidated after being used or after a certain period, requiring new tokens for subsequent requests.

Implementing CSRF tokens in forms and AJAX requests is a standard practice in modern web frameworks. This mechanism ensures that every state-changing request originates from your application, not an attacker.

Without Ajax (simplified)

Conclusion : Keeping Data Secure

Remember, security isn’t a one-time task but a continuous process of learning, implementing, and evolving with the digital landscape.

© 2024 Kvnbbg.fr

Theme by Anders NorénUp ↑

Verified by MonsterInsights