diff --git a/.idea/modules.xml b/.idea/modules.xml index 5aba50b..50c8b88 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -7,8 +7,10 @@ + + diff --git a/Week7/Week7.iml b/Week7/Week7.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week7/Week7.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week7/src/cn/czyx007/DAO/StudentDAO.java b/Week7/src/cn/czyx007/DAO/StudentDAO.java new file mode 100644 index 0000000..b806b7e --- /dev/null +++ b/Week7/src/cn/czyx007/DAO/StudentDAO.java @@ -0,0 +1,8 @@ +package cn.czyx007.DAO; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/12 - 10:12 + */ +public class StudentDAO { +} diff --git a/Week7/src/cn/czyx007/Driver.java b/Week7/src/cn/czyx007/Driver.java new file mode 100644 index 0000000..59ea558 --- /dev/null +++ b/Week7/src/cn/czyx007/Driver.java @@ -0,0 +1,82 @@ +package cn.czyx007; + +import cn.czyx007.VO.Result; +import cn.czyx007.bean.Student; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/12 - 10:14 + */ +public class Driver { + public static void main(String[] args) { + List studentList = inputFromKeyBoard(); + List resultList = calculateProvince(studentList); + display(studentList, resultList); + } + + public static List inputFromKeyBoard(){ + List studentList = new ArrayList<>(); + Scanner scanner = new Scanner(System.in); + String data = ""; + while (!(data=scanner.nextLine()).equals("end")){ + String[] stuArray = data.split(",|,"); + studentList.add(new Student(stuArray[0], stuArray[1], stuArray[2], stuArray[3])); + } + return studentList; + } + + public static int calculateByGender(List studentList, String gender){ + int count=0; + for (Student student : studentList) { + if(student.getGender().equals(gender)){ + count++; + } + } + return count; + } + + public static List calculateProvince(List studentList){ + List resultList = new ArrayList<>(); + for (Student stu : studentList) { + String province = stu.getProvince(); + String name = stu.getName(); + int index = isProvinceExist(resultList, province); + if (index != -1) { + Result oldResult = resultList.get(index); + oldResult.setCount(oldResult.getCount() + 1); + oldResult.setNames(oldResult.getNames() + "," + name); + resultList.set(index, oldResult); + } else { + resultList.add(new Result(province, 1, name)); + } + } + return resultList; + } + + public static int isProvinceExist(List resultList, String province){ + int index=-1; + for (int i = 0; i < resultList.size(); i++) { + Result result = resultList.get(i); + if (result.getProvince().equals(province)){ + return i; + } + } + return index; + } + + public static void display(List studentList, List resultList){ + int totalCount = studentList.size(); + int maleCount = calculateByGender(studentList, "男"); + int femaleCount = calculateByGender(studentList, "女"); + System.out.println("总人数:"+totalCount); + System.out.println("其中男:"+maleCount+"人," + maleCount*100.0f/totalCount + "%,女:"+femaleCount+"人,"+femaleCount*100.0f/totalCount+"%"); + for (Result result : resultList) { + System.out.println(result); + } + } +} + diff --git a/Week7/src/cn/czyx007/VO/Result.java b/Week7/src/cn/czyx007/VO/Result.java new file mode 100644 index 0000000..9bf5c9a --- /dev/null +++ b/Week7/src/cn/czyx007/VO/Result.java @@ -0,0 +1,53 @@ +package cn.czyx007.VO; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/12 - 10:13 + */ +public class Result { + private String province; + private Integer count; + private String names; + + public Result() { + } + + public Result(String province, Integer count, String names) { + this.province = province; + this.count = count; + this.names = names; + } + + public String getProvince() { + return province; + } + + public void setProvince(String province) { + this.province = province; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + public String getNames() { + return names; + } + + public void setNames(String names) { + this.names = names; + } + + @Override + public String toString() { + return "Result{" + + "province='" + province + '\'' + + ", count=" + count + + ", names='" + names + '\'' + + '}'; + } +} diff --git a/Week7/src/cn/czyx007/bean/Student.java b/Week7/src/cn/czyx007/bean/Student.java new file mode 100644 index 0000000..e6da6d4 --- /dev/null +++ b/Week7/src/cn/czyx007/bean/Student.java @@ -0,0 +1,64 @@ +package cn.czyx007.bean; + +/** + * @author : 张宇轩 + * @createTime : 2022/10/12 - 10:12 + */ +public class Student { + private String id; + private String name; + private String gender; + private String province; + + public Student() { + } + + public Student(String id, String name, String gender, String province) { + this.id = id; + this.name = name; + this.gender = gender; + this.province = province; + } + + 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 String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public String getProvince() { + return province; + } + + public void setProvince(String province) { + this.province = province; + } + + @Override + public String toString() { + return "Student{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", gender='" + gender + '\'' + + ", province='" + province + '\'' + + '}'; + } +} diff --git a/work10/src/Validate.java b/work10/src/Validate.java new file mode 100644 index 0000000..5d9c02d --- /dev/null +++ b/work10/src/Validate.java @@ -0,0 +1,122 @@ +import java.util.Scanner; + +public class Validate { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + //学号,姓名,性别,手机,邮箱,固定电话 + inputStudentID(); + inputName(); + inputGender(); + inputPhoneNumber(); + inputEmail(); + inputLinePhoneNumber(); + } + + public static void inputStudentID() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("请输入学号:"); + String studentID = scanner.nextLine(); + if (isStudentID(studentID)) { + System.out.println("格式正确!学号输入完毕"); + return; + } else { + System.out.println("格式错误!请重新输入"); + } + } + } + + public static void inputName() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("请输入姓名:"); + String name = scanner.nextLine(); + if (isName(name)) { + System.out.println("格式正确!姓名输入完毕"); + return; + } else { + System.out.println("格式错误!请重新输入"); + } + } + } + + public static void inputGender() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("请输入性别:"); + String gender = scanner.nextLine(); + if (isGender(gender)) { + System.out.println("格式正确!性别输入完毕"); + return; + } else { + System.out.println("格式错误!请重新输入"); + } + } + } + + public static void inputPhoneNumber() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("请输入手机号码:"); + String phoneNumber = scanner.nextLine(); + if (isPhoneNumber(phoneNumber)) { + System.out.println("格式正确!手机号码输入完毕"); + return; + } else { + System.out.println("格式错误!请重新输入"); + } + } + } + + public static void inputEmail() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("请输入邮箱:"); + String email = scanner.nextLine(); + if (isEmail(email)) { + System.out.println("格式正确!邮箱输入完毕"); + return; + } else { + System.out.println("格式错误!请重新输入"); + } + } + } + + public static void inputLinePhoneNumber() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("请输入固定电话:"); + String linePhoneNumber = scanner.nextLine(); + if (isLinePhoneNumber(linePhoneNumber)) { + System.out.println("格式正确!固定电话输入完毕"); + return; + } else { + System.out.println("格式错误!请重新输入"); + } + } + } + + public static boolean isStudentID(String id) { + return id.matches("[0-9]*"); + } + + public static boolean isName(String name) { + return name.matches("[a-zA-Z]\\w{4,15}"); + } + + public static boolean isGender(String gender) { + return gender.matches("[\\u7537\\u5973]"); + } + + public static boolean isPhoneNumber(String phoneNumber) { + return phoneNumber.matches("1[358]\\d{9}"); + } + + public static boolean isEmail(String email) { + return email.matches("\\w[-\\w.+]*@([A-Za-z\\d][-A-Za-z\\d]+\\.)+[A-Za-z]{2,14}"); + } + + public static boolean isLinePhoneNumber(String linePhoneNumber) { + return linePhoneNumber.matches("(0\\d{2,3}\\-{0,1}){0,1}[1-9]\\d{6,7}"); + } +} diff --git a/work10/src/cn/czyx007/DAO/StudentDAO.java b/work10/src/cn/czyx007/DAO/StudentDAO.java new file mode 100644 index 0000000..cfd112e --- /dev/null +++ b/work10/src/cn/czyx007/DAO/StudentDAO.java @@ -0,0 +1,35 @@ +package cn.czyx007.DAO; + +import cn.czyx007.bean.Student; + +import java.util.List; + +public interface StudentDAO { + /** + * 获取该学生的各科课程平均分 + * @param student 某个学生 + * @return 平均分 + */ + double getAverageScoreByStu(Student student); + + /** + * 获取有该课程的所有学生的平均分 + * @param studentList 所有学生 + * @param course 特定课程 + * @return 平均分 + */ + double getAverageScoreByCourse(List studentList, String course); + + /** + * 显示学生所有信息 + * @param studentList 所有学生 + */ + void displayByStu(List studentList); + + /** + * 显示各课程所有信息 + * @param studentList 所有学生 + */ + void displayByCourse(List studentList); + +} diff --git a/work10/src/cn/czyx007/DAO/impl/StudentDAOImpl.java b/work10/src/cn/czyx007/DAO/impl/StudentDAOImpl.java new file mode 100644 index 0000000..dbba365 --- /dev/null +++ b/work10/src/cn/czyx007/DAO/impl/StudentDAOImpl.java @@ -0,0 +1,58 @@ +package cn.czyx007.DAO.impl; + +import cn.czyx007.DAO.StudentDAO; +import cn.czyx007.bean.Student; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class StudentDAOImpl implements StudentDAO { + @Override + public double getAverageScoreByStu(Student student) { + double sum=0; + Map courseScore = student.getCourseScoreMap(); + Set courses = courseScore.keySet(); + for (String cours : courses) { + sum+=courseScore.get(cours); + } + return sum/courses.size(); + } + + @Override + public double getAverageScoreByCourse(List studentList, String course) { + double sum=0; + int cnt=0; + for (Student student : studentList) { + if (student.getCourseScoreMap().containsKey(course)) { + sum += student.getCourseScore(course); + cnt++; + } + } + return sum/cnt; + } + + @Override + public void displayByStu(List studentList) { + System.out.println("学号\t姓名\t性别\t平均分"); + for (Student student : studentList) { + System.out.print(student); + System.out.printf("\t\t%.2f\n", getAverageScoreByStu(student)); + } + } + + @Override + public void displayByCourse(List studentList) { + Set courses = new HashSet<>(); + for (Student student : studentList) { + Map courseScoreMap = student.getCourseScoreMap(); + Set keys = courseScoreMap.keySet(); + courses.addAll(keys); + } + System.out.println("课程\t平均分"); + for (String cour : courses) { + System.out.println(cour + "\t" + getAverageScoreByCourse(studentList, cour)); + } + } +} diff --git a/work10/src/cn/czyx007/Driver.java b/work10/src/cn/czyx007/Driver.java new file mode 100644 index 0000000..8ad3adf --- /dev/null +++ b/work10/src/cn/czyx007/Driver.java @@ -0,0 +1,44 @@ +package cn.czyx007; + +import cn.czyx007.DAO.StudentDAO; +import cn.czyx007.DAO.impl.StudentDAOImpl; +import cn.czyx007.bean.Student; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Driver { + private static StudentDAO studentDAO = new StudentDAOImpl(); + + public static void main(String[] args) { + List studentList = inputFromKeyBoard(); + System.out.println("按学生统计:"); + studentDAO.displayByStu(studentList); + System.out.println("按课程统计:"); + studentDAO.displayByCourse(studentList); + } + + public static List inputFromKeyBoard(){ + List studentList = new ArrayList<>(); + Scanner scanner = new Scanner(System.in); + String data = ""; + System.out.println("请输入学生基本信息:"); + while (!(data=scanner.nextLine()).equals("end")){ + String[] stuArray = data.split(",|,"); + studentList.add(new Student(stuArray[0], stuArray[1], stuArray[2])); + } + + data = ""; + System.out.println("请输入学生成绩:"); + while (!(data=scanner.nextLine()).equals("end")){ + String[] courseArray = data.split(",|,"); + for (Student student : studentList) { + if (student.getId().equals(courseArray[0])) { + student.setCourseScore(courseArray[1], Double.valueOf(courseArray[2])); + } + } + } + return studentList; + } +} diff --git a/work10/src/cn/czyx007/bean/Student.java b/work10/src/cn/czyx007/bean/Student.java new file mode 100644 index 0000000..c4670b7 --- /dev/null +++ b/work10/src/cn/czyx007/bean/Student.java @@ -0,0 +1,62 @@ +package cn.czyx007.bean; + +import java.util.HashMap; +import java.util.Map; + +public class Student { + private String id; + private String name; + private String gender; + private Map courseScore; + + public Student() { + } + + public Student(String id, String name, String gender) { + this.id = id; + this.name = name; + this.gender = gender; + courseScore = new HashMap<>(); + } + + 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 String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public Map getCourseScoreMap() { + return courseScore; + } + + public Double getCourseScore(String course) { + return courseScore.get(course); + } + + public void setCourseScore(String course, Double score) { + courseScore.put(course, score); + } + + @Override + public String toString() { + return id + "\t\t" + name + "\t" + gender; + } +} diff --git a/work10/work10.iml b/work10/work10.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/work10/work10.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file