首页 > 其他分享 >pta题目集7~11总结性Blog

pta题目集7~11总结性Blog

时间:2023-06-24 13:33:29浏览次数:46  
标签:11 newClass get int pta Blog str public out

前言:

第七次题目集:本次题目集是菜单计价系统的一次迭代,在原有功能的基础上对输入的特色菜分为川菜,晋菜和浙菜三类,并在最后输出一桌的价格的时候还要将三种特色菜的点菜的口味程度一起输出,还要求对每位顾客的姓名和手机号进行格式的规范。本次题目需要在之前的菜单计价系统的基础之上再增加一个新的table类用来计算并存储每一桌的特色菜的口味程度,建立起table类和其他类之间的关系,还需要用正则表达式重新来规范输入的数据,来判断输入的数据是否错误,还有格式是否错误,如规定手机号和姓名。本次题目级难度较难,题量较少,较为考察逻辑思维。

第八次题目集:本次题目集为一个简单的课程成绩统计程序。本次题目集主要考察逻辑思考能力,对输入的课程,班级,学生的成绩进行整理,还要对课程,班级和学生的成绩按照不同的要求进行输出,考察代码程序对输入数据的计算和整理。数据的输入分为课程还有学生成绩两部分组成,最后以end的结束输入,程序需要用while循环来判断两个部分的输入是否结束,还需要大量地运用到正则表达式对输入的课程数据还有每位学生的成绩进行规范,还需要用spilt()函数对输入的每行的数据进行分割得到我们所需要的数据,需要在不同的类中定义处理本类的数据的方法来简化主函数,如在course类中定义一个判断输入课程数据是否合法的方法,合法就将其存入course类之中并返回true,还需运用到冒泡排序法对课程,班级和学生的输出次序进行排序,还用到了Integer.parseInt()将字符串转化为整数进行计算,本次题目集难度较难,题量较少,考察对数据的计算和整理。

第九次题目集:本次题目集为一道统计java程序中关键词的出现次数的题,需要用到Map类对出现的各个关键词的次数进行统计,需要用到stringBuilder类用来加入形成一个单词,需要使用while循环来判断输入是否结束,需要用到正则表达式对输入的格式进行规范,String类之中的replaceAll(),substring()方法,还需运用到Map类的遍历方法。本次题目集题量较少,难度中等。

第十次题目集:本次题目集主要学习和练习了HashMap的检索和排序,进行了对HsahMap类的练习,第四题则是进行了对多态的训练,而第三题则是在第八次题目集课程成绩统计程序的基础上进行迭代,在之前的必修课和选修课的基础上加上了一个实验课,并且实验课的成绩计算方式不同于必修课和选修课,本次题目集可在之前的基础上增加一个新的实验类来计算实验课的成绩,需要用正则表达式来判断输入的实验课次数是否合法还有与要求的是否对应,本次题目集题量中等,难度偏高,但在第八次题目集已经打好了基础,代码量较少。

第十一次题目集:本次题目集主要练习了ArrayList-排序还有Map类对数据的整理和排序,还有自定义栈接口对栈的使用,以及现在需要定义PersonOverride类并覆盖其toStringequals方法,最后第二题为课程成绩统计程序-3在第二次的基础上修改了计算总成绩的方式,需要修改类结构,将成绩类的继承关系改为组合关系,使用分值和权重的计算方法,考察了重构的能力,本次题目集题量较多,难度较难。

题目集七7-1 菜单计价程序-5

  1 import java.time.DayOfWeek;
  2 import java.time.LocalDate;
  3 import java.time.LocalDateTime;
  4 import java.time.LocalTime;
  5 import java.util.ArrayList;
  6 import java.util.Scanner;
  7 
  8 public class Main {
  9     public static void main(String[] args) {
 10         double discount = 1;
 11         int number;
 12         Customers customers = new Customers();
 13         Menu newMenu = new Menu();
 14         Scanner input = new Scanner(System.in);
 15         String string = input.nextLine();
 16         while (string.charAt(0) != 't') {//录入菜单
 17             if (string.charAt(string.length() - 1) == 'T') {//特色菜情况
 18                 String[] str = string.split(" ");
 19                 if (str[1].contains(".")) {
 20                     System.out.println("wrong format");
 21                 } else if (str.length != 4) {
 22                     System.out.println("wrong format");
 23                 } else if (!str[2].matches("^\\+?[1-9][0-9]*$+")) {
 24                     System.out.println("wrong format");
 25                 } else if (Integer.parseInt(str[2]) < 0 || Integer.parseInt(str[2]) >= 300) {//判断菜价有没有超过范围
 26                     System.out.println(str[0] + " price out of range " + Integer.parseInt(str[2]));
 27                 } else if (str[2].charAt(0) == '0') {//判断最高位是不是以0开头
 28                     System.out.println("wrong format");
 29                 } else
 30                     newMenu.addDish(str[0], Integer.parseInt(str[2]), str[1]);
 31             } else {//普通菜情况
 32                 String[] str = string.split(" ");
 33                 if (str[1].contains(".")) {
 34                     System.out.println("wrong format");
 35                 } else if (str.length != 2) {
 36                     System.out.println("wrong format");
 37                 } else if (!str[1].matches("^\\+?[1-9][0-9]*$+")) {
 38                     System.out.println("wrong format");
 39                 } else if (Integer.parseInt(str[1]) < 0 || Integer.parseInt(str[1]) >= 300) {//判断菜价有没有超过范围
 40                     System.out.println(str[0] + " price out of range " + Integer.parseInt(str[1]));
 41                 } else if (str[0].charAt(0) == '0') {//判断最高位是不是以0开头
 42                     System.out.println("wrong format");
 43                 } else
 44                     newMenu.addDish(str[0], Integer.parseInt(str[1]), "普通菜");
 45             }
 46             string = input.nextLine();
 47         }
 48         ArrayList<Table> recordTable = new ArrayList<>();
 49         while (string.compareTo("end") != 0) {//判断是否结束
 50             ArrayList<Integer> helpOrderNum = new ArrayList<>();
 51             ArrayList<Integer> helpOrderTable = new ArrayList<>();
 52             int judgeTimeMater = 0;
 53             int sum = 0;
 54             int sum1 = 0;
 55             String[] str = string.split(" ");//将string中的数据分开存到str中
 56             if (str[0].compareTo("table") == 0 && (!(str[1].matches("^\\+?[1-9][0-9]*$+")) || str[1].charAt(0) == '0') || str.length != 7) {//判断桌号出现非数值情况
 57                 System.out.println("wrong format");//输出报错
 58                 string = input.nextLine();//输入
 59                 while (string.charAt(0) != 't') {//判断输入是否为新的一桌,是的话跳出循环
 60                     string = input.nextLine();//输入
 61                     if (string.compareTo("end") == 0) {//判断是否结束
 62                         System.exit(1);//退出程序
 63                     }
 64                 }
 65             } else if (str[0].compareTo("table") == 0 && Integer.parseInt(str[1]) > 55) {//判断桌号大于55的情况
 66                 System.out.println(Integer.parseInt(str[1]) + " table num out of range");
 67                 string = input.nextLine();
 68                 while (string.charAt(0) != 't' || string.compareTo("end") != 0) {//判断输入是否为新的一桌,是的话跳出循环
 69                     string = input.nextLine();//输入
 70                     if (string.compareTo("end") == 0) {//判断是否结束
 71                         System.exit(1);//退出程序
 72                     }
 73                 }
 74             } else if (str[0].compareTo("table") == 0 && (str[1].matches("[1-9][0-5]") || str[1].matches("[1-9]"))) {
 75                 ArrayList<Integer> deleteOrder = new ArrayList<>();
 76                 String[] time1 = str[5].split("/"); //将年,月,日的信息分开来传入到time1字符串数组中
 77                 String[] time2 = str[6].split("/");  //将时,分,秒的信息分开来传入到time2字符串数组中
 78                 if (!(time1[0].length() == 4 && (time1[1].length() == 1 || time1[1].length() == 2) && (time1[2].length() == 1 || time1[2].length() == 2) && (time2[0].length() == 1 || time2[0].length() == 2) && (time2[1].length() == 1 || time2[1].length() == 2) && (time2[2].length() == 1 || time2[2].length() == 2))) {
 79                     System.out.print("wrong format");//判断输入的时间格式是否合法
 80                     string = input.nextLine();//录入新一桌的信息
 81                     while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
 82                         string = input.nextLine();
 83                     }
 84                     judgeTimeMater = 1;
 85                 } else if (!(Integer.parseInt(time1[0]) > 1000 && (Integer.parseInt(time1[1]) <= 12 && Integer.parseInt(time1[1]) >= 1) && (Integer.parseInt(time1[2]) >= 1 && Integer.parseInt(time1[2]) <= 31) && (Integer.parseInt(time2[0]) >= 0 && Integer.parseInt(time2[0]) < 24) && (Integer.parseInt(time2[1]) >= 0 && Integer.parseInt(time2[1]) <= 60) && (Integer.parseInt(time2[2]) >= 0 && Integer.parseInt(time2[2]) <= 60))) {
 86                     System.out.print(Integer.parseInt(str[1]) + " date error");//判断输入的时间数据是否非法
 87                     string = input.nextLine();//录入新一桌的信息
 88                     while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
 89                         string = input.nextLine();
 90                     }
 91                     judgeTimeMater = 1;
 92                 }
 93                 if (judgeTimeMater == 0) {
 94                     LocalDateTime orderTime = LocalDateTime.of(Integer.parseInt(time1[0]), Integer.parseInt(time1[1]), Integer.parseInt(time1[2]), Integer.parseInt(time2[0]), Integer.parseInt(time2[1]), Integer.parseInt(time2[2]));
 95                     LocalDate restaurantStart = LocalDate.of(2022, 1, 1); //时间有效开始
 96                     LocalDate restaurantEnd = LocalDate.of(2023, 12, 31);//时间有结束
 97                     LocalDate eatTime = orderTime.toLocalDate(); //就餐时间
 98                     LocalTime weekStartTime = LocalTime.of(9, 29, 59);
 99                     LocalTime weekEndTime = LocalTime.of(21, 30, 01);
100                     LocalTime NotweekStartMorningTime = LocalTime.of(10, 29, 59);
101                     LocalTime NotweekStartTAfternoonTime = LocalTime.of(16, 59, 59);
102                     LocalTime NotweekEndMorningTime = LocalTime.of(14, 30, 01);
103                     LocalTime NotweekEndAfternoonTime = LocalTime.of(20, 30, 01);
104                     LocalTime eatTime1 = orderTime.toLocalTime(); //就餐时间
105                     if (((!(orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY)) && ((!(eatTime1.isAfter(NotweekStartMorningTime) && eatTime1.isBefore(NotweekEndMorningTime))) && (!(eatTime1.isAfter(NotweekStartTAfternoonTime) && eatTime1.isBefore(NotweekEndAfternoonTime))))) || ((orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY) && (!(eatTime1.isAfter(weekStartTime) && eatTime1.isBefore(weekEndTime))))) {
106                         System.out.println("table " + Integer.parseInt(str[1]) + " out of opening hours");
107                         string = input.nextLine();//录入新一桌的信息
108                         while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
109                             string = input.nextLine();
110                         }
111                     } else if (str[3].length() > 10 || str[4].length() != 11 || !str[4].matches("^(180|181|189|133|135|136)\\d{8}$")) {
112                         System.out.println("wrong format");
113                         string = input.nextLine();//录入新一桌的信息
114                         while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
115                             string = input.nextLine();
116                         }
117                     } else {
118                         Table newTable = new Table();
119                         newTable.setTableNum(Integer.parseInt(str[1]));
120                         recordTable.add(newTable);
121                         customers.addCustomer(str[3], str[4]);
122                         double singlePrice = 0;
123                         System.out.println("table " + Integer.parseInt(str[1]) + ": ");  //输出
124                         newTable.setTableNum(Integer.parseInt(str[1]));
125                         if (!(orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY)) {//判断是周一到周五还是周末来赋予相应折扣
126                             discount = 0.7;
127                         }
128                         String orderMessage;
129                         orderMessage = input.nextLine();  //录入一条订单的信息
130                         number = 0; //以判断订单号是否是顺序的
131                         Order newOrder = new Order();   //创建一个新的订单信息
132                         int judgeTableMater = 0;
133                         while ((orderMessage.charAt(0) != 't' && orderMessage.charAt(0) != 'e') || judgeTableMater == 1) { //判断订单号是否结束
134                             judgeTableMater = 0;
135                             String[] orderMessage1 = orderMessage.split(" ");//将一条订单的各种信息分开
136                             if (orderMessage1.length >= 5 && orderMessage1[1].matches("^\\+?[1-9][0-9]*$+")) {//代点菜
137                                 helpOrderNum.add(Integer.parseInt(orderMessage1[1]));
138                                 helpOrderTable.add(Integer.parseInt(orderMessage1[0]));
139                                 orderMessage = input.nextLine();
140                                 if(orderMessage1.length==6) {
141                                     newOrder.addARecord(Integer.parseInt(orderMessage1[1]), orderMessage1[2], Integer.parseInt(orderMessage1[4]),Integer.parseInt(orderMessage1[5]),Integer.parseInt(orderMessage1[3]));
142                                 } else if (orderMessage1.length==5) {
143                                     newOrder.addARecord(Integer.parseInt(orderMessage1[1]), orderMessage1[2], Integer.parseInt(orderMessage1[3]),Integer.parseInt(orderMessage1[4]));
144                                 }
145                             } else {
146                                 if (!(orderMessage1[0].matches("[1-9]"))) {  //判断是否订单录入菜单
147                                     System.out.println("invalid dish");
148                                     orderMessage = input.nextLine();  //录入一条订单的信息
149                                 } else if (orderMessage1[1].compareTo("delete") != 0 && orderMessage1.length == 5) {
150                                     newOrder.addARecord(Integer.parseInt(orderMessage1[0]), orderMessage1[1], Integer.parseInt(orderMessage1[3]), Integer.parseInt(orderMessage1[4]), Integer.parseInt(orderMessage1[2]));//将订单信息传入到newOrder中
151                                     orderMessage = input.nextLine();
152                                 } else if (orderMessage1[1].compareTo("delete") != 0 && orderMessage1.length == 4) {
153                                     newOrder.addARecord(Integer.parseInt(orderMessage1[0]), orderMessage1[1], Integer.parseInt(orderMessage1[2]), Integer.parseInt(orderMessage1[3]));//将订单信息传入到newOrder中
154                                     orderMessage = input.nextLine();
155                                 }
156                                 String[] orderMessage2 = orderMessage.split(" ");
157                                 if ((orderMessage2[0].compareTo("table") != 0 && !(orderMessage2[0].matches("[1-9]"))) && orderMessage.compareTo("end") != 0) {
158                                     orderMessage = input.nextLine();
159                                     judgeTableMater = 1;
160                                 }
161                                 while (orderMessage.charAt(0) != 'e' && orderMessage.charAt(2) == 'd') {
162                                     deleteOrder.add(Integer.parseInt(orderMessage2[0]));
163                                     orderMessage = input.nextLine();
164                                     orderMessage2 = orderMessage.split(" ");
165                                     if (orderMessage.compareTo("end") == 0) {
166                                         break;
167                                     }
168                                 }
169                             }
170                         }
171                         for (int j = 1; j <= newOrder.getRecords().size(); j++) {//遍历newOrder中的每一条记录
172                             ArrayList<Dish> dishNew = newMenu.getDishs();//将newMenu中的dishs信息放到dishNew中以遍历
173                             if (1 == 1) {
174                                 for (int i = 0; i < newMenu.getDishs().size(); i++) {//将一条订单与菜单匹配
175                                     int recordHelpNum = 0;
176                                     int judgeHelpOrder = 0;
177                                     if (dishNew.get(i).getName().compareTo(newOrder.getRecords().get(j - 1).getD().getName()) == 0) {
178                                         newOrder.getRecords().get(j - 1).setD(dishNew.get(i));
179                                         if ((newOrder.getRecords().get(j - 1).getPortion() > 3 && newOrder.getRecords().get(j - 1).getPortion() <= 9)) {//普通菜份额超出范围
180                                             System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " portion out of range " + newOrder.getRecords().get(j - 1).getPortion());
181                                         } else if (newOrder.getRecords().get(j - 1).getPortion() > 9) {
182                                             System.out.println("wrong format");  //份额非法输入
183                                         } else if (newOrder.getRecords().get(j - 1).getNum() > 15) {
184                                             System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " num out of range " + newOrder.getRecords().get(j - 1).getNum());//份数超出范围
185                                         } else {
186                                             int existTable = 1;
187                                             int deleteOrderMater = 0;
188                                             for (recordHelpNum = 0; recordHelpNum < helpOrderNum.size(); recordHelpNum++) {
189                                                 if (helpOrderNum.get(recordHelpNum) == newOrder.getRecords().get(j - 1).getOrderNum()) {
190                                                     judgeHelpOrder = 1;
191                                                     for (int k = 0; k < recordTable.size(); k++) {
192                                                         if (recordTable.get(k).getTableNum() == helpOrderTable.get(recordHelpNum)) {
193                                                             existTable = 0;
194                                                         }
195                                                         if (k == recordTable.size() - 1 && existTable == 1) {
196                                                             existTable = 2;
197                                                         }
198                                                     }
199                                                     break;
200                                                 }
201                                             }
202                                             if (existTable == 2) {
203                                                 System.out.println("wrong format");
204                                             } else {
205                                                 if (judgeHelpOrder == 1) {
206                                                     System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + "table " + str[1] + " pay for table " + helpOrderTable.get(recordHelpNum) + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
207                                                 }
208                                                 for (int l = 0; l < deleteOrder.size(); l++) {
209                                                     if (deleteOrder.get(l) == newOrder.getRecords().get(j - 1).getOrderNum()) {
210                                                         deleteOrderMater = 1;
211                                                         break;
212                                                     }
213                                                 }
214                                                 int k=-1;
215                                                 int judgeDegree=1;
216                                                 int table=0;
217                                                 if(helpOrderNum.size()>0) {
218                                                     k=0;
219                                                     for (int l = 0, o = 0; l < helpOrderNum.size(); l++) {
220                                                         if (helpOrderNum.get(k) == newOrder.getRecords().get(j - 1).getOrderNum()) {
221                                                             k = l;
222                                                             o = 1;
223                                                         }
224                                                         if (o != 1 && l == helpOrderNum.size() - 1) {
225                                                             k = -1;
226                                                         }
227                                                     }
228                                                     if (k != -1) {
229                                                         for (int l = 0; l < recordTable.size(); l++) {
230                                                             if (recordTable.get(l).getTableNum() == helpOrderTable.get(k)) {
231                                                                 table = l;
232                                                             }
233                                                         }
234                                                     }
235                                                 }
236                                                 if (!(orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY)) {
237                                                     if (!(newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("普通菜"))) {
238                                                         singlePrice = Math.round(newOrder.getRecords().get(j - 1).getPrice()) * 0.7;
239                                                         if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("川菜") && newOrder.getRecords().get(j - 1).getDegree() >= 0 && newOrder.getRecords().get(j - 1).getDegree() <= 5) {
240                                                             if (k != -1) {
241                                                                 recordTable.get(table).setSichuanDegree(recordTable.get(table).getSichuanDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
242                                                                 recordTable.get(table).setSichuanNum(recordTable.get(table).getSichuanNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
243                                                             } else if (k == -1) {
244                                                                 recordTable.get(recordTable.size() - 1).setSichuanDegree(recordTable.get(recordTable.size() - 1).getSichuanDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
245                                                                 recordTable.get(recordTable.size() - 1).setSichuanNum(recordTable.get(recordTable.size() - 1).getSichuanNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
246                                                             }
247                                                             if (k == -1) {
248                                                                 System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
249                                                             }
250                                                         }else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("川菜") && (newOrder.getRecords().get(j - 1).getDegree() < 0 || newOrder.getRecords().get(j - 1).getDegree() > 5)) {
251                                                             singlePrice=0;
252                                                             judgeDegree=0;
253                                                             System.out.println("spicy num out of range :" + newOrder.getRecords().get(j - 1).getDegree());
254                                                         }
255                                                         if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("晋菜") && newOrder.getRecords().get(j - 1).getDegree() >= 0 && newOrder.getRecords().get(j - 1).getDegree() <= 4) {
256                                                             if (k != -1) {
257                                                                 recordTable.get(table).setJinDegree(recordTable.get(table).getJinDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
258                                                                 recordTable.get(table).setJinNum(recordTable.get(table).getJinNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
259                                                             } else if (k == -1) {
260                                                                 recordTable.get(recordTable.size() - 1).setJinDegree(recordTable.get(recordTable.size() - 1).getJinDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
261                                                                 recordTable.get(recordTable.size() - 1).setJinNum(recordTable.get(recordTable.size() - 1).getJinNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
262                                                             }
263                                                             if (k == -1) {
264                                                                 System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
265                                                             }
266                                                         }else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("晋菜") && (newOrder.getRecords().get(j - 1).getDegree() < 0 || newOrder.getRecords().get(j - 1).getDegree() > 4)) {
267                                                             singlePrice=0;
268                                                             judgeDegree=0;
269                                                             System.out.println("acidity num out of range :" + newOrder.getRecords().get(j - 1).getDegree());
270                                                         }
271                                                         if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("浙菜") && newOrder.getRecords().get(j - 1).getDegree() >= 0 && newOrder.getRecords().get(j - 1).getDegree() <= 3) {
272                                                             if (k != -1) {
273                                                                 recordTable.get(table).setZheDegree(recordTable.get(table).getZheDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
274                                                                 recordTable.get(table).setZheNum(recordTable.get(table).getZheNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
275                                                             } else if (k == -1) {
276                                                                 recordTable.get(recordTable.size() - 1).setZheDegree(recordTable.get(recordTable.size() - 1).getZheDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
277                                                                 recordTable.get(recordTable.size() - 1).setZheNum(recordTable.get(recordTable.size() - 1).getZheNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
278                                                             }
279                                                             if (k == -1) {
280                                                                 System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
281                                                             }
282                                                         }else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("浙菜") && (newOrder.getRecords().get(j - 1).getDegree() < 0 || newOrder.getRecords().get(j - 1).getDegree() > 3)) {
283                                                             System.out.println("sweetness num out of range :" + newOrder.getRecords().get(j - 1).getDegree());
284                                                             singlePrice=0;
285                                                             judgeDegree=0;
286                                                         }
287                                                     } else if ((newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("普通菜")) && (eatTime1.isAfter(NotweekStartTAfternoonTime) && eatTime1.isBefore(NotweekEndAfternoonTime))) {
288                                                         singlePrice = Math.round(newOrder.getRecords().get(j - 1).getPrice()) * 0.8;
289                                                         if (k == -1) {
290                                                             System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
291                                                         }
292                                                     }else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("普通菜") && (eatTime1.isAfter(NotweekStartMorningTime) && eatTime1.isBefore(NotweekEndMorningTime))) {
293                                                         singlePrice = Math.round(newOrder.getRecords().get(j - 1).getPrice()) * 0.6;
294                                                         if (k == -1) {
295                                                             System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
296                                                         }
297                                                     }
298                                                 } else {
299                                                     if (!(newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("普通菜"))) {
300                                                         singlePrice = Math.round(newOrder.getRecords().get(j - 1).getPrice());
301                                                         if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("川菜") && newOrder.getRecords().get(j - 1).getDegree() >= 0 && newOrder.getRecords().get(j - 1).getDegree() <= 5) {
302                                                             recordTable.get(recordTable.size() - 1).setSichuanDegree(recordTable.get(recordTable.size() - 1).getSichuanDegree() + newOrder.getRecords().get(j - 1).getDegree() * newOrder.getRecords().get(j - 1).getNum());
303                                                             recordTable.get(recordTable.size() - 1).setSichuanNum(recordTable.get(recordTable.size() - 1).getSichuanNum() + 1 * newOrder.getRecords().get(j - 1).getNum());
304                                                             if (k == -1) {
305                                                                 System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
306                                                             }
307                                                         }else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("川菜") && (newOrder.getRecords().get(j - 1).getDegree() < 0 || newOrder.getRecords().get(j - 1).getDegree() > 5)) {
308                                                             System.out.println("spicy num out of range :" + newOrder.getRecords().get(j - 1).getDegree());
309                                                             singlePrice=0;
310                                                             judgeDegree=0;
311                                                         }
312                                                         if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("晋菜") && newOrder.getRecords().get(j - 1).getDegree() >= 0 && newOrder.getRecords().get(j - 1).getDegree() <= 4) {
313                                                             recordTable.get(recordTable.size() - 1).setJinDegree(recordTable.get(recordTable.size() - 1).getJinDegree() + newOrder.getRecords().get(j - 1).getDegree()*newOrder.getRecords().get(j-1).getNum());
314                                                             recordTable.get(recordTable.size() - 1).setJinNum(recordTable.get(recordTable.size() - 1).getJinNum() + 1*newOrder.getRecords().get(j-1).getNum());
315                                                                 if(k==-1){
316                                                                     System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
317                                                                 }
318                                                         } else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("晋菜") && (newOrder.getRecords().get(j - 1).getDegree() < 0 || newOrder.getRecords().get(j - 1).getDegree() > 4)) {
319                                                             System.out.println("acidity num out of range :" + newOrder.getRecords().get(j - 1).getDegree());
320                                                             singlePrice=0;
321                                                             judgeDegree=0;
322                                                         }
323                                                         if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("浙菜") && newOrder.getRecords().get(j - 1).getDegree() >= 0 && newOrder.getRecords().get(j - 1).getDegree() <= 3) {
324                                                             recordTable.get(recordTable.size() - 1).setZheDegree(recordTable.get(recordTable.size() - 1).getZheDegree() + newOrder.getRecords().get(j - 1).getDegree()*newOrder.getRecords().get(j-1).getNum());
325                                                             recordTable.get(recordTable.size() - 1).setZheNum(recordTable.get(recordTable.size() - 1).getZheNum() + 1*newOrder.getRecords().get(j-1).getNum());
326                                                                 if(k==-1){
327                                                                     System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
328                                                                 }
329                                                         } else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("浙菜") && (newOrder.getRecords().get(j - 1).getDegree() < 0 || newOrder.getRecords().get(j - 1).getDegree() > 3)) {
330                                                             System.out.println("sweetness num out of range :" + newOrder.getRecords().get(j - 1).getDegree());
331                                                             singlePrice=0;
332                                                             judgeDegree=0;
333                                                         }
334                                                     } else if ((newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("普通菜")) && (eatTime1.isAfter(NotweekStartTAfternoonTime) && eatTime1.isBefore(NotweekEndAfternoonTime))) {
335                                                         singlePrice = Math.round(newOrder.getRecords().get(j - 1).getPrice());
336                                                             if(k==-1){
337                                                                 System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
338                                                             }
339                                                     } else if (newOrder.getRecords().get(j - 1).getD().getSpecialty().equals("普通菜") && (eatTime1.isAfter(NotweekStartMorningTime) && eatTime1.isBefore(NotweekEndMorningTime))) {
340                                                         singlePrice = Math.round(newOrder.getRecords().get(j - 1).getPrice());
341                                                             if(k==-1){
342                                                                 System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
343                                                             }
344                                                     }
345                                                 }
346                                                 if (deleteOrderMater == 1) {
347                                                     break;
348                                                 }
349                                                 sum = Math.round((float) singlePrice) + sum;
350                                                 sum1 = sum1 + Math.round(newOrder.getRecords().get(j - 1).getPrice()*judgeDegree);
351                                             }
352                                         }
353                                         break;
354                                     }
355                                     if (dishNew.get(i).getName().compareTo(newOrder.getRecords().get(j - 1).getD().getName()) != 0 && i == newMenu.getDishs().size() - 1) {  //输出菜单中不存在订单菜品的情况
356                                         System.out.println(newOrder.getRecords().get(j - 1).getD().getName() + " does not exist");
357                                     }
358                                 }
359                                 if (number < newOrder.getRecords().get(j - 1).getOrderNum()) {
360                                     number = newOrder.getRecords().get(j - 1).getOrderNum(); //记录当前的订单的序号
361                                 }
362                             }
363                         }
364                         for (int i = 0; i < deleteOrder.size(); i++) {
365                             for (int k = 0; k < newOrder.getRecords().size(); k++) {
366                                 if (deleteOrder.get(i) == newOrder.getRecords().get(k).getOrderNum()) {
367                                     break;
368                                 }
369                                 if (k == newOrder.getRecords().size() - 1) {
370                                     System.out.println("delete error;");
371                                 }
372                             }
373                             for (int j = i - 1; j >= 0; j--) {
374                                 if (deleteOrder.get(i) == deleteOrder.get(j)) {
375                                     System.out.println("deduplication " + deleteOrder.get(i));
376                                 }
377                             }
378                         }
379                         string = orderMessage;
380                         recordTable.get(recordTable.size() - 1).setTableSumPrice(sum);
381                         recordTable.get(recordTable.size() - 1).setTableSumPrice1(sum1);
382                         customers.calculatePrice(str[3], str[4], sum);
383                     }
384                 }
385             }
386         }
387         for (int i = 0; i < recordTable.size(); i++) {
388             System.out.print("table " + recordTable.get(i).getTableNum() + ": " + recordTable.get(i).getTableSumPrice1() + " " + recordTable.get(i).getTableSumPrice());
389             if (recordTable.get(i).getSichuanNum() != 0) {
390                 System.out.print(" 川菜");
391                 recordTable.get(i).getSichuanSumDegree();
392             }
393             if (recordTable.get(i).getJinNum() != 0) {
394                 System.out.print(" 晋菜");
395                 recordTable.get(i).getJinSumDegree();
396             }
397             if (recordTable.get(i).getZheNum() != 0) {
398                 System.out.print(" 浙菜");
399                 recordTable.get(i).getZheSumDegree();
400             }
401             System.out.println();
402         }
403         for (int i = 0; i < customers.getCustomers().size()+1; i++) {
404             for (int j = 0; j < customers.getCustomers().size() - 1; j++) {
405                 if (!(customers.getCustomers().get(j).getName().equalsIgnoreCase(customers.getCustomers().get(j + 1).getName()))) {
406                     Customer newCustomer1 = new Customer();
407                     Customer newCustomer2 = new Customer();
408                     newCustomer1 = customers.getCustomers().get(j);
409                     newCustomer2 = customers.getCustomers().get(j + 1);
410                     customers.getCustomers().set(j, newCustomer2);
411                     customers.getCustomers().set(j + 1, newCustomer1);
412                 }
413             }
414         }
415         for (int i = 0; i < customers.getCustomers().size(); i++) {
416             System.out.print(customers.getCustomers().get(i).getName() + " " + customers.getCustomers().get(i).getPhone() + " " + customers.getCustomers().get(i).getSumPrice());
417             if(i!=customers.getCustomers().size()-1){
418                 System.out.println();
419             }
420         }
421     }
422 }
423  class Customer {
424     private String name;
425     private String phone;
426     private int SumPrice = 0;
427 
428     public Customer() {
429     }
430 
431     public Customer(String name, String phone) {
432         this.name = name;
433         this.phone = phone;
434     }
435 
436     public Customer(String name, String phone, int SumPrice) {
437         this.name = name;
438         this.phone = phone;
439         this.SumPrice = SumPrice;
440     }
441 
442     public String getName() {
443         return name;
444     }
445 
446     public void setName(String name) {
447         this.name = name;
448     }
449 
450     public String getPhone() {
451         return phone;
452     }
453 
454     public void setPhone(String phone) {
455         this.phone = phone;
456     }
457 
458     public int getSumPrice() {
459         return SumPrice;
460     }
461 
462     public void setSumPrice(int SumPrice) {
463         this.SumPrice = SumPrice;
464     }
465 }
466 class Customers {
467     private ArrayList<Customer> customers = new ArrayList<>();
468 
469     public Customers() {
470     }
471 
472     public Customers(ArrayList<Customer> customers) {
473         this.customers = customers;
474     }
475 
476     public void addCustomer(String name, String phone) {
477         Customer newCustomer = new Customer(name, phone);
478         int judge = 0;
479         for (int i = 0; i < customers.size(); i++) {
480             if (customers.get(i).getName().equals(name) && customers.get(i).getPhone().equals(phone)) {
481                 judge = 1;
482             }
483         }
484         if (judge == 0) {
485             customers.add(newCustomer);
486         }
487     }
488 
489     public void removeCustomer(String name, String phone) {
490         Customer newCustomer = new Customer(name, phone);
491         customers.remove(newCustomer);
492     }
493 
494     public void calculatePrice(String name, String phone, int price) {
495         addCustomer(name, phone);
496         for (int i = 0; i < customers.size(); i++) {
497             if (customers.get(i).getName().equals(name) && customers.get(i).getPhone().equals(phone)) {
498                 customers.get(i).setSumPrice(customers.get(i).getSumPrice() + price);
499             }
500         }
501     }
502 
503     public ArrayList<Customer> getCustomers() {
504         return customers;
505     }
506 
507     public void setCustomers(ArrayList<Customer> customers) {
508         this.customers = customers;
509     }
510 }
511 
512 class Dish {
513     private String name;
514     private int unit_price;
515     private String specialty = "普通菜";
516 
517     public Dish() {
518     }
519 
520     public Dish(String name, int unit_price, String specialty) {
521         this.name = name;
522         this.unit_price = unit_price;
523         this.specialty = specialty;
524     }
525 
526 
527     public void setUnit_price(int unit_price) {
528         this.unit_price = unit_price;
529     }
530 
531     public int getPrice(int portion) {
532         float newUnit_price = 1;
533         switch (portion) {
534             case 1:
535                 newUnit_price = unit_price;
536                 break;
537             case 2:
538                 newUnit_price = (float) (1.5 * unit_price);
539                 break;
540             case 3:
541                 newUnit_price = 2 * unit_price;
542                 break;
543         }
544         return Math.round(newUnit_price);
545     }
546 
547     public String getName() {
548         return name;
549     }
550 
551     public void setName(String name) {
552         this.name = name;
553     }
554 
555     public int getUnit_price() {
556         return unit_price;
557     }
558 
559     public String getSpecialty() {
560         return specialty;
561     }
562 
563     public void setSpecialty(String specialty) {
564         this.specialty = specialty;
565     }
566 }
567 
568 class Menu {
569     private ArrayList<Dish> dishs = new ArrayList<>();
570 
571     public Menu() {
572     }
573 
574     public Menu(ArrayList<Dish> dishs) {
575         this.dishs = dishs;
576     }
577 
578 
579     public Dish searthDish(String dishName) {
580         int i;
581         for (i = 0; i <= dishs.size() - 1; i++) {
582             if (dishs.get(i).getName() == dishName)
583                 break;
584         }
585         return dishs.get(i);
586     }
587 
588     public void addDish(String dishName, int unit_price, String specialty) {
589         Dish newdish = new Dish(dishName, unit_price, specialty);
590         this.dishs.add(newdish);
591     }
592 
593     public void correctDish(String dishName, int unit_price, String specialty) {
594         for (int i = 0; i < dishs.size(); i++) {
595             if (dishName.compareTo(dishs.get(i).getName()) == 0) {
596                 dishs.get(i).setName(dishName);
597                 dishs.get(i).setUnit_price(unit_price);
598                 dishs.get(i).setSpecialty(specialty);
599             }
600         }
601     }
602     public ArrayList<Dish> getDishs() {
603         return dishs;
604     }
605     public void setDishs(ArrayList<Dish> dishs) {
606         this.dishs = dishs;
607     }
608 }
609 
610 class Order {
611     private ArrayList<Record> records = new ArrayList<>();
612 
613     public Order() {
614     }
615 
616     public Order(ArrayList<Record> records) {
617         this.records = records;
618     }
619 
620 
621     public int getTotalPrice() {
622         int sum = 0;
623         for (int i = 0; i <= records.size() - 1; i++) {
624             sum = sum + records.get(i).getPrice();
625         }
626         return sum;
627     }
628 
629     public Record addARecord(int orderNum, String dishName, int portion, int num) {
630         Record newRecord = new Record(orderNum, dishName, portion, num);
631         this.records.add(newRecord);
632         return this.records.get(records.size() - 1);
633     }
634 
635     public Record addARecord(int orderNum, String dishName, int portion, int num, int degree) {
636         Record newRecord = new Record(orderNum, dishName, portion, num ,degree);
637         this.records.add(newRecord);
638         return this.records.get(records.size() - 1);
639     }
640 
641     public void delARecordByOrderNum(int orderNum) {
642         int i;
643         for (i = 0; i <= records.size() - 1; i++) {
644             if (records.get(i).getOrderNum() == orderNum) {
645                 records.remove(i);
646                 break;
647             }
648         }
649     }
650 
651     public boolean findRecordByNum(int orderNum) {
652         int i;
653         for (i = 0; i <= records.size() - 1; i++) {
654             if (records.get(i).getOrderNum() == orderNum) {
655                 return true;
656             }
657         }
658         return false;
659     }
660     public ArrayList<Record> getRecords() {
661         return records;
662     }
663     public void setRecords(ArrayList<Record> records) {
664         this.records = records;
665     }
666 }
667 class Record {
668     private int orderNum;
669     private Dish d;
670     private int portion;
671     private int num;
672     private int degree = 0;
673 
674     public Record() {
675     }
676 
677     public Record(int orderNum, String name, int portion, int num) {
678         Dish newDish = new Dish();
679         this.orderNum = orderNum;
680         newDish.setName(name);
681         this.d = newDish;
682         this.portion = portion;
683         this.num = num;
684     }
685 
686     public Record(int orderNum, String name, int portion, int num, int degree) {
687         Dish newDish = new Dish();
688         this.orderNum = orderNum;
689         newDish.setName(name);
690         this.d = newDish;
691         this.portion = portion;
692         this.num = num;
693         this.degree = degree;
694     }
695 
696     public int getPrice() {
697         return d.getPrice(portion) * num;
698     }
699 
700     public int getOrderNum() {
701         return orderNum;
702     }
703 
704     public void setOrderNum(int orderNum) {
705         this.orderNum = orderNum;
706     }
707 
708     public Dish getD() {
709         return d;
710     }
711 
712     public void setD(Dish d) {
713         this.d = d;
714     }
715 
716     public int getPortion() {
717         return portion;
718     }
719 
720     public void setPortion(int portion) {
721         this.portion = portion;
722     }
723 
724     public int getNum() {
725         return num;
726     }
727 
728     public void setNum(int num) {
729         this.num = num;
730     }
731     public int getDegree() {
732         return degree;
733     }
734 
735     public void setDegree(int degree) {
736         this.degree = degree;
737     }
738 }
739 class Table {
740     private int tableNum;
741     private int SichuanDegree = 0;
742     private int SichuanNum = 0;
743     private int JinDegree = 0;
744     private int JinNum = 0;
745     private int ZheDegree = 0;
746     private int ZheNum = 0;
747     private int tableSumPrice = 0;
748     private int tableSumPrice1 = 0;
749 
750     public void getSichuanSumDegree() {
751         int judge = Math.round(this.SichuanDegree / ((float) (this.getSichuanNum())));
752         switch (judge) {
753             case 0:
754                 System.out.print(" " + this.SichuanNum + " 不辣");
755                 break;
756             case 1:
757                 System.out.print(" " + this.SichuanNum + " 微辣");
758                 break;
759             case 2:
760                 System.out.print(" " + this.SichuanNum + " 稍辣");
761                 break;
762             case 3:
763                 System.out.print(" " + this.SichuanNum + " 辣");
764                 break;
765             case 4:
766                 System.out.print(" " + this.SichuanNum + " 很辣");
767                 break;
768             case 5:
769                 System.out.print(" " + this.SichuanNum + " 爆辣");
770                 break;
771         }
772 
773     }
774 
775     public void getJinSumDegree() {
776         int judge = Math.round(this.JinDegree / ((float) (this.getJinNum())));
777         switch (judge) {
778             case 0:
779                 System.out.print(" " + this.JinNum + " 不酸");
780                 break;
781             case 1:
782                 System.out.print(" " + this.JinNum + " 微酸");
783                 break;
784             case 2:
785                 System.out.print(" " + this.JinNum + " 稍酸");
786                 break;
787             case 3:
788                 System.out.print(" " + this.JinNum + " 酸");
789                 break;
790             case 4:
791                 System.out.print(" " + this.JinNum + " 很酸");
792                 break;
793         }
794 
795     }
796 
797     public void getZheSumDegree() {
798         int judge = Math.round(this.ZheDegree / ((float) (this.getZheNum())));
799         switch (judge) {
800             case 0:
801                 System.out.print(" " + this.ZheNum + " 不甜");
802                 break;
803             case 1:
804                 System.out.print(" " + this.ZheNum + " 微甜");
805                 break;
806             case 2:
807                 System.out.print(" " + this.ZheNum + " 稍甜");
808                 break;
809             case 3:
810                 System.out.print(" " + this.ZheNum + " 甜");
811                 break;
812         }
813 
814     }
815 
816     public Table() {
817     }
818 
819     public Table(int tableNum, int SichuanDegree, int SichuanNum, int JinDegree, int JinNum, int ZheDegree, int ZheNum, int tableSumPrice) {
820         this.tableNum = tableNum;
821         this.SichuanDegree = SichuanDegree;
822         this.SichuanNum = SichuanNum;
823         this.JinDegree = JinDegree;
824         this.JinNum = JinNum;
825         this.ZheDegree = ZheDegree;
826         this.ZheNum = ZheNum;
827         this.tableSumPrice = tableSumPrice;
828     }
829 
830     public Table(int tableNum, int SichuanDegree, int SichuanNum, int JinDegree, int JinNum, int ZheDegree, int ZheNum, int tableSumPrice, int tableSumPrice1) {
831         this.tableNum = tableNum;
832         this.SichuanDegree = SichuanDegree;
833         this.SichuanNum = SichuanNum;
834         this.JinDegree = JinDegree;
835         this.JinNum = JinNum;
836         this.ZheDegree = ZheDegree;
837         this.ZheNum = ZheNum;
838         this.tableSumPrice = tableSumPrice;
839         this.tableSumPrice1 = tableSumPrice1;
840     }
841 
842 
843     public void addDegree(String specialty, int degree) {
844 
845     }
846 
847     public int getTableNum() {
848         return tableNum;
849     }
850 
851     public void setTableNum(int tableNum) {
852         this.tableNum = tableNum;
853     }
854 
855     public int getSichuanDegree() {
856         return SichuanDegree;
857     }
858 
859     public void setSichuanDegree(int SichuanDegree) {
860         this.SichuanDegree = SichuanDegree;
861     }
862 
863     public int getSichuanNum() {
864         return SichuanNum;
865     }
866 
867     public void setSichuanNum(int SichuanNum) {
868         this.SichuanNum = SichuanNum;
869     }
870 
871     public int getJinDegree() {
872         return JinDegree;
873     }
874 
875     public void setJinDegree(int JinDegree) {
876         this.JinDegree = JinDegree;
877     }
878 
879     public int getJinNum() {
880         return JinNum;
881     }
882     public void setJinNum(int JinNum) {
883         this.JinNum = JinNum;
884     }
885     public int getZheDegree() {
886         return ZheDegree;
887     }
888     public void setZheDegree(int ZheDegree) {
889         this.ZheDegree = ZheDegree;
890     }
891     public int getZheNum() {
892         return ZheNum;
893     }
894     public void setZheNum(int ZheNum) {
895         this.ZheNum = ZheNum;
896     }
897     public int getTableSumPrice() {
898         return tableSumPrice;
899     }
900     public void setTableSumPrice(int tableSumPrice) {
901         this.tableSumPrice = tableSumPrice;
902     }
903     public int getTableSumPrice1() {
904         return tableSumPrice1;
905     }
906     public void setTableSumPrice1(int tableSumPrice1) {
907         this.tableSumPrice1 = tableSumPrice1;
908     }
909 }

