编写一个Java程序,程序提供记事本功能:
构建记事本类,该类能存储不定数量的记录;能获得已经存储的记录数量;能追加记录;能展示已经存储的全部记录或其中任何一条记录;能删除已经存储的全部记录或其中任何一条记录。
构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用户的操作请求。
程序应具有良好的人机交互性能,即:程序应向用户提示功能说明,并可根据用户的功能选择,执行对应的功能,并给出带详细描述信息的最终执行结果。10)构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用户的操作请求。
1.该程序主要要满足6个功能:1.添加记录;2. 查看全部记录;3. 查看某一条记录;4. 删除全部记录;5. 删除某一条记录;6。显示记录数量
2.该程序应该有良好的交互性。
为了实现上述目标,我们需要创建2个类,NoteBook类用来编写记事本的功能, TestSystem为测试类,用来满足和用户的交互
NoteBook类
package Note;
import java.util.ArrayList;
public class NoteBook {
private ArrayList<String> notes=new ArrayList<String>();
//存储记录
public void addNote(String note) {
notes.add(note);
}
//获取已储存的记录数量
public void getSize() {
System.out.println("数量为:"+notes.size());;
}
//展示记录
public void getAllNotes() {
if(notes.size()==0) {
System.out.println("没有记录");
}
else {
for(int i=0;i<notes.size();i++) {
System.out.println((i+1)+"."+notes.get(i));
}
}
}
public void getNoteAtIndex(int index) {
if(index<0||index>=notes.size()) {
System.out.println("索引无效");
}
else {
System.out.println((index+1)+"."+notes.get(index));
}
}
//删除记录
public void deleteAllNotes() {
notes.clear();
System.out.println("已删除全部记录");
}
public void deleteNoteAtIndex(int index) {
if (index<0||index>=notes.size()) {
System.out.println("索引无效");
}
else {
notes.remove(index);
System.out.println("已删除记录");
}
}
}
TestSystem类
package Note;
import java.util.Scanner;
public class TestSystem {
public static void main(String[] args) {
NoteBook notepad=new NoteBook();
Scanner scanner=new Scanner(System.in);
System.out.println("欢迎使用记事本");
System.out.println("输入数字选择功能");
System.out.println("1. 添加记录");
System.out.println("2. 查看全部记录");
System.out.println("3. 查看某一条记录");
System.out.println("4. 删除全部记录");
System.out.println("5. 删除某一条记录");
System.out.println("6. 显示记录数量");
System.out.println("0. 退出");
while(true) {
System.out.println("选择功能:");
int choice =scanner.nextInt();
scanner.nextLine();//读取换行符
switch(choice) {
case 1:
System.out.println("输入记录内容:");
String note=scanner.nextLine();
notepad.addNote(note);
System.out.println("已添加记录");
break;
case 2:
notepad.getAllNotes();
break;
case 3:
System.out.println("输入记录索引:");
int index = scanner.nextInt();
scanner.nextLine(); // 读取换行符
notepad.getNoteAtIndex(index - 1);
break;
case 4:
notepad.deleteAllNotes();
break;
case 5:
System.out.println("输入记录索引:");
index = scanner.nextInt();
scanner.nextLine(); //读取换行符
notepad.deleteNoteAtIndex(index - 1);
break;
case 6:
notepad.getSize();
break;
case 0:
System.out.println("谢谢使用");
System.exit(0);
default:
System.out.println("无效选择");
break;
}
}
}
}
该程序创建了一个NoteBook类,用于存储和管理记录,包括添加、查看全部、查看特定、删除全部和删除特定记录的功能。TestSystem类实现了用户交互,允许用户通过命令选择执行相应操作。程序注重人机交互,提供清晰的操作指引和反馈。

1840

被折叠的 条评论
为什么被折叠?



