diff --git a/.idea/modules.xml b/.idea/modules.xml
index 0a02a5b..5aba50b 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -6,6 +6,7 @@
+
diff --git a/Week6/Week6.iml b/Week6/Week6.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/Week6/Week6.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Week6/src/Exercise5.java b/Week6/src/Exercise5.java
new file mode 100644
index 0000000..314cf14
--- /dev/null
+++ b/Week6/src/Exercise5.java
@@ -0,0 +1,101 @@
+import java.io.IOException;
+import java.util.Scanner;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/10/5 - 18:24
+ */
+public class Exercise5 {
+ public static void main(String[] args) throws IOException {
+ while (true) {
+ int opt = menu();
+ switch (opt) {
+ case 1:
+ checkPasswordFormat();
+ break;
+ case 2:
+ break;
+ case 3:
+ 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("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 checkPasswordFormat() {
+ System.out.println();
+ while (true) {
+ System.out.println("密码要求:\n1.长度必须在8到15个字符之间\n" +
+ "2.不得包含空格\n" +
+ "3.必须至少包含一个非数字的大写字符和一个非数字的小写字符\n" +
+ "4.必须包含至少一个数字数字");
+ System.out.println(">>请输入密码:");
+ Scanner scanner = new Scanner(System.in);
+ String password = scanner.nextLine();
+
+ String info = "";
+ if (password.length() >= 8 && password.length() <= 15) {
+ int numCnt = 0, lowCnt = 0, upCnt = 0, spaceCnt = 0;
+ for (int i = 0; i < password.length(); i++) {
+ if (password.charAt(i) >= '0' && password.charAt(i) <= '9')
+ numCnt++;
+ else if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z')
+ lowCnt++;
+ else if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z')
+ upCnt++;
+ else if (password.charAt(i) == ' ')
+ spaceCnt++;
+ }
+ if (numCnt >= 1 && lowCnt >= 1 && upCnt >= 1 && spaceCnt == 0) {
+ info = "符合要求";
+ System.out.println("恭喜你,你的密码'" + password + "'" + info);
+ return;
+ }
+ else{
+ if (numCnt == 0) info += "密码至少需要一个数字\n";
+ if (upCnt == 0) info += "密码至少需要一个大写字母\n";
+ if (lowCnt == 0) info += "密码至少需要一个小写字母\n";
+ if (spaceCnt == 1) info += "密码包含了空格\n";
+ }
+ } else info += "密码长度不符合要求\n";
+ System.out.println("你的密码" + password + "无效,原因如下:\n" + info);
+ }
+ }
+}
diff --git a/Week6/src/PasswordCheck.java b/Week6/src/PasswordCheck.java
new file mode 100644
index 0000000..588198d
--- /dev/null
+++ b/Week6/src/PasswordCheck.java
@@ -0,0 +1,46 @@
+import java.util.Scanner;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/10/5 - 8:17
+ */
+public class PasswordCheck {
+ public static void main(String[] args) {
+ System.out.println("密码要求:\n长度必须在8到15个字符之间\n" +
+ "不得包含空格\n" +
+ "必须至少包含一个非数字的大写字符和一个非数字的小写字符\n" +
+ "必须包含至少一个数字数字");
+ System.out.println("请输入密码:");
+ Scanner scanner = new Scanner(System.in);
+ String password = scanner.nextLine();
+ System.out.println(checkFormat(password));
+ }
+
+ public static String checkFormat(String pwd) {
+ String info = "";
+ if (pwd.length() >= 8 && pwd.length() <= 15) {
+ int numCnt = 0, lowCnt = 0, upCnt = 0, spaceCnt = 0;
+ for (int i = 0; i < pwd.length(); i++) {
+ if (pwd.charAt(i) >= '0' && pwd.charAt(i) <= '9')
+ numCnt++;
+ else if (pwd.charAt(i) >= 'a' && pwd.charAt(i) <= 'z')
+ lowCnt++;
+ else if (pwd.charAt(i) >= 'A' && pwd.charAt(i) <= 'Z')
+ upCnt++;
+ else if (pwd.charAt(i) == ' ')
+ spaceCnt++;
+ }
+ if (numCnt >= 1 && lowCnt >= 1 && upCnt >= 1 && spaceCnt == 0) {
+ info = "符合要求";
+ return "恭喜你,你的密码'" + pwd + "'" + info;
+ }
+ else{
+ if (numCnt == 0) info += "密码至少需要一个数字\n";
+ if (upCnt == 0) info += "密码至少需要一个大写字母\n";
+ if (lowCnt == 0) info += "密码至少需要一个小写字母\n";
+ if (spaceCnt == 1) info += "密码包含了空格\n";
+ }
+ } else info += "密码长度不符合要求";
+ return "你的密码" + pwd + "无效,原因如下:\n" + info;
+ }
+}
\ No newline at end of file
diff --git a/Week6/src/exercise5/Driver.java b/Week6/src/exercise5/Driver.java
new file mode 100644
index 0000000..0430b1e
--- /dev/null
+++ b/Week6/src/exercise5/Driver.java
@@ -0,0 +1,62 @@
+package exercise5;
+
+import java.io.IOException;
+import java.util.Scanner;
+
+public class Driver {
+ public static void main(String[] args) throws IOException {
+ while (true) {
+ int opt = menu();
+ switch (opt) {
+ case 1:
+ PasswordCheck.checkPasswordFormat();
+ break;
+ case 2:
+ ExpressionCalculate.calculate();
+ break;
+ case 3:
+ MarketCashier.cashier();
+ 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("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;
+ }
+}
+
diff --git a/Week6/src/exercise5/ExpressionCalculate.java b/Week6/src/exercise5/ExpressionCalculate.java
new file mode 100644
index 0000000..66609ac
--- /dev/null
+++ b/Week6/src/exercise5/ExpressionCalculate.java
@@ -0,0 +1,114 @@
+package exercise5;
+
+import java.util.Scanner;
+
+public class ExpressionCalculate {
+ public static void calculate() {
+ System.out.println("add将两个操作数相加\n" +
+ "sub将两个操作数相减\n" +
+ "max求解两个数中的较大数\n" +
+ "min求解两个数中的较小数\n" +
+ "doubleMe将一个操作数翻倍(乘以2)");
+ System.out.println("请输入待运算的表达式:");
+ String expression = new Scanner(System.in).nextLine();
+
+ if (!checkFormat(expression)){
+ System.out.println("输入格式错误!");
+ return;
+ }
+
+ String[] opts = expression.split(",");
+ double res;
+ if(opts.length == 2){
+ res = calcSingletonExpression(expression);
+ } else {
+ res = calcMultipleExpression(opts);
+ }
+ System.out.println(expression + "=" + res);
+ }
+
+ /**
+ * 检查输入格式(括号是否匹配)
+ * @param expression 输入的表达式
+ * @return true为表达式括号格式正确
+ */
+ public static boolean checkFormat(String expression){
+ int cnt = 0;
+ for (int i = 0; i < expression.length(); i++) {
+ if ('(' == expression.charAt(i)){
+ cnt++;
+ } else if (')' == expression.charAt(i)){
+ cnt--;
+ }
+ }
+ return cnt == 0;
+ }
+
+ /**
+ * 计算单运算符的表达式
+ * @param expression 输入的单运算符的表达式
+ * @return 运算结果
+ */
+ public static double calcSingletonExpression(String expression){
+ double res = 0;
+ 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;
+ }
+ }
+ return res;
+ }
+
+ /**
+ * 计算多运算符(运算符嵌套)的表达式
+ * @param opts 输入的多运算符的表达式以','分割的String数组
+ * @return 运算结果
+ */
+ public static double calcMultipleExpression(String[] opts){
+ double res = 0;
+ 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;
+ }
+ }
+ return res;
+ }
+}
\ No newline at end of file
diff --git a/Week6/src/exercise5/MarketCashier.java b/Week6/src/exercise5/MarketCashier.java
new file mode 100644
index 0000000..7a83513
--- /dev/null
+++ b/Week6/src/exercise5/MarketCashier.java
@@ -0,0 +1,37 @@
+package exercise5;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Scanner;
+
+public class MarketCashier {
+ public static void cashier(){
+ Date date = new Date();
+ SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
+ String serialNumber = df.format(date);
+
+ Scanner scanner = new Scanner(System.in);
+ int cnt = 1, allSaleNum = 0;
+ double allSaleMoney = 0;
+ StringBuilder info = new StringBuilder();
+ System.out.println("请依次输入销售记录(商品名称,销售数量,商品单价),输入'0000'时结束输入:");
+ while (true){
+ String input = scanner.nextLine();
+ if ("0000".equals(input)){
+ break;
+ }
+ String tmpSerialNumber = serialNumber + String.format("%04d",cnt++);
+
+ String[] detail = input.split(",");
+
+ int saleNum = Integer.parseInt(detail[1]);
+ double saleMoney = Double.parseDouble(detail[2]);
+ allSaleNum += saleNum;
+ allSaleMoney += saleNum*saleMoney;
+ info.append(tmpSerialNumber).append("\t").append(detail[0]).append("\t").append(detail[1]).append("\t\t").append(detail[2]).append("\t\t").append(saleNum * saleMoney).append("\n");
+ }
+ System.out.println("流水号\t\t\t商品名称\t商品数量\t单价\t总价");
+ System.out.println(info);
+ System.out.println("总计:销售笔数 " + (cnt-1) + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
+ }
+}
diff --git a/Week6/src/exercise5/PasswordCheck.java b/Week6/src/exercise5/PasswordCheck.java
new file mode 100644
index 0000000..484beb3
--- /dev/null
+++ b/Week6/src/exercise5/PasswordCheck.java
@@ -0,0 +1,45 @@
+package exercise5;
+
+import java.util.Scanner;
+
+public class PasswordCheck {
+ public static void checkPasswordFormat() {
+ System.out.println();
+ while (true) {
+ System.out.println("密码要求:\n1.长度必须在8到15个字符之间\n" +
+ "2.不得包含空格\n" +
+ "3.必须至少包含一个非数字的大写字符和一个非数字的小写字符\n" +
+ "4.必须包含至少一个数字数字");
+ System.out.println(">>请输入密码:");
+ Scanner scanner = new Scanner(System.in);
+ String password = scanner.nextLine();
+
+ String info = "";
+ if (password.length() >= 8 && password.length() <= 15) {
+ int numCnt = 0, lowCnt = 0, upCnt = 0, spaceCnt = 0;
+ for (int i = 0; i < password.length(); i++) {
+ if (password.charAt(i) >= '0' && password.charAt(i) <= '9')
+ numCnt++;
+ else if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z')
+ lowCnt++;
+ else if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z')
+ upCnt++;
+ else if (password.charAt(i) == ' ')
+ spaceCnt++;
+ }
+ if (numCnt >= 1 && lowCnt >= 1 && upCnt >= 1 && spaceCnt == 0) {
+ info = "符合要求";
+ System.out.println("恭喜你,你的密码'" + password + "'" + info);
+ return;
+ }
+ else{
+ if (numCnt == 0) info += "密码至少需要一个数字\n";
+ if (upCnt == 0) info += "密码至少需要一个大写字母\n";
+ if (lowCnt == 0) info += "密码至少需要一个小写字母\n";
+ if (spaceCnt != 0) info += "密码包含了空格\n";
+ }
+ } else info += "密码长度不符合要求\n";
+ System.out.println("你的密码" + password + "无效,原因如下:\n" + info);
+ }
+ }
+}
diff --git a/work9/src/Exercise1.java b/work9/src/Exercise1.java
index a03e7bc..c533ee5 100644
--- a/work9/src/Exercise1.java
+++ b/work9/src/Exercise1.java
@@ -1,13 +1,30 @@
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();
+ if (!checkFormat(expression)){
+ System.out.println("输入格式错误!");
+ return;
+ }
+
+ String[] opts = expression.split(",");
+ double res = 0;
+ if(opts.length == 2){
+ res = calcSingletonExpression(expression);
+ } else {
+ res = calcMultipleExpression(opts);
+ }
+ System.out.println(expression + "=" + res);
+ }
+
+ /**
+ * 检查输入格式(括号是否匹配)
+ * @param expression 输入的表达式
+ * @return true为表达式括号格式正确
+ */
+ public static boolean checkFormat(String expression){
int cnt = 0;
for (int i = 0; i < expression.length(); i++) {
if ('(' == expression.charAt(i)){
@@ -16,64 +33,74 @@ public class Exercise1 {
cnt--;
}
}
- if (cnt != 0){
- System.out.println("输入格式错误!");
- return;
- }
+ return cnt == 0;
+ }
- String[] opts = expression.split(",");
+ /**
+ * 计算单运算符的表达式
+ * @param expression 输入的单运算符的表达式
+ * @return 运算结果
+ */
+ public static double calcSingletonExpression(String expression){
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;
- }
- }
+ 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 {
- 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;
- }
+ 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;
}
}
- System.out.println(expression + "=" + res);
+ return res;
+ }
+
+ /**
+ * 计算多运算符(运算符嵌套)的表达式
+ * @param opts 输入的多运算符的表达式以','分割的String数组
+ * @return 运算结果
+ */
+ public static double calcMultipleExpression(String[] opts){
+ double res = 0;
+ 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;
+ }
+ }
+ return res;
}
}
diff --git a/work9/src/Exercise2.java b/work9/src/Exercise2.java
index d30a829..e256c14 100644
--- a/work9/src/Exercise2.java
+++ b/work9/src/Exercise2.java
@@ -1,44 +1,34 @@
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;
+ int cnt = 1, allSaleNum = 0;
+ double allSaleMoney = 0;
Scanner scanner = new Scanner(System.in);
- List details = new ArrayList<>();
+ String info = "";
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);
+ String[] detail = input.split(",");
+
+ int saleNum = Integer.parseInt(detail[1]);
+ double saleMoney = Double.parseDouble(detail[2]);
allSaleNum += saleNum;
allSaleMoney += saleNum*saleMoney;
+ info += tmpSerialNumber + "\t" + detail[0] + "\t" + detail[1] + "\t\t" + detail[2] + "\t\t" + saleNum*saleMoney + "\n";
}
- System.out.println("总计:销售笔数 " + details.size() + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
+ System.out.println("流水号\t\t\t商品名称\t商品数量\t单价\t总价");
+ System.out.println(info);
+ System.out.println("总计:销售笔数 " + (cnt-1) + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
}
}