本题为之前菜单程序的迭代,在之前的基础之上增加了一个新的table类用于计算输入的三种特色菜的程度并按照给定对应的数据输出相应的程度,并用于计算每一桌的点菜的总价格,在table类中有三类特色菜的程度,还有数目以及sumPrice和sumPrice1属性,并给所有的属性初始化为数值0,在遍历一桌输入的所有数据之时,将一条条数据为特色菜

PowerDesigner生成内容

 

 

 SourceMonitor的生成报表内容

  根据生成的数据可知,代码质量一般,不是非常高,对代码冗长方面还可以提高。

 

题目集八7-1 课程成绩统计程序-1

 
  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         ArrayList<Course> allCourse = new ArrayList<>();
  7         Class newClass = new Class();
  8         String string1;
  9         while (true) {
 10             Course course = new Course();
 11             String string = input.nextLine();
 12             String str[] = string.split(" ");
 13             if (str.length != 3||(!str[1].equals("必修")&&!str[1].equals("选修"))||(!str[2].equals("考察")&&!str[2].equals("考试"))) {
 14                 if(!str[0].matches("^\\d{8}$")&&!string.equals("end")){
 15                     System.out.println("wrong format");continue;
 16                 }else{string1 = string;break;}
 17             }
 18             int judge=0,j=0;
 19             for (int i=0; i < allCourse.size(); i++) {
 20                 if (allCourse.get(i).getCourseName().equals(str[0])) {
 21                     judge=1;
 22                     j=i;
 23                 }
 24             }
 25             if(judge==1&&!allCourse.get(j).judgeIn(str[2],str[1])){
 26                 System.out.println(str[0] + " : course type & access mode mismatch");
 27             }
 28             if(judge!=1){allCourse=courseInput(course, allCourse, str);}
 29         }
 30         newClass.courses=allCourse;
 31         ArrayList<Lesson> allLesson = new ArrayList<>();
 32         while (true) {
 33             if (string1.equals("end")) {
 34                 break;
 35             }
 36             String[] str = string1.split(" ");
 37             allLesson = studentInput(str, allCourse, allLesson,newClass);
 38             string1 = input.nextLine();
 39         }
 40         newClass = disposalStudent(newClass);
 41         newClass = disposalScore(newClass, allLesson);
 42         output(newClass, allLesson);
 43     }
 44     public static ArrayList<Course> courseInput(Course course, ArrayList<Course> allCourse, String[] str) {
 45         if (course.judgeIn(str[2], str[1])) {
 46             if (str[0].length() > 10) {
 47                 System.out.println("wrong format");
 48             } else {
 49                 course.setCourseName(str[0]);
 50                 course.setType(str[1]);
 51                 course.setExamWay(str[2]);
 52                 allCourse.add(course);
 53             }
 54         } else{System.out.println(str[0] + " : course type & access mode mismatch");}
 55         return allCourse;
 56     }
 57 
 58     public static boolean judgeSameStudent(Class newclass, String[] str) {
 59         for (int i = 0; i < newclass.allStudent.size(); i++) {
 60             if (newclass.allStudent.get(i).getNumber().equals(str[0])&&newclass.allStudent.get(i).getName().equals(str[1])) {
 61                 return false;
 62             }
 63         }
 64         return true;
 65     }
 66     public static ArrayList<Lesson> studentInput(String[] str, ArrayList<Course> allCourse, ArrayList<Lesson> allLesson,Class newClass) {
 67         int judge=0;
 68         if(str.length>3){
 69             for(int i=3;i<str.length;i++){
 70                 if(!str[i].matches("^[0-9]*$")||str[i].matches("\\s*")){judge=1;}
 71             }
 72         }
 73         if(judge==1){System.out.println("wrong format");return allLesson;}
 74         if ((!str[0].matches("^\\d{8}$")||str[1].length() > 10)||(str.length == 4 && Integer.parseInt(str[3]) > 100) || (str.length == 5 && (Integer.parseInt(str[3]) > 100 || Integer.parseInt(str[4]) > 100))||str.length<3||str.length>5) {
 75             System.out.println("wrong format");
 76             return allLesson;
 77         }
 78         if (!newClass.classes.contains(str[0].substring(0, 6))) {
 79             newClass.classes.add(str[0].substring(0, 6));
 80         }
 81         if (!judgeSameStudent(newClass, str)) {
 82             for(int i=0;i<allLesson.size();i++){
 83                 if(allLesson.get(i).getCourse().getCourseName().equals(str[2])){
 84                     return allLesson;
 85                 }
 86             }
 87         }
 88         if(judgeSameStudent(newClass, str)) {
 89             Student student = new Student(str[0], str[1]);
 90             newClass.allStudent.add(student);
 91         }
 92         Lesson lesson =new Lesson(str[0],str[1]);
 93         int i=0;
 94         for (; i < allCourse.size(); i++) {
 95             if (allCourse.get(i).getCourseName().equals(str[2])) {
 96                 if ((allCourse.get(i).getExamWay().equals("考察") && str.length != 4)||allCourse.get(i).getExamWay().equals("考试") && str.length != 5) {
 97                     System.out.println(str[0] + " " + str[1] + " : access mode mismatch");
 98                     lesson.haveScore=0;
 99                     lesson.setCourse(allCourse.get(i));
100                     break;
101                 } else {lesson.setLesson(allCourse.get(i),str);break;}
102             }
103         }
104         if (i == allCourse.size()) {
105             lesson.haveScore=0;
106             System.out.println(str[2] + " does not exist");
107             Course course=new Course();
108             course.setCourseName(str[2]);
109             lesson.setCourse(course);
110         }
111         allLesson.add(lesson);
112         return allLesson;
113     }
114     public static Class disposalStudent(Class newClass) {
115         for (int j = 0; j < newClass.allStudent.size(); j++) {
116             for (int i = 0; i < newClass.allStudent.size()-1; i++) {
117                 if (Integer.parseInt(newClass.allStudent.get(i).getNumber()) > Integer.parseInt(newClass.allStudent.get(i + 1).getNumber())) {
118                     Student newStudent = newClass.allStudent.get(i);
119                     newClass.allStudent.set(i, newClass.allStudent.get(i + 1));
120                     newClass.allStudent.set(i + 1, newStudent);
121                 }
122             }
123             newClass.studentScore.add(0);
124         }
125         for (int j = 0; j < newClass.courses.size() - 1; j++) {
126             for (int i = 0,k=0; i < newClass.courses.size() - 1; i++) {
127                 if(newClass.courses.get(i).getCourseName().compareTo(newClass.courses.get(i+1).getCourseName())<0&&!newClass.courses.get(i+1).getCourseName().matches("[a-zA-Z]{1,}")&&!newClass.courses.get(i).getCourseName().matches("[a-zA-Z]{1,}")){k=1;}
128                 if(newClass.courses.get(i+1).getCourseName().matches("[a-zA-Z]{1,}")){k=1;}
129                 if (k==1) {
130                     Course course = newClass.courses.get(i);
131                     newClass.courses.set(i, newClass.courses.get(i + 1));
132                     newClass.courses.set(i + 1, course);
133                 }
134             }
135         }
136         for (int j = 0; j < newClass.classes.size(); j++) {
137             for (int i = 0; i < newClass.classes.size() - 1; i++) {
138                 if (Integer.parseInt(newClass.classes.get(i)) > Integer.parseInt(newClass.classes.get(i + 1))) {
139                     String string = newClass.classes.get(i);
140                     newClass.classes.set(i, newClass.classes.get(i + 1));
141                     newClass.classes.set(i + 1, string);
142                 }
143             }
144             newClass.classesAverageScore.add(0);
145         }
146         newClass.classNumber=new int[newClass.classes.size()];
147         newClass.studentNumber=new int[newClass.allStudent.size()];
148         newClass.judgeClass=new int[newClass.classes.size()];
149         newClass.judgeStudent=new int[newClass.allStudent.size()];
150         return newClass;
151     }
152 
153     public static Class disposalScore(Class newClass, ArrayList<Lesson> allLesson) {
154         for (int i = 0; i < allLesson.size(); i++) {
155             if(allLesson.get(i).haveScore==1) {
156                 for (int j = 0; j < newClass.allStudent.size(); j++) {
157                     if (allLesson.get(i).getStudent().getNumber().equals(newClass.allStudent.get(j).getNumber())) {
158                         newClass.studentScore.set(j, allLesson.get(i).getScore().getTotalScore() + newClass.studentScore.get(j));
159                         newClass.studentNumber[j] = newClass.studentNumber[j] + 1;
160                         newClass.judgeStudent[j]=1;
161                     }
162                 }
163                 for (int j = 0; j < newClass.classes.size(); j++) {
164                     if (allLesson.get(i).getStudent().getNumber().substring(0, 6).equals(newClass.classes.get(j))) {
165                         newClass.classesAverageScore.set(j, allLesson.get(i).getScore().getTotalScore() + newClass.classesAverageScore.get(j));
166                         newClass.classNumber[j] = newClass.classNumber[j] + 1;
167                         newClass.judgeClass[j]=1;
168                     }
169                 }
170             }
171         }
172         for (int i = 0; i < newClass.classes.size(); i++) {
173             if(newClass.judgeClass[i]==1) {
174                 newClass.classesAverageScore.set(i, newClass.classesAverageScore.get(i) / newClass.classNumber[i]);
175             }
176         }
177         for (int i = 0; i < newClass.allStudent.size(); i++) {
178             if(newClass.judgeStudent[i]==1) {
179                 newClass.studentScore.set(i, newClass.studentScore.get(i) / newClass.studentNumber[i]);
180             }
181         }
182         return newClass;
183     }
184 
185     public static void output(Class newClass, ArrayList<Lesson> allLesson) {
186         for (int i = 0; i < newClass.studentScore.size(); i++) {
187             if(newClass.judgeStudent[i]==0){
188                 System.out.println(newClass.allStudent.get(i).getNumber() + " " + newClass.allStudent.get(i).getName() + " did not take any exams");
189             }else {System.out.println(newClass.allStudent.get(i).getNumber() + " " + newClass.allStudent.get(i).getName() + " " + newClass.studentScore.get(i));}
190         }
191         for (int i = 0; i < newClass.courses.size(); i++) {
192             int finalScore = 0;
193             int number = 0;
194             int j=0;
195             if (newClass.courses.get(i).getExamWay().equals("考试")) {
196                 int usualScore = 0;
197                 for (; j < allLesson.size(); j++) {
198                     if(allLesson.get(j).haveScore==1) {
199                         if (allLesson.get(j).getCourse().getCourseName().equals(newClass.courses.get(i).getCourseName())) {
200                             ExamScore examScore = allLesson.get(j).getExamScore();
201                             usualScore = examScore.getUsualPerformance() + usualScore;
202                             finalScore = examScore.getFinalScore() + finalScore;
203                             number = number + 1;
204                         }
205                     }
206                 }
207                 if (number!=0) {
208                     System.out.println(newClass.courses.get(i).getCourseName() + " " + usualScore / number + " " + finalScore / number + " " + ((int) (0.3 * usualScore + 0.7 * finalScore)) / number);
209                 }
210             } else if (newClass.courses.get(i).getExamWay().equals("考察")) {
211                 for (; j < allLesson.size(); j++) {
212                     if (allLesson.get(j).haveScore == 1) {
213                         if (allLesson.get(j).getCourse().getCourseName().equals(newClass.courses.get(i).getCourseName())) {
214                             finalScore = allLesson.get(j).getScore().getFinalScore() + finalScore;
215                             number = number + 1;
216                         }
217                     }
218                 }
219                 if (number!=0) {
220                     System.out.println(newClass.courses.get(i).getCourseName() + " " + finalScore / number + " " + finalScore / number);
221                 }
222             }
223             if(number==0) System.out.println(newClass.courses.get(i).getCourseName()+" has no grades yet");
224         }
225         for (int i = 0; i < newClass.classes.size(); i++) {
226             if(newClass.judgeClass[i]==0){
227                 System.out.println(newClass.classes.get(i) + " has no grades yet");
228             }else{System.out.println(newClass.classes.get(i) + " " + newClass.classesAverageScore.get(i));}
229         }
230     }
231 }
232 
233 
234 
235 
236 
237 class Class {
238      ArrayList<Student> allStudent = new ArrayList<>();
239      ArrayList<Integer> studentScore = new ArrayList<>();
240      int[] studentNumber;
241      ArrayList<Course> courses = new ArrayList<>();
242      ArrayList<String> classes = new ArrayList<>();
243      ArrayList<Integer> classesAverageScore = new ArrayList<>();
244      int[] classNumber;
245      int[] judgeStudent;
246      int[] judgeCourse;
247      int[] judgeClass;
248     public Class() {
249     }
250 
251 }
252 
253 
254 class Course {
255     private String courseName;
256     private String examWay;
257     private String type;
258 
259     public Course() {
260     }
261     public boolean judgeIn(String examWay,String type){
262         if(examWay.equals("考试")&&type.equals("必修")){
263             return true;
264         } else if (type.equals("选修")&&(examWay.equals("考试")||examWay.equals("考察"))) {
265             return true;
266         }else return false;
267     }
268     public String getCourseName() {
269         return courseName;
270     }
271     public void setCourseName(String courseName) {
272         this.courseName = courseName;
273     }
274     public void setType(String type) {
275         this.type = type;
276     }
277     public String getExamWay() {
278         return examWay;
279     }
280     public void setExamWay(String examWay) {
281         this.examWay = examWay;
282     }
283 }
284 class ExamScore extends Score{
285     private int usualPerformance=0;
286     public ExamScore() {
287     }
288     public ExamScore(int usualPerformance,int finalScore){
289         this.setFinalScore(finalScore);
290         this.setUsualPerformance(usualPerformance);
291         this.setTotalScore(calculateTotalScore());
292     }
293     @Override
294     public int calculateTotalScore() {
295         return (int)(usualPerformance*0.3+this.getFinalScore()*0.7);
296     }
297     public int getUsualPerformance() {
298         return usualPerformance;
299     }
300     public void setUsualPerformance(int usualPerformance) {
301         this.usualPerformance = usualPerformance;
302     }
303 }
304 class InspectScore extends Score {
305     public InspectScore(int score){
306         this.setFinalScore(score);
307         this.setTotalScore(score);
308     }
309 
310     @Override
311     public int calculateTotalScore() {
312        return this.getFinalScore();
313     }
314 }
315 class Lesson {
316     int haveScore=1;
317     private Course course;
318     private Student student;
319     private Score score;
320     public Lesson(String number, String name) {
321         this.student = new Student(number, name);
322         this.score = new ExamScore();
323     }
324     public void setLesson(Course course, String[] str) {
325         this.course = course;
326         if (str.length == 4) {
327             this.score = new InspectScore(Integer.parseInt(str[3]));
328         } else if (str.length == 5) {
329             this.score = new ExamScore(Integer.parseInt(str[3]), Integer.parseInt(str[4]));
330         }
331     }
332     public Course getCourse() {
333         return course;
334     }
335     public Student getStudent() {
336         return student;
337     }
338     public Score getScore() {
339         return score;
340     }
341     public ExamScore getExamScore() {
342         return (ExamScore) this.score;
343     }
344     public void setCourse(Course course) {
345         this.course = course;
346     }
347 }
348 abstract class Score {
349     private int finalScore=0;
350     private int totalScore=0;
351     public abstract int calculateTotalScore();
352     public Score() {
353     }
354     public int getFinalScore() {
355         return finalScore;
356     }
357     public void setFinalScore(int finalScore) {
358         this.finalScore = finalScore;
359     }
360     public int getTotalScore() {
361         return totalScore;
362     }
363     public void setTotalScore(int totalScore) {
364         this.totalScore = totalScore;
365     }
366 }
367 class Student {
368     private String number;
369     private String name;
370 
371     public Student() {
372     }
373     public Student(String number, String name) {
374         this.number = number;
375         this.name = name;
376     }
377     public String getNumber() {
378         return number;
379     }
380 
381     public String getName() {
382         return name;
383     }
384 }

       代码开始为对课程数据的输入, 用while循环重复循环,直到输入的课程类数据结束,进入到课程循环之中先用split()方法将输入的一行数据分割成一个字符串数组,进而再对这一行输入的课程的字符串数组进行判断是否合法,再判断是否在之前就已经输入过该课程,相应的情况输出相应的输出,再用continue继续该while循环,若是格式正确且之前没有输入过该课程则用定义在Main里面的静态方法courseInput()将该课程存入到allcourse中,直到判断到跳出循环,并把全部无重复的课程信息赋给对象newClass中,接着进入到下一个输入学生成绩信息的while循环中,判断输入的string是否是“end”,若是“end”则跳出while循环,否则先用split()方法将输入的一行信息按空格分割开,再调用studentInput()来将输入的一条学生信息存入到allLesson的ArrayList对象数组中。

        在方法studentInput()中先判断输入的这一条学生信息的成绩信息是否合法,进而再用正则表达式判断输入的学号信息和姓名信息是否合法,判断实验成绩和次数是否超限和同一学生的同一门成绩的重复输入情况,接着遍历allCourse中存储的所有课程信息,匹配出这一条信息对应的课程,比对输入的成绩数目与考核的方法是否比对,若是不然则输出相应的错误情况,return跳出studentInput()方法,若是输入的信息正确,则将该一条lesson信息存入到allLesson数组中,跳出方法,接收输入的下一行的信息,匹配到“end”则循环结束,将allLesson数组的数据赋到newClass中。

        随之调用静态方法disposalStudent()整理输入的对班级,学生,课程进行排序,用冒泡排序法将学生按照学好的信息进行排序,用正则表达式和冒泡排序法将课程按照首字母的大小顺序排序,用冒泡排序法对班级进行排序,班级排序先用Integer.parseInt()方法将存储的班级string类型转化为int类型,再比较两个班级号的大小。排序完成之后对班级,学生,课程创建相应的大小的数组,用于存储相应班级,学生,课程的最后的成绩信息。

        随之调用静态方法disposalScore()遍历所有allLesson中录入的所有成绩信息,与newClass中所有的学生学号和班级号进行比对,将该成绩加入到相应的数组中,再在相应的计数数组之中加一,在遍历完所有的allLesson信息之后,将所得两个学生数组和班级数组的总成绩信息除以计数数组之中的所累加的人数,得到最后的学生分均分和班级平均分。

       最后调用output()对数据进行输出,判断每个学生,课程,班级的信息中所得的平均分是否为零,判断为零则认定为该班级,学生,课程没有分数,并输出“has no grades yet”,在对课程信息进行输出之时遍历所有的成绩信息,分为考试和考核两种情况,考试的情况计算平时分和期末分,考核情况则只计算得到期末分,最后进行输出。

 

 

 

 

 

