其他分享
首页 > 其他分享> > 第一周开学测试

第一周开学测试

作者:互联网

//学生类
package Test;

public class Student {
private String name;
private int age;
private String id;
private String are;

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getId() {
return id;
}

public String getAre() {
return are;
}

public void setName(String name)
{
this.name = name;
}

public boolean setAge(int age) {
if (age < 0 | age > 120) {
return false;
} else {
this.age = age;
return true;
}
}

public void setId(String id)
{
this.id=id;
}

public void setAre(String are) {
this.are = are;
}

public Student(String name, String id, String are) {
this.name = name;
this.id = id;
this.are = are;
}

public Student() {
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", id='" + id + '\'' +
", are='" + are + '\'' +
'}';
}
}

//管理类
package Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class StudentsManger {
public static void main(String[] args)
{
int option=0;
Scanner sc=new Scanner(System.in);
ArrayList students = 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.print("请输入你的选择:");
if(sc.hasNextInt()==true)
{
option=sc.nextInt();
}else{
System.out.println("请输入正确的数字");
}
if(option==1)//添加学生
{
addStudents(students);

}else if(option==2){//删除学生
System.out.println("请输入要删除的学生id");
String id=sc.nextLine();
id=sc.nextLine();
delStudents(students,id);
}else if(option==3){//修改学生
setStudents(students);
}else if(option==4){//查看学生
findAllStudents(students);
}else if(option==5){
System.out.println("谢谢使用");
return;
}else{
System.out.println("请输入正确的数字");
}
}
}
public static void addStudents(ArrayList<Student> students)//添加学生
{
Scanner sc= new Scanner(System.in);
System.out.println("请输入要添加的学生的姓名:");
String name=sc.nextLine();
System.out.println("请输入要添加的学生的学号:");
String id=sc.nextLine();
System.out.println("请输入要添加的学生的年龄:");
int age=sc.nextInt();
System.out.println("请输入要添加的学生的地址:");
String are=sc.next();
Student st= new Student(name,id,are);
students.add(st);
if(st.setAge(age))
{
System.out.println("添加成功!");
}else {
delStudents(students,id);
System.out.println("添加失败,年龄不合实际");
}


}
public static void delStudents(ArrayList<Student> students,String id)//删除学生
{

for(int i=0;i<students.size();i++)
{
Student st =students.get(i);
if(id.equals(st.getId()))
{
students.remove(i);
return;
}
}
System.out.println("您搜索的学号不存在");

}

public static void setStudents(ArrayList<Student> students)
{
Scanner sc= new Scanner(System.in);
System.out.println("请输入要修改的学生id");
String id=sc.nextLine();
for(int i=0;i<students.size();i++)
{
Student st =students.get(i);
if(id.equals(st.getId()))
{
int option=0;boolean l=false;
System.out.println("请输入要修改的内容:1.全部 2.姓名 3.年龄 4.学号 5.地址");
option=sc.nextInt();
switch (option)
{
case 1:

case 2:
System.out.println("请输入要修改的姓名:");
st.setName(sc.next());
if(option==2)break;
case 3:
System.out.println("请输入要修改的年龄:");
if(st.setAge(sc.nextInt()))l=true;
if(option==3)break;
case 4:
System.out.println("请输入要修改的学号:");
st.setId(sc.next());
if(option==4)break;
case 5:
System.out.println("请输入要修改的地址:");
st.setAre(sc.next());
}
System.out.println("修改成功");
break;
}
}
}
public static void findAllStudents(ArrayList<Student> students)//查看学生
{
Iterator it = students.iterator();
if(!it.hasNext())
{
System.out.println("暂无内容");
}
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

 

标签:开学,String,第一周,System,public,测试,println,id,out
来源: https://www.cnblogs.com/yzx-sir/p/16667011.html