package 项目1 ;
import java. util. Scanner;
public class main {
static int index= 0 ;
static Student [ ] students = new Student [ 45 ] ;
public static void main ( String[ ] args) {
System. out. println ( "***********************" ) ;
System. out. println ( "*** 欢迎使用 ***" ) ;
System. out. println ( "***********************" ) ;
for ( ; ; ) {
menu ( ) ;
}
}
public static void menu ( ) {
System. out. println ( "1、添加学生信息" ) ;
System. out. println ( "2、删除学生信息" ) ;
System. out. println ( "3、修改学生信息" ) ;
System. out. println ( "4、查询学生信息" ) ;
System. out. println ( "请输入操作项编号,Enter结束" ) ;
Scanner scanner = new Scanner ( System. in) ;
int option = scanner. nextInt ( ) ;
switch ( option) {
case 1 :
if ( index>= students. length)
System. out. println ( "人员已满" ) ;
else {
System. out. println ( "1、添加学生信息" ) ;
Student student = new Student ( ) ;
System. out. println ( "请输入学号,Enter结束" ) ;
String id = scanner. next ( ) ;
student. setId ( id) ;
for ( Student iid: students) {
if ( iid == null)
break ;
else if ( iid. getId ( ) . equals ( student. getId ( ) ) ) {
System. out. println ( "学生已录入" ) ;
return ;
}
}
System. out. println ( "请输入姓名,Enter结束" ) ;
String name = scanner. next ( ) ;
student. setName ( name) ;
System. out. println ( "请输入电话,Enter结束" ) ;
String mobile = scanner. next ( ) ;
student. setMobile ( mobile) ;
System. out. println ( "请输入地址,Enter结束" ) ;
String address = scanner. next ( ) ;
student. setAddress ( address) ;
students[ index] = student;
index++ ;
System. out. println ( "添加成功,第" + index+ "位同学" ) ; }
break ;
case 2 :
System. out. println ( "2、删除学生信息" ) ;
System. out. println ( "请输入被删除删除学生学号,Enter结束" ) ;
String did = scanner. next ( ) ;
for ( int i= 1 ; i<= index; i++ ) {
if ( students[ i] . getId ( ) . equals ( did) ) {
students[ i] = students[ index] ;
students[ index] = null;
index-- ;
System. out. println ( "删除成功" ) ;
return ;
}
}
System. out. println ( "无此学生信息" ) ;
break ;
case 3 :
System. out. println ( "3、修改学生信息" ) ;
System. out. println ( "请输入修改学号,Enter键结束" ) ;
String logo = scanner. next ( ) ;
for ( Student student: students) {
if ( student == null) {
break ;
}
else if ( student. getId ( ) . equals ( logo) ) {
System. out. println ( "请输入姓名,Enter结束" ) ;
String name = scanner. next ( ) ;
student. setName ( name) ;
System. out. println ( "请输入电话,Enter结束" ) ;
String mobile = scanner. next ( ) ;
student. setMobile ( mobile) ;
System. out. println ( "请输入地址,Enter结束" ) ;
String address = scanner. next ( ) ;
student. setAddress ( address) ;
System. out. println ( "修改成功" ) ;
return ;
}
}
System. out. println ( "无该学生信息" ) ;
break ;
case 4 :
System. out. println ( "4、查询学生信息" ) ;
System. out. println ( "请选择查询方式id:学号1,姓名2,电话3地址4,Enter结束" ) ;
int number = scanner. nextInt ( ) ;
boolean flag = false ;
switch ( number) {
case 1 :
System. out. println ( "请输入学号,Enter结束" ) ;
String id = scanner. next ( ) ;
for ( Student student: students) {
if ( student == null)
break ;
else if ( student. getId ( ) . equals ( id) ) {
System. out. println ( student) ;
flag = true ;
return ;
}
}
case 2 :
System. out. println ( "请输入姓名,Enter结束" ) ;
String name = scanner. next ( ) ;
for ( Student student: students) {
if ( student == null)
break ;
else if ( student. getName ( ) . equals ( name) ) {
System. out. println ( student) ;
flag = true ;
}
}
return ;
case 3 :
System. out. println ( "请输入电话,Enter结束" ) ;
String mobile = scanner. next ( ) ;
for ( Student student: students) {
if ( student == null)
break ;
else if ( student. getMobile ( ) . equals ( mobile) ) {
System. out. println ( student) ;
flag = true ;
return ;
}
}
case 4 :
System. out. println ( "请输入地址,Enter结束" ) ;
String address = scanner. next ( ) ;
for ( Student student: students) {
if ( student == null)
break ;
else if ( student. getAddress ( ) . equals ( address) ) {
System. out. println ( student) ;
flag = true ;
return ;
}
}
default :
System. out. println ( "您的输入有误请重新输入" ) ;
flag = true ;
break ;
}
if ( ! flag) {
System. out. println ( "查无此人" ) ;
}
default :
System. out. println ( "您的输入有误请重新输入" ) ;
break ;
}
}
}
package 项目1 ;
public class Student {
private String id;
private String name;
private String mobile;
private String address;
public String getId ( ) {
return id;
}
public void setId ( String id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getMobile ( ) {
if ( mobile. length ( ) != 11 ) {
}
return mobile;
}
public void setMobile ( String mobile) {
this . mobile = mobile;
}
public String getAddress ( ) {
return address;
}
public void setAddress ( String address) {
this . address = address;
}
@Override
public String toString ( ) {
return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", address=" + address + "]" ;
}
}