Java记事本

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();
	}
}

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值