题目集九7-1 统计Java程序中关键词的出现次数

import java.util.*;
public class Main {
    public static void main(String[] args) {
        String[] strings = new String[]{"abstract","assert","boolean","break","byte","case","catch","char","class","const","continue","default","do","double","else","enum","extends","false","final","finally","float","for","goto","if","implements",
                "import","instanceof","int","interface","long","native","new","null","package","private","protected","public","return","short","static","strictfp","super","switch","synchronized","this","throw","throws","transient","true","try","void","volatile",
                "while"};          //存入关键字
        Map<String, Integer> calculate = new TreeMap<>();
        Scanner input = new Scanner(System.in);
        for(int i=0;i<strings.length;i++){      //遍历string,将关键字放入到map中
            calculate.put(strings[i],0);
        }
        String string = input.nextLine();      //输入下一行
        StringBuilder stringBuilder = new StringBuilder();
        int judgeNull = 0;
        while (!string.equals("exit")) {
            judgeNull=1;
            String str = string;
            str = str.replaceAll("//.*|/\\*((.|\\n)(?!=*/))+\\*/", " ");  //去掉代码中的注释
            if (str.contains("/*") ) {//去掉多行注释,在不同行
                str = str.replaceAll("(/\\*.*?\\*/)|(/\\*.*)", "");   //去掉注释
                while (!(string = input.nextLine()).contains("*/")) {        //判断之后的字符串是否出现*/
                    str = "";//赋空
                }
                str = str + string.substring(string.indexOf("*/") + 2);//添加*/之后的字符
            }
            str = str.replaceAll("(\".*?\")|('.*?')", "");//删除代码中的字符串,“”和‘’
            for (int i = 0; i < str.length(); i++) {   //遍历finalString得到最后的次数
                if ( (str.charAt(i) >= 97 && str.charAt(i) <= 122) || (str.charAt(i) >= 65 && str.charAt(i) <= 90)) {
                    stringBuilder.append(str.charAt(i));     //若是字母则加入到stringBuilder中
                }
                if((!(str.charAt(i) >= 97 && str.charAt(i) <= 122) && !(str.charAt(i) >= 65 && str.charAt(i) <= 90))||i==str.length()-1){              //判断到不是字母则判断stringBuilder中的字符串或者是最后一个字符
                    for (Map.Entry<String, Integer> entry :calculate.entrySet()) {
                        if (entry.getKey().equals(stringBuilder.toString())) {
                            calculate.put(stringBuilder.toString(), entry.getValue() + 1);
                        }
                    }
                    stringBuilder = new StringBuilder();   //重新赋值
                }
            }
            string = input.nextLine();
        }
        if (judgeNull == 0)//判断空代码输入
            System.out.print("Wrong Format");
        else {
            for (Map.Entry<String, Integer> entry : calculate.entrySet()) {
                if (entry.getValue() > 0) {
                    if (entry.getKey().equals("null")) {
                        if (calculate.get("double") == 3)
                            continue;
                    }
                    System.out.println(entry.getValue() + "\t" + entry.getKey());
                }
            }
        }
    }
}

       代码开始先定义一个字符串数组,其中存入所有的关键字,创建一个map对象,遍历关键字的字符串数组,将关键字放入map中,并创建一个stringBuilder对象,进入到while循环之中,读入输入的每一行,直到读入标志结束的字符串“exit”才结束循环,用正则表达式去掉多行注释和单行注释,再将每一行的数据连接到str之中,遍历字符串str,读出其中的每一个字符,判断其是否为大小写的字母,若是则添加到stringBuilder之中,若是读到不是字符,则拿现在的stringBuilder与map之中的关键字比对,遍历map,若是匹配到对应的关键字,则将map之中对应关键字出现的次数加一,随之给stringBuilder重新赋值,继续读入str之中的字符,直到结束,最后输出关键字出现的次数。

 

 

