功能:
1.增加学生
2.查看学生
3.根据学号修改学生信息
4.根据学号查看某一个学生信息
5.根据性别查看同性别的所有学生
6.根据学号删除某一个学生
7.按照学号升序排序(默认)
8.按照年龄降序排序
Student 类- - -学生的javabean类
package com.studentByArray.www;
public class Student {
//定义一个Student类{学号、姓名、年龄、性别}
private String Sno;
private String name;
private int age;
private String sex;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String sno, String name, int age, String sex) {
super();
Sno = sno;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getSno() {
return Sno;
}
public void setSno(String Sno) {
this.Sno = Sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [Sno=" + Sno + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
StudentOperate 类- - -学生管理系统功能实现类
package com.studentByArray.www;
import java.util.ArrayList;
public class StudentOperate {
ArrayList<Student> list = new ArrayList<Student>();
//增加学生
public boolean addStudent(Student student) {
for(int i=0;i<list.size();i++) {
Student s = list.get(i);
if(s.getSno().equals(student.getSno())) {
return false;
}else {
list.add(student);
return true;
}
}
list.add(student);
return true;
}
//查看学生信息
public void showStudent() {
if(list.size() == 0) {
System.out.println("没有学生信息");
}
for(Student stu : list) {
System.out.println("学生学号: "+stu.getSno()+" "+"学生姓名: "+stu.getName()+" "+"学生年龄: "+stu.getAge()+" "+"学生性别: "+stu.getSex());
}
}
//根据学号修改学生信息
public boolean updataBySno(Student student) {
if(student.getSno() == null) {
return false;
}
for(int i=0;i<list.size();i++) {
Student s = list.get(i);
if(s.getSno().equals(student.getSno())) {
s.setName(student.getName());
s.setAge(student.getAge());
s.setSex(student.getSex());
return true;
}
}
return false;
}
//根据学号查看某一个学生信息
public Student showStuBySno(String sno) {
if(sno == null) {
return null;
}
for(int i=0;i<list.size();i++) {
Student s = list.get(i);
if(s.getSno().equals(sno)) {
return s;
}
}
return null;
}
//根据性别查看同性别的所有学生
public ArrayList<Student> selectStuByAge(String sex) {
ArrayList<Student> newStu = new ArrayList<Student>();
for(int i=0;i<list.size();i++) {
Student s = list.get(i);
if(s.getSex().equals(sex)) {
newStu.add(s);
}
}
return newStu;
}
//根据学号删除某一个学生
public boolean deleteBySno(String sno) {
Student stu = this.showStuBySno(sno);
if(stu!=null) {
list.remove(stu);
return true;
}
return false;
}
//按照学号升序排序(默认)
public ArrayList<Student> sortBySno() {
for(int i=0;i<list.size()-1;i++) {
for(int j=0;j<list.size()-1-i;j++) {
if(list.get(j).getSno().compareTo(list.get(j+1).getSno())>0) {
Student temp = list.get(j);
list.set(j, list.get(j+1));
list.set(j+1, temp);
}
}
}
return list;
}
//按照年龄降序排序
public ArrayList<Student> sortByAge() {
for(int i=0;i<list.size()-1;i++) {
for(int j=0;j<list.size()-1-i;j++) {
if(list.get(j).getAge()<list.get(j+1).getAge()) {
Student temp = list.get(j);
list.set(j, list.get(j+1));
list.set(j+1, temp);
}
}
}
return list;
}
}
StudentTest 类- - -操作界面测试类
package com.studentByArray.www;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
StudentOperate stuO = new StudentOperate();
Scanner input = new Scanner(System.in);
boolean choose = true;
while(choose) {
System.out.println("1.增加学生");
System.out.println("2.查看学生");
System.out.println("3.根据学号修改学生信息");
System.out.println("4.根据学号查看某一个学生信息");
System.out.println("5.根据性别查看同性别的所有学生");
System.out.println("6.根据学号删除某一个学生");
System.out.println("7.按照学号升序排序(默认)");
System.out.println("8.按照年龄降序排序");
System.out.print("请输入你的选择:");
int i = input.nextInt();
switch(i){
case 1:
{
System.out.println("请输入你要增加的学生学号:");
String sno = input.next();
System.out.println("请输入要增加的学生姓名:");
String name = input.next();
System.out.println("请输入要增加的学生年龄:");
int age = input.nextInt();
System.out.println("请输入要增加的学生性别:");
String sex = input.next();
Student stu = new Student();
stu.setSno(sno);
stu.setName(name);
stu.setAge(age);
stu.setSex(sex);
boolean receive = stuO.addStudent(stu);
if(receive) {
System.out.println("添加成功!");
}else {
System.out.println("输入的学号已存在!");
}
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 2:
{
stuO.showStudent();
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 3:
{
System.out.println("请输入你要修改的学生学号:");
String sno = input.next();
System.out.println("将学生姓名改为:");
String name = input.next();
System.out.println("将学生年龄改为:");
int age = input.nextInt();
System.out.println("将学生性别改为:");
String sex = input.next();
Student stu = new Student();
stu.setSno(sno);
stu.setName(name);
stu.setAge(age);
stu.setSex(sex);
boolean receive = stuO.updataBySno(stu);
if(receive) {
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 4:
{
System.out.println("请输入要查询的学生学号:");
String sno = input.next();
Student receive = stuO.showStuBySno(sno);
if(receive!=null) {
// String stu = receive.toString();
System.out.println(receive);
}else {
System.out.println("没有查询到此学生!");
}
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 5:
{
System.out.println("请输入你要查询的学生性别:");
String sex = input.next();
ArrayList<Student> newStu= stuO.selectStuByAge(sex);
System.out.println(newStu);
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 6:
{
System.out.println("请输入要删除的学生学号:");
String sno = input.next();
boolean receive = stuO.deleteBySno(sno);
if(receive) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 7:
{
ArrayList<Student> stus = stuO.sortBySno();
System.out.println(stus);
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
case 8:
{
ArrayList<Student> stus = stuO.sortByAge();
System.out.println(stus);
System.out.println("请问是否继续:(true/false)");
choose = input.nextBoolean();
break;
}
}
}
}
}