学生信息管理系统Java版(信息自动生成)

文章目录

  • 前言
  • 一、学生信息管理系统内容要求
  • 二、实现思路
    • 1.构造学生对象
    • 2.构造工具类
    • 3.实现控制台交互
  • 三、代码实现
  • 四、效果展示
  • 总结


前言

本案例作为对Java基础学习后的练习,掌握集合的使用方法,字符串的处理操作以及一系列循环开关语句的运用。在eclipse编译环境下实现,体会类的封装,方法的封装与调用,适合初学者学习和理解!


提示:以下是本篇文章正文内容,下面案例可供参考

一、学生信息管理系统内容要求

  1. 存储学生对象信息
  2. 实现增、删、改、查四个功能
  3. 进行简单的封装实现交互信息

二、实现思路

1.构造学生对象

  • 创建学生类
  • 申明学生类的成员
  • 初始化成员信息
  • 建立一系列Get、Set方法及显示方法

2.构造工具类

  • 存储方式(集合)
  • 查询学生信息
  • 增加学生对象
  • 删除学生对象
  • 修改学生对象信息
  • 显示全部对象信息
  • 自动生成学生信息
  • 显示菜单
  • 提示用户输入一个学生的信息

3.实现控制台交互

	利用swicth开关与菜单的显示实现与用户的交互

三、代码实现

构造学生类

package student.jess;
/*
 * 构建学生对象类并建立Get、Set方法
 */
public class Student {
     
	private String name;
	private String no;
	private int sex;
	private int age;
	
	public Student() {
     
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, String no, int sex, int age) {
     
		super();
		this.name = name;//姓名
		this.no = no;//学号
		this.sex = sex;//性别  1是男 2为女
		this.age = age;//年龄
	}
	public String getName() {
     
		return name;
	}
	public void setName(String name) {
     
		this.name = name;
	}
	public String getNo() {
     
		return no;
	}
	public void setNo(String no) {
     
		this.no = no;
	}
	public int getSex() {
     
		return sex;
	}
	public void setSex(int sex) {
     
		this.sex = sex;
	}
	public int getAge() {
     
		return age;
	}
	public void setAge(int age) {
     
		this.age = age;
	}
	@Override
	public String toString() {
     
  	 return "|	Student [name=" + name + ", no=" + no + ", sex=" + sex + ", age=" + age +"]	|";
	}
}

构造工具类

package student.jess;
/*
 * 工具服务类
 */
import java.util.ArrayList;

import java.util.List;
import java.util.Random;
import java.util.Scanner;

import student.jess.tool.Name2;
import student.jess.tool.No;

public class Tool {
     
	public static int count=0;//记录当前集合所存储的元素
	private List<Student> stuList;//泛型  集合存储student类对象
	/*
    *构造方法初始化
    */ 
	public Tool() {
     
	 super();
		stuList=new ArrayList<Student>();
	}
	/*
	 * 存入一个学生对象
	 */
	public void addStudent(Student stu) {
     
		Student findStudent = findStudent(stu.getNo());
		if (findStudent!=null) {
     
			System.out.println("该学生已存储"+stu.toString());
		}
		else {
     
			stuList.add(stu);
			count++;
		}
		
	}
	/*
	 * 按学号查询 返回该对象
	 */
	public Student  findStudent(String no) {
     
		int k=0;
		for (int i = 0; i < stuList.size(); i++) {
     
			if (stuList.get(i).getNo().equals(no)) {
     
				System.out.println(stuList.get(i).toString());
				k++;
				return stuList.get(i);
			}
		}
		System.err.println("未找到这个学生!");
		return null;
	}
	/*
	 * 删除一个学生 按学号
	 */
	public void Delete(String no) {
     
		int i;
		for ( i = 0; i < stuList.size(); i++) {
     
			if (stuList.get(i).getNo().equals(no)) {
     
				stuList.remove(i);
				count--;
				break;
			}
		}
		if (i>stuList.size()) {
     
			
			System.err.println("未找到该学生!");
		}
	}
	/*
	 * 修改一个学生的信息
	 */
	public Student Update(Student stu) {
     
		int k=1;
		 for (int i = 0; i < stuList.size(); i++) {
     
			if (stuList.get(i).getNo().equals(stu.getNo())) {
     
				stuList.set(i, stu);
				return stuList.get(i);
			}
			k++;
		}
		 if (k>stuList.size()){
     
			System.err.println("未找到该学生");
		}
		 return null;
	}
	/*
	 * 显示集合内的信息
	 */
	public void Display() {
     
		System.out.println("=========================下面显示集合内的信息=====================");
		System.out.println("________________________________________________________________");
		for (int i = 0; i < stuList.size(); i++) {
     
			System.out.println(stuList.get(i).toString());
		}
		System.out.println("|_______________________________________________________________|");
	}
	
