功能简介:
a:可以对学生信息增删改查,
b:每一种操作都是使用容器进行
c:使用自己写的工具类,可以实现多次调用,实现代码的复用,增加可读性
d:语法使用嵌套循环,一般使用while,swich。。case,for
代码实现:
//测试类
package com.xinboedu.www.test;
public class TestSystem {
public static void main(String[] args)
{
StudentSystem studentSystem = new StudentSystem();
studentSystem.start();
}
}
学生信息系统:
package com.xinboedu.www.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
//学生信息系统
public class StudentSystem {
//记录学生个数
private Student student;
//容器保存学生信息
LinkedList<Student> list=new LinkedList<>();//增删效率高,所以就linkedlist
ArrayList<Student> alist=new ArrayList<>();//读写效率高
//系统开始
boolean isStart=true;
public void start()
{
System.out.println("欢迎使用学生信息管理系统");
while(isStart)
{
System.out.println("1.输入学生信息");
System.out.println("2.查看学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.删除学生信息");
System.out.println("5.退出");
int i = GetScanner.getscanner(5);
switch (i) {
case Content.INDEX_PUTNIFOR:
putInfo(1);
break;
case Content.INDEX_SELECTIFOR:
selectInfo();
break;
case Content.INDEX_SETIFOR:
setInfo();
break;
case Content.INDEX_DELECTNIFOR:
Student st = delectInfo();
System.out.println("毕业的学生为:"+st);
break;
case Content.EXIT:
isStart=false;
break;
default:
break;
}
}
}
private Student delectInfo() {
System.out.println("请输入要删除学生的学号:");
int reNum = GetScanner.getscanner(list.size());
Student student2 = list.remove(reNum-1);//根据学号移除某个学生的信息,返回值是被移除的那个学生信息
return student2;
}
//修改学生信息
private void setInfo()
{
boolean yesOrNO=true;
while(yesOrNO)
{
bianli() ;
System.out.print("请输入要修改学生的学号");
int index = GetScanner.getscanner(list.size());
Student st= list.get(index-1);
setOneStudent(st);
System.out.print("继续修改其他学生信息?");
yesOrNO = GetScanner.getYesOrNO();
}
}
//修改一个学生信息
private void setOneStudent(Student st) {
boolean yesOrNO=true;
while(yesOrNO)
{
System.out.println("1.修改名字");
System.out.println("2.修改年龄");
System.out.println("3.修改性别");
System.out.println("4.修改联系方式");
System.out.println("5.修改修改地址");
setInfo(st);
System.out.println("是否继续修改:");
yesOrNO = GetScanner.getYesOrNO();
}
}
private void setInfo(Student st) {
switch (GetScanner.getscanner(5)) {
case Content.INDEX_SETNAME:
String name = GetScanner.getString("姓名");
st.setName(name);
break;
case Content.INDEX_SETAGE:
int age=GetScanner.getInt("年龄");
st.setAge(age);
break;
case Content.INDEX_SETSEX:
String sex = GetScanner.getString("性别");
st.setSex(sex);
break;
case Content.INDEX_SETPHONE:
String phone = GetScanner.getString("联系方式");
st.setPhone(phone);
break;
case Content.INDEX_SETADDRESS:
String address = GetScanner.getString("联系地址");
st.setAddress(address);
break;
default:
break;
}
}
//按学号查询学生信息
private void selectInfo() {
bianli();
boolean yesOrNO=true;
while(yesOrNO)
{
System.out.println("请输入你要查询的学生学号:");
int index = GetScanner.getscanner(list.size());
Student st= list.get(index-1);
if(index<=list.size())
{
//alist.add(st);
System.out.println(st);
System.out.println("是否继续查询:");
yesOrNO = GetScanner.getYesOrNO();
}
}
}
//输入学生信息
private void putInfo(int num) {
int index=0;
boolean yesOrNO=true;
while(yesOrNO)
{
if(index<=num)
{
String name = GetScanner.getString("姓名");
int age=GetScanner.getInt("年龄");
String sex = GetScanner.getString("性别");
String phone = GetScanner.getString("联系方式");
String address = GetScanner.getString("联系地址");
Student student2 = new Student(name, age, sex, phone, address);
list.add(student2);
index++;//计算学生信息录入的个数
System.out.println("是否继续输入");
yesOrNO = GetScanner.getYesOrNO();
}else
{
System.out.println("学生以录入满了~~");
break ;
}
}
bianli();
}
//遍历
private void bianli()
{
Iterator<Student> iterator = list.iterator();
boolean hasNext = iterator.hasNext();
for(int i=0;i<list.size();i++)
{
Student next = iterator.next();
System.out.println((i+1)+"."+next);
}
}
}
//学生类
package com.xinboedu.www.test;
public class Student {
private String name;//姓名
private int age;//年龄
private String sex;//性别
private String phone;//联系方式
private String address;//联系电话
public Student(String name, int age, String sex, String phone,
String address) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
//重写toString方法
public String toString() {
return " 姓名=" + name + ", 年龄=" + age + ", 性别=" + sex
+ ", 联系方式=" + phone + ", 联系地址=" + address ;
}
}
//接口,用常量避免出现魔鬼数字
package com.xinboedu.www.test;
//接口
public interface Content {
//设置常量
int INDEX_PUTNIFOR=1;
int INDEX_SELECTIFOR=2;
int INDEX_SETIFOR=3;
int INDEX_DELECTNIFOR=4;
int EXIT=5;
int INDEX_SETNAME = 1;
int INDEX_SETAGE = 2;
int INDEX_SETSEX = 3;
int INDEX_SETPHONE = 4;
int INDEX_SETADDRESS = 5;
}
//复用代码,可以封装成库,实现多次调用
package com.xinboedu.www.test;
import java.util.Scanner;
public class GetScanner {
private static Scanner s=new Scanner(System.in);
//获取索引
public static int getscanner(int num)
{
int nextInt=0;
boolean isOver=false;
while(!isOver)
{
System.out.println("输入(1~"+num+")");
nextInt=s.nextInt();
if(nextInt>=1&&nextInt<=num)
{
isOver=true;
}else
{
System.out.println("输入错误,");
}
}
return nextInt;
}
//获取Yes Or No
public static boolean getYesOrNO()
{
System.out.println("是否确定? 1.确定2.取消");
int i = getscanner(2);
if(i==1)
{
return true;
}else{
return false;
}
}
//获取字符串
public static String getString(String info)
{
System.out.println("请输入"+info+":");
String str = s.next();
return str;
}
//获取int类型
public static int getInt(String info)
{
System.out.println("请输入"+info+":");
int i = s.nextInt();
return i;
}
}