标签:
转眼间,开学六周过去了,我也学习java这么长时间了。在此国庆期间闲来无事,做个小系统玩玩。
-------------student类封装----------- package StudentSystem; public class student extends user{ private int stuId; private String stuName; private String stuBirthday; public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuBirthday() { return stuBirthday; } public void setStuBirthday(String stuBirthday) { this.stuBirthday = stuBirthday; } @Override public String toString() { return "student [stuId=" + stuId + ", stuName=" + stuName + ", stuBirthday=" + stuBirthday + "]"; } public student(){ super(); } public student(int stuId,String stuName,String stuBirthday){ this.stuId=stuId; this.stuName=stuName; this.stuBirthday=stuBirthday; } } -------------------istudentDao接口-------------------------- package StudentSystem; public interface istudentDao { /* * 新增加学生 * */ public abstract boolean insert(student students); /* * 删除学生 * */ public abstract boolean delete(int stuId); /* * 更新学生 * */ public abstract boolean update(student students); /* * 查找学生 * */ public abstract student search(int stuId); public abstract void show(); } ------------------istudentDao接口实现------------------------------ package StudentSystem; public class studentDaoDemo implements studentDao { private student student1[] = new student[100]; public void start(){ for(int i=0;i<10;i++){ student student = new student(1000+i,"student"+i,"1995-10-"+i); student1[i] = student; } } @Override public boolean insert(student students) { /*遍历数组,找到第一个为空的位置*/ for(int i=0; i<student1.length;i++){ if(student1[i] == null){ System.out.println("插入第"+(i+1)+"个"); student1[i]=students; break; } } return true; } @Override public boolean delete(int stuId) { if(search(stuId) == null) return false; for(int i=0;i<student1.length;i++){ if(student1[i].getStuId()==stuId){ int j=i; while(student1[j+1]!=null){ student1[j]=student1[j+1]; j++; } student1[j]=null; // 把最后一个不为空的元素置空 break; } } return true; } @Override public boolean update(student students) { /*按学号查找,更新数据*/ System.out.println("test uodate stuId"+students.getStuId()); if(search(students.getStuId()) == null) return false; else{ for(int i=0;i<student1.length;i++){ if(student1[i].getStuId() == students.getStuId()){ student1[i] = students; break; } } return true; } } @SuppressWarnings("unused") public student search (int stuId) { student student = null; for(int i=0;i<student1.length; i++){ if(student1[i].getStuId() == stuId){ student = student1[i]; break; } return null; } return student; } /* * 输出所有学生信息 * */ public void show(){ int j=0; for(int i=0;i<student1.length;i++){ if(student1[i]!=null) j++; } System.out.println("系统中共有"+j+"名学生信息!"); for(int i=0;i<j;i++){ String result = student1[i].toString(); System.out.println(result); } } } ------------------studentMune------------------------------- package StudentSystem; import java.util.Scanner; public class studentMune { private studentDaoDemo stuDao; private Scanner input=null; public studentMune(){ super(); this.input = new Scanner(System.in); this.stuDao = new studentDaoDemo(); } public void mainMune(){ //stuDao.start(); 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("====================="); System.out.println("请输入对应编号进行操作:"); String i = input.next(); switch(i){ case "1": show();break; case "2": Insert();break; case "3": Delete();break; case "4": Update(); break; case "5": Search();break; case "6": System.exit(0);break; default: System.out.println("你的输入有误,请重新输入!"); } returnMain(); } /* * 查找学生信息 * */ public void Search(){ System.out.println("==按学号查找==>>"); System.out.println("请输要查找的学号:"); student students = stuDao.search(input.nextInt()); if( students!=null){ System.out.println("查找成功!"+students.toString()); } else { System.out.println("查找失败!"); } returnMain(); } /* * 插入一个学生 * */ public void Insert(){ System.out.println("请输入姓名:"); String name = input.next(); System.out.println("请输入stuId:"); int Id = input.nextInt(); System.out.println("请输入stuBir:"); String bir = input.next(); student students = new student(Id,name,bir); if(stuDao.insert(students)) System.out.println("插入成功!"); else System.out.println("插入失败!"); } /* *删除一个学生信息 * */ public void Delete(){ System.out.println("====按学号delete=====>>"); System.out.println("请输要delete的学号:"); if(stuDao.delete(input.nextInt())) System.out.println("删除成功!"); else System.out.println("删除失败!"); returnMain(); } /** * 更新一个学生的信息 * */ public void Update(){ System.out.println("请直接输入新信息更新!"); System.out.println("请输入new姓名:"); String name = input.next(); System.out.println("请输入stuId:"); int Id = input.nextInt(); System.out.println("请输入new stuBir:"); String bir = input.next(); student students = new student(Id,name,bir); if( stuDao.update(students)) System.out.println("更新成功!"); else System.out.println("更新失败!"); } public void show(){ stuDao.show(); } /* * 按0返回主菜单 * */ public void returnMain(){ System.out.println("=================>>"); System.out.println("输入0返回主菜单"); int i = input.nextInt(); while(i!=0){ System.out.println("输入0返回主菜单"); i = input.nextInt(); if(i==0) mainMune(); } mainMune(); } } -------------------系统测试函数---------------------- package StudentSystem; public class SystemTest { static student students = new student(); public static void main(String[] args) { studentMune mune = new studentMune(); mune.mainMune(); } } 系统还会继续完善的,现在只实现了增、删、改、查基本操作!
标签:
原文地址:http://www.cnblogs.com/he-shao/p/4862527.html