	/*
	 * 自动生成一定数量的学生信息
	 */
	public void information(int n) {
     
	     //生成随机数
		Random random=new Random();
		//姓名与学号的生成在工具包中单独封装为两个类(见下文)
		Name2 name2=new Name2();
		//学号传入参数 前缀 和最后控制位数0-99两位即可
		No no=new No("41940011", 2);
		for (int i = 0; i <n ; i++) {
     
			Student stu=new Student();
			stu.setName(name2.makename());
			stu.setNo(no.MakeNo(i));
			//年龄自动生成  范围在18-22岁  random的用法
			stu.setAge(random.nextInt(5)+18);
			//性别随机1或2
			stu.setSex(random.nextInt(2)+1);
			stuList.add(stu);
			count++;
		}
	}
	/*
	 * 菜单显示
	 */
	
	public void menu() {
     
		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("|7.显示当前储存人数!                 	 |");
		System.out.println("|0.退出                                            	 |");
		System.out.println("|________________________________|");
		
	}
	/*
	 * 提示用户输入一个学生对象的信息
	 */
	public Student input() {
     
		Student student=new Student();
		System.out.println("下面根据提示输入学生的信息");
		System.out.println("请输入学号");
		Scanner scanner=new Scanner(System.in);
		student.setNo(scanner.next());
		System.out.println("请输入姓名");
		student.setName(scanner.next());
		System.out.println("请输入年龄");
		student.setAge(scanner.nextInt());
		System.out.println("请输入性别(1为男、2为女)");
		student.setSex(scanner.nextInt());
		return student;
	}
}


工具包中的姓名自动生成

package student.jess.tool;
 /*
  * 自动随机生成姓名
  */
import java.util.Random;

public class Name2 {
     
	private String [] firstName;
	private String [] word;
	/*
	*构造函数初始化两个字符串数组
	*/
	public Name2() {
     
		super();
		firstName= new String[]{
     
              "赵","钱","孙","李","周","吴","郑","王","冯","陈",
             "楮","卫","蒋","沈","韩","杨","朱","秦","尤","许",
              "何","吕","施","张","孔","曹","严","华","金","魏",
             "陶","姜","戚","谢","邹","喻","柏","水","窦","章",
             "云","苏","潘","葛","奚","范","彭","郎","鲁","韦",
             "昌","马","苗","凤","花","方","俞","任","袁","柳",
              "酆","鲍","史","唐","费","廉","岑","薛","雷","贺",
              "倪","汤","滕","殷","罗","毕","郝","邬","安","常",
              "乐","于","时","傅","皮","卞","齐","康","伍","余",
             "元","卜","顾","孟","平","黄","和","穆","萧","尹",
              "姚","邵","湛","汪","祁","毛","禹","狄","米","贝",
              "明","臧","计","伏","成","戴","谈","宋","茅","庞",
              "熊","纪","舒","屈","项","祝","董","梁","杜","阮",
              "蓝","闽","席","季","麻","强","贾","路","娄","危",
              "江","童","颜","郭","梅","盛","林","刁","锺","徐",
              "丘","骆","高","夏","蔡","田","樊","胡","凌","霍",
              "虞","万","支","柯","昝","管","卢","莫","经","房",
             "裘","缪","干","解","应","宗","丁","宣","贲","邓",
             "郁","单","杭","洪","包","诸","左","石","崔","吉",
              "钮","龚","程","嵇","邢","滑","裴","陆","荣","翁",
              "荀","羊","於","惠","甄","麹","家","封","芮","羿",
              "储","靳","汲","邴","糜","松","井","段","富","巫",
              "乌","焦","巴","弓","牧","隗","山","谷","车","侯",
              "宓","蓬","全","郗","班","仰","秋","仲","伊","宫",
              "宁","仇","栾","暴","甘","斜","厉","戎","祖","武",
              "符","刘","景","詹","束","龙","叶","幸","司","韶",
              "郜","黎","蓟","薄","印","宿","白","怀","蒲","邰",
              "从","鄂","索","咸","籍","赖","卓","蔺","屠","蒙",
              "池","乔","阴","郁","胥","能","苍","双","闻","莘",
              "党","翟","谭","贡","劳","逄","姬","申","扶","堵",
              "冉","宰","郦","雍","郤","璩","桑","桂","濮","牛",
              "寿","通","边","扈","燕","冀","郏","浦","尚","农",
              "温","别","庄","晏","柴","瞿","阎","充","慕","连",
              "茹","习","宦","艾","鱼","容","向","古","易","慎",
              "戈","廖","庾","终","暨","居","衡","步","都","耿",
              "满","弘","匡","国","文","寇","广","禄","阙","东",
              "欧","殳","沃","利","蔚","越","夔","隆","师","巩",
              "厍","聂","晁","勾","敖","融","冷","訾","辛","阚",
              "那","简","饶","空","曾","毋","沙","乜","养","鞠",
              "须","丰","巢","关","蒯","相","查","后","荆","红",
              "游","竺","权","逑","盖","益","桓","公","仉","督",
              "晋","楚","阎","法","汝","鄢","涂","钦","归","海",
              "岳","帅","缑","亢","况","后","有","琴","商","牟",
              "佘","佴","伯","赏","墨","哈","谯","笪","年","爱",
              "阳","佟",
              "万俟","司马","上官","欧阳","夏侯",
              "诸葛","闻人","东方","赫连","皇甫",
              "尉迟","公羊","澹台","公冶","宗政",
              "濮阳","淳于","单于","太叔","申屠",
              "公孙","仲孙","轩辕","令狐","锺离",
              "宇文","长孙","慕容","鲜于","闾丘",
             "司徒","司空","丌官","司寇","南宫",
              "子车","颛孙","端木","巫马","公西",
              "漆雕","乐正","壤驷","公良","拓拔",
              "夹谷","宰父","谷梁","段干","百里",
              "东郭","南门","呼延","羊舌","微生",
              "梁丘","左丘","东门","西门"
              }; 
		word= new String[]{
     "一","乙","二","十","丁","厂","七","卜","人","入","八","九","几","儿","了","力","乃","刀","又",
				"三","于","干","亏","士","工","土","才","寸","下","大","丈","与","万","上","小","口","巾","山",
				"千","乞","川","亿","个","勺","久","凡","及","夕","丸","么","广","亡","门","义","之","尸","弓",
				"己","已","子","卫","也","女","飞","刃","习","叉","马","乡","丰","王","井","开","夫","天","无",
				"元","专","云","扎","艺","木","五","支","厅","不","太","犬","区","历","尤","友","匹","车","巨",
				"牙","屯","比","互","切","瓦","止","少","日","中","冈","贝","内","水","见","午","牛","手","毛",
				"气","升","长","仁","什","片","仆","化","仇","币","仍","仅","斤","爪","反","介","父","从","今",
				"凶","分","乏","公","仓","月","氏","勿","欠","风","丹","匀","乌","凤","勾","文","六","方","火",
				"为","斗","忆","订","计","户","认","心","尺","引","丑","巴","孔","队","办","以","允","予","劝",
				"双","书","幻","玉","刊","示","末","未","击","打","巧","正","扑","扒","功","扔","去","甘","世",
				"古","节","本","术","可","丙","左","厉","右","石","布","龙","平","灭","轧","东","卡","北","占",
				"业","旧","帅","归","且","旦","目","叶","甲","申","叮","电","号","田","由","史","只","央","兄",
				"叼","叫","另","叨","叹","四","生","失","禾","丘","付","仗","代","仙","们","仪","白","仔","他",
				"斥","瓜","乎","丛","令","用","甩","印","乐","句","匆","册","犯","外","处","冬","鸟","务","包",
				"饥","主","市","立","闪","兰","半","汁","汇","头","汉","宁","穴","它","讨","写","让","礼","训",
				"必","议","讯","记","永","司","尼","民","出","辽","奶","奴","加","召","皮","边","发","孕","圣",
				"对","台","矛","纠","母","幼","丝","式","刑","动","扛","寺","吉","扣","考","托","老","执","巩",
				"圾","扩","扫","地","扬","场","耳","共","芒","亚","芝","朽","朴","机","权","过","臣","再","协",
				"西","压","厌","在","有","百","存","而","页","匠","夸","夺","灰","达","列","死","成","夹","轨",
				"邪","划","迈","毕","至","此","贞","师","尘","尖","劣","光","当","早","吐","吓","虫","曲","团",
				"同","吊","吃","因","吸","吗","屿","帆","岁","回","岂","刚","则","肉","网","年","朱","先","丢",
				"舌","竹","迁","乔","伟","传","乒","乓","休","伍","伏","优","伐","延","件","任","伤","价","份",
				"华","仰","仿","伙","伪","自","血","向","似","后","行","舟","全","会","杀","合","兆","企","众",
				"爷","伞","创","肌","朵","杂","危","旬","旨","负","各","名","多","争","色","壮","冲","冰","庄",
				"庆","亦","刘","齐","交","次","衣","产","决","充","妄","闭","问","闯","羊","并","关","米","灯",
				"州","汗","污","江","池","汤","忙","兴","宇","守","宅","字","安","讲","军","许","论","农","讽",
				"设","访","寻","那","迅","尽","导","异","孙","阵","阳","收","阶","阴","防","奸","如","妇","好",
				"她","妈","戏","羽","观","欢","买","红","纤","级","约","纪","驰","巡","寿","弄","麦","形","进",
				"};
		
	}
	/*
	 * 生成两个字的名字
	 */
	public  String makeName1() {
     
		Random random=new Random();
		//随机生成姓
		int index=random.nextInt(firstName.length);
		String firString=firstName[index];
		//随机生成名
		int index2=random.nextInt(word.length);
		String tempString=firString+word[index2];
		return tempString;
	}
	/*
	 * 生成三个字的名字
	 */
	public String makeName2() {
     
		Random random=new Random();
		//随机生成姓
		int index=random.nextInt(firstName.length);
		String firString=firstName[index];
		//随机生成名
		int index2=random.nextInt(word.length);
		int index3=random.nextInt(word.length);
		String tempString=firString+word[index2]+word[index3];
		return tempString;
	}
	/*
	 * 生成三个字或者两个字的名字
	 */
	public String makename() {
     
		Name2 name=new Name2();
		Random random=new Random();
		//随机产生一个布尔值
		if(random.nextBoolean()){
     
			return name.makeName1();
		}
		else {
     
			return name.makeName2();
		}
	}
}

工具包中的学号自动生成

package student.jess.tool;
/*
 * 自动生成学号
 */
public class No {
     
	private String preString;//前缀
	private int count;//后几位工号
	//初始化
	public No(String preString, int count) {
     
		super();
		this.preString = preString;
		this.count = count;
	}
	public String MakeNo(int i) {
     
	    //字符串格式化
		String str=String.format("%0"+count+"d",i+1);
		return preString+str;
	}
}


实现菜单功能进行交互

package student.jess;
/*
 * 测试类main
 */
import java.util.Scanner;

public class Test {
     

	public static void main(String[] args) {
     
		/*
		 * 交互系统
		 */
		int k=0;
		Tool tool=new Tool();
		Scanner scanner2=new Scanner(System.in);
		while (true) {
     
			tool.menu();
			System.out.println("-------请输入你的选项(0-7)---------");
			k=scanner2.nextInt();
			//开关控制结构
			switch (k) {
     
			case 1:
				tool.Display();
				break;
			case 2:
				tool.addStudent(tool.input());
				break;
			case 3:
				System.out.println("请输入要查询的学生的学号");
				tool.findStudent(scanner2.next());
				break;
			case 4:
				tool.Display();
				System.out.println("请输入要删除的学生的学号");
				tool.Delete(scanner2.next());
				break;
			case 5:
				tool.Display();
				System.out.println("请输入要修改学生对象的信息");
				tool.Update(tool.input());
				break;
			case 6:
				System.out.println("请输入要模拟生成几个学生对象的信息");
				tool.information(scanner2.nextInt());
				break;
			case 7:
				System.out.println("当前共储存"+tool.count+"人");
				break;
			case 0:
				System.err.println("退出成功,欢迎下次使用!");
				System.exit(0);
			default:
				System.err.println("输入错误,请重新输入!");
				break;
			}
		}

	}

}

四、效果展示

菜单界面
学生信息管理系统Java版(信息自动生成)_第1张图片

显示集合信息
学生信息管理系统Java版(信息自动生成)_第2张图片
显示增加元素
学生信息管理系统Java版(信息自动生成)_第3张图片
在这里插入图片描述
显示功能
学生信息管理系统Java版(信息自动生成)_第4张图片


总结


以上就是今天对学生信息管理系统的简单介绍与实现,其中还有许多不足,希望各位大佬评论,对于姓名生成过于单一的问题,由于小编在此不宜过多丰富字典所以删除了一部分,如有需要完整版源代码,请在下方评论。这是对java基础的学习与总结,适合初学者,希望可以给大家带来帮助,欢迎大家评论,共同成长!

你可能感兴趣的:(小白,Java,java,字符串)