java日历行程提醒
时间: 2023-12-18 18:30:20 浏览: 138
以下是一个使用Java实现日历行程提醒的示例:
```java
import java.util.ArrayList;import java.util.Calendar;
import java.util.List;
import java.util.Scanner;
class Event {
private String title;
private Calendar date;
public Event(String title, Calendar date) {
this.title = title;
this.date = date;
}
public String getTitle() {
return title;
}
public Calendar getDate() {
return date;
}
}
class CalendarReminder {
private List<Event> events;
public CalendarReminder() {
events = new ArrayList<>();
}
public void addEvent(String title, Calendar date) {
Event event = new Event(title, date);
events.add(event);
}
public void deleteEvent(Calendar date) {
for (Event event : events) {
if (event.getDate().equals(date)) {
events.remove(event);
break;
}
}
}
public void editEvent(Calendar date, String newTitle) {
for (Event event : events) {
if (event.getDate().equals(date)) {
event.setTitle(newTitle);
break;
}
}
}
public void showEvents(Calendar date) {
for (Event event : events) {
if (event.getDate().equals(date)) {
System.out.println(event.getTitle());
}
}
}
public void showEventsByMonth(int month) {
for (Event event : events) {
if (event.getDate().get(Calendar.MONTH) == month) {
System.out.println(event.getTitle());
}
}
}
public void searchEvents(String keyword) {
for (Event event :) {
if (event.getTitle().contains(keyword)) {
System.out.println(event.getTitle());
}
}
}
}
public class CalendarReminderApp {
public static void main(String[] args) {
CalendarReminder reminder = new CalendarReminder();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
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.println("0. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // 清除输入缓冲区的换行符
if (choice == 0) {
break;
}
switch (choice) {
case 1:
System.out.println("请输入事件标题:");
String title = scanner.nextLine();
System.out.println("请输入事件日期(格式:yyyy-MM-dd):");
String dateString = scanner.nextLine();
Calendar date = Calendar.getInstance();
String[] dateParts = dateString.split("-");
date.set(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[2]));
reminder.addEvent(title, date);
break;
case 2:
System.out.println("请输入要删除事件的日期(格式:yyyy-MM-dd):");
dateString = scanner.nextLine();
date = Calendar.getInstance();
dateParts = dateString.split("-");
date.set(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[2]));
reminder.deleteEvent(date);
break;
case 3:
System.out.println("请输入要编辑事件的日期(格式:yyyy-MM-dd):");
dateString = scanner.nextLine();
date = Calendar.getInstance();
dateParts = dateString.split("-");
date.set(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[2]));
System.out.println("请输入新的事件标题:");
title = scanner.nextLine();
reminder.editEvent(date, title);
break;
case 4:
System.out.println("请输入要显示事件的日期(格式:yyyy-MM-dd):");
dateString = scanner.nextLine();
date = Calendar.getInstance();
dateParts = dateString.split("-");
date.set(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[2]));
reminder.showEvents(date);
break;
case 5:
System.out.println("请输入要显示事件的月份(1-12):");
int month = scanner.nextInt();
reminder.showEventsByMonth(month - 1);
break;
case 6:
System.out.println("请输入关键字:");
String keyword = scanner.nextLine();
reminder.searchEvents(keyword);
break;
default:
System.out.println("无效的选择!");
break;
}
}
scanner.close();
}
}
```
阅读全文