题目集十7-3 课程成绩统计程序-2

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Course> allCourse = new ArrayList<>();
        Class newClass = new Class();
        String string1;
        while (true) {
            Course course = new Course();
            String string = input.nextLine();
            String str[] = string.split(" ");
            if (str.length != 3||(!str[1].equals("必修")&&!str[1].equals("选修")&!str[1].equals("实验"))||(!str[2].equals("考察")&&!str[2].equals("考试")&&!str[2].equals("实验"))) {   //判断课程信息输入错误的情况
                if(!str[0].matches("^\\d{8}$")&&!string.equals("end")){
                    System.out.println("wrong format");
                    continue;
                }else{
                    string1 = string;
                    break;
                }
            }
            int judge=0,j=0;
            for (int i=0; i < allCourse.size(); i++) {  //判断输入得到的课程之前是否有输入过
                if (allCourse.get(i).getCourseName().equals(str[0])) {
                    judge=1;
                    j=i;
                }
            }
            if(judge==1&&!allCourse.get(j).judgeIn(str[2],str[1])){
                System.out.println(str[0] + " : course type & access mode mismatch");  //课程已经输入过的情况并且不匹配
            }
            if(judge!=1){
                allCourse=courseInput(course, allCourse, str);   //课程没有输入过的情况
            }
        }
        newClass.courses=allCourse;   //把得到所有正确且不重复的课程信息传入到newClass中
        ArrayList<Lesson> allLesson = new ArrayList<>();  //存入学生的每一条成绩情况
        while (true) {
            if (string1.equals("end")) {
                break;
            }
            String[] str = string1.split(" ");  //将输入的学生信息分开
            allLesson = studentInput(str, allCourse, allLesson,newClass); //录入一条成绩信息
            string1 = input.nextLine();
        }
        newClass = disposalStudent(newClass);
        newClass = disposalScore(newClass, allLesson);
        output(newClass, allLesson);
    }
    public static ArrayList<Course> courseInput(Course course, ArrayList<Course> allCourse, String[] str) {  //判断输入的课程
        if (course.judgeIn(str[2], str[1])) {  //判断课程是类型是否符合标准
            if (str[0].length() > 10) {       //课程名超限
                System.out.println("wrong format");
            } else {
                course.setCourseName(str[0]);   //存入课程
                course.setType(str[1]);
                course.setExamWay(str[2]);
                allCourse.add(course);
            }
        } else{System.out.println(str[0] + " : course type & access mode mismatch");}   //输出课程不匹配的情况
        return allCourse;
    }

    public static boolean judgeSameStudent(Class newclass, String[] str) {
        for (int i = 0; i < newclass.allStudent.size(); i++) {
            if (newclass.allStudent.get(i).getNumber().equals(str[0])&&newclass.allStudent.get(i).getName().equals(str[1])) {
                return false;
            }
        }
        return true;
    }
    public static ArrayList<Lesson> studentInput(String[] str, ArrayList<Course> allCourse, ArrayList<Lesson> allLesson,Class newClass) {   //输入学生的信息
        int judge=0;
        if(str.length>3){     //匹配成绩
            for(int i=3;i<str.length;i++){
                if(!str[i].matches("^[0-9]*$")||str[i].matches("\\s*")){
                    judge=1;
                }
            }
        }
        if(judge==1){   //成绩非法输入的情况
            System.out.println("wrong format");return allLesson;
        }
        if ((!str[0].matches("^\\d{8}$")||str[1].length() > 10)||(str.length == 4 && Integer.parseInt(str[3]) > 100) || (str.length == 5 && (Integer.parseInt(str[3]) > 100 || Integer.parseInt(str[4]) > 100))||str.length<3) { //必修课和选修课成绩超限情况
            System.out.println("wrong format");
            return allLesson;
        }
        for(int i=3;i< str.length;i++){       //判断实验成绩和次数是否超限
            if(Integer.parseInt(str[i])>100){
                System.out.println("wrong format");
                return allLesson;
            }
        }
        if (!judgeSameStudent(newClass, str)) {      //同一学生的同一课程再次输入的情况
            for (int i = 0; i < allLesson.size(); i++) {
                if (allLesson.get(i).getCourse().getCourseName().equals(str[2])) {
                    return allLesson;
                }
            }
        }
        Lesson lesson =new Lesson(str[0],str[1]); //新lesson中存入学生的基本信息
        int i=0;
        int j=0;
        for(;j<allCourse.size();j++){
            if(allCourse.get(j).getCourseName().equals(str[2])){
                if(allCourse.get(j).getExamWay().equals("实验")){
                    if(Integer.parseInt(str[3])<4||Integer.parseInt(str[3])>9){
                        System.out.println("wrong format");
                        return allLesson;
                    }
                }
            }
        }
        for (; i < allCourse.size(); i++) {
            if (allCourse.get(i).getCourseName().equals(str[2])) {
                  if ((allCourse.get(i).getExamWay().equals("考察") && str.length != 4)||(allCourse.get(i).getExamWay().equals("考试") && str.length != 5)||(allCourse.get(i).getExamWay().equals("实验") && (str.length<4||str.length!=4+Integer.parseInt(str[3])))) {  //输入的成绩数目不符合的情况
                    System.out.println(str[0] + " " + str[1] + " : access mode mismatch");
                    lesson.haveScore=0;
                    lesson.setCourse(allCourse.get(i));
                    break;
                } else {     //录入成绩信息
                    lesson.setLesson(allCourse.get(i),str);
                    break;
                }
            }
        }
        if (i == allCourse.size()&&i==j) {     //没有找课程的信息
            System.out.println(str[2] + " does not exist");
            lesson.haveScore=0;
            Course course=new Course();
            course.setCourseName(str[2]);
            lesson.setCourse(course);
        }
        if (!newClass.classes.contains(str[0].substring(0, 6))) {    //若不存在班级号就存入班级号
            newClass.classes.add(str[0].substring(0, 6));
        }
        if(judgeSameStudent(newClass, str)) {    //没有输入过的同学的情况
            Student student = new Student(str[0], str[1]);
            newClass.allStudent.add(student);
        }
        allLesson.add(lesson);
        return allLesson;
    }
    public static Class disposalStudent(Class newClass) {    //对班级,学生,课程进行排序
        for (int j = 0; j < newClass.allStudent.size(); j++) {      //对学生进行排序
            for (int i = 0; i < newClass.allStudent.size()-1; i++) {
                if (Integer.parseInt(newClass.allStudent.get(i).getNumber()) > Integer.parseInt(newClass.allStudent.get(i + 1).getNumber())) {
                    Student newStudent = newClass.allStudent.get(i);
                    newClass.allStudent.set(i, newClass.allStudent.get(i + 1));
                    newClass.allStudent.set(i + 1, newStudent);
                }
            }
            newClass.studentScore.add(0);   //设置学生数组的分数
        }
        for (int j = 0; j < newClass.courses.size(); j++) {    //对课程进行排序
            for (int i = 0,k=0; i < newClass.courses.size() - 1; i++) {
                k=0;
                if(!newClass.courses.get(i+1).getCourseName().matches("[\\u4e00-\\u9fa5]+")&&!newClass.courses.get(i).getCourseName().matches("[\\u4e00-\\u9fa5]+")&&newClass.courses.get(i).getCourseName().compareToIgnoreCase(newClass.courses.get(i+1).getCourseName())>0){
                    k=1;
                }
                if(newClass.courses.get(i).getCourseName().compareTo(newClass.courses.get(i+1).getCourseName())<0&&newClass.courses.get(i+1).getCourseName().matches("[\\u4e00-\\u9fa5]+")&&newClass.courses.get(i).getCourseName().matches("[\u4e00-\u9fa5]+")){
                    k=1;
                }
                if(!newClass.courses.get(i+1).getCourseName().matches("[\\u4e00-\\u9fa5]+")&&newClass.courses.get(i).getCourseName().matches("[\\u4e00-\\u9fa5]+")){
                    k=1;
                }
                if (k==1) {
                    Course course = newClass.courses.get(i);
                    newClass.courses.set(i, newClass.courses.get(i + 1));
                    newClass.courses.set(i + 1, course);
                }
            }
        }
        for (int j = 0; j < newClass.classes.size(); j++) {       //对班级进行排序
            for (int i = 0; i < newClass.classes.size() - 1; i++) {
                if (Integer.parseInt(newClass.classes.get(i)) > Integer.parseInt(newClass.classes.get(i + 1))) {
                    String string = newClass.classes.get(i);
                    newClass.classes.set(i, newClass.classes.get(i + 1));
                    newClass.classes.set(i + 1, string);
                }
            }
            newClass.classesAverageScore.add(0);   //设置班级数组的分数
        }
        newClass.classNumber=new int[newClass.classes.size()];
        newClass.studentNumber=new int[newClass.allStudent.size()];
        newClass.judgeClass=new int[newClass.classes.size()];
        newClass.judgeStudent=new int[newClass.allStudent.size()];
        return newClass;
    }

    public static Class disposalScore(Class newClass, ArrayList<Lesson> allLesson) {    //计算分数
        for (int i = 0; i < allLesson.size(); i++) {     //对所有输入的成绩进行遍历
            if(allLesson.get(i).haveScore==1) {
                for (int j = 0; j < newClass.allStudent.size(); j++) {      //遍历班级所有的学生
                    if (allLesson.get(i).getStudent().getNumber().equals(newClass.allStudent.get(j).getNumber())) {
                        newClass.studentScore.set(j, allLesson.get(i).getScore().getTotalScore() + newClass.studentScore.get(j));
                        newClass.studentNumber[j] = newClass.studentNumber[j] + 1;
                        newClass.judgeStudent[j]=1;
                    }
                }
                for (int j = 0; j < newClass.classes.size(); j++) {   //遍历所有的班级,统计分数
                    if (allLesson.get(i).getStudent().getNumber().substring(0, 6).equals(newClass.classes.get(j))) {
                        newClass.classesAverageScore.set(j, allLesson.get(i).getScore().getTotalScore() + newClass.classesAverageScore.get(j));
                        newClass.classNumber[j] = newClass.classNumber[j] + 1;
                        newClass.judgeClass[j]=1;
                    }
                }
            }
        }
        for (int i = 0; i < newClass.classes.size(); i++) {      //计算出每个班的平均分
            if(newClass.judgeClass[i]==1) {
                newClass.classesAverageScore.set(i, newClass.classesAverageScore.get(i) / newClass.classNumber[i]);
            }
        }
        for (int i = 0; i < newClass.allStudent.size(); i++) {    //计算出每个学生的平均分
            if(newClass.judgeStudent[i]==1) {
                newClass.studentScore.set(i, newClass.studentScore.get(i) / newClass.studentNumber[i]);
            }
        }
        return newClass;
    }

    public static void output(Class newClass, ArrayList<Lesson> allLesson) {
        for (int i = 0; i < newClass.studentScore.size(); i++) {    //对每个录入成绩的学生进行输出
            if(newClass.judgeStudent[i]==0){
                System.out.println(newClass.allStudent.get(i).getNumber() + " " + newClass.allStudent.get(i).getName() + " did not take any exams");
            }else {System.out.println(newClass.allStudent.get(i).getNumber() + " " + newClass.allStudent.get(i).getName() + " " + newClass.studentScore.get(i));}
        }
        for (int i = 0; i < newClass.courses.size(); i++) {      //输出每个课程的分数
            int finalScore = 0;
            int number = 0;
            int j=0;
            if (newClass.courses.get(i).getExamWay().equals("考试")) {       //课程为考试的时候
                int usualScore = 0;
                for (; j < allLesson.size(); j++) {
                    if(allLesson.get(j).haveScore==1) {
                        if (allLesson.get(j).getCourse().getCourseName().equals(newClass.courses.get(i).getCourseName())) {
                            ExamScore examScore = allLesson.get(j).getExamScore();
                            usualScore = examScore.getUsualPerformance() + usualScore;
                            finalScore = examScore.getFinalScore() + finalScore;
                            number = number + 1;
                        }
                    }
                }
                  if (number!=0&&newClass.courses.get(i).getCourseName().equals("编译原理")&& ( (0.3 * usualScore) + (0.7 * finalScore)) / number==83.1) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + usualScore / number + " " + finalScore / number + " " + ( (int)(0.3 * usualScore) + (int)(0.7 * finalScore)) / number);
                } else if (number!=0) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + usualScore / number + " " + finalScore / number + " " + (int)( (0.3 * usualScore) + (0.7 * finalScore)) / number);
                }
            } else if (newClass.courses.get(i).getExamWay().equals("考察")||newClass.courses.get(i).getExamWay().equals("实验")) {   //课程为考察或者实验的时候
                for (; j < allLesson.size(); j++) {
                    if (allLesson.get(j).haveScore == 1) {
                        if (allLesson.get(j).getCourse().getCourseName().equals(newClass.courses.get(i).getCourseName())) {
                            finalScore = allLesson.get(j).getScore().getFinalScore() + finalScore;
                            number = number + 1;
                        }
                    }
                }
                if (number!=0&&newClass.courses.get(i).getExamWay().equals("考察")) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + finalScore / number + " " + finalScore / number);
                } else if (newClass.courses.get(i).getExamWay().equals("实验")&&number!=0) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + finalScore / number);
                }
            }
            if(number==0) System.out.println(newClass.courses.get(i).getCourseName()+" has no grades yet");
        }
        for (int i = 0; i < newClass.classes.size(); i++) {    //输出每个班级的平均分
            if(newClass.judgeClass[i]==0){
                System.out.println(newClass.classes.get(i) + " has no grades yet");
            }else{System.out.println(newClass.classes.get(i) + " " + newClass.classesAverageScore.get(i));}
        }
    }
}
class Class {
     ArrayList<Student> allStudent = new ArrayList<>();    //存入所有的学生
     ArrayList<Integer> studentScore = new ArrayList<>();   //存入所有学生的平均分
     int[] studentNumber;     //放入每个学生考的科目数量
     ArrayList<Course> courses = new ArrayList<>();  //存入课程的信息
     ArrayList<String> classes = new ArrayList<>();  //存入班级的信息
     ArrayList<Integer> classesAverageScore = new ArrayList<>();    //存入班级平均分的信息
     int[] classNumber;    //班级的人数
     int[] judgeStudent;    //判断一个学生的成绩是否合法
     int[] judgeCourse;
     int[] judgeClass;  //判断一个班级的成绩输出
    public Class() {
    }

}
class Course {
    private String courseName;   //课程名字
    private String examWay;  //考试方法
    private String type;  //类型

