java 保存

This commit is contained in:
GuanM 2024-10-22 09:49:04 +08:00
parent 4d824685e8
commit 6c41a66593
7 changed files with 289 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package TodoListApp;
public class EmptyDescriptionException extends Exception {
public EmptyDescriptionException(String message){
super(message);
}
}

View File

@ -0,0 +1,6 @@
package TodoListApp;
public class InvalidIdException extends Exception {
public InvalidIdException(String message){
super(message);
}
}

View File

@ -0,0 +1,37 @@
package TodoListApp;
// 待办事项类
class TodoItem {
private int id;
private String description;
private boolean isCompleted;
public TodoItem(int id, String description) {
this.id = id;
this.description = description;
this.isCompleted = false;
}
// Getter方法
public int getId() {
return id;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean completed) {
isCompleted = completed;
}
public String getDescription() {
return description;
}
// 显示待办事项信息
@Override
public String toString() {
String status = isCompleted? "[已完成]" : "[未完成]";
return String.format("[%d] %s %s", id,status, description);
}
}

View File

@ -0,0 +1,110 @@
package TodoListApp;
import java.util.Scanner;
// 主类
public class TodoListApp {
private static final String ADD_TODO = "1";
private static final String LIST_TODOS = "2";
private static final String REMOVE_TODO = "3";
private static final String MARK_AS_COMPLETED = "4";
private static final String SAVE_TODOS = "5";
private static final String EXIT = "6";
private static final TodoListManager manager = new TodoListManager();
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean exit = false;
while (!exit) {
printMenu();
String choice = scanner.nextLine();
switch (choice) {
case ADD_TODO:
addTodoUI();
break;
case LIST_TODOS:
manager.listTodos();
break;
case REMOVE_TODO:
removeTodoUI();
break;
case MARK_AS_COMPLETED:
markAsCompletedUI();
break;
case SAVE_TODOS:
manager.saveToFile("todos.dat");
break;
case EXIT:
exit = true;
System.out.println("退出程序。");
break;
default:
System.out.println("无效的选择,请重新输入。");
}
}
scanner.close();
}
// 打印菜单
private static void printMenu() {
System.out.println("\n===== 简易待办事项管理器 =====");
System.out.println("1. 添加待办事项");
System.out.println("2. 查看所有待办事项");
System.out.println("3. 删除待办事项");
System.out.println("4. 标记待办为已完成");
System.out.println("5. 保存待办事项(字节流)");
System.out.println("6. 退出");
System.out.print("请选择操作1-6");
}
// 添加待办事项的用户界面
private static void addTodoUI() {
String description = getNonEmptyInput("请输入待办事项描述:");
try {
manager.addTodo(description);
} catch (EmptyDescriptionException e) {
System.out.println("自定义异常:" + e.getMessage());
}
}
// 删除待办事项的用户界面
private static void removeTodoUI() {
System.out.print("请输入要删除的待办事项编号:");
String input = scanner.nextLine().trim();
try {
int id = Integer.parseInt(input);
manager.removeTodo(id);
} catch (NumberFormatException e) {
try {
throw new InvalidIdException("请输入有效的ID编号。");
} catch (InvalidIdException ex) {
System.out.println("自定义异常:" + ex.getMessage());
;
}
} catch (TodoNotFoundException e) {
System.out.println("自定义异常:" + e.getMessage());
}
}
//用于对用户的输入进行提示和预处理的能用方法
private static String getNonEmptyInput(String prompt) {
System.out.println(prompt);
String input = scanner.nextLine().trim();
while (input.isEmpty()) {
System.out.println("输入不能为空,请重新输入。");
System.out.println(prompt);
input = scanner.nextLine().trim();
}
return input;
}
private static void markAsCompletedUI() {
String input = getNonEmptyInput("请输入要标记为已完成的待办事项编号:");
try {
int id = Integer.parseInt(input);
manager.markAsCompleted(id);
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字编号。");
}
}
}

View File

@ -0,0 +1,123 @@
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());
}
}
}

View File

@ -0,0 +1,6 @@
package TodoListApp;
public class TodoNotFoundException extends Exception {
public TodoNotFoundException(String message){
super(message);
}
}

View File

@ -0,0 +1 @@
1:123123