File input and output buffer stream IO comprehensive exercise - student management system file version

Comprehensive exercises (1)

  • Keyboard input 3 student information(Student number ,Name,Age, City of residence) is deposited into the collection. Then traverse the collection and store each student information into a text file(Each student information is one line, and define the split mark yourself)

Student category:

package com.itheima;
/*
 * 标准的学生类
 */
public class Student {
	//学号
	private String id;
	//姓名
	private String name;
	//年龄
	private String age;
	//居住地
	private String address;
	
	public Student() {
		
	}

	public Student(String id, String name, String age, String address) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
}

Test class:

  

Comprehensive exercises (2)

  • Student management system file version

 The left side is the function of the student information management system, which mainly includes the following functional codes:

* A: Define student class

* B: Code writing of the main interface of the student management system

* C: View all students' code writing in the student management system

* D: Code writing for adding students to the student management system

* E: Code writing for deleting students in the student management system

* F: Modification of student code writing for student management system

Analysis: The left side is the writing of program functions, and a collection is used for temporary storage in the middle. The right side is a file, which is written into the file by writing data. Deletion is to retrieve data from the collection, retrieve the data, and delete the collection. At the same time, delete the data of which row of the file, modify the data and find that row to modify, and then save it to the collection after modification.

Analysis of main interface function implementation: Mainly divided into 5 functions, namely viewing student information, adding student information, deleting student information, modifying student information, exiting, using the Scanner class Enter your own choices from the keyboard, which are 1, 2, 3, 4, and 5, corresponding to the corresponding function methods. Use the Switch cases statement to select, and use While(true) to implement repeated selections.

