import java.util.ArrayList;
import java.util.Scanner;
public class test {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
kaishi();
}
public static void kaishi(){
jiekou jk = new chuli();
while(true){
System.out.println("===========学生管理系统==============");
System.out.println("\t1,添加新学生信息");
System.out.println("\t2,删除学生信息");
System.out.println("\t3,修改学生信息");
System.out.println("\t4,查询学生信息");
System.out.println("\t5,输出所有学生信息");
System.out.println("\t6,退出系统");
int key = sc.nextInt();
switch(key){
case 1:
if (jk.add()) System.out.println("添加成功");
else System.out.println("添加失败");
break;
case 2:
if(jk.rmove()) System.out.println("删除成功");
else System.out.println("删除失败");
break;
case 3:
if(jk.updata()) System.out.println("修改成功");
else System.out.println("修改失败");
break;
case 4:
jk.show();
break;
case 5:
jk.print();
break;
case 6:
System.out.println("退出成功");
return;
default:
System.out.println("输入有误");
}
}
}
}
interface jiekou{
boolean add(); //添加
boolean rmove(); //删除
boolean updata(); //修改
void show(); //查询
void print(); //输出
}
class chuli implements jiekou{
Scanner sc = new Scanner(System.in);
public ArrayList<Student> arr = new ArrayList<>();
//构造方法
public chuli(){
arr.add(new Student("aierken","nan",18,"19199467744" ,"xinjiang"));
arr.add(new Student("nijia","nan",18,"19199466445" ,"shandong"));
arr.add(new Student("aier","nv",25,"19199557744" ,"beijing"));
arr.add(new Student("ken","nv",36,"19199467744" ,"xinjiang"));
arr.add(new Student("aier","nv",25,"19199557744" ,"beijing"));
}
//添加
@Override
public boolean add(){
try{
System.out.println("-------添加学生信息--------");
System.out.print("\t请输入学生姓名:");
String name = sc.next();
System.out.print("\t请输入学生性别:");
String bl = sc.next();
System.out.print("\t请输入学生年龄:");
int age = sc.nextInt();
System.out.print("\t请输入学生电话:");
String phone = sc.next();
System.out.print("\t请输入学生地址:");
String address = sc.next();
arr.add(new Student(name,bl,age,phone,address));
return true;
}catch(Exception e){
sc.next();
System.out.println("输入格式有误");
}
return false;
}
//删除
@Override
public boolean rmove(){
System.out.println("-------删除学生信息--------");
System.out.print("\t请输入要删除的学生姓名:");
String name = sc.next();
Student[] stunum = chazhao(name);
int num = stunum.length;
if(num!=0){
if (num == 1){
arr.remove(stunum[0]);
return true;
}
System.out.println("您要删除哪一个?(请输入序号)");
int key = sc.nextInt();
if(key<=num){
arr.remove(stunum[key-1]);
return true;
}
System.out.println("输入有误");
}
return false;
}
//修改 查询要修改的学生
@Override
public boolean updata() {
System.out.println("-------修改学生信息--------");
System.out.print("\t请输入要修改的学生姓名:");
String name = sc.next();
Student[] stunum = chazhao(name);
int num = stunum.length;
if(num!=0){
if (num == 1){
arr.add(xiugai(stunum[0]));
arr.remove(stunum[0]);
return true;
}
System.out.println("您要修改哪一个?(请输入序号)");
int key = sc.nextInt();
if(key<=num){
arr.add(xiugai(stunum[0]));
arr.remove(stunum[0]);
return true;
}
System.out.println("输入有误");
}
return false;
}
//查询
@Override
public void show(){
System.out.println("---------查询学生信息--------");
System.out.print("请输入要查询的学生姓名:");
String name = sc.next();
chazhao(name);
}
//输出所有
@Override
public void print(){
System.out.println("----------输出所有学生信息----------");
for (Student stu:arr){
System.out.println(stu.toString());
}
}
//修改方法 对值进行修改并返回
public Student xiugai(Student stu){
System.out.println("请输入修改选项:");
System.out.println("\t1,姓名:"+"\n"+
"\t2,年龄:"+"\n"+
"\t3,电话:"+"\n"+
"\t4,地址:");
int key = sc.nextInt();
switch(key){
case 1:
System.out.print("请输入新名:");
stu.setName(sc.next());
break;
case 2:
System.out.print("请输入年龄:");
stu.setAge(sc.nextInt());
break;
case 3:
System.out.print("请输入电话:");
stu.setPhone(sc.next());
break;
case 4:
System.out.print("请输入地址:");
stu.setAddress(sc.next());
break;
default:
System.out.println("输入有误");
}
return stu;
}
//查找方法 查找给定的学生信息并返回
public Student[] chazhao(String name){
Student[] stunum = new Student[arr.size()];
int num = 0;
for (Student stu : arr){
if(name.equals(stu.getName())){
stunum[num] = stu;
num++;
}
}
System.out.println("共找到"+num+"个"+name);
System.out.println();
Student[] students = new Student[num];
for(int i=0;i<num;i++){
System.out.println((i+1)+","+stunum[i].toString());
students[i] = stunum[i];
}
return students;
}
}
class Student{
private String name; //姓名
final String bl; //性别
private int age; //年龄
private String phone; //电话
private String address; //地址
public Student(String name,String bl,int age,String phone,String address){
this.name = name;
this.bl = bl;
this.age = age;
this.phone = phone;
this.address = address;
}
@Override
public String toString() {
return "姓名=" + name + "\t" +
", 性别=" + bl +
", 年龄=" + age +
", 电话=" + phone + "\t" +
", 地址=" + address + "\t" ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*public String isBl() {
return bl;
}
public void setBl(String bl) {
this.bl = bl;
}*/
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/*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;
}
}
标签:String,每日,System,name,println,打卡,public,out From: https://www.cnblogs.com/yunbianshangdadun/p/17179283.html