Set exercise-Map part, set map part
Exercise:
Enter the name of the poem to query the content of the poem. When you enter exit, exit the program, such as Chunxiao, sleep, and goose ".
package CollectionPart;public class Poetry { private String title; private String poet; private String content; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPoet() { return poet; } public void setPoet(String poet) { this.poet = poet; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Poetry [title=" + title + ", poet=" + poet + ", content=" + content + "]"; } public Poetry(String title, String poet, String content) { super(); this.title = title; this.poet = poet; this.content = content; } public Poetry() { super(); }}
Package CollectionPart; import java. util. hashMap; import java. util. map; import java. util. summary; public class Map1 {public static void main (String [] args) {Poetry p1 = new Poetry ("Chunxiao", "Meng Haoran", "chunmian doesn't know, it's everywhere. The amount of rain and rain in the night is unknown. "); Poetry p2 = new Poetry (" quiet night thinking "," Li Bai "," Moonlight in front of the bed, suspected to be frost on the ground. Look up at the moon and look down at your hometown. "); Poetry p3 = new Poetry (" wing goose "," Luo binwang "," goose, Qu Xiang Tian ge. White hair float green water, red palm dial clear wave. "); Map <String, Poetry> myMap = new HashMap <String, Poetry> (); myMap. put (p1.getTitle (), p1); myMap. put (p2.getTitle (), p2); myMap. put (p3.getTitle (), p3); sums in = new loads (System. in); String string = null; do {System. out. println ("enter the name of the poem to be searched:"); String name = in. nextLine (); findThePoem (myMap, name); System. out. println ("Continue search? "); String = in. nextLine ();} while (string. equals ("yes");} private static void findThePoem (Map <String, Poetry> myMap, String name) {if (myMap. containsKey (name) {System. out. println ("Poetry content:"); System. out. println (myMap. get (name ). getContent ();} else {System. out. println ("the poem does not exist, or the collection is incomplete. ");}}}