allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

โ€œPeace I leave with you; my peace I give you.โ€
[John 14:27]


Integration Practice


Goal

Add:

  • LocalDateTime field for created date.

  • File export using BufferedWriter.

  • Show how CRUD can combine with File I/O and DateTime.


User.java (extended)

java
import java.time.LocalDateTime; public class User { private int id; private String name; private String email; private LocalDateTime createdAt; public User(int id, String name, String email) { this.id = id; this.name = name; this.email = email; this.createdAt = LocalDateTime.now(); } public int getId() { return id; } public String getName() { return name; } public String getEmail() { return email; } public LocalDateTime getCreatedAt() { return createdAt; } public void setName(String name) { this.name = name; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + ", createdAt=" + createdAt + '}'; } }

UserRepository.java (extended)

java
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class UserRepository { private Map<Integer, User> storage = new HashMap<>(); private int nextId = 1; public User create(String name, String email) { User user = new User(nextId++, name, email); storage.put(user.getId(), user); return user; } public User read(int id) { return storage.get(id); } public boolean update(int id, String newName, String newEmail) { User user = storage.get(id); if (user == null) return false; user.setName(newName); user.setEmail(newEmail); return true; } public boolean delete(int id) { return storage.remove(id) != null; } public void listAll() { for (User user : storage.values()) { System.out.println(user); } } // EXPORT all users to a text file public void exportToFile(String filename) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { for (User user : storage.values()) { bw.write(user.toString()); bw.newLine(); } System.out.println("โœ… Export complete: " + filename); } catch (IOException e) { System.out.println("Error exporting: " + e.getMessage()); } } }

Main.java (integration practice)

java
public class Main { public static void main(String[] args) { UserRepository repo = new UserRepository(); // CREATE users repo.create("Alice", "alice@example.com"); repo.create("Bob", "bob@example.com"); repo.create("Charlie", "charlie@example.com"); // LIST all users System.out.println("All users:"); repo.listAll(); // EXPORT to file repo.exportToFile("users.txt"); } }

Mini Project Summary

  • Learned how to combine multiple topics: CRUD + collections + I/O + LocalDateTime.

  • Used Map for simple in-memory database.

  • Added export to file using BufferedWriter.

  • Added creation timestamps using LocalDateTime.

This small structure can scale to real databases, web APIs, and even Spring Boot REST services.
Itโ€™s a perfect starting point for your first real CRUD back-end logic!