1.思路分析实现
- 要求一和要求二,一个要求使用顺序表一个使用链表,但又因为这两个都是List的实现类,所以我就使用多态的形式通过一个方法进行实现上面两种内容,需要用什么方法实现就传入什么实现类,形参是List类型。
- 创建一个Student的javabean封装,学生的信息。
- 实现的增删该查,我都是直接先判断是否符合要求,比如删除的是否需要判断这个要删除的位置是否有效,有效我才进行删除,不能你给我传个位置然后我在越界报错吧。然后在转调list中的方法。
- 排序,就自定义了排序规则,按照Student的javabean的成绩排序
- 使用swich控制台输入选项,然后对应的选项对应一个功能然后转调方法即可。
代码实现
studentjavabean
java
package entity;
public class Student {
private String name;
private Integer age;
private Integer mark;
public Student() {
}
public Student(String name, Integer age, Integer mark) {
this.name = name;
this.age = age;
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getMark() {
return mark;
}
public void setMark(Integer mark) {
this.mark = mark;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", mark=" + mark +
'}';
}
}
1.创建Service(业务层接口)
java
package service;
import entity.Student;
import java.util.List;
public interface StudentService {
/**
* 初始化的创建和输出
*返回创建好的学生信息集合
*/
void init();
//打印
void info();
/**
* 添加学生信息
* @param student 学生信息
* @return 是否添加成功
*/
boolean addStudent(Student student);
/**
* 修改学生信息
* @param index 学生位置
* @param student 学生信息
* @return 是否修改成功
*/
boolean updateStudent(int index,Student student);
/**
* 删除学生信息
* @param index 要删除学生的位置
* @return 是否删除成功
*/
boolean deleteStudent(int index);
/**
* 按照学生成绩排序
*
* @return 排序完的集合
*/
void sort();
/**
* 查询学生的最高成绩
*
* @return 返回最高成绩的信息
*/
Student lookUp();
}
2.创建业务层实现类
java
package service.Impl;
import entity.Student;
import service.StudentService;
import java.util.Collections;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private List<Student> students;
public StudentServiceImpl(List<Student> students) {
this.students = students;
}
@Override
public void init() {
students.add(new Student("张三",18,98));
students.add(new Student("李四",18,54));
students.add(new Student("周小燕",28,92));
students.add(new Student("李旭",21,79));
students.add(new Student("李想",19,50));
for (Student student : students) {
System.out.println("姓名为:" + student.getName());
System.out.println("年龄为:" + student.getAge());
System.out.println("成绩为:" + student.getMark());
System.out.println("-------------------------------");
}
}
@Override
public void info() {
for (Student student : students) {
System.out.println("姓名为:" + student.getName());
System.out.println("年龄为:" + student.getAge());
System.out.println("成绩为:" + student.getMark());
System.out.println("-------------------------------");
}
}
@Override
public boolean addStudent(Student student) {
if (student.getMark() > 100 || student.getMark() < 0){
return false;
}
return students.add(student);
}
@Override
public boolean updateStudent(int index,Student student) {
if (index <= 0 || index > students.size()) return false;
Student student1 = students.get(index - 1);
if (student1 == null){
return false;
}
student1.setAge(student.getAge());
student1.setName(student.getName());
student1.setMark(student.getMark());
return true;
}
@Override
public boolean deleteStudent(int index) {
if (index <= 0 || index > students.size()) return false;
students.remove(index - 1);
return true;
}
@Override
public void sort() {
students.sort((a, b) -> {
return b.getMark() - a.getMark();
});
}
@Override
public Student lookUp() {
int max = -1;
Student student1 = new Student();
for (Student student : students) {
if (student.getMark() > max){
max = student.getMark();
student1 = student;
}
}
return student1;
}
}
3.打印等细节信息的输出
java
package info;
import entity.Student;
import service.Impl.StudentServiceImpl;
import service.StudentService;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class TestStudent {
public TestStudent() {
app();
}
public static void printInfo(){
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("任意数字. 退出程序");
System.out.println("----------------------------");
}
public static void studentInfo(List<Student> students) {
StudentService studentService = new StudentServiceImpl(students);
Scanner scanner = new Scanner(System.in);
while(true){
printInfo();
int x = scanner.nextInt();
switch (x){
case 1:{
//1.初始化
studentService.init();
break;
}
case 2:{
//2.添加学生
Student addStudent = new Student();
System.out.println("请输入添加的学生姓名:");
addStudent.setName(scanner.next());
System.out.println("请输入添加的学生年龄:");
addStudent.setAge(scanner.nextInt());
System.out.println("请输入添加的学生成绩:");
addStudent.setMark(scanner.nextInt());
boolean flag = studentService.addStudent(addStudent);
if (flag) studentService.info();
else System.out.println("添加有误");
break;
}
case 3:{
//3.排序
studentService.sort();
studentService.info();
break;
}
case 4:{
//4.修改
System.out.println("请输入要修改第几位学生");
int index = scanner.nextInt();
Student updateStudent = new Student();
System.out.println("请输入修改的学生姓名:");
updateStudent.setName(scanner.next());
System.out.println("请输入修改的学生年龄:");
updateStudent.setAge(scanner.nextInt());
System.out.println("请输入修改的学生成绩:");
updateStudent.setMark(scanner.nextInt());
boolean flag = studentService.updateStudent(index, updateStudent);
if (flag) studentService.info();
else System.out.println("修改有误");
break;
}
case 5:{
//5.删除
System.out.println("请输入要删除第几位学生");
int index = scanner.nextInt();
boolean flag = studentService.deleteStudent(index);
if (flag) studentService.info();
else System.out.println("删除有误");
break;
}
case 6:{
//6.查询最高成绩
Student student = studentService.lookUp();
System.out.println("最高成绩学生的信息如下");
System.out.println("姓名为:" + student.getName());
System.out.println("年龄为:" + student.getAge());
System.out.println("成绩为:" + student.getMark());
System.out.println("-------------------------------");
break;
}
default:{
//退出系统
System.exit(0);
}
}
}
}
//1.使用顺序表实现
public static void arraysList(){
List<Student> list = new ArrayList<>();
studentInfo(list);
}
//2.使用链表实现
public static void linkedList(){
List<Student> list = new LinkedList<>();
studentInfo(list);
}
public static void app() {
Scanner sc = new Scanner(System.in);
System.out.println("1. 使用顺序表实现");
System.out.println("2. 使用链表实现");
switch (sc.nextInt()){
case 1:{
arraysList();
break;
}
case 2:{
linkedList();
break;
}
}
}
}
启动类
java
import info.TestStudent;
//启动类
public class App {
public static void main(String[] args) {
new TestStudent();
}
}