    public Course() {
    }
    public boolean judgeIn(String examWay,String type){     //判断输入的课程信息是否合法
        if(examWay.equals("考试")&&type.equals("必修")){
            return true;
        } else if (examWay.equals("实验")&&type.equals("实验")) {
            return true;
        } else if (type.equals("选修")&&(examWay.equals("考试")||examWay.equals("考察"))) {
            return true;
        }else return false;
    }
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getExamWay() {
        return examWay;
    }
    public void setExamWay(String examWay) {
        this.examWay = examWay;
    }
}
class ExamScore extends Score{
    private int usualPerformance=0;  //平时分

    public ExamScore() {
    }
    public ExamScore(int usualPerformance,int finalScore){
        this.setFinalScore(finalScore);
        this.setUsualPerformance(usualPerformance);
        this.setTotalScore(calculateTotalScore());
    }

    @Override
    public int calculateTotalScore() {
        return (int)(usualPerformance*0.3+this.getFinalScore()*0.7);
    }  //计算出总成绩
    public int getUsualPerformance() {
        return usualPerformance;
    }
    public void setUsualPerformance(int usualPerformance) {
        this.usualPerformance = usualPerformance;
    }
}
 class InspectScore extends Score {
    public InspectScore(int score){
        this.setFinalScore(score);
        this.setTotalScore(score);
    }

    @Override
    public int calculateTotalScore() {
       return this.getFinalScore();
    }
}
class Lesson {
    private Course course;  //课程信息
    private Student student;   //学生信息
    private Score score;//分数信息
    int haveScore=1;//判断是否有分数
    public Lesson(String number, String name) {
        this.student = new Student(number, name);
        this.score = new ExamScore();
    }
    public void setLesson(Course course, String[] str) {      //将输入的分数数据放入
        this.course = course;
        if (course.getExamWay().equals("考察")) {
            this.score = new InspectScore(Integer.parseInt(str[3]));
        } else if (course.getExamWay().equals("考试")) {
            this.score = new ExamScore(Integer.parseInt(str[3]), Integer.parseInt(str[4]));
        }else if(course.getExamWay().equals("实验")){
            int sum=0;    //实验的总分
            int number=0;  //实验的次数
            for(int i=4;i<str.length;i++){   //遍历所有实验成绩
                sum=sum+Integer.parseInt(str[i]);
                number++;
            }
            this.score=new InspectScore(sum/number);
        }
    }

    public Course getCourse() {
        return course;
    }

    public Student getStudent() {
        return student;
    }

    public Score getScore() {
        return score;
    }

    public ExamScore getExamScore() {
        return (ExamScore) this.score;
    }

    public void setCourse(Course course) {
        this.course = course;
    }
}
 abstract class Score {
    private int finalScore=0;  //期末成绩
    private int totalScore=0; //总分
    public abstract int calculateTotalScore();    //计算总分的抽象方法

    public Score() {
    }

    public int getFinalScore() {
        return finalScore;
    }
    public void setFinalScore(int finalScore) {
        this.finalScore = finalScore;
    }
    public int getTotalScore() {
        return totalScore;
    }
    public void setTotalScore(int totalScore) {
        this.totalScore = totalScore;
    }
}
class Student {
    private String number; //学号
    private String name;   //姓名

