java学生信息管理系统_io操作

本程序借鉴自:https://blog.csdn.net/qq_40819918/article/details/79947068

本程序还有三个文件

第一个 学生信息格式


public class Info {
	private String id;
	private String name;
	private String tel;
	private String score;
	
	public Info() {
		
	}
	
	
	public void setId(String id) {
		this.id = id;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setTel(String tel) {
		this.tel = tel;
	}
	
	public void setScore(String score) {
		this.score = score;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getTel() {
		return tel;
	}

	public String getScore() {
		return score;
	}
	
	
}

第二个文件 学生信息操作(增、删、改、查,排序、统计,读文件、写文件)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
import java.io.IOException;

//本程序学生信息数组因为没有填满,所以不能使用 数组名.length 获取数组长度
//只好定义一个length变量来记录数组元素数量(在插入元素时加一)
public class ScoreList {
	int maxSize = 100;//最大存储容量
	int length = 0;
	Info[] scoreList = new Info[maxSize]; // 用信息类创建数组
	Scanner sc = new Scanner(System.in);
	Random rand = new Random();
	public ScoreList() {
		
	}
	
	//测试demo,每次测试不需要重新输入数据
	public void demo() {
		for(int i=0;i<9;i++) {
			scoreList[i] = new Info();
			scoreList[i].setId((String)("00"+(i+1)));
			scoreList[i].setName((String)((char)(i+65)+""));//先加上65转换为ASCII的大写英文字母((char)(i+65)) ,然后加上""转换为字符串
			scoreList[i].setTel((String)(rand.nextInt(900000)+100000+""));
			scoreList[i].setScore(rand.nextInt(100) + "");
			length++;
		}
	}
	
	
	//添加学生
	public void add() {
		if(length < maxSize) {
			scoreList[length]  = new Info();
			
			//此处修改,id不得重复
			//System.out.println("请输入学生的学号:");
			//scoreList[length].setId(sc.next());
			
			//自动生成格式为001的id
			if(length < 9) {
				scoreList[length].setId((String)("00" + (length + 1)));
			}else if(length <99){
				scoreList[length].setId((String)("0" + (length + 1)));
			}else {
				scoreList[length].setId((String)("" + (length + 1)));
			}
			
			System.out.println("=======================================");
			//修改,姓名与电话不为空
			System.out.println("请输入学生的姓名:");
			scoreList[length].setName(sc.next());
			System.out.println("请输入学生的电话:");
			scoreList[length].setTel(sc.next());
			System.out.println("请输入学生的分数:");
			//修改,分数在【0,100】之间
			scoreList[length].setScore(sc.next());
			
			length++;
			System.out.println("添加成功");
			System.out.println("=======================================");
		}
		else {
			System.out.println("添加失败,数组已经满了");
			System.out.println("=======================================");
		}
	}
	
	//删除学生
	public void deleat() {
		int flag = -1;
		String id;
		System.out.println("=======================================");
		System.out.println("请输入学生的id");
		id = sc.next();
		if(length>0) {
			for(int i = 0;i<length;i++) {
				if(scoreList[i].getId().equals(id)) {
					flag = i;
				}
			}
			if(flag != -1) {
				for(int k = flag; k < length; k++) {
					scoreList[k] = scoreList[k + 1];
				}
				length--;
			}
			else {
				System.out.println("没有此学生");
			}
		}else {
			System.out.println("列表为空,没有学生");
			
		}
		System.out.println("=======================================");
		
	}
	
	//修改学生
	public void chance() {
		String id;
		int flag = -1;
		if(length>0) {
			System.out.println("=======================================");
			System.out.println("请输入需要修改的学生的id");
			id = sc.next();
			for(int i=0;i<length;i++) {
				if(scoreList[i].getId().equals(id)) {
					flag = i;
				}
			}
			if(flag != -1) {
				System.out.println(scoreList[flag].getId() + " " + scoreList[flag].getName()
						+ " " + scoreList[flag].getTel() + " " + scoreList[flag].getScore());
				System.out.println("请输入学生的姓名");
				scoreList[flag].setName(sc.next());
				System.out.println("请输入学生的电话");
				scoreList[flag].setTel(sc.next());
				System.out.println("请输入学生的分数");
				scoreList[flag].setScore(sc.next());
			}else {
				System.out.println("没有这个学生");
				
			}
			
		}else {
			System.out.println("列表为空,没有学生");
			
		}
		System.out.println("=======================================");
		
	}
	
	//查询学生
	public void search() {
		String id;
		boolean flag = false;
		if(length>0) {
			System.out.println("=======================================");
			System.out.println("请输入需要查询的学生的id");
			id = sc.next();
			for(int i=0;i<length;i++) {
				if(scoreList[i].getId().equals(id)) {
					System.out.println("学号\t姓名\t电话\t\t分数");
					System.out.println(scoreList[i].getId() + "\t" + scoreList[i].getName()
							+ "\t" + scoreList[i].getTel() + "\t\t" + scoreList[i].getScore());
					flag = true;
				}
			}
			if(flag == false) {
				System.out.println("没有这个学生");
				
			}
		}else {
			System.out.println("列表为空,没有学生");
			
		}
		System.out.println("=======================================");
	}
	
	//平均分数等
	public void score() {
		int max=0,min=0;
		double avg=0.0;
		if(length>0) {
			for(int i=0;i<length;i++) {
				if(Integer.parseInt(scoreList[i].getScore()) > Integer.parseInt(scoreList[max].getScore())) {
					max = i;
				}
				if(Integer.parseInt(scoreList[i].getScore()) < Integer.parseInt(scoreList[min].getScore())) {
					min = i;
				}
				avg += Integer.parseInt(scoreList[i].getScore());
			}
			System.out.println("=======================================");
			System.out.println("最高分\t最低分\t平均分");
			System.out.println(scoreList[max].getScore() + "\t" + scoreList[min].getScore() + "\t" + String.format("%.2f", (avg / length)));
			System.out.println("=======================================");
		}else {
			System.out.println("列表为空,没有学生");
		}
		
	}
	
	//升序
	public void up() {
		if(length>0) {
			Info demo = new Info();
			for(int i=1;i<length;i++) {
				for(int j=0;j<length-1;j++) {
					if(Integer.parseInt(scoreList[j].getScore()) > Integer.parseInt(scoreList[j + 1].getScore())) {
						demo = scoreList[j];
						scoreList[j] = scoreList[j+1];
						scoreList[j+1] = demo;
					}
				}
			}
			display();
		}else {
			System.out.println("列表为空,没有学生");
		}
		
	}
	
	//降序
	public void down() {
		if(length>0) {
			Info demo = new Info();
			for(int i=1;i<length;i++) {
				for(int j=0;j<length-1;j++) {
					if(Integer.parseInt(scoreList[j].getScore()) < Integer.parseInt(scoreList[j + 1].getScore())) {
						demo = scoreList[j];
						scoreList[j] = scoreList[j+1];
						scoreList[j+1] = demo;
					}
				}
			}
			display();
		}else {
			System.out.println("列表为空,没有学生");
		}
		
	}
	
	//打印列表
	public void display() {
		if(length>0) {
			System.out.println("=======================================");
			System.out.println("序号\t学号\t姓名\t电话\t\t分数");
			for(int i = 0;i < length;i++) {
				System.out.println((i + 1) + "\t" + scoreList[i].getId() + "\t" + scoreList[i].getName() + "\t" + scoreList[i].getTel() + "\t\t" + scoreList[i].getScore());
			}
			System.out.println("=======================================");
		}
		else {
			System.out.println("暂时没有数据");
		}
		
	}
	
	public void loadData() throws IOException {
		File file = new File("student_info.txt");//D:/Student_java_data/student_info.txt
		if(!file.exists()) {
			try {
				file.createNewFile();
				System.out.println("初始化成功");
			}catch (Exception e) {
				e.printStackTrace();
				System.out.println("初始化失败");
			}
		}
		
		//读取数据
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while((line = br.readLine()) != null) {
				String[] str = line.split("\t\t");
				Info stu = new Info();
				stu.setId(str[0]);
				stu.setName(str[1]);
				stu.setTel(str[2]);
				stu.setScore(str[3]);
				scoreList[length++] = stu;
				
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	//写入数据
	public void writeData() throws IOException {
		File file = new File("student_info.txt");
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			for(int i = 0; i < length; i++) {
				Info stu = scoreList[i];
				bw.write(stu.getId());
				bw.write("\t\t");
				bw.write(stu.getName());
				bw.write("\t\t");
				bw.write(stu.getTel());
				bw.write("\t\t");
				bw.write(stu.getScore());
				bw.newLine();
			}
			bw.flush();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

第三个文件 主函数

import java.io.IOException;
import java.util.Scanner;

public class Test {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		String choice = "";
		ScoreList stulist = new ScoreList();
		//获取数据
		stulist.loadData();
		//stulist.demo();//数据demo
		System.out.println("============学生信息管理系统============");
		while(choice.equals("0") == false) {
			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("7.升序");
			System.out.println("8.降序");
			System.out.println("9.导入测试数据(在文件内容为空时)");
			System.out.println("0.退出");
			System.out.println("=======================================");
			choice = sc.next();
			switch(choice) {
			//增加,删除,修改需要覆盖数据
			case "1":stulist.add();stulist.writeData();;System.out.println("请问还需要什么服务?"); break;
			case "2":stulist.display();System.out.println("请问还需要什么服务?");break;
			case "3":stulist.deleat();stulist.writeData();System.out.println("请问还需要什么服务?");break;
			case "4":stulist.chance();stulist.writeData();System.out.println("请问还需要什么服务?");break;
			case "5":stulist.search();System.out.println("请问还需要什么服务?");break;
			case "6":stulist.score();System.out.println("请问还需要什么服务?");break;
			case "7":stulist.up();System.out.println("请问还需要什么服务?");break;
			case "8":stulist.down();System.out.println("请问还需要什么服务?");break;
			case "9":stulist.demo();stulist.writeData();System.out.println("请问还需要什么服务?");break;
			case "0":System.out.println("退出程序");
			}
			
		}
		
	}

}

第一个完整的java程序,还请多多支持。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值