用Java 写出简单代码包括古诗词朝代管理,古诗词题材管理,和古诗词的增删改查
时间: 2024-02-29 21:51:17 浏览: 47
好的,以下是简单的Java代码,实现古诗词朝代管理、古诗词题材管理、和古诗词的增删改查功能:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class PoemManagementSystem {
private Map<String, List<String>> dynastyMap; // 朝代和古诗词列表的映射
private Map<String, List<String>> themeMap; // 题材和古诗词列表的映射
public PoemManagementSystem() {
dynastyMap = new HashMap<>();
themeMap = new HashMap<>();
}
// 添加古诗词
public void addPoem(String dynasty, String theme, String poem) {
List<String> dynastyList = dynastyMap.getOrDefault(dynasty, new ArrayList<>());
List<String> themeList = themeMap.getOrDefault(theme, new ArrayList<>());
dynastyList.add(poem);
themeList.add(poem);
dynastyMap.put(dynasty, dynastyList);
themeMap.put(theme, themeList);
System.out.println("成功添加古诗词:" + poem);
}
// 删除古诗词
public void deletePoem(String poem) {
for (List<String> poemList : dynastyMap.values()) {
if (poemList.remove(poem)) {
System.out.println("成功删除古诗词:" + poem);
break;
}
}
for (List<String> poemList : themeMap.values()) {
if (poemList.remove(poem)) {
break;
}
}
}
// 修改古诗词
public void updatePoem(String oldPoem, String newPoem) {
for (List<String> poemList : dynastyMap.values()) {
int index = poemList.indexOf(oldPoem);
if (index != -1) {
poemList.set(index, newPoem);
break;
}
}
for (List<String> poemList : themeMap.values()) {
int index = poemList.indexOf(oldPoem);
if (index != -1) {
poemList.set(index, newPoem);
break;
}
}
System.out.println("成功将古诗词 " + oldPoem + " 修改为 " + newPoem);
}
// 根据朝代查询古诗词
public void queryPoemByDynasty(String dynasty) {
List<String> poemList = dynastyMap.getOrDefault(dynasty, new ArrayList<>());
if (poemList.isEmpty()) {
System.out.println("没有查询到 " + dynasty + " 的古诗词");
} else {
System.out.println(dynasty + " 的古诗词有:");
for (String poem : poemList) {
System.out.println(poem);
}
}
}
// 根据题材查询古诗词
public void queryPoemByTheme(String theme) {
List<String> poemList = themeMap.getOrDefault(theme, new ArrayList<>());
if (poemList.isEmpty()) {
System.out.println("没有查询到 " + theme + " 的古诗词");
} else {
System.out.println(theme + " 的古诗词有:");
for (String poem : poemList) {
System.out.println(poem);
}
}
}
public static void main(String[] args) {
PoemManagementSystem pms = new PoemManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加古诗词 2.删除古诗词 3.修改古诗词 4.查询古诗词");
int op = scanner.nextInt();
scanner.nextLine();
switch (op) {
case 1:
System.out.println("请输入古诗词所属朝代:");
String dynasty = scanner.nextLine();
System.out.println("请输入古诗词所属题材:");
String theme = scanner.nextLine();
System.out.println("请输入古诗词:");
String poem = scanner.nextLine();
pms.addPoem(dynasty, theme, poem);
break;
case 2:
System.out.println("请输入要删除的古诗词:");
String poemToDelete = scanner.nextLine();
pms.deletePoem(poemToDelete);
break;
case 3:
System.out.println("请输入要修改的古诗词:");
String oldPoem = scanner.nextLine();
System.out.println("请输入修改后的古诗词:");
String newPoem = scanner.nextLine();
pms.updatePoem(oldPoem, newPoem);
break;
case 4:
System.out.println("请选择查询方式:1.按朝代查询 2.按题材查询");
int queryOp = scanner.nextInt();
scanner.nextLine();
switch (queryOp) {
case 1:
System.out.println("请输入要查询的朝代:");
String dynastyToQuery = scanner.nextLine();
pms.queryPoemByDynasty(dynastyToQuery);
break;
case 2:
System.out.println("请输入要查询的题材:");
String themeToQuery = scanner.nextLine();
pms.queryPoemByTheme(themeToQuery);
break;
default:
System.out.println("无效的操作");
}
break;
default:
System.out.println("无效的操作");
}
}
}
}
```
阅读全文