package demo05;
public class Student {
private String name;//姓名
private int age;//年龄
private char gender;//性别
private String tel;//电话
private String address;//地址
private String email;//邮箱
public Student() {
}
public Student(String name, int age, char gender, String tel, String address, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.tel = tel;
this.address = address;
this.email = email;
}
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 String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void eat(){
System.out.println("吃");
}
public void drink() {
System.out.println("喝水");
}
public void play() {
System.out.println("玩");
}
public void sleep() {
System.out.println("睡觉");
}
public String getDetails() {
return "\t"+name + "\t" + gender + "\t\t" + age + "\t\t\t" + tel + "\t" + address + "\t\t" + email;
}
}
}
import java.util.Scanner;
/**
CMUtility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/**
用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
*/
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
*/
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
/**
从键盘读取一个字符,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
/**
从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
*/
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static int readInt(int defaultValue) {
int n;
for (; ; ) {
String str = readKeyBoard(2, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
*/
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/**
从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
*/
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
2、界面显示类
public class view {
private list students = new list(10);
public view() {
Student cust = new Student("张三", 30,'男',"173444355455","北京天安门",
"[email protected]");
students.addStudent(cust);
}
public void enterMainMenu() {
boolean loopFlag = true;
do {
System.out.println("\n-----------------学生信息管理软件-----------------\n");
System.out.println(" 1 添 加 学 生");
System.out.println(" 2 修 改 学 生");
System.out.println(" 3 删 除 学 生");
System.out.println(" 4 学 生 列 表");
System.out.println(" 5 查 询 学 生");
System.out.println(" 6 退 出\n");
System.out.print(" 请选择(1-5):");
char key = CMUtility.readMenuSelection();
System.out.println();
switch (key) {
case '1':
addNewStudent();
break;
case '2':
modifyStudent();
break;
case '3':
deleteStudent();
break;
case '4':
listAllStudent();
break;
case '5':
selectStudent();
case '6':
System.out.print("确认是否退出(Y/N):");
char yn = CMUtility.readConfirmSelection();
if (yn == 'Y')
loopFlag = false;
break;
}
} while (loopFlag);
}
private void addNewStudent() {
System.out.println("---------------------添加学生信息---------------------");
System.out.print("姓名:");
String name = CMUtility.readString(4);
System.out.print("性别:");
char gender = CMUtility.readChar();
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(15);
System.out.print("邮箱:");
String email = CMUtility.readString(15);
System.out.print("地址:");
String address = CMUtility.readString(30);
Student cust = new Student(name, age, gender, phone, address, email);
boolean flag = students.addStudent(cust);
if (flag) {
System.out
.println("---------------------添加完成---------------------");
} else {
System.out.println("----------------记录已满,无法添加-----------------");
}
}
private void modifyStudent() {
System.out.println("---------------------修改学生信息---------------------");
int index = 0;
Student cust = null;
for (;;) {
System.out.print("请选择待修改学生编号(-1退出):");
index = CMUtility.readInt();
if (index == -1) {
return;
}
cust = students.getStudent(index - 1);
if (cust == null) {
System.out.println("无法找到指定学生!");
} else
break;
}
System.out.print("姓名(" + cust.getName() + "):");
String name = CMUtility.readString(4, cust.getName());
System.out.print("性别(" + cust.getGender() + "):");
char gender = CMUtility.readChar(cust.getGender());
System.out.print("年龄(" + cust.getAge() + "):");
int age = CMUtility.readInt(cust.getAge());
System.out.print("电话(" + cust.getTel() + "):");
String phone = CMUtility.readString(15, cust.getTel());
System.out.print("邮箱(" + cust.getEmail() + "):");
String email = CMUtility.readString(15, cust.getEmail());
System.out.print("地址("+ cust.getAddress() + "):");
String address = CMUtility.readString(30,cust.getAddress());
cust = new Student(name, age, gender, phone, address, email);
boolean flag = students.replaceStudent(index - 1, cust);
if (flag) {
System.out
.println("---------------------修改完成---------------------");
} else {
System.out.println("----------无法找到指定学生,修改失败--------------");
}
}
private void deleteStudent() {
System.out.println("---------------------删除学生---------------------");
int index = 0;
Student cust = null;
for (;;) {
System.out.print("请选择待删除学生编号(-1退出):");
index = CMUtility.readInt();
if (index == -1) {
return;
}
cust = students.getStudent(index - 1);
if (cust == null) {
System.out.println("无法找到指定学生!");
} else
break;
}
System.out.print("确认是否删除(Y/N):");
char yn = CMUtility.readConfirmSelection();
if (yn == 'N')
return;
boolean flag = students.deleteStudent(index - 1);
if (flag) {
System.out
.println("---------------------删除完成---------------------");
} else {
System.out.println("----------无法找到指定学生,删除失败--------------");
}
}
private void listAllStudent() {
System.out.println("---------------------------学生列表---------------------------");
Student[] custs = students.getAllStudents();
if (custs.length == 0) {
System.out.println("没有学生记录!");
} else {
System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t\t地址\t\t\t邮箱");
for (int i = 0; i < custs.length; i++) {
// System.out.println(i + 1 + "\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail());
System.out.println((i+1) + "\t" + custs[i].getDetails());
}
}
System.out.println("-------------------------学生列表-------------------------");
}
private void selectStudent(){
String selectNEA = CMUtility.readString(20);
Student[] custs = students.getAllStudents();//获取数组中的所有对象赋给新的数组
//遍历数组
for (int i = 0; i < custs.length; i++) {
//对数组中的对象中的内容与输入的内容进行比较
if (selectNEA.equals(custs[i].getName()) ||
selectNEA.equals(custs[i].getEmail()) ||
selectNEA.equals(custs[i].getAddress()) ){
System.out.println("存在该学生");
}else{
System.out.println("第"+i+"行没有该学生");
}
}
}
public static void main(String[] args) {
view View = new view();
View.enterMainMenu();
}
}
3、list类业务逻辑(增删改查)
public class list {
private Student[] students;
private int total = 0;
public list(int number){
students = new Student[number];
}
public boolean addStudent(Student student) {
if (total >= students.length) return false;
students[total++] = student;
return true;
}
public boolean replaceStudent(int index, Student cust) {
if (index < 0 || index >= total) return false;
students[index] = cust;
return true;
}
public boolean deleteStudent(int index) {
if (index < 0 || index >= total) return false;
for (int i = index; i <= total - 1; i++) {
students[i] = students[i + 1];
}
students[--total] = null;
return true;
}
public Student[] getAllStudents() {
Student[] custs = new Student[total];
for (int i = 0; i < total; i++) {
custs[i] = students[i];
}
return custs;
}
public int getTotal() {
return total;
}
public Student getStudent(int index) {
if (index < 0 || index >= total) return null;
return students[index];
}
}