database #3

Merged
Ferryry merged 11 commits from database into main 2025-12-08 18:10:08 +01:00
Showing only changes of commit 0dd7fd4687 - Show all commits

View File

@@ -8,87 +8,186 @@ import java.util.*;
public class SimpleDatabaseManager implements DatabaseManager {
private boolean connected = false;
private Map<Integer, Task> temporaryTaskList = new HashMap();
private Map<Integer, Worker> temporaryWorkerList = new HashMap<>();
@Override
public void saveTask(Task task) throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (!temporaryTaskList.containsKey(task.getTaskId())) {
temporaryTaskList.put(task.getTaskId(), task);
} else {
throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists.");
}
}
@Override
public void saveTasks(Collection<Task> tasks) throws SQLException {
throw new SQLException();
for (Task task : tasks) {
saveTask(task);
}
}
@Override
public void saveWorker(Worker worker) throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (!temporaryWorkerList.containsKey(worker.getId())) {
temporaryWorkerList.put(worker.getId(), worker);
} else {
throw new SQLException("[INSERTION FAILED] Task with the same Task ID already exists.");
}
}
@Override
public void saveWorkers(Collection<Worker> workers) throws SQLException {
throw new SQLException();
for (Worker worker : workers) {
saveWorker(worker);
}
}
@Override
public void updateTask(int taskId, Task newTaskObject) throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryTaskList.containsKey(taskId) && newTaskObject.getTaskId() == taskId) {
temporaryTaskList.remove(taskId);
temporaryTaskList.put(taskId, newTaskObject);
return;
}
throw new SQLException("[UPDATE FAILED] Task ID not found OR Task ID does not equal New Task ID.");
}
@Override
public void updateWorker(int workerId, Worker newWorkerObject) throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryWorkerList.containsKey(workerId) && newWorkerObject.getId() == workerId) {
temporaryWorkerList.remove(workerId);
temporaryWorkerList.put(workerId, newWorkerObject);
return;
}
throw new SQLException("[UPDATE FAILED] Worker ID not found OR Worker ID does not equal New Worker ID.");
}
@Override
public void deleteTask(int taskId) throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryTaskList.containsKey(taskId)) {
temporaryTaskList.remove(taskId);
return;
}
throw new SQLException("[DELETION FAILED] Could not find Task ID. Nothing to delete.");
}
@Override
public void deleteWorker(int workerId) throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryWorkerList.containsKey(workerId)) {
temporaryWorkerList.remove(workerId);
return;
}
throw new SQLException("[DELETION FAILED] Could not find Worker ID. Nothing to delete.");
}
@Override
public Collection<Task> getTasks() throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryTaskList.values();
}
@Override
public Collection<Worker> getWorkers() throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryWorkerList.values();
}
@Override
public Task getTaskByTaskId(int taskId) throws SQLException {
return new Task(0, 0, null, null);
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryTaskList.containsKey(taskId)) {
return temporaryTaskList.get(taskId);
}
throw new SQLException("[SELECTION FAILED] Could not find Task ID. Nothing to get.");
}
@Override
public Worker getWorkerByWorkerId(int workerId) throws SQLException {
return new Worker(null, 0);
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
if (temporaryWorkerList.containsKey(workerId)) {
return temporaryWorkerList.get(workerId);
}
throw new SQLException("[SELECTION FAILED] Could not find Worker ID. Nothing to get.");
}
@Override
public int getTotalNumberOfTasks() throws SQLException {
return -1;
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryTaskList.size();
}
@Override
public int getTotalNumberOfWorkers() throws SQLException {
return -1;
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
return temporaryWorkerList.size();
}
public void clearDatabase() throws SQLException {
throw new SQLException();
if (!connected) {
throw new SQLException("[CONNECTION FAILED] Instance is not connected to database.");
}
temporaryTaskList.clear();
temporaryWorkerList.clear();
}
@Override
public void connect() throws SQLException {
throw new SQLException();
connected = true;
}
@Override
public void close() throws SQLException {
throw new SQLException();
connected = false;
}
}