ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java管理系统(存储在文件)

2019-08-19 20:03:12  阅读:186  来源: 互联网

标签:存储 java String no int 管理系统 sc msg public


简介

制作一个简单的学生管理系统,通过键盘选择操作进行添加学生(学 号,姓名,性别,年龄)的信息,这些信息存储在一个文件里面,同时还可以进行 查询全体学生,修改学生信息,删除学生的操作, 要求按照学 生年龄从大到小排序。

代码

package com.softeem.project4;

public class Student {
	private int no;
	private String name;
	private String sex;
	private int age;

	public Student() {
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

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

	public Student(int no, String name, String sex, int age) {
		super();
		this.no = no;
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	@Override
	public String toString() {
		return "no=" + no + "\t" + "name=" + name + "\t" + " sex=" + sex + "\t" + " age=" + age;
	}
}
package com.softeem.project4;

import java.util.ArrayList;

public class StudentManage {
	static ArrayList<Student> list = new ArrayList<>();

	public void addS(Student s) {
		list.add(s);
	}

	public ArrayList<Student> findAll() {
		list.sort((a, b) -> a.getAge() - b.getAge());
		return list;
	}

	public Student find(int no) {
		Student stu = null;
		for (Student s : list) {
			if (s.getNo() == no) {
				stu = s;
				break;
			}
		}
		return stu;
	}

	public boolean modify(int no, Student s) {
		Student stu = find(no);
		if (stu != null) {
			stu.setNo(s.getNo());
			stu.setName(s.getName());
			stu.setSex(s.getSex());
			stu.setAge(s.getAge());
			return true;
		}
		return false;
	}

	public boolean delete(int no) {
		Student stu = find(no);
		if (stu != null) {
			return list.remove(stu);
		}
		return false;
	}
}
package com.softeem.project4;

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

public class Test {
	private StudentManage sm = new StudentManage();
	private Scanner sc;

	public void menu() throws IOException {
		msg("*****1.添加学生*****2.修改学生*****3.查询全体学生*****4.删除学生*****5.退出*****");
		msg("请选择操作:");
		start();
	}

	private void start() throws IOException {
		sc = new Scanner(System.in);
		String i = sc.next();
		switch (i) {
		case "1":
			add();
			break;
		case "2":
			update();
			break;
		case "3":
			list();
			break;
		case "4":
			delete();
			break;
		case "5":
			exit();
			break;
		default:
			msg("请输入正确的操作指令!!!");
			break;
		}
		menu();
	}

	private void exit() {
		sc = new Scanner(System.in);
		msg("是否确定退出?(Y/N)");
		String op = sc.next();
		if (op.equalsIgnoreCase("y")) {
			msg("再你妈的见");
			System.exit(1);
		}
	}

	private void delete() throws IOException {
		sc = new Scanner(System.in);
		msg("请输入学生学号:");
		int no = sc.nextInt();
		if (sm.delete(no)) {
			msg("删除成功!");
			addFile(sm.findAll());
		} else {
			msg("删除失败!");
		}
	}

	private void list() {
		msg("学号\t姓名\t性别\t年龄");
		for (Student s : sm.findAll()) {
			msg(s);
		}
	}

	private void update() throws IOException {
		sc = new Scanner(System.in);
		msg("请输入学生学号:");
		int s1 = sc.nextInt();
		if (sm.find(s1) == null) {
			msg("该学生不存在,请重新输入");
			update();
			return;
		}
		msg("请输入学生姓名:");
		String s2 = sc.next();
		msg("请输入学生性别:");
		String s3 = sc.next();
		msg("请输入学生年龄:");
		int s4 = sc.nextInt();
		Student s = new Student(s1, s2, s3, s4);
		if (sm.modify(s1, s)) {
			msg("修改成功!");
			addFile(sm.findAll());
		} else {
			msg("修改失败!");
		}
	}

	private void add() throws IOException {
		msg("请输入学生学号:");
		sc = new Scanner(System.in);
		int s1 = sc.nextInt();
		if (sm.find(s1) != null) {
			msg("该学生已存在,请重新输入");
			add();
			return;
		} else {
			msg("请输入学生姓名:");
			String s2 = sc.next();
			msg("请输入学生性别:");
			String s3 = sc.next();
			msg("请输入学生年龄:");
			int s4 = sc.nextInt();
			Student s = new Student(s1, s2, s3, s4);
			sm.addS(s);
			addFile(sm.findAll());
			msg("添加成功");
		}
	}

	public void addFile(ArrayList<Student> list) throws IOException {
		list.sort((a, b) -> a.getAge() - b.getAge());
		FileOutputStream fos = new FileOutputStream("src/student");
		for (Student stu : list) {
			fos.write((stu.toString() + "\n").getBytes());
		}
		fos.close();
	}

	public void msg(Object obj) {
		System.out.println(obj);
	}

	public static void main(String[] args) throws IOException {
		new Test().menu();
	}
}

运行结果

*****1.添加学生*****2.修改学生*****3.查询全体学生*****4.删除学生*****5.退出*****
请选择操作:

总结

上面代码还没有实现从文件里读取数据并显示到控制台的功能,只是在控制台中展示了数据,并且将展示的数据写入到文件中了

标签:存储,java,String,no,int,管理系统,sc,msg,public
来源: https://blog.csdn.net/qq_45414508/article/details/99755215

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有