HBNU-School/Java/TodoListApp/TodoListManager.java
2024-10-22 09:49:04 +08:00

124 lines
4.2 KiB
Java

package TodoListApp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// 待办事项管理类
class TodoListManager {
private final List<TodoItem> todoList;
private int nextId;
public TodoListManager() {
todoList = new ArrayList<>();
nextId = 1; // 起始编号
}
// 添加待办事项
public void addTodo(String description) throws EmptyDescriptionException{ //同时声明可能抛出的异常类型
if(description == null || description.trim().isEmpty()){
EmptyDescriptionException e = new EmptyDescriptionException("待办事项的描述不能为空。");
throw e;
}
try {
string sql = "insert into todos (description) values('"+description+"')"
system.out.println(sql);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql)
}
TodoItem item = new TodoItem(nextId++, description);
todoList.add(item);
System.out.println("待办事项已添加: " + item);
}o
// 列出所有待办事项
public void listTodos() {
if (todoList.isEmpty()) {
System.out.println("当前没有待办事项。");
return;
}
System.out.println("\n===== 待办事项列表 =====");
for (TodoItem item : todoList) {
System.out.println(item);
}
}
// 删除指定编号的待办事项
public void removeTodo(int id) throws TodoNotFoundException{
boolean removed = false;
for (TodoItem item : todoList) {
if (item.getId() == id) {
todoList.remove(item);
System.out.println("已删除待办事项: " + item);
removed = true;
break;
}
}
if (!removed) {
throw new TodoNotFoundException("未找到编号为 " + id + " 的待办事项。");
//System.out.println("未找到编号为 " + id + " 的待办事项。");
}
}
public void markAsCompleted(int id) {
TodoItem item = null;
for (TodoItem todo : todoList) {
if (todo.getId() == id) {
item = todo;
break;
}
}
if (item != null) {
if (!item.isCompleted()) {
item.setCompleted(true);
System.out.println("待办事项已标记为完成: " + item);
} else {
System.out.println("待办事项已完成,无需重复标记。");
}
}
else {
System.out.println("未找到编号为:" + id + " 的待办事项。");
}
}
public void saveToFile(String filename){
try {
File file = new File(filename);
FileOutputStream fos = new FileOutputStream(file);
for (TodoItem item : todoList) {
String line = item.getId() + ":" + item.getDescription() + "\n";
fos.write(line.getBytes());
}
System.out.println("待办事项已经保存到文件:" + filename);
fos.close();
}
catch (IOException e){
System.out.println("待办事项保存失败:" + e.getMessage());
}
}
private void backupToFile(String backupFilename) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
String timestamp = now.format(formatter);
String fullBackupFilename = backupFilename + "_" + timestamp + ".bak";
try (FileOutputStream fos = new FileOutputStream(new File(fullBackupFilename))) {
for (TodoItem item : todoList) {
String line = item.getId() + ":" + item.getDescription() + ":" + (item.isCompleted() ? "完成" : "未完成") + "\n";
fos.write(line.getBytes());
}
System.out.println("待办事项已备份到文件:" + fullBackupFilename);
} catch (IOException e) {
System.out.println("待办事项备份失败:" + e.getMessage());
}
}
}