📄 unit.java
字号:
import java.util.ArrayList;
public class Unit {
/**
* 空构造方法
*/
public Unit() {
}
/**
* 向studentList列表中添加一个学生到适当索引值上,如果列表中存在和要添加的学生ID一样的学生,则不能添加学生
*
* @param newSto
*/
public void addStudent(Student newSto) {
String ID = newSto.getID();
boolean b = false;
boolean a = false;
for (int i = 0; i < studentList.size(); i++) {
if (ID.equalsIgnoreCase(studentList.get(i).getID())) {
b = true;
}
}
if (studentList.size() == 0) {
studentList.add(position(ID), newSto);
a = true;
}
if (!a && !b) {
studentList.add(position(ID), newSto);
System.out
.println("The new student's information has been inserted successfully!");
} else {
System.out.println("There is a student whose ID is "
+ newSto.getID() + "in this unit already!");
}
}
/**
* 删除studengList列表上一个指定ID的学生的信息,如果没有该学生则不能删除
*
* @param deteleID
*/
public void deleteStudent(String deteleID) {
boolean b = false;
for (int i = 0; i < studentList.size(); i++) {
if (deteleID.equalsIgnoreCase(studentList.get(i).getID())) {
b = true;
}
}
if (b) {
studentList.remove(anotherposition(deteleID));
System.out
.println("The student's information has been deleted successfully!");
} else {
System.out.println("No student has the ID " + deteleID + "!");
}
}
/**
* 设置属性unitName的值
*
* @param unitName
*/
public void setUnitName(String unitName) {
this.unitName = unitName;
}
/**
* 设置属性name,gender,department的值
*
* @param name
* @param gender
* @param department
*/
public void setlnstructor(String name, String gender, String department) {
instructor = new Staff(name, gender, department);
}
/**
* 查找要添加的学生应添加的位置索引值
*
* @param ID
* @return
*/
public int position(String ID) {
int a = 0;
for (int i = 0; i < studentList.size(); i++) {
if (ID.compareToIgnoreCase(studentList.get(i).getID()) < 0) {
a = i;
}
if (ID.compareToIgnoreCase(studentList.get(i).getID()) > 0) {
a = i + 1;
}
}
return a;
}
/**
* 查找要删除的学生的位置索引值
*
* @param ID
* @return
*/
public int anotherposition(String ID) {
int a = 0;
for (int i = 0; i < studentList.size(); i++) {
if (ID.compareToIgnoreCase(studentList.get(i).getID()) == 0) {
a = i;
}
}
return a;
}
/**
* 重写toString()方法
*/
public String toString() {
String s = "-------------------------------------------------\n";
s += " ******Unit Report******\n";
s += "Unit Name:" + unitName + "\n";
s += "-------------------------------------------------\n";
s += instructor.toString() + "\n";
s += "-------------------------------------------------\n";
s += "ID\tName\tGender\tAge\tScore\tGrade\n";
s += "-------------------------------------------------\n";
for (int i = 0; i < studentList.size(); i++) {
s += studentList.get(i).toString() + "\n";
}
s += "-------------------------------------------------\n";
return s;
}
private String unitName;
private Staff instructor;
private ArrayList<Student> studentList = new ArrayList<Student>();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -