package homework;
public class Student {
private String id;
private String name;
private int age;
private char gender;
private float grade;
public Student() {
super();
}
public Student(String id, String name, int age, char gender, float grade) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.grade = grade;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public float getGrade() {
return grade;
}
public void setGrade(float grade) {
this.grade = grade;
}
}
package homework;
import java.util.Scanner;
public class ManageStudents {
private Student[] allStu;
private int numOfStu;
private Scanner sc;
public ManageStudents() {
super();
}
public ManageStudents(Student[] allStu, int numOfStu, Scanner sc) {
super();
this.allStu = allStu;
this.numOfStu = numOfStu;
this.sc = sc;
}
public Student typeInStuInfor(Student stu) {
System.out.print("请输入学生姓名:");
stu.setName(sc.nextLine());
System.out.print("请输入" + stu.getName() + "的ID:");
stu.setId(sc.nextLine());
System.out.print("请输入" + stu.getName() + "的年龄:");
stu.setAge(sc.nextInt());
System.out.print("请输入" + stu.getName() + "的性别:");
stu.setGender(sc.next().charAt(0));
System.out.print("请输入" + stu.getName() + "的成绩:");
stu.setGrade(sc.nextFloat());
sc.nextLine();
return stu;
}
public void addNewStu(Student stu) {
allStu[numOfStu] = stu;
numOfStu += 1;
}
public Student findStu( ) {
System.out.println("请输入需要查找的学生的ID:");
String id = sc.nextLine();
int index = -1;
for (int i = 0; i < allStu.length && allStu[i] != null ; i++) {
if (id.equals(allStu[i].getId())) {
index = i;
break;
}
}
if (-1 == index) {
System.out.println("输入的ID有误,未找到此ID的学生");
return null;
} else {
return allStu[index];
}
}
public void deleteStu(){
System.out.println("请输入需要删除学生的ID:");
String id = sc.nextLine();
int index = -1;
for (int i = 0; i < allStu.length && allStu[i] != null; i++) {
if (id.equals(allStu[i].getId())) {
index = i;
}
}
if (-1 == index) {
System.out.println("输入的ID有误,没有此ID的学生");
} else {
int j;
for (j = index; j < allStu.length - 1 && allStu[j] != null; j++) {
allStu[j] = allStu[j + 1];
}
allStu[j] = null;
}
}
public void showAllStuInfor() {
for (int i = 0; i < allStu.length && allStu[i] != null; i++) {
System.out.println(allStu[i].getName() + "的信息:");
System.out.print("id:" + allStu[i].getId() + "\t");
System.out.print("年龄:" + allStu[i].getAge() + "\t");
System.out.print("性别::" + allStu[i].getGender() + "\t");
System.out.println("成绩::" + allStu[i].getGrade() + "\t");
}
}
public void gradeSort() {
int index = 0;
Student temp = null;
for (int i = 0; i < allStu.length - 1 && allStu[i] != null; i++) {
index = i;
temp = null;
for (int j = i + 1; j < allStu.length && allStu[j] != null; j++) {
if (allStu[index].getGrade() < allStu[j].getGrade()) {
index = j;
}
if (i != index) {
temp = allStu[index];
allStu[index] = allStu[i];
allStu[i] = temp;
}
}
}
}
public Student[] getAllStu() {
return allStu;
}
public void setAllStu(Student[] allStu) {
this.allStu = allStu;
}
public int getNumOfStu() {
return numOfStu;
}
public void setNumOfStu(int numOfStu) {
this.numOfStu = numOfStu;
}
public Scanner getSc() {
return sc;
}
public void setSc(Scanner sc) {
this.sc = sc;
}
}
package homework;
import java.util.Scanner;
public class HomeWork {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int addStuNum = 0;
Student[] allStu = new Student[100];
allStu[0] = new Student("001","张三",11,'男',93.0F);
allStu[1] = new Student("002","李四",11,'女',95.0F);
allStu[2] = new Student("003","王五",11,'男',97.0F);
allStu[3] = new Student("004","刘某",11,'女',90.0F);
allStu[4] = new Student("005","唐六",11,'男',83.0F);
ManageStudents manage = new ManageStudents(allStu,5,sc);
System.out.print("请输入需要录入的学生个人:" );
addStuNum = sc.nextInt();
sc.nextLine();
for (int i = 0; i < addStuNum; i++) {
System.out.println("第" + (i+1) + "个录入的同学");
manage.addNewStu(manage.typeInStuInfor(new Student()));
System.out.println();
}
System.out.println("-------------------------------------------");
System.out.println("展示学生信息:");
manage.showAllStuInfor();
System.out.println("-------------------------------------------");
System.out.println("查找某一ID的同学");
Student stu = manage.findStu();
if (null != stu) {
System.out.println("通过ID查到的同学是:" + stu.getName());
}
System.out.println("-------------------------------------------");
System.out.println("成绩排序");
manage.gradeSort();
System.out.println("成绩排序后");
for (int i = 0; i < manage.getNumOfStu(); i++) {
System.out.println(manage.getAllStu()[i].getName() + "的成绩为:" + manage.getAllStu()[i].getGrade());
}
System.out.println("-------------------------------------------");
System.out.println("删除某一ID的同学");
manage.deleteStu();
System.out.println("-------------------------------------------");
System.out.println("展示学生信息:");
manage.showAllStuInfor();
System.out.println("-------------------------------------------");
System.out.println("查找某一ID的同学");
Student stu1 = manage.findStu();
if (null != stu1) {
System.out.println("通过ID查到的同学是:" + stu1.getName());
}
System.out.println("-------------------------------------------");
System.out.println("删除某一ID的同学");
manage.deleteStu();
}
}