2022/10/01 23:24 完成第8 9次作业

This commit is contained in:
zyx 2022-10-01 23:24:58 +08:00
parent 66bc7cfa1a
commit 7f4916cfbb
8 changed files with 382 additions and 0 deletions

2
.idea/modules.xml generated
View File

@ -8,6 +8,8 @@
<module fileurl="file://$PROJECT_DIR$/Week5/Week5.iml" filepath="$PROJECT_DIR$/Week5/Week5.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$/work8/work8.iml" filepath="$PROJECT_DIR$/work8/work8.iml" />
<module fileurl="file://$PROJECT_DIR$/work9/work9.iml" filepath="$PROJECT_DIR$/work9/work9.iml" />
</modules>
</component>
</project>

37
work8/src/Student.java Normal file
View File

@ -0,0 +1,37 @@
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 21:21
*/
public class Student {
private String id;
private String name;
public Student() {
}
public Student(String id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return id + "\t" + name + "\n";
}
}

View File

@ -0,0 +1,87 @@
import java.util.Arrays;
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 21:21
*/
public class SubjectEnrolment {
private String id;
private String name;
private Integer semester;
private Integer year;
private Student[] students;
private Integer count;
public SubjectEnrolment() {
}
public SubjectEnrolment(String id, String name, Integer semester, Integer year, Integer max) {
this.id = id;
this.name = name;
this.semester = semester;
this.year = year;
this.students = new Student[max];
this.count = 0;
}
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 Integer getSemester() {
return semester;
}
public void setSemester(Integer semester) {
this.semester = semester;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public boolean addStudent(Student student) {
if (students == null || isFull()) {
return false;
}
students[this.count++] = student;
return true;
}
public boolean isFull() {
return students.length <= this.count;
}
@Override
public String toString() {
String info = "学科报名详情:" + "\n----------------\n" +
"课程号:" + id + "\n" +
"课程名:" + name + "\n" +
"授课学期:" + semester + "\n" +
"授课学年:" + year + "\n";
if (count != 0) {
info += "================\n学生详情\n----------------\n学号\t姓名\n";
for (int i = 0; i < count; i++) {
info += students[i];
}
}
return info;
}
}

View File

@ -0,0 +1,111 @@
import java.io.IOException;
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 21:20
*/
public class SubjectEnrolmentDriver {
private static SubjectEnrolment subjectEnrolment = null;
public static void main(String[] args) throws IOException {
while (true) {
int opt = menu();
switch (opt) {
case 1:
createNewSubject();
break;
case 2:
addStudent();
break;
case 3:
printSubjectDetails();
break;
case 4:
return;
}
System.out.println("按任意键继续...");
System.in.read();
}
}
/**
* 检查菜单选项输入合法性
* @param low 选项下界
* @param high 选项上界
* @return 合法选项值
*/
public static int checkInput(int low, int high) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int opt = scanner.nextInt();
if (opt >= low && opt <= high) {
return opt;
}
System.out.println("输入超限,请输入" + low + "-" + high + "的数字");
} else {
System.out.println("输入数据类型错误,请输入" + low + "-" + high + "的数字");
scanner.next();
}
return -1;
}
public static int menu() {
System.out.println("欢迎使用学科报名系统");
System.out.println("1.创建新学科");
System.out.println("2.添加学生");
System.out.println("3.查看学科详情");
System.out.println("4.退出");
System.out.print(">>>请选择(1-4)");
int opt = -1;
while (opt == -1) {
opt = checkInput(1,4);
}
return opt;
}
public static void createNewSubject() {
if (subjectEnrolment == null) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程号:");
String id = scanner.nextLine();
System.out.println("请输入课程名:");
String name = scanner.nextLine();
System.out.println("请输入授课学期:");
int semester = scanner.nextInt();
System.out.println("请输入授课学年:");
int year = scanner.nextInt();
subjectEnrolment = new SubjectEnrolment(id, name, semester, year, 5);
} else {
System.out.println("课程已创建!无需重复创建");
}
}
public static void addStudent() {
if (subjectEnrolment == null){
System.out.println("学科不存在请先执行功能1");
return;
}
if (!subjectEnrolment.isFull()) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
subjectEnrolment.addStudent(new Student(id, name));
System.out.println("学生添加成功!");
} else {
System.out.println("学生已满!无法添加更多学生");
}
}
public static void printSubjectDetails() {
if (subjectEnrolment != null) {
System.out.println(subjectEnrolment);
} else {
System.out.println("学科不存在请先执行功能1");
}
}
}

11
work8/work8.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>

79
work9/src/Exercise1.java Normal file
View File

@ -0,0 +1,79 @@
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 22:21
*/
public class Exercise1 {
public static void main(String[] args) {
String expression = new Scanner(System.in).nextLine();
int cnt = 0;
for (int i = 0; i < expression.length(); i++) {
if ('(' == expression.charAt(i)){
cnt++;
} else if (')' == expression.charAt(i)){
cnt--;
}
}
if (cnt != 0){
System.out.println("输入格式错误!");
return;
}
String[] opts = expression.split(",");
double res = 0;
if(opts.length == 2){
String exp = expression.substring(0, expression.indexOf("("));
if ("doubleMe".equals(exp)){
double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")")));
res = num*2;
} else {
double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(",")));
double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")")));
switch (exp){
case "add":
res = num1+num2;
break;
case "sub":
res = num1-num2;
break;
case "max":
res = Math.max(num1, num2);
break;
case "min":
res = Math.min(num1, num2);
break;
}
}
} else {
for (int i = opts.length-1; i >= 0; i--) {
String exp = opts[i].substring(0, opts[i].indexOf("("));
double num;
if(i != opts.length-1){
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(")+1));
} else {
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(") + 1, opts[i].indexOf(")")));
}
switch (exp){
case "add":
res += num;
break;
case "sub":
res -= num;
break;
case "max":
res = Math.max(res, num);
break;
case "min":
res = Math.min(res, num);
break;
case "doubleMe":
res = num*2;
break;
}
}
}
System.out.println(expression + "=" + res);
}
}

44
work9/src/Exercise2.java Normal file
View File

@ -0,0 +1,44 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 23:00
*/
public class Exercise2 {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String serialNumber = df.format(date);
int cnt = 0;
Scanner scanner = new Scanner(System.in);
List<String> details = new ArrayList<>();
while (true){
String input = scanner.nextLine();
if ("0000".equals(input)){
break;
}
String tmpSerialNumber = serialNumber + String.format("%04d",cnt++);
String detail = tmpSerialNumber + "," +input;
details.add(detail);
}
System.out.println("流水号\t\t\t商品名称\t商品数量\t单价\t总价");
int allSaleNum = 0;
double allSaleMoney = 0;
for (String each : details) {
String[] detail = each.split(",");
System.out.print(detail[0] + "\t" + detail[1] + "\t" + detail[2] + "\t\t" + detail[3] + "\t\t" );
int saleNum = Integer.parseInt(detail[2]);
double saleMoney = Double.parseDouble(detail[3]);
System.out.println(saleNum*saleMoney);
allSaleNum += saleNum;
allSaleMoney += saleNum*saleMoney;
}
System.out.println("总计:销售笔数 " + details.size() + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
}
}

11
work9/work9.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>