学生的类,有学生的性别,籍贯,姓名和五门课程的成绩,用private进行封装,同时写出getter和setter方法
package com.zheng.people;
public class Student {
private String name;
private String jiguan;
private String number;//学生学号
private String gender;//性别
private String classname;//学生班级
private int age;//
private double english;//英语成鸡
private double math;//数学成绩
private double cscore;//c语言成绩
private double javascore;//java成绩
private double PE;//体育成绩
private double total;//总成绩
private double avergae;//平均成绩
public Student(){
}
public String getJiguan() {
return jiguan;
}
public double getAvergae() {
return avergae;
}
public void setAvergae(double avergae) {
this.avergae = avergae;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public void setJiguan(String jiguan) {
this.jiguan = jiguan;
}
public String getName() {
return name;
}
public Student(String name,String number,String gender,String classname,int age,double english,double math,double cscore,double javascore,double PE,double avergae,double total){
this.age=age;
this.classname=classname;
this.gender=gender;
this.number=number;
this.name=name;
this.cscore=cscore;
this.english=english;
this.javascore=javascore;
this.PE=PE;
this.math=math;
this.avergae=avergae;
this.total=total;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setNumber(String number) {
this.number=number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public double getPE() {
return PE;
}
public void setPE(double PE) {
this.PE = PE;
}
public double getJavascore() {
return javascore;
}
public void setJavascore(double javascore) {
this.javascore = javascore;
}
public double getCscore() {
return cscore;
}
public void setCscore(double cscore) {
this.cscore = cscore;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", jiguan='" + jiguan + '\'' +
", number='" + number + '\'' +
", gender='" + gender + '\'' +
", classname='" + classname + '\'' +
", age=" + age +
", english=" + english +
", math=" + math +
", cscore=" + cscore +
", javascore=" + javascore +
", PE=" + PE +
", total=" + total +
", avergae=" + avergae +
'}';
}
}
coust类提示操作成功和失败!
package com.zheng.util;
public class COUST {
public static final String SUCCESS="操作成功";
public static final String ERROR="操作失败";
}
Result类
package com.zheng.util;
import com.sun.org.apache.regexp.internal.RE;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Result {
private int code;
private String message;
private Map<String,Object> data=new HashMap<>();
public Result() {
}
public Result(int code, String message, Map<String, Object> data) {
this.code = code;
this.message = message;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> data) {
this.data = data;
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
'}';
}
public static Result ok(){
Result r=new Result();
r.setCode(ResultCode.SUCCESS);
r.setMessage(COUST.SUCCESS);
return r;
}
public static Result error(){
Result r=new Result();
r.setCode(ResultCode.ERROR);
r.setMessage(COUST.ERROR);
return r;
}
public Result message(String message){
this.setMessage(message);
return this;
}
public Result code(Integer code){
this.setCode(code);
return this;
}
public Result data(String key,Object value){
this.data.put(key,value);
return this;
}
public Result data(Map<String,Object> map){
this.setData(map);
return this;
}
}
ResultCode类
package com.zheng.util;
public class ResultCode {
public static Integer SUCCESS=200;
public static Integer ERROR=201;
}
StudnetUtils类,采用随机数法模拟班级成员,但这样会使班级成员分数较低,仅仅是模拟
package com.zheng.util;
import com.zheng.people.Student;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;//采用随机数发给随机的学生赋值;
public class StudentUtils {
public static List<Student> studentList(){
List<Student> students=new ArrayList<>();
for (int i = 0; i < 15; i++) {
Student s=new Student();
Random r = new Random();
s.setName("张"+i);
s.setJiguan(i>5?"陕西":"河南");
s.setNumber("2109124000"+i);
s.setGender(i>5?"男":"女");
s.setClassname("计算机2101班");
s.setAge(18);
double english= r.nextDouble()*100;
s.setEnglish(english);
double math=r.nextDouble()*100;
s.setMath(math);
double PE=r.nextDouble()*100;
s.setPE(PE);
double java=r.nextDouble()*100;
s.setJavascore(java);
double c=r.nextDouble()*100;
s.setCscore(c);
s.setTotal(english+math+PE+java+c);
s.setAvergae((english+math+PE+java+c)/5);
students.add(s);
}
return students;
}
}
StudentInterface类创建学生的接口(比较草率的写了,应该在写查找和修改的)
package com.zheng.service;
import com.zheng.people.Student;
import com.zheng.util.Result;
import java.util.List;
public interface StudentInterface {
/**
* 增加一个学生
*
* @param stu
* @param list
* @return
*/
public static Result addStudent(Student stu, List<Student> list) {
return null;
}
/**
* 这个方法是删除一个学生
* @param stu
* @param list
* @return
*/
public Result deleteStudent(Student stu, List<Student> list);
//修改学生。查询学生
}
Studentcaozuo类,重写接口的方法add,delete,search和recover(修改)的方法
有一个问题,在修改学生的成绩后怎么样把单位学生的总体成绩和平均成绩修改???(修改单个成绩后,total成绩应该有变动)
package com.zheng.service;
import com.zheng.people.Student;
import com.zheng.service.StudentInterface;
import com.zheng.util.Result;
import java.util.List;
import java.util.Scanner;
public class Studentcaozuo implements StudentInterface {
public static Result addStudent(Student stu, List<Student> list) {
list.add(stu);
return Result.ok().data("studentList", list);
}
public Result deleteStudent(Student stu, List<Student> list) {
for (Student student : list) {
if (stu.getName().equals(student.getName())) {
list.remove(student);
return Result.ok();
}
}
return Result.error();
}
public Result searchStudent(Student stu, List<Student> StudentProject) {
for(Student student02:StudentProject)
{
if(stu.getName().equals(student02.getName()))
System.out.println("查到的结构为"+student02);
return Result.ok();
}
Result.error();
return null;
}
public Result recover(Student stu,String need, List<Student> StudentProject) {
for(Student s03:StudentProject)
if(stu.getName().equals(s03.getName()))
switch (need)
{
case"姓名":
System.out.print("请输入姓名");
Scanner Q = new Scanner(System.in);
String name;
name = Q.next();
s03.setName(name);break;
case"籍贯":
System.out.print("请输入籍贯");
Scanner W = new Scanner(System.in);
String jiguan;
jiguan = W.next();
s03.setJiguan(jiguan);break;
case"学号":
System.out.print("请输入学生学号");
Scanner E = new Scanner(System.in);
String number;
number = E.next();
s03.setNumber(number);break;
case"性别":
System.out.print("请输入性别");
Scanner R = new Scanner(System.in);
String gender;
gender = R.next();
s03.setGender(gender);break;
case"班级":
System.out.print("请输入学生班级");
Scanner T = new Scanner(System.in);
String Classname;
Classname = T.next();
s03.setClassname(Classname);break;
case"年龄":
System.out.print("请输入学生年龄");
Scanner Y = new Scanner(System.in);
int age;
age = Y.nextInt();
s03.setAge(age);break;
case"英语成绩":
System.out.print("请输入英语成绩");
Scanner U = new Scanner(System.in);
double English;
English = U.nextDouble();
s03.setEnglish(English);break;
case"数学成绩":
System.out.print("请输入数学成绩");
Scanner I = new Scanner(System.in);
double Math;
Math = I.nextDouble();
s03.setMath(Math);break;
case"c语言成绩":
System.out.print("请输入c语言成绩");
Scanner O = new Scanner(System.in);
double C;
C = O.nextDouble();
s03.setCscore(C);break;
case"java成绩":
System.out.print("请输入java成绩");
Scanner P = new Scanner(System.in);
double Java;
Java = P.nextDouble();
s03.setJavascore(Java);break;
case"体育成绩":
System.out.print("请输入体育成绩");
Scanner A = new Scanner(System.in);
double PE;
PE = A.nextDouble();
s03.setPE(PE);
;break;
default:
System.out.println("输入错误");
}
return null;
}}
测试类如下
package com.zheng.Text;
import com.zheng.people.Student;
import com.zheng.service.Studentcaozuo;
import com.zheng.util.Result;
import com.zheng.util.StudentUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentText {
public static void main(String[] args) {
Scanner n = new Scanner(System.in);
List<Student> list = new ArrayList<>();
list = StudentUtils.studentList();//这个list已经赋值了,随机数法模拟班级成绩(班级成绩很低)
Studentcaozuo studentservice = new Studentcaozuo();
Result Result;
int choice=0;
while(true) {
System.out.println("请输入操作数");
System.out.println("【1】增加学生");
System.out.println("【2】删除学生");
System.out.println("【3】修改学生");
System.out.println("【4】查找学生");
System.out.println("【5】显示所有学生");
System.out.println("【6】平均成绩及格的学生");
choice = n.nextInt();
switch (choice) {
case 1:
System.out.println("添加学生");
Student s01 = new Student();
System.out.print("请输入姓名");
Scanner q = new Scanner(System.in);
String name;
name = q.next();
s01.setName(name);
System.out.print("请输入籍贯");
Scanner w = new Scanner(System.in);
String jiguan;
jiguan = w.next();
s01.setJiguan(jiguan);
System.out.print("请输入学生学号");
Scanner e = new Scanner(System.in);
String number;
number = w.next();
s01.setNumber(number);
System.out.print("请输入性别");
Scanner r = new Scanner(System.in);
String gender;
gender = r.next();
s01.setGender(gender);
System.out.print("请输入学生班级");
Scanner t = new Scanner(System.in);
String Classname;
Classname = t.next();
s01.setClassname(Classname);
System.out.print("请输入学生年龄");
Scanner y = new Scanner(System.in);
int age;
age = y.nextInt();
s01.setAge(age);
System.out.print("请输入英语成绩");
Scanner u = new Scanner(System.in);
double english;
english = u.nextDouble();
s01.setEnglish(english);
System.out.print("请输入数学成绩");
Scanner i = new Scanner(System.in);
double math;
math = i.nextDouble();
s01.setMath(math);
System.out.print("请输入c语言成绩");
Scanner o = new Scanner(System.in);
double c;
c = o.nextDouble();
s01.setCscore(c);
System.out.print("请输入java成绩");
Scanner p = new Scanner(System.in);
double java;
java = p.nextDouble();
s01.setJavascore(java);
System.out.print("请输入体育成绩");
Scanner a = new Scanner(System.in);
double PE;
PE = a.nextDouble();
s01.setPE(PE);
s01.setTotal(PE + java + c + math + english);
s01.setAvergae((PE + java + c + math + english) / 5);
Result=studentservice.addStudent(s01,list);
System.out.println("新的学生已经添加!");
break;
case 2:System.out.print("删除学生信息:");
Student s02=new Student();
String name02;
System.out.print("请输入想删除的学生姓名");
name02=n.next();
s02.setName(name02);
Result=studentservice.deleteStudent(s02,list);break;
case 3:
System.out.print("修改学生信息:");
Student s03=new Student();
System.out.print("请输入要修改学生的姓名:");
String name03;
name03=n.next();
s03.setName(name03);
System.out.print("请输入需要修改的信息类型:");
String need;
need =n.next();
studentservice.recover(s03,need,list);break;
case 4: System.out.println("查找学生信息");
Student s04 = new Student();
System.out.print("请输入学生的姓名:");
String name04 = n.next();
s04.setName(name04);
studentservice.searchStudent(s04,list);
break;
case 5:
if (list.size() == 0) {
System.out.print("表内还没有学生");
} else {
System.out.println("展示班级所有学生");
for (int count = 0; count < list.size(); count++) {
System.out.println(list.get(count));
}
}break;
case 6:
if (list.size() == 0) {
System.out.print("表内还没有学生");
} else {
System.out.println("平均成绩及格的学生如下");
for (int count = 0; count < list.size(); count++) {
if(list.get(count).getAvergae()>60)
System.out.println(list.get(count));
}
}
}
}
}
}
如下是测试结果