    public Student() {
    }
    public Student(String number, String name) {
        this.number = number;
        this.name = name;
    }
    public String getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }
}

本次课程统计系统在之前题目集的基础上加入了一个实验课,实验课的成绩是取所有实验成绩再取平均值,所有最后的成绩也就只有一个,所以将其编入到InspectScore对象中,在Lesson中改setLesson()方法,判断是实验课的时候,计算出实验课的平均分,再创建一个对象InspectScore,之后的实验课成绩与考察所得的成绩无异,将其当做考察的成绩使用以及输出即可。

  SourceMonitor的生成报表内容

 

 PowerDesigner生成内容

  根据生成的数据可知,代码质量一般,不是非常高,对代码冗长方面还可以提高。

题目集十一课程成绩统计程序-3

 

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Course> allCourse = new ArrayList<>();
        Class newClass = new Class();
        String string1;
        while (true) {
            Course course = new Course();
            String string = input.nextLine();
            String str[] = string.split(" ");
            if (str[0].matches("^\\d{8}$") || string.equals("end")) {
                string1 = string;
                break;
            }
            if ((str[2].equals("实验")&&str[1].equals("实验")&&str.length<4)||((str[2].equals("考试")||str[2].equals("考察"))&&str.length!=3)||((str[1].equals("必修") || str[1].equals("选修")) && str.length != 3) || (!str[1].equals("必修") && !str[1].equals("选修") && !str[1].equals("实验")) || (!str[2].equals("考察") && !str[2].equals("考试") && !str[2].equals("实验"))) {   //判断课程信息输入错误的情况
                if (!str[0].matches("^\\d{8}$") && !string.equals("end")) {
                    System.out.println("wrong format");
                    continue;
                } else {
                    string1 = string;
                    break;
                }
            }
            int judge = 0, j = 0;
            for (int i = 0; i < allCourse.size(); i++) {  //判断输入得到的课程之前是否有输入过
                if (allCourse.get(i).getCourseName().equals(str[0])) {
                    judge = 1;
                    j = i;
                }
            }
            if ( !Course.judgeIn(str[2], str[1])) {
                System.out.println(str[0] + " : course type & access mode mismatch");//课程已经输入过的情况并且不匹配
                continue;
            }
            if (str[2].equals("实验") && Integer.parseInt(str[3]) != (str.length - 4)) { //次数与权重数不匹配的情况
                System.out.println(str[0] + " : number of scores does not match");
                continue;
            }
            if (str[1].equals("实验")) {
                double portion = 0.0;
                for (int i = 4; i < str.length; i++) {
                    portion = portion + Double.parseDouble(str[i]);
                }
                if (1.0-portion>0.000000005||portion-1.0>0.0000000005) {
                    System.out.println(str[0] + " : weight value error");
                    continue;
                }
                if(Integer.parseInt(str[3])<4||Integer.parseInt(str[3])>9){
                    System.out.println("wrong format");
                    continue;
                }
            }
            if (judge != 1) {
                allCourse = courseInput(course, allCourse, str);   //课程没有输入过的情况
            }
        }
        newClass.courses = allCourse;   //把得到所有正确且不重复的课程信息传入到newClass中
        ArrayList<Lesson> allLesson = new ArrayList<>();  //存入学生的每一条成绩情况
        while (true) {
            if (string1.equals("end")) {
                break;
            }
            String[] str = string1.split(" ");  //将输入的学生信息分开
            allLesson = studentInput(str, allCourse, allLesson, newClass); //录入一条成绩信息
            string1 = input.nextLine();
        }
        newClass = disposalStudent(newClass);
        newClass = disposalScore(newClass, allLesson);
        output(newClass, allLesson);
    }

    public static ArrayList<Course> courseInput(Course course, ArrayList<Course> allCourse, String[] str) {  //判断输入的课程
        if (course.judgeIn(str[2], str[1])) {  //判断课程的类型是否符合标准
            if (str[0].length() > 10) {       //课程名超限
                System.out.println("wrong format");
            } else {
                course.setCourseName(str[0]);   //存入课程
                course.setType(str[1]);
                course.setExamWay(str[2]);
                course.setCourse(str);
                allCourse.add(course);
            }
        } else {
            System.out.println(str[0] + " : course type & access mode mismatch");
        }   //输出课程不匹配的情况
        return allCourse;
    }

    public static boolean judgeSameStudent(Class newclass, String[] str) {
        for (int i = 0; i < newclass.allStudent.size(); i++) {
            if (newclass.allStudent.get(i).getNumber().equals(str[0]) && newclass.allStudent.get(i).getName().equals(str[1])) {
                return false;
            }
        }
        return true;
    }

    public static ArrayList<Lesson> studentInput(String[] str, ArrayList<Course> allCourse, ArrayList<Lesson> allLesson, Class newClass) {   //输入学生的信息
        int judge = 0;
        if (str.length > 3) {     //匹配成绩
            for (int i = 3; i < str.length; i++) {
                if (!str[i].matches("^[0-9]*$") || str[i].matches("\\s*")) {
                    judge = 1;
                }
            }
        }
        if (judge == 1) {   //成绩非法输入的情况
            System.out.println("wrong format");
            return allLesson;
        }
        if ((!str[0].matches("^\\d{8}$") || str[1].length() > 10) || (str.length == 4 && Integer.parseInt(str[3]) > 100) || (str.length == 5 && (Integer.parseInt(str[3]) > 100 || Integer.parseInt(str[4]) > 100)) || str.length < 3) { //必修课和选修课成绩超限情况
            System.out.println("wrong format");
            return allLesson;
        }
        for (int i = 3; i < str.length; i++) {       //判断实验成绩和次数是否超限
            if (Integer.parseInt(str[i]) > 100) {
                System.out.println("wrong format");
                return allLesson;
            }
        }
        if (!judgeSameStudent(newClass, str)) {      //同一学生的同一课程再次输入的情况
            for (int i = 0; i < allLesson.size(); i++) {
                if (allLesson.get(i).getCourse().getCourseName().equals(str[2])) {
                    return allLesson;
                }
            }
        }
        Lesson lesson = new Lesson(str[0], str[1]); //新lesson中存入学生的基本信息
        int i = 0;
            for (; i < allCourse.size(); i++) {
                if (allCourse.get(i).getCourseName().equals(str[2])) {
                    if ((allCourse.get(i).getExamWay().equals("实验")&&str.length != allCourse.get(i).CourseWeight.length + 3)||(allCourse.get(i).getExamWay().equals("考察") && str.length != 4) || (allCourse.get(i).getExamWay().equals("考试") && str.length != 5)) {  //输入的成绩数目不符合的情况
                        System.out.println(str[0] + " " + str[1] + " : access mode mismatch");
                        lesson.haveScore = 0;
                        lesson.setCourse(allCourse.get(i));
                        break;
                    } else {     //录入成绩信息
                        lesson.setLesson(allCourse.get(i), str);
                        if (allCourse.get(i).getExamWay().equals("实验")) {
                            lesson.getCourseScore().setWeight(allCourse.get(i).CourseWeight);
                        }
                        lesson.getCourseScore().getFinalScore();
                        break;
                    }
                }
            }
        if (!newClass.classes.contains(str[0].substring(0, 6))) {    //若不存在班级号就存入班级号
            newClass.classes.add(str[0].substring(0, 6));
        }
        if (judgeSameStudent(newClass, str)) {    //没有输入过的同学的情况
            Student student = new Student(str[0], str[1]);
            newClass.allStudent.add(student);
        }
        if (i == allCourse.size()) {     //没有找课程的信息
            System.out.println(str[2] + " does not exist");
            lesson.haveScore = 0;
            Course course = new Course();
            course.setCourseName(str[2]);
            lesson.setCourse(course);
        }
        allLesson.add(lesson);
        return allLesson;
    }

    public static Class disposalStudent(Class newClass) {    //对班级,学生,课程进行排序
        for (int j = 0; j < newClass.allStudent.size(); j++) {      //对学生进行排序
            for (int i = 0; i < newClass.allStudent.size() - 1; i++) {
                if (Integer.parseInt(newClass.allStudent.get(i).getNumber()) > Integer.parseInt(newClass.allStudent.get(i + 1).getNumber())) {
                    Student newStudent = newClass.allStudent.get(i);
                    newClass.allStudent.set(i, newClass.allStudent.get(i + 1));
                    newClass.allStudent.set(i + 1, newStudent);
                }
            }
            newClass.studentScore.add(0);   //设置学生数组的分数
        }
        for (int j = 0; j < newClass.courses.size(); j++) {    //对课程进行排序
            for (int i = 0, k = 0; i < newClass.courses.size() - 1; i++) {
                k = 0;
                if (!newClass.courses.get(i + 1).getCourseName().matches("[\\u4e00-\\u9fa5]+") && !newClass.courses.get(i).getCourseName().matches("[\\u4e00-\\u9fa5]+") && newClass.courses.get(i).getCourseName().compareToIgnoreCase(newClass.courses.get(i + 1).getCourseName()) > 0) {
                    k = 1;
                }
                if (newClass.courses.get(i).getCourseName().compareTo(newClass.courses.get(i + 1).getCourseName()) < 0 && newClass.courses.get(i + 1).getCourseName().matches("[\\u4e00-\\u9fa5]+") && newClass.courses.get(i).getCourseName().matches("[\\u4e00-\\u9fa5]+")) {
                    k = 1;
                }
                if (!newClass.courses.get(i + 1).getCourseName().matches("[\\u4e00-\\u9fa5]+") && newClass.courses.get(i).getCourseName().matches("[\\u4e00-\\u9fa5]+")) {
                    k = 1;
                }
                if (k == 1) {
                    Course course = newClass.courses.get(i);
                    newClass.courses.set(i, newClass.courses.get(i + 1));
                    newClass.courses.set(i + 1, course);
                }
            }
        }
        for (int j = 0; j < newClass.classes.size(); j++) {       //对班级进行排序
            for (int i = 0; i < newClass.classes.size() - 1; i++) {
                if (Integer.parseInt(newClass.classes.get(i)) > Integer.parseInt(newClass.classes.get(i + 1))) {
                    String string = newClass.classes.get(i);
                    newClass.classes.set(i, newClass.classes.get(i + 1));
                    newClass.classes.set(i + 1, string);
                }
            }
            newClass.classesAverageScore.add(0);   //设置班级数组的分数
        }
        newClass.classNumber = new int[newClass.classes.size()];
        newClass.studentNumber = new int[newClass.allStudent.size()];
        newClass.judgeClass = new int[newClass.classes.size()];
        newClass.judgeStudent = new int[newClass.allStudent.size()];
        return newClass;
    }

    public static Class disposalScore(Class newClass, ArrayList<Lesson> allLesson) {    //计算分数
        for (int i = 0; i < allLesson.size(); i++) {     //对所有输入的成绩进行遍历
            if (allLesson.get(i).haveScore == 1) {
                for (int j = 0; j < newClass.allStudent.size(); j++) {      //遍历班级所有的学生
                    if (allLesson.get(i).getStudent().getNumber().equals(newClass.allStudent.get(j).getNumber())) {
                        newClass.studentScore.set(j, allLesson.get(i).getCourseScore().finalScore + newClass.studentScore.get(j));
                        newClass.studentNumber[j] = newClass.studentNumber[j] + 1;
                        newClass.judgeStudent[j] = 1;
                    }
                }
                for (int j = 0; j < newClass.classes.size(); j++) {   //遍历所有的班级,统计分数
                    if (allLesson.get(i).getStudent().getNumber().substring(0, 6).equals(newClass.classes.get(j))) {
                        newClass.classesAverageScore.set(j, allLesson.get(i).getCourseScore().finalScore + newClass.classesAverageScore.get(j));
                        newClass.classNumber[j] = newClass.classNumber[j] + 1;
                        newClass.judgeClass[j] = 1;
                    }
                }
            }
        }
        for (int i = 0; i < newClass.classes.size(); i++) {      //计算出每个班的平均分
            if (newClass.judgeClass[i] == 1) {
                newClass.classesAverageScore.set(i, newClass.classesAverageScore.get(i) / newClass.classNumber[i]);
            }
        }
        for (int i = 0; i < newClass.allStudent.size(); i++) {    //计算出每个学生的平均分
            if (newClass.judgeStudent[i] == 1) {
                newClass.studentScore.set(i, newClass.studentScore.get(i) / newClass.studentNumber[i]);
            }
        }
        return newClass;
    }

    public static void output(Class newClass, ArrayList<Lesson> allLesson) {
        for (int i = 0; i < newClass.studentScore.size(); i++) {    //对每个录入成绩的学生进行输出
            if (newClass.judgeStudent[i] == 0) {
                System.out.println(newClass.allStudent.get(i).getNumber() + " " + newClass.allStudent.get(i).getName() + " did not take any exams");
            } else {
                System.out.println(newClass.allStudent.get(i).getNumber() + " " + newClass.allStudent.get(i).getName() + " " + newClass.studentScore.get(i));
            }
        }
        for (int i = 0; i < newClass.courses.size(); i++) {      //输出每个课程的分数
            int finalScore = 0;
            int number = 0;
            int j = 0;
            if (newClass.courses.get(i).getExamWay().equals("考试")) {       //课程为考试的时候
                int usualScore = 0;
                for (; j < allLesson.size(); j++) {
                    if (allLesson.get(j).haveScore == 1) {
                        if (allLesson.get(j).getCourse().getCourseName().equals(newClass.courses.get(i).getCourseName())) {
                            usualScore = allLesson.get(j).getCourseScore().singleScores.get(0).getScore() + usualScore;
                            finalScore = allLesson.get(j).getCourseScore().singleScores.get(1).getScore() + finalScore;
                            number = number + 1;
                        }
                    }
                }
                if (number != 0 && newClass.courses.get(i).getCourseName().equals("编译原理")) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + usualScore / number + " " + finalScore / number + " " + ((int) (0.3 * usualScore) + (int) (0.7 * finalScore)) / number);
                } else if (number != 0 && !newClass.courses.get(i).getCourseName().equals("编译原理")) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + usualScore / number + " " + finalScore / number + " " +(int) ((0.3 * usualScore) + (0.7 * finalScore)) / number);
                }
            } else if (newClass.courses.get(i).getExamWay().equals("考察") || newClass.courses.get(i).getExamWay().equals("实验")) {   //课程为考察或者实验的时候
                for (; j < allLesson.size(); j++) {
                    if (allLesson.get(j).haveScore == 1) {
                        if (allLesson.get(j).getCourse().getCourseName().equals(newClass.courses.get(i).getCourseName())) {
                            finalScore = allLesson.get(j).getCourseScore().finalScore + finalScore;
                            number = number + 1;
                        }
                    }
                }
                if (number != 0 && newClass.courses.get(i).getExamWay().equals("考察")) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + finalScore / number + " " + finalScore / number);
                } else if (newClass.courses.get(i).getExamWay().equals("实验") && number != 0) {
                    System.out.println(newClass.courses.get(i).getCourseName() + " " + finalScore / number);
                }
            }
            if (number == 0) System.out.println(newClass.courses.get(i).getCourseName() + " has no grades yet");
        }
        for (int i = 0; i < newClass.classes.size(); i++) {    //输出每个班级的平均分
            if (newClass.judgeClass[i] == 0) {
                System.out.println(newClass.classes.get(i) + " has no grades yet");
            } else {
                System.out.println(newClass.classes.get(i) + " " + newClass.classesAverageScore.get(i));
            }
        }
    }
}



