同一个软件包内
package student.work1;
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student(){
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student(String sid, String name, String age, String address){
this.address=address;
this.age=age;
this.name=name;
this.sid=sid;
}
}
package student.work1;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
while (true) {
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("请输入你的选择");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
if (line.equals("1")) {
System.out.println("添加学生");
addStudent(array);
}
if (line.equals("2")) {
delStudent(array);
}
if (line.equals("3")) {
changeStudent(array);
}
if (line.equals("4")) {
findStudent(array);
}
if (line.equals("5")) {
System.out.println("谢谢使用");
break;
}
}
}
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String sid;
while (true){
System.out.println("请输入学生学号");
sid = sc.nextLine();
boolean f=isUsed(array,sid);
if(f==true){
System.out.println("重复学号!");
}else {
break;
}
}
System.out.println("请输入学生名字");
String name = sc.nextLine();
System.out.println("请输入学生年龄");
String age = sc.nextLine();
System.out.println("请输入学生居住地");
String address = sc.nextLine();
Student s = new Student();
s.setAddress(address);
s.setAge(age);
s.setName(name);
s.setSid(sid);
array.add(s);
System.out.println("添加学生成功");
}
public static void findStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("无信息,请添加后再查询");
} else {
System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getSid() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁\t\t" + s.getAddress());
}
}
}
public static boolean isUsed(ArrayList<Student> array,String sid) {
boolean flag=false;
for(int i=0;i<array.size();i++){
Student s=array.get(i);
if(s.getSid().equals(sid)){
flag=true;
break;
}
}
return flag;
}
public static void delStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
boolean s11=false;
System.out.println("请输入你要删除学生的学号");
String sid=sc.nextLine();
for(int i=0;i<array.size();i++){
Student s=array.get(i);
if(s.getSid().equals(sid)){
array.remove(i);
s11=true;
}
}
if(s11==true){
System.out.println("删除成功");
}else{
System.out.println("查询不到需要删除者学号");
}
}
public static void changeStudent(ArrayList<Student> array) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要修改学生的学号");
String sid=sc.nextLine();
Student s=new Student();
boolean s11=false;
for(int i=0;i<array.size();i++){
Student std =array.get(i);
if(std.getSid().equals(sid)){
System.out.println("请输入学生新姓名");
String name=sc.nextLine();
System.out.println("请输入学生新年龄");
String age=sc.nextLine();
System.out.println("请输入学生新居住地");
String address=sc.nextLine();
s.setAddress(address);
s.setAge(age);
s.setName(name);
s.setSid(sid);
array.set(i,s);
s11=true;
break;
}
}
if(s11==true){
System.out.println("修改成功");
}else{
System.out.println("查无此人");
}
}
}