2022/10/14 18:32

This commit is contained in:
zyx 2022-10-14 18:32:45 +08:00
parent 442a867c4f
commit a4bdf0730e
12 changed files with 552 additions and 0 deletions

2
.idea/modules.xml generated
View File

@ -7,8 +7,10 @@
<module fileurl="file://$PROJECT_DIR$/Week4/Week4.iml" filepath="$PROJECT_DIR$/Week4/Week4.iml" />
<module fileurl="file://$PROJECT_DIR$/Week5/Week5.iml" filepath="$PROJECT_DIR$/Week5/Week5.iml" />
<module fileurl="file://$PROJECT_DIR$/Week6/Week6.iml" filepath="$PROJECT_DIR$/Week6/Week6.iml" />
<module fileurl="file://$PROJECT_DIR$/Week7/Week7.iml" filepath="$PROJECT_DIR$/Week7/Week7.iml" />
<module fileurl="file://$PROJECT_DIR$/java_class.iml" filepath="$PROJECT_DIR$/java_class.iml" />
<module fileurl="file://$PROJECT_DIR$/week1/week1.iml" filepath="$PROJECT_DIR$/week1/week1.iml" />
<module fileurl="file://$PROJECT_DIR$/work10/work10.iml" filepath="$PROJECT_DIR$/work10/work10.iml" />
<module fileurl="file://$PROJECT_DIR$/work8/work8.iml" filepath="$PROJECT_DIR$/work8/work8.iml" />
<module fileurl="file://$PROJECT_DIR$/work9/work9.iml" filepath="$PROJECT_DIR$/work9/work9.iml" />
</modules>

11
Week7/Week7.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,8 @@
package cn.czyx007.DAO;
/**
* @author : 张宇轩
* @createTime : 2022/10/12 - 10:12
*/
public class StudentDAO {
}

View File

@ -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<Student> studentList = inputFromKeyBoard();
List<Result> resultList = calculateProvince(studentList);
display(studentList, resultList);
}
public static List<Student> inputFromKeyBoard(){
List<Student> 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<Student> studentList, String gender){
int count=0;
for (Student student : studentList) {
if(student.getGender().equals(gender)){
count++;
}
}
return count;
}
public static List<Result> calculateProvince(List<Student> studentList){
List<Result> 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<Result> 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<Student> studentList, List<Result> 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);
}
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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 + '\'' +
'}';
}
}

122
work10/src/Validate.java Normal file
View File

@ -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}");
}
}

View File

@ -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<Student> studentList, String course);
/**
* 显示学生所有信息
* @param studentList 所有学生
*/
void displayByStu(List<Student> studentList);
/**
* 显示各课程所有信息
* @param studentList 所有学生
*/
void displayByCourse(List<Student> studentList);
}

View File

@ -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<String, Double> courseScore = student.getCourseScoreMap();
Set<String> courses = courseScore.keySet();
for (String cours : courses) {
sum+=courseScore.get(cours);
}
return sum/courses.size();
}
@Override
public double getAverageScoreByCourse(List<Student> 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<Student> 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<Student> studentList) {
Set<String> courses = new HashSet<>();
for (Student student : studentList) {
Map<String, Double> courseScoreMap = student.getCourseScoreMap();
Set<String> keys = courseScoreMap.keySet();
courses.addAll(keys);
}
System.out.println("课程\t平均分");
for (String cour : courses) {
System.out.println(cour + "\t" + getAverageScoreByCourse(studentList, cour));
}
}
}

View File

@ -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<Student> studentList = inputFromKeyBoard();
System.out.println("按学生统计:");
studentDAO.displayByStu(studentList);
System.out.println("按课程统计:");
studentDAO.displayByCourse(studentList);
}
public static List<Student> inputFromKeyBoard(){
List<Student> 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;
}
}

View File

@ -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<String, Double> 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<String, Double> 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;
}
}

11
work10/work10.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>