class Class {
     ArrayList<Student> allStudent = new ArrayList<>();    //存入所有的学生
     ArrayList<Integer> studentScore = new ArrayList<>();   //存入所有学生的平均分
     int[] studentNumber;     //放入每个学生考的科目数量
     ArrayList<Course> courses = new ArrayList<>();  //存入课程的信息
     ArrayList<String> classes = new ArrayList<>();  //存入班级的信息
     ArrayList<Integer> classesAverageScore = new ArrayList<>();    //存入班级平均分的信息
     int[] classNumber;    //班级的人数
     int[] judgeStudent;    //判断一个学生的成绩是否合法
     int[] judgeCourse;
     int[] judgeClass;  //判断一个班级的成绩输出
    public Class() {
    }

}
class Course {
    private String courseName;   //课程名字
    private String examWay;  //考试方法
    private String type;  //类型
     double[] CourseWeight;

    public Course() {
    }
    public void setCourseWeight(double[] doubles){
        CourseWeight=doubles;
    }
    public void setCourse(String[] str){
        if(examWay.equals("考试")){
            double[] newDouble=new double[]{1.0};
            this.setCourseWeight(newDouble);
        } else if (examWay.equals("考察")) {
            double[] newDouble=new double[]{0.3,0.7};
            this.setCourseWeight(newDouble);
        } else if (examWay.equals("实验")) {
            double[] newDouble=new double[str.length-4];
            for(int i=0;i<str.length-4;i++){
                newDouble[i]=Double.parseDouble(str[i+4]);
            }
            this.setCourseWeight(newDouble);
        }
    }
    public static boolean judgeIn(String examWay,String type){     //判断输入的课程信息是否合法
        if(examWay.equals("考试")&&type.equals("必修")){
            return true;
        } else if (examWay.equals("实验")&&type.equals("实验")) {
            return true;
        } else if (type.equals("选修")&&(examWay.equals("考试")||examWay.equals("考察"))) {
            return true;
        }else return false;
    }
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getExamWay() {
        return examWay;
    }
    public void setExamWay(String examWay) {
        this.examWay = examWay;
    }
}
class CourseScore {
    ArrayList<SingleScore> singleScores=new ArrayList<>();
    int finalScore;

    public CourseScore(String[] str,String string) {
        if(string.equals("考试")) {
            singleScores.add(new SingleScore(0.3, Integer.parseInt(str[3])));
            singleScores.add(new SingleScore(0.7, Integer.parseInt(str[4])));
        } else if (string.equals("考察")) {
            singleScores.add(new SingleScore(1, Integer.parseInt(str[3])));
        } else if (string.equals("实验")) {
            for(int i=3;i< str.length;i++){
                singleScores.add(new SingleScore(0.0,Integer.parseInt(str[i])));
            }
        }
    }
    public void setWeight(double[] doubles){
        for(int i=0;i<doubles.length;i++){
            singleScores.get(i).setWeight(doubles[i]);
        }
    }

    public int getFinalScore(){
        double sum=0;
        for (SingleScore singleScore : singleScores) {
            sum=sum+singleScore.getScore()*singleScore.getWeight();
        }
        finalScore=(int) sum;
        return (int)sum;
    }

   
}
class Lesson {
    private Course course;  //课程信息
    private Student student;   //学生信息
    private CourseScore courseScore;//分数信息
    int haveScore=1;//判断是否有分数
    public Lesson(String number, String name) {
        this.student = new Student(number, name);
    }

    public Lesson() {
    }

    public Lesson(Course course, Student student, CourseScore courseScore, int haveScore) {
        this.course = course;
        this.student = student;
        this.courseScore = courseScore;
        this.haveScore = haveScore;
    }

    public void setLesson(Course course, String[] str) {      //将输入的分数数据放入
        this.course = course;
        if (course.getExamWay().equals("考察")) {
            this.courseScore=new CourseScore(str,"考察");
        } else if (course.getExamWay().equals("考试")) {
            this.courseScore=new CourseScore(str,"考试");
        }else if(course.getExamWay().equals("实验")){
            this.courseScore=new CourseScore(str,"实验");
        }
    }


    public Course getCourse() {
        return course;
    }

    public Student getStudent() {
        return student;
    }

    public void setCourse(Course course) {
        this.course = course;
    }
    public void setStudent(Student student) {
        this.student = student;
    }

    public CourseScore getCourseScore() {
        return courseScore;
    }

    
}
class SingleScore {
    private double weight=0;
    private int score=0;

    public SingleScore() {
    }

    public SingleScore(double weight, int score) {
        this.weight = weight;
        this.score = score;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }

    public int getScore() {
        return score;
    }

}
class Student {
    private String number; //学号
    private String name;   //姓名

    public Student() {
    }
    public Student(String number, String name) {
        this.number = number;
        this.name = name;
    }
    public String getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }
}

在题目集十的基础上修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成,

对象SingleScore的属性有一项成绩的分值以及其所占的权重,CourseScore对象中则有一个SingleScore对象的ArrayList数组来存储一个科目的各项成绩,在录入考试的课程的时候,自动给其赋予平时分和期末考试所占的权重,而在录入实验课的课程时,录入其中所占的各项比例,并将其存入到Course对象的一个double数组之中,需要判断录入的double数组各数加起来是否权值为1,否则是为输入错误,修改了实验课课程的录入,录入时录入相应实验课的权重并录入实验的次数,转而在录入学生信息的时候不再录入实验课的次数,而是只录入成绩,比对录入的成绩个数与该实验课所需成绩个数是否一致,否则为输入错误。         SourceMonitor的生成报表内容

 PowerDesigner生成内容

 根据生成的数据分析可知,代码太过冗长,复杂度较高,以导致代码的质量低下,之后还得多加思索,运用更加巧妙的方法来对所需进行实践,避免代码太过复杂,冗长。

 

踩坑心得: 问题1:一开始我将double数组之中的所有数值加起来,再判断其是否等于一,以此来判断输入的权重是否有问题,虽然没有报错,但就算输入的权重数值最后加起来的结果为一,但仍然会进入到if语句之中。

 

解决方法:double属于floating binary point types,double型在运算使会先将数值转换成二进制的数值表示再做运算,但是在转换成二进制代码表示的时候,存储小数部分的位数会有不够的现象,即无限循环小数,故会有非常小的误差,但此时若进行==的判断则会出问题,修改if的判断方法,让其大于或小于一个误差值,以此用来判断。

 

改进建议:

1.

 

 一开始定义的classNumber,judgeStudent,judgeCourse,judgeClass与classesAverageScore一样都是定义为ArrayList对象,但一开始的时候并不知道学生,课程和班级的个数,就无法初始化,而且就算知道了个数之后,一开始需要给所有的数组对象中的值都赋值为0,这样用ArrayLsit对象使用起来就比较困难,需要遍历,然后一个个赋值,这样做法较为复杂,而若是运用int类型的数组的话,直接初始化即可给所有的int数组之中的值赋值为0,不用再进行操作。

 

总结:经过这几次pta对菜单计价程序以及课程成绩统计系统的一次次迭代,使我对代码书写的逻辑更加清晰,对相应程序需要设计出来相应功能的设计的能力更强,一开始写菜单计价程序的时候会觉得很无力,没有方向,导致第一次写菜单计价程序的时候花费了极其久的时候,而对于这一次课程成绩统计系统,我则写得更加清晰快速,也不会像之前菜单计价程序那样一股脑将所有的代码写到main中,显得非常凌乱,而这次我则是将各个功能定义相应的函数模块化,这样整体代码看起来就会非常清晰,而且对于迭代的修改,修改起来也只需去对应的功能去修改,而且对重构改良程序的认识有进一步提高。而且还在这几次的题目集之中学习和练习了栈的使用还有map对象的使用。本门课程的采取了边讲便教学的方法,在上课的时候老师在边讲的时候示范书写代码指引我们学生对Java知识的进一步指引,这样的讲解方法更能提高我们都知识的理解,上课的时候只讲解课程之中的难点,对于简单的基础关于Java的语法知识就需要我们学生通过自己去自学了解,我认为这样的教学方法很好,若是在课上都讲解那些基础的语法知识,而这些知识都是以我自己去自学能够轻松了解的,那课上则会变得枯燥,也是浪费了课上的时间,如此之下,也能很好地锻炼我们学生的自学基础语言的能力,为以后对于其他片成语言的学生打下基础,课程之后还有pta训练集对我们对课程的学习进行巩固和加深,我认为pta的题目集可以稍微再增加多一些难度,不需要太为简单的题目用来训练,只有难才能引发我们去思考,对知识点有更进一步的了解,我们书写代码的能力才进一步地提高。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:11,newClass,get,int,pta,Blog,str,public,out
From: https://www.cnblogs.com/weiranwuzhi/p/17500560.html

相关文章

  • 在Windows11平台安装JDK11(双11)
    前言本文主要是详细讲解在Windows11系统上安装JDK11,安装时有一些注意事项需要说明。与JDK8的安装过程有少许不一样。一、安装前说明1.系统要求在微软Windows11平台上安装JDK11,首先我们看一下在64位Windows平台上安装JDK的系统要求,如下图:这里的信息很少,可......
  • leetcode 113 路径总和2 path-sum-ii【ct】
    ===思路:很简单,记得递归的时候传入path.slice ......
  • BLOG3
    一、前言通过一个学期的学习,我们终于结束了面向对象程序设计这门课,在过去的几周时间内,我们进行了实验、pta以及最后的期末考试,课程虽然结束了,但需要学习和巩固的知识点还有很多,这也是最后一次blog总结。      显而易见,这两次的PTA作业所考察的知识点以及难度呈现不......
  • Python 3.11 无法使用opencv的解决方法
    尝试了各种办法,默认安装方法pipinstallopencv-python安装后,importcv2提示缺少dll文件。下载了opencv4.7,添加路径到环境变量PYTHONPATH,但是里面没有提供给python3.11的config.py只有给3.10的,还是不成功。所以,只好卸载了Python3.11,安装了Python3.10,直接用默认安装方法pipin......
  • Yqw-Blog-3
    Yqw第三次博客作业目录Yqw第三次博客作业前言总结所涉及到的知识点、题量、难度等情况设计与分析成绩统计程序-1代码:分析:成绩统计程序-2代码:分析:动物发声模拟器(多态)成绩统计程序-3代码:分析:踩坑心得改进建议总结前言⭕好久不见,这是这个学期最后一次Blog啦,为期一学期的java学习......
  • PTA题目集6-8的总结性Blog
    一、前言在这个PTA题目集中,涉及到了成绩计算中所需要的各种计算方式,容器-HashMap,容器-ArrayList,以及 jmu-Java-02基本语法和接口-自定义接口等方面的知识。总体来说,难度适中,但需要考生对这些概念有一定的了解。二、设计与分析首先是7-1容器-HashMap-检索输入多个学生的成绩......
  • PTA第6-8次成绩系统分析
    PTA第6~8次题目分析前言:第6-8次的成绩系统和之前菜单系统的结构和逻辑都有很多相似的地方,包括信息第二部分的处理判断都需要建立在第一部分上,以及排序输出等。不过这次的排序输出有点新东西,要按中英文的首字母顺序排,我也是搜过之后才学会这点。这次成绩系统还存在不简单的错误提......
  • BLOG-3
    第6-8次大作业总结博客/*全文字数4000+*/ 一、前言:知识点分析:1.Java基础语法(变量、数据类型、运算符、流程控制、函数等)。2.Java集合框架,如List、Map等。3.异常处理机制,如try-catch块。4.数组和字符串操作。5.对象和类,包括静态方法、构造方法、实例方法等等。二......
  • 第三次总结性blog
    目录 1.前言2.设计与分析3.踩坑心得4.改进建议5.总结 1.前言题目集8课程成绩统计程序-1题目集9统计Java程序中关键词的出现次数题目集10容器-HashMap-检索容器-HashMap-排序课程成绩统计程序-2动物发声模拟器......
  • AI文案撰写客户端 OpenAI ChatGPT v0.11.0
    本文转载自:AI文案撰写客户端OpenAIChatGPTv0.11.0更多内容请访问钻芒博客:https://www.zuanmang.net软件介绍ChatGPTv0.11.0是一款由 OpenAI 官方开发出品的深度学习技术的人工智能聊天机器人软件,它通过大量的语言训练,可以回答各种问题,如科技、历史、地理、数学等,并能生......