import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student> al = new ArrayList<Student>();
int a = 0;
for(;true;){
System.out.println("------输入选项------");
System.out.println("1.查看学生");
System.out.println("2.添加学生");
System.out.println("3.删除学生");
System.out.println("4.修改学生");
System.out.println("5.退出");
a=sc.nextInt();
switch(a){
case 1:
a(al);
break;
case 2:
b(al);
break;
case 3:
c(al);
break;
case 4:
d(al);
break;
default:
case 5:
return;
}
}
}
static void a(ArrayList<Student> al){
System.out.println("ID\t名字\t地址");
for(int i = 0;i<al.size();i++){
Student s = al.get(i);
System.out.print(s.getId()+"\t");
System.out.print(s.getName()+"\t");
System.out.println(s.getAddress());
}
}
static void b(ArrayList<Student> al){
Scanner sc = new Scanner(System.in);
Student s = new Student();
System.out.println("输入名字:");
s.setName(sc.nextLine());
System.out.println("输入地址:");
s.setAddress(sc.nextLine());
System.out.println("输入id:");
s.setId(sc.nextInt());
al.add(s);
System.out.println("录入成功!");
}
static void c(ArrayList<Student> al){
Scanner sc = new Scanner(System.in);
System.out.println("输入id:");
for(int i = 0;i<al.size();i++){
if(al.get(i).getId()==sc.nextInt()){
al.remove(i);
System.out.println("已删除");
return;
}
}
System.out.println("未找到");
}
static void d(ArrayList<Student> al){
Scanner sc = new Scanner(System.in);
System.out.println("输入id:");
for(int i =0;i<al.size();i++){
if(al.get(i).getId()==sc.nextInt()){
_d(al.get(i));
return;
}
}
System.out.println("未找到");
}
static void _d(Student s){
Scanner sc = new Scanner(System.in);
Scanner _sc =new Scanner(System.in);
int a;
for(;true;){
System.out.println("------输入选项------");
System.out.println("1.修改id");
System.out.println("2.修改名字");
System.out.println("3.修改地址");
System.out.println("4.返回");
a=_sc.nextInt();
switch(a){
case 1:
System.out.println("输入id:");
s.setId(sc.nextInt());break;
case 2:
System.out.println("输入名字:");
s.setName(sc.nextLine());break;
case 3:
System.out.println("输入地址:");
s.setAddress(sc.nextLine());break;
default:
case 4:return;
}
}
}
}
public class Student {
private String name;
private int id;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
|
|