Java作业~记事本
作业要求:
- 构建记事本类,该类能存储不定数量的记录;能获得已经存储的记录数量;能追加记录;能展示已经存储的全部记录或其中任何一条记录;能删除已经存储的全部记录或其中任何一条记录。
- 构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用户的操作请求。
程序应具有良好的人机交互性能,即:程序应向用户提示功能说明,并可根据用户的功能选择,执行对应的功能,并给出带详细描述信息的最终执行结果。
import java.util.*;
public class MemoTest {
static int id = 0;
static String help =
"read this\n" +
"Type 0 to get help\n" +
"Type 1 to write new memo\n" +
"Type 2 to show all memos\n" +
"Type 3 to show chosen memo\n" +
"Type 4 to delete chosen memo\n" +
"Type 5 to check the curent number of memo" +
"Type 6 to end the program $ del all memo";
public static void write(Map<Integer, String> memo, String memothing) {
memo.put(id++, memothing);
System.out.println("已添加");
}
public static int GetId(Map<Integer, String> memo) {
Iterator<Integer> iterator = memo.keySet().iterator();
int id = 0;
while(iterator.hasNext()) {
id++;
}
return id;
}
public static void PrintTheMemo(Map<Integer, String> memo, int id) {
Iterator<Integer> iterator = memo.keySet().iterator();
boolean checked = false;
while(iterator.hasNext()) {
if(iterator.next() == id) {
System.out.println(memo.get(id));
checked = true;
}
}
if(!checked) {
System.out.println("未找到相关内容!");
}
}
public static void ShowAllMemo(Map<Integer, String> memo) {
Iterator<Integer> iterator = memo.keySet().iterator();
while(iterator.hasNext()) {
id = iterator.next();
System.out.println(id +":"+ memo.get(id));
}
}
public static void DelTheMemo(Map<Integer, String> memo, int id) {
String trush = memo.remove(id);
if(trush == null) {
System.out.println("无法移除不存在的memo");
}
else {
System.out.println("以将“"+ trush + "”移除");
}
}
public static void main(String[] args) {
Map<Integer, String> memo = new HashMap<Integer, String>();
Scanner in = new Scanner(System.in);
System.out.println("input 0 for beginner");
String memothing = new String();
while(true) {
System.out.println("请输入指令:");
int a = in.nextInt();
in.nextLine();
if(a == 0) { //get help
System.out.println(help);
}
if(a == 1) { //write a memo
System.out.println("请输入memo:");
memothing= in.nextLine();
write(memo, memothing);
}
if(a == 2) { //print all memo
ShowAllMemo(memo);
}
if(a == 3) {
System.out.println("请输入查找的memo编号");
id = in.nextInt();
PrintTheMemo(memo, id);
}
if(a == 4) {
ShowAllMemo(memo);
System.out.println("请输入移除的memo的编号");
id = in.nextInt();
DelTheMemo(memo, id);
}
if(a == 5) { //getid
System.out.println("现在共记录"+GetId(memo)+"个");
}
if(a == 6) { //end
System.out.println("已关闭并清除所有数据!");
break;
}
}
in.close();
}
}