面向对象大作业-自主选题 教务管理系统

自主选题:教务管理系统;
代码实现:对学生实现增删改查;

具体代码如下

Students类的创建

import java.util.*;

import java.io.*;
public class Students {
	private String Class;//学生所在班级;
	private String id;//学生学号;
	private String name;//学生姓名;
	private static ArrayList<Students> Stu=new ArrayList<>();
    public Students(String Class, String id, String name) {
        this.Class = Class;
        this.id = id;
        this.name = name;
    }
    public Students() {
    	
    }
    
    @Override
	public String toString() {
		return "Students [Class=" + Class + ", id=" + id + ", name=" + name + "]";
	}

	static{
		FileReader fr = null;
		try {
            File file = new File("students.txt");
            fr = new FileReader(file);
            BufferedReader in = new BufferedReader(fr);
            String line;
            while ((line = in.readLine()) != null) {

                String[] split = line.split(" ");
                Students s =new Students(split[0],split[1],split[2]);
                Stu.add(s);
            	}
            }catch (IOException e) {
            e.printStackTrace();
            }finally {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
	}

在Students类中包含有对Students对象的处理方法;

public void searchStudent() {//查找学生,输入学生姓名从而输出学生的班级以及学号信息,若无此学生输出“查无此学生”;
    	Scanner stuName=new Scanner(System.in);
    	String stName =stuName.nextLine();
    	int k=0;
    	for(int i=0;i<Stu.size();i++) {
    		if(Stu.get(i).name.contains(stName)) {
    	    	System.out.println(Stu.get(0));
    	    	k++;
    		}
    	}
    	if(k==0) {
			System.out.println("查无此学生");
		}
    }
    public void showStudents() {//将所有学生信息展示出来;
    	for(int i=0;i<Stu.size();i++) {
    		System.out.println(Stu.get(i));
    	}
    }
    public void addStudent(String stu) {//添加学生,输入学生信息添加;
    	try {
    		File file =new File("students.txt");
    		FileWriter fw=new FileWriter(file,true);
    		BufferedWriter bw = new BufferedWriter(fw);
    		bw.write("\n");
    		bw.write(stu);
    		bw.flush();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
    }
    public void removeStudent(String stu) {//输入学生姓名删除学生信息;
    	for(int i=0;i<Stu.size();i++) {
    		if(Stu.get(i).name.contains(stu)) {
    			Stu.remove(i);
    		}
    	}
    	try {
    		File file =new File("students.txt");
    		
    		FileWriter fw=new FileWriter(file);
    		BufferedWriter bw = new BufferedWriter(fw);
    		bw.write("");
    		bw.flush();
    		bw.close();
    		for(int i=0;i<Stu.size();i++) {
        		FileWriter f=new FileWriter(file,true);
        		BufferedWriter b = new BufferedWriter(f);
        		b.write(Stu.get(i).Class+" "+Stu.get(i).id+" "+Stu.get(i).name+"\n");
        		b.flush();
    		}
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    public void changeStudengt(String stu,String stud) {//更改学生信息,通过名字查找;
    	String[] s= stud.split(" ");
    	for(int i=0;i<Stu.size();i++) {
    		if(Stu.get(i).name.contains(stu)) {
    			Stu.get(i).Class=s[0];
    			Stu.get(i).id=s[1];
    			Stu.get(i).name=s[2];
    		}else {
    			System.out.println("查无此学生。");
    		}
    		try {
        		File file =new File("students.txt");
        		
        		FileWriter fw=new FileWriter(file);
        		BufferedWriter bw = new BufferedWriter(fw);
        		bw.write("");
        		bw.flush();
        		bw.close();
        		for(int j=0;j<Stu.size();j++) {
            		FileWriter f=new FileWriter(file,true);
            		BufferedWriter b = new BufferedWriter(f);
            		b.write(Stu.get(j).Class+" "+Stu.get(j).id+" "+Stu.get(j).name+"\n");
            		b.flush();
        		}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

Menu类:

public class Menu {
	void showMesu() {
		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.更改学生信息");
	}
}

主类:

	public static void main(String[] args) {
		Students students=new Students();
		Scanner sc=new Scanner(System.in);
		Menu mesu=new Menu();
		mesu.showMesu();
		int c=sc.nextInt();
		switch(c) {
			case 1:
		    	System.out.println("请输入学生姓名");
				students.searchStudent();
				break;
			case 2:
				students.showStudents();
				break;
			case 3:
				System.out.println("请输入添加的学生信息");
				Scanner st=new Scanner(System.in);
				students.addStudent(st.nextLine());
				System.out.println("添加成功");
				break;
			case 4:
				System.out.println("请输入学生姓名");
				Scanner sc1=new Scanner(System.in);
				students.removeStudent(sc1.nextLine());
				System.out.println("删除成功");
				break;
			case 5:
				System.out.println("请输入待修改的学生姓名");
				Scanner sc2=new Scanner(System.in);
				String stu=sc2.next();
				System.out.println("请输入修改后的学生信息");
				Scanner sc3=new Scanner(System.in);
				String stud=sc3.nextLine();
				students.changeStudengt(stu, stud);
				System.out.println("修改成功。");
				
				
		}
		}	

文本内容如下:

在这里插入图片描述

运行测试

增加学生:

在这里插入图片描述
在这里插入图片描述

查找学生:

在这里插入图片描述

更改学生信息:

在这里插入图片描述
在这里插入图片描述

删除学生

在这里插入图片描述
在这里插入图片描述
代码基本实现了学生管理功能,还可增添学科类进而实现对课程的管理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值