while(true) {
			//这是学生管理系统的主界面
			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("请输入你的选择:");
			//创建键盘录入对象
			Scanner sc = new Scanner(System.in);
			String choiceString = sc.nextLine();
			//用switch语句实现选择
			switch(choiceString) {
			case "1":
				//查看所有学生
				findAllStudent(fileName);
				break;
			case "2":
				//添加学生
				addStudent(fileName);
				break;
			case "3":
				//删除学生
				deleteStudent(fileName);
				break;
			case "4":
				//修改学生
				updateStudent(fileName);
				break;
			case "5":
			default:
				System.out.println("谢谢你的使用");
				System.exit(0); //JVM退出
				break;
			}

Read data from the file into the collection:Requires two parameters - file name and collection name, creates an input buffer stream object, creates a student class object, and adds it into the data collection.

//从文件中读数据到集合
	public static void readData(String fileName,ArrayList<Student> array) throws IOException {
		//创建输入缓冲流对象
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		
		String line;
		while((line=br.readLine())!=null) {
			String[] datas = line.split(",");			//001,向问天,20,北京
			Student s = new Student();
			s.setId(datas[0]);
			s.setName(datas[1]);
			s.setAge(datas[2]);
			s.setAddress(datas[3]);
			array.add(s);
		}
		
		br.close();
	}

Write data from the collection to the file:First create an output buffer stream object, traverse to obtain each element, then create a box of StringBuildeer objects, and finally convert it into string. a>

public static void readData(String fileName,ArrayList<Student> array) throws IOException {
		//创建输入缓冲流对象
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		
		String line;
		while((line=br.readLine())!=null) {
			String[] datas = line.split(",");			//001,向问天,20,北京
			Student s = new Student();
			s.setId(datas[0]);
			s.setName(datas[1]);
			s.setAge(datas[2]);
			s.setAddress(datas[3]);
			array.add(s);
		}
		
		br.close();
	}

View the implementation of all student functions:First we create a collection object called array, read the data from the file into the file, first determine whether there is data, if there is no data, Prompt.

//查看所有学生
	public static void findAllStudent(String fileName) throws IOException {
		//创建集合对象
		ArrayList<Student> array = new ArrayList<Student>();
		//从文件中把数据读取到集合中
		readData(fileName, array);
		
		//首先来判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续往下执行
		if(array!=null&&array.size() == 0) {
			System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");
			return;
		}
		
		//\t 其实就是一个tab键的位置
		System.out.println("学号\t姓名\t年龄\t居住地");
		for(int x=0; x<array.size(); x++) {
			Student s = array.get(x);
			System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
		}
	}

Add students: First create a collection object, use readData to read the data from the file into the collection, then create a keyboard entry object, determine whether it is occupied, and enter the information Put it into the student object, add the student object as an element to the collection, and then use writeData to write the data in the collection back to the file.

public static void addStudent(String fileName) throws IOException {
		//创建集合对象
		ArrayList<Student> array = new ArrayList<Student>();
		//从文件中把数据读取到集合中
		readData(fileName, array);
				
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
		//为了让id能够被访问到,我们就把id定义在了循环的外面
		String id;
		
		//为了让代码能够回到这里,用循环
		while(true) {
			System.out.println("请输入学生学号:");
			//String id = sc.nextLine();
			id = sc.nextLine();
			
			//判断学号有没有被人占用
			//定义标记
			boolean flag = false;
			//遍历集合,得到每一个学生
			for(int x=0; x<array.size(); x++) {
				Student s = array.get(x);
				//获取该学生的学号,和键盘录入的学号进行比较
				if(s.getId().equals(id)) {
					flag = true; //说明学号被占用了
					break;
				}
			}
			
			if(flag) {
				System.out.println("你输入的学号已经被占用,请重新输入");
			}else {
				break; //结束循环
			}
		}
		
		
		System.out.println("请输入学生姓名:");
		String name = sc.nextLine();
		System.out.println("请输入学生年龄:");
		String age = sc.nextLine();
		System.out.println("请输入学生居住地:");
		String address = sc.nextLine();
		
		//创建学生对象
		Student s = new Student();
		s.setId(id);
		s.setName(name);
		s.setAge(age);
		s.setAddress(address);
		
		//把学生对象作为元素添加到集合
		array.add(s);
		//把集合中的数据重新写回到文件
		writeData(fileName, array);
		
		//给出提示
		System.out.println("添加学生成功");
	}
	

Modify students: First create a collection object, read the data from the file into the collection, enter a student ID on the keyboard, search in the collection to see if there is a student using the student ID, and if so, modify the student

	//修改学生
	public static void updateStudent(String fileName) throws IOException {
		//创建集合对象
		ArrayList<Student> array = new ArrayList<Student>();
		//从文件中把数据读取到集合中
		readData(fileName, array);
		
		//修改学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就修改该学生
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要修改的学生的学号:");
		String id = sc.nextLine();
		
		//定义一个索引
		int index = -1;
		
		//遍历集合
		for(int x=0; x<array.size(); x++) {
			//获取每一个学生对象
			Student s = array.get(x);
			//拿学生对象的学号和键盘录入的学号进行比较
			if(s.getId().equals(id)) {
				index = x;
				break;
			}
		}
		
		if(index == -1) {
			System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新你的选择");
		}else {
			System.out.println("请输入学生新姓名:");
			String name = sc.nextLine();
			System.out.println("请输入学生新年龄:");
			String age = sc.nextLine();
			System.out.println("请输入学生新居住地:");
			String address = sc.nextLine();
			
			//创建学生对象
			Student s = new Student();
			s.setId(id);
			s.setName(name);
			s.setAge(age);
			s.setAddress(address);
			
			//修改集合中的学生对象
			array.set(index, s);
			//把集合中的数据重新写回到文件
			writeData(fileName, array);
			//给出提示
			System.out.println("修改学生成功");
		}
	}

Delete students: First create a collection object, read the data from the file into the collection, enter a student number on the keyboard, and search in the collection to see if there are students Use the student ID and delete the student if there is one.

//删除学生
	public static void deleteStudent(String fileName) throws IOException {
		//创建集合对象
		ArrayList<Student> array = new ArrayList<Student>();
		//从文件中把数据读取到集合中
		readData(fileName, array);
		
		//删除学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就删除该学生
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要删除的学生的学号:");
		String id = sc.nextLine();
		
		//我们必须给出学号不存在的时候的提示
		
		//定义一个索引
		int index = -1;
		
		//遍历集合
		for(int x=0; x<array.size(); x++) {
			//获取到每一个学生对象
			Student s = array.get(x);
			//拿这个学生对象的学号和键盘录入的学号进行比较
			if(s.getId().equals(id)) {
				index = x;
				break;
			}
		}
		
		if(index == -1) {
			System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新你的选择");
		}else {
			array.remove(index);
			//把集合中的数据重新写回到文件
			writeData(fileName, array);
			System.out.println("删除学生成功");
		}
		
	}
	

Guess you like

Origin blog.csdn.net/zywcxz/article/details/128840476