本文链接:http://m.blog.csdn.net/article/details?id=52037379#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2751b6d1
用集合编写简单的学生信息管理系统。实现增删改查操作。
public class Info {
private int stu_id;
private String stu_name;
private String stu_sex;
private int stu_age;
private int chinese;
private int math;
private int english;
private String stu_address;
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public String getStu_address() {
return stu_addrese;
}
public void setStu_address(String stu_address) {
this.stu_addrese = stu_address;
}
public double getAvgScose(){
return (math+chinese+english)/3;
}
public int getSunScose(){
return math+chinese+english;
}
}
import java.io.ObjectInputStream.GetField;
import java.util.Scanner;
public class ChooseDemo {
/*
*得到int类型的数据
*/
public int getInputIntInfo(String info){
int input_int=0;
try{
System.out.println(info);
Scanner s=new Scanner(System.in);
input_int=s.nextInt();
}catch (Exception e) {
System.out.println("输入错误!!请重新输入。");
input_int=getInputIntInfo(info);
}
return input_int;
}
/*
*得到String类型的数据
*/
public String getInputStringInfo(String string){
String input_String="";
try{
System.out.println(string);
Scanner s=new Scanner(System.in);
input_String=s.next();
}catch (Exception e) {
System.out.println("输入错误!!请重新输入");
input_String=getInputStringInfo(string);
}
return input_String;
}
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("8.查询学生信息(根据姓名)");
System.out.println("9.退出系统");
System.out.println("************************");
}
public void Choose(int coad){
switch (coad) {
//添加学生信息
case 1:
System.out.println("<---你已进入添加学生信息界面--->");
Info add_info=getStudentInfo();
if(!(Handle.selectStuId(add_info.getStu_id()))){
if(Handle.AddInfo(add_info)){
System.out.println("\n添加成功!!\n");
}else{
System.out.println("\n添加失败!!\n");
}
}else{
System.out.println("该学号已存在!!! 请重新添加!!");
}
break;
//修改学生信息(根据学号修改)
case 2:
System.out.println("<---你已进入修改学生信息(根据学号修改)界面--->");
int update_id=getInputIntInfo("请输入学号");
if(Handle.selectStuId(update_id)){
System.out.println("<---请输入新的学生信息--->");
Info update_infoById=getStudentInfo();
if(!(Handle.selectStuId(update_infoById.getStu_id()))||update_infoById.getStu_id()==update_id){
if(Handle.updateInfoById(update_id,update_infoById)){
System.out.println("\n修改成功!!!\n");
}else{
System.out.println("\n修改失败!!!\n");
}
}else{
System.out.println("该学号的学生信息已存在!!! 请重新添加!!");
}
}else{
System.out.println("\n没有该学号的学生信息!!请重新选择!\n");
}
break;
//修改学生信息(根据姓名修改)
case 3:
System.out.println("<---你已进入修改学生信息(根据姓名修改)界面--->");
String update_name=getInputStringInfo("请输入姓名");
if(Handle.selectStuName(update_name)){
System.out.println("<---请输入新的学生信息--->");
Info update_infoByName=getStudentInfo();
if(!(Handle.selectStuId(update_infoByName.getStu_id()))||update_name.equals(update_infoByName.getStu_name())){
if(Handle.updateInfoByName(update_name,update_infoByName)){
System.out.println("\n修改成功!!\n");
}else{
System.out.println("\n修改失败!!\n");
}
}else{
System.out.println("该学号的学生信息已存在!!! 请重新添加!!");
}
}else{
System.out.println("\n没有该学号的学生信息!!请重新选择!\n");
}
break;
//删除学生信息(根据学号删除)
case 4:
System.out.println("<---你已进入删除学生信息(根据学号删除)界面--->");
int delete_id=getInputIntInfo("请输入学号");
if(Handle.selectStuId(delete_id)){
if(Handle.deleteInfoById(delete_id)){
System.out.println("\n删除成功!!!\n");
}else{
System.out.println("\n删除失败!!!\n");
}
}else{
System.out.println("\n没有该学号的学生信息!!请重新选择!\n");
}
break;
//删除学生信息(根据姓名删除)
case 5:
System.out.println("<---你已进入删除学生信息(根绝姓名删除)界面--->");
String delete_name=getInputStringInfo("请输入姓名");
if(Handle.selectStuName(delete_name)){
if(Handle.deleteInfoByName(delete_name)){
System.out.println("\n删除成功!!!\n");
}else{
System.out.println("\n删除失败!!!\n");
}
}else{
System.out.println("\n没有该学生的信息!!请重新选择!\n");
}
break;
//查询所有学生信息
case 6:
System.out.println("<---你已进入所有学生信息界面--->");
Handle.selectAllInfo();
break;
//查询学生信息界面(根据学号查询)
case 7:
System.out.println("<---你已进入查询学生信息界面(根据学号查询)--->");
int select_id=getInputIntInfo("请输入学号");
if(Handle.selectStuId(select_id)){
Info id=Handle.selectInfoById(select_id);
if(id!=null){
}
}else{
System.out.println("\n没有该学生信息!!请重新选择!\n");
}
break;
//查询学生信息界面(根据姓名查询)
case 8:
System.out.println("<---你已进入查询学生信息界面(根据姓名查询)--->");
String select_name=getInputStringInfo("请输入姓名");
if(Handle.selectStuName(select_name)){
Info name=Handle.selectInfoByName(select_name);
if(name!=null){
}
}else{
System.out.println("\n没有该学生信息!!请重新选择!\n");
}
break;
//退出系统
case 9:System.out.println("退出成功!");
System.exit(0);
break;
default:
System.out.println("没有该选项!!请重新输入!");
break;
}
}
//给 Info类的学生属性进行赋值
private Info getStudentInfo(){
Info info=new Info();
info.setStu_id(getInputIntInfo("请输入学号"));
info.setStu_name(getInputStringInfo("请输入姓名"));
info.setStu_sex(getInputStringInfo("请输入性别"));
info.setStu_age(getInputIntInfo("请输入年龄"));
info.setChinese(getInputIntInfo("请输入语文成绩"));
info.setMath(getInputIntInfo("请输入数学成绩"));
info.setEnglish(getInputIntInfo("请输入英语成绩"));
info.setStu_address(getInputStringInfo("请输入地址"));
info.getAvgScose();
info.getSunScose();
return info;
}
}
import java.util.ArrayList;
public class Handle {
private static ArrayList list=new ArrayList();
//添加学生信息
public static boolean AddInfo(Info add_info) {
boolean add=false;
add=list.add(add_info);
return add;
}
//根据学号修改学生信息
public static boolean updateInfoById(int id, Info update_infoById) {
boolean update=false;
for(Object obj:list){
Info info=(Info)obj;
if(id==info.getStu_id()){
list.set(list.indexOf(info), update_infoById);
update=true;
break;
}
}
return update;
}
//根据姓名修改学生信息
public static boolean updateInfoByName(String name, Info update_infoByName) {
boolean update=false;
for(Object obj:list){
Info info=(Info)obj;
if(name.equals(info.getStu_name())){
list.set(list.indexOf(info), update_infoByName);
update=true;
break;
}
}
return update;
}
//根据学号删除学生信息
public static boolean deleteInfoById(int delete_id) {
boolean delete=false;
for(Object obj:list){
Info info=(Info)obj;
if(delete_id==info.getStu_id()){
delete=list.remove(info);
break;
}
}
return delete;
}
//根据姓名删除信息
public static boolean deleteInfoByName(String delete_name) {
boolean delete=false;
for(Object obj:list){
Info info=(Info)obj;
if(delete_name.equals(info.getStu_name())){
delete=list.remove(info);
break;
}
}
return delete;
}
//查询所有学生信息
public static void selectAllInfo() {
for(Object obj:list){
Info info=(Info)obj;
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("学号:"+info.getStu_id());
stringBuilder.append("\t姓名:"+info.getStu_name());
stringBuilder.append("\t性别:"+info.getStu_sex());
stringBuilder.append("\t年龄:"+info.getStu_age());
stringBuilder.append("\n语文成绩:"+info.getChinese());
stringBuilder.append("\t数学成绩:"+info.getMath());
stringBuilder.append("\t英语成绩:"+info.getEnglish());
stringBuilder.append("\t平均成绩:"+info.getAvgScose());
stringBuilder.append("\t总成绩:"+info.getSunScose());
stringBuilder.append("\n地址:"+info.getStu_address()+"\n");
System.out.println(stringBuilder.toString());
}
}
//根据学号查询信息
public static Info selectInfoById(int select_id) {
Info in=null;
for(Object obj:list){
Info info=(Info)obj;
if(select_id==info.getStu_id()){
System.out.println("\n该学生信息如下:\n");
StringBuilder stringBuilder=new StringBuilder();//同上
stringBuilder.append("学号:"+info.getStu_id());
stringBuilder.append("\t姓名:"+info.getStu_name());
stringBuilder.append("\t性别:"+info.getStu_sex());
stringBuilder.append("\t年龄:"+info.getStu_age());
stringBuilder.append("\n语文成绩:"+info.getChinese());
stringBuilder.append("\t数学成绩:"+info.getMath());
stringBuilder.append("\t英语成绩:"+info.getEnglish());
stringBuilder.append("\t平均成绩:"+info.getAvgScose());
stringBuilder.append("\t总成绩:"+info.getSunScose());
stringBuilder.append("\n地址:"+info.getStu_address()+"\n");
System.out.println(stringBuilder.toString());
in=info;
}
}
return in;
}
//根据姓名查询信息
public static Info selectInfoByName(String select_name) {
Info in=null;
for(Object obj:list){
Info info=(Info)obj;
if(select_name.equals(info.getStu_name())){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("学号:"+info.getStu_id());
stringBuilder.append("\t姓名:"+info.getStu_name());
stringBuilder.append("\t性别:"+info.getStu_sex());
stringBuilder.append("\t年龄:"+info.getStu_age());
stringBuilder.append("\n语文成绩:"+info.getChinese());
stringBuilder.append("\t数学成绩:"+info.getMath());
stringBuilder.append("\t英语成绩:"+info.getEnglish());
stringBuilder.append("\t平均成绩:"+info.getAvgScose());
stringBuilder.append("\t总成绩:"+info.getSunScose());
stringBuilder.append("\n地址:"+info.getStu_address()+"\n");
System.out.println(stringBuilder.toString());
in=info;
}
}
return in;
}
//得到学号
public static boolean selectStuId(int id) {
boolean fag=false;
for(Object obj:list){
Info info=(Info)obj;
if(id==info.getStu_id()){
fag=true;
break;
}
}
return fag;
}
//得到姓名
public static boolean selectStuName(String name){
boolean fag=false;
for(Object obj:list){
Info info=(Info)obj;
if(name.equals(info.getStu_name())){
fag=true;
break;
}
}
return fag;
}
}
public class TestMemo {
public static void main(String[] args) {
ChooseDemo ch=new ChooseDemo();
while(true){
ch.menu();
int choose=ch.getInputIntInfo("<请输入数字选择操作>");
ch.Choose(choose);
}
}
}