用Java 写出简单代码包括古诗词朝代管理,古诗词题材管理,和古诗词的增删改查
时间: 2024-02-28 10:57:53 浏览: 17
好的,以下是一个简单的Java代码,包括古诗词朝代管理,古诗词题材管理以及古诗词的增删改查操作:
```
import java.util.*;
class Dynasty {
String name;
List<Poem> poems;
public Dynasty(String name) {
this.name = name;
this.poems = new ArrayList<>();
}
public void addPoem(Poem poem) {
this.poems.add(poem);
}
public void removePoem(Poem poem) {
this.poems.remove(poem);
}
public List<Poem> getPoems() {
return this.poems;
}
}
class Poem {
String title;
String author;
String content;
String type;
public Poem(String title, String author, String content, String type) {
this.title = title;
this.author = author;
this.content = content;
this.type = type;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setContent(String content) {
this.content = content;
}
public void setType(String type) {
this.type = type;
}
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
public String getContent() {
return this.content;
}
public String getType() {
return this.type;
}
}
public class PoemManager {
Map<String, Dynasty> dynasties;
Map<String, List<Poem>> types;
public PoemManager() {
this.dynasties = new HashMap<>();
this.types = new HashMap<>();
}
public void addDynasty(String name) {
this.dynasties.put(name, new Dynasty(name));
}
public void addPoem(String dynastyName, String title, String author, String content, String type) {
Dynasty dynasty = this.dynasties.get(dynastyName);
Poem poem = new Poem(title, author, content, type);
dynasty.addPoem(poem);
if (!this.types.containsKey(type)) {
this.types.put(type, new ArrayList<>());
}
this.types.get(type).add(poem);
}
public void removePoem(String dynastyName, String title) {
Dynasty dynasty = this.dynasties.get(dynastyName);
List<Poem> poems = dynasty.getPoems();
Poem target = null;
for (Poem poem : poems) {
if (poem.getTitle().equals(title)) {
target = poem;
break;
}
}
if (target != null) {
dynasty.removePoem(target);
this.types.get(target.getType()).remove(target);
}
}
public void updatePoem(String dynastyName, String title, String author, String content, String type) {
Dynasty dynasty = this.dynasties.get(dynastyName);
List<Poem> poems = dynasty.getPoems();
Poem target = null;
for (Poem poem : poems) {
if (poem.getTitle().equals(title)) {
target = poem;
break;
}
}
if (target != null) {
target.setTitle(title);
target.setAuthor(author);
target.setContent(content);
target.setType(type);
this.types.get(target.getType()).add(target);
}
}
public List<Poem> getPoemsByType(String type) {
if (this.types.containsKey(type)) {
return this.types.get(type);
}
return new ArrayList<>();
}
}
```
这个代码中,我们定义了两个类:`Dynasty` 和 `Poem`。`Dynasty` 表示一个朝代,包含了该朝代的名称和所有的古诗词。`Poem` 表示一首古诗词,包含了标题、作者、内容和题材。
我们还定义了一个 `PoemManager` 类,用于管理所有的朝代和古诗词。通过 `addDynasty` 方法可以添加一个新的朝代,通过 `addPoem` 方法可以向指定的朝代中添加一首新的古诗词。通过 `removePoem` 方法可以删除指定朝代中的一首古诗词,通过 `updatePoem` 方法可以修改指定朝代中的一首古诗词。最后,通过 `getPoemsByType` 方法可以获取指定题材的所有古诗词。
以上是一个简单的 Java 代码,可以实现古诗词朝代管理、古诗词题材管理以及古诗词的增删改查操作。