PTA最后一次作业Blog

(1)前言:

 第七次题目集为菜单的计价程序的延展,考察类之间的关系,类与类的关系,就这一道题,但是难度对于我来说有点大,因为前几次做的不是很熟练,所以这次的菜单也是处于一个不会做的状态,还是有点难受的;第八次题目集做的是与菜单类的题型相似的期末计价程序,看到这类题目的时候还是很崩溃的,感觉自己可能又是不太会,当做的时候又感觉还好了,比菜单的更加的清晰明了,难度的话比菜单的简单,这次题目集也是只有一道题,考察的同样是类与类之间的关系;第九次题目集是一次课堂的练习,关于关键字计算的,刚好课堂上面讲到了利用HashMap来进行关键字的统计,然后就需要我们来使用这个进行计算,对我来说难度也是比较大的,还是挺复杂的,而且其中有一个点还一直过不去,不知道为啥;第十次题目集共有四道题,前面两道考查的是HashMap的存储以及检索功能第三题考查的是期末统计程序2,相对于第一题来说进行了一些改动,第四题是动物发生器的模拟,相对来说还挺简单的,比较好理解;第十一次题目集第一次考查的是ArrayList,第二题考查的是期末成绩统计3,后面三道题考查的是接口,覆盖以及身份证排序,难度来说有点大,需要慢慢的学习,题量的话还行;

(2)设计与分析:

 



import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Table[] tables = new Table[10];
Dish dish = new Dish();
Menu menu = new Menu();
Order order = new Order();
int tableNum = 0;
//约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。
String dishName = "[\\u4e00-\\u9fa5]+ \\d+";//普通菜的正则表达
String specilName = "[\\u4e00-\\u9fa5]+ [\\u4e00-\\u9fa5]+ \\d+ T";//特色菜的正则表达
String regexTable1 = "table \\d+ \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}";//普通桌的table的正则表达
String regexTable2 = "table \\d+ : [a-zA-Z]{1,10} 1(80|81|89|33|35|36)\\d{8} \\d{4}/\\d{1,2}/\\d{1,2} \\d{2}/\\d{2}/\\d{2}";//用户桌的正则表达
String orderT = "\\d+ [\\u4e00-\\u9fa5]+( \\d){2,3}";//点菜的正则表达
String helpOrder = "\\d \\d [\\u4e00-\\u9fa5]+( \\d){2,3}";//带点菜的正则表达
String orderdel = "\\d+ delete";//删除的正则表达
String line = input.nextLine();
int[] flag1 = new int[10];//判断是否营业
int dishNum = 0;
int recordNum = 0;
//Table table = new Table();
int flag = 0;
while (!line.equals("end")) {
String[] parts = line.split(" ");
if (line.matches(dishName)) {
menu.dishs[dishNum] = menu.addDish(parts[0], Integer.parseInt(parts[1]));
dishNum++;
} else if (line.matches(specilName)) {
/*String name = null;
String tasteA = null;
int unit_price = ;*/
//int price = Integer.parseInt(parts[3]);
flag = 1;
// dish = new Dish( parts[0],parts[1], price);
menu.dishs[dishNum] = menu.addDish(parts[0], parts[1], Integer.parseInt(parts[2]));
dishNum++;
} else if (parts.length == 3)
System.out.println("wrong format");//特色菜输出错误
else if (parts[0].equals("table")) {
tableNum++;
if (line.matches(regexTable2) || line.matches(regexTable1)) {
tables[tableNum - 1] = new Table();
tables[tableNum - 1].setTime(parts[5]);
tables[tableNum - 1].setDayTime(parts[6]);
if (tables[tableNum - 1].getSale() == 0) {//不在营业时间内
System.out.println("table " + parts[1] + " out of opening hours");
flag1[tableNum - 1] = 1;
} else
System.out.println("table " + parts[1] + ": ");

} else
System.out.println("wrong format");//桌号信息错误
}
if (line.matches(orderT)) {
if (parts.length == 4) {//普通菜的输出
if (flag1[tableNum - 1] == 0) {
int orderNum = Integer.parseInt(parts[0]);
dish = menu.searchDish(parts[1]);
int portion = Integer.parseInt(parts[2]);
int num = Integer.parseInt(parts[3]);
if (dish == null) {
System.out.println(parts[1] + " does not exist");

} else {
//Dish d;
Record record = new Record();
order.records[recordNum] = new Record();
// Record record = new Record(orderNum,d,portion,num);
order.records[recordNum] = order.addARecord(orderNum, dish, portion, num);

System.out.println(orderNum + " " + parts[1] + " " + Math.round(dish.unit_price * portion * num));
recordNum++;
}
}
}
if (parts.length == 5) {//特色菜的输出

if (flag1[tableNum - 1] == 0) {
int orderNum = Integer.parseInt(parts[0]);
dish = menu.searchDish(parts[1]);//根据菜名在菜谱中查找
int taste = Integer.parseInt(parts[2]);
int portion = Integer.parseInt(parts[3]);
int num = Integer.parseInt(parts[4]);
if (dish == null) {
System.out.println(parts[1] + " does not exist");
} else {//存在这种菜
//Dish dish4 = new Dish(parts[1],taste);

if (dish.tasteA.equals("川菜") && taste > 5) {
System.out.print("spicy num out of range :" + taste);
} else if (dish.tasteA.equals("晋菜") && taste > 4) {
System.out.print("acidity num out of range :" + taste);
} else if (dish.tasteA.equals("浙菜") && taste > 3) {
System.out.print("sweetness num out of range :" + taste);
} else {
Record record = new Record();
order.records[recordNum] = new Record();
//order.records[recordNum] =
order.addARecord(orderNum, dish, taste, portion, num);
//Math.round(dish.unit_price * portion * num)
System.out.println(orderNum + " " + parts[1] + " " + order.records[recordNum].getPrice());
recordNum++;
}

}
}
}

}

if (line.matches(orderdel)) {
if (flag1[tableNum - 1] == 0) {
int orderNum = Integer.parseInt(parts[0]);
Record findRecord = order.findRecordByNum(orderNum);
if (findRecord == null) {
System.out.println("delete error;");
}
order.delARecordByOrderNum(orderNum);

}
}
line = input.nextLine();
}
if (flag1[tableNum - 1] == 0) {
if (flag == 0) {
System.out.println("table " + tables[tableNum - 1].tableNum + ": " + order.getTotalPrice() + " " + order.getdiscount() + " ");
} else {
System.out.print("table " + tables[tableNum - 1].tableNum + ": " + order.getTotalPrice() + " " + order.getdiscount() + " ");

for (int i = 0; order.records[i] != null; i++) {
int taste = order.records[i].taste;
if (dish.tasteA.equals("川菜")) {

switch (taste) {
case 0:
System.out.print(" 川菜 " + order.getSpicy() + " 不辣");
break;
case 1:
System.out.print(" 川菜 " + order.getSpicy() + " 微辣");
break;
case 2:
System.out.print(" 川菜 " + order.getSpicy() + " 稍辣");
break;
case 3:
System.out.print(" 川菜 " + order.getSpicy() + " 辣");
break;
case 4:
System.out.print(" 川菜 " + order.getSpicy() + " 很辣");
break;
case 5:
System.out.print(" 川菜 " + order.getSpicy() + " 爆辣");
break;
default:
break;
}
}
if (dish.tasteA.equals("晋菜")) {
switch (taste) {
case 0:
System.out.print(" 晋菜 " + order.getAcidity() + " 不酸");
break;
case 1:
System.out.print(" 晋菜 " + order.getAcidity() + " 微酸");
break;
case 2:
System.out.print(" 晋菜 " + order.getAcidity() + " 稍酸");
break;
case 3:
System.out.print(" 晋菜 " + order.getAcidity() + " 酸");
break;
case 4:
System.out.print(" 晋菜 " + order.getAcidity() + " 很酸");
break;
default:
break;
}
}
if (dish.tasteA.equals("浙菜")) {
switch (taste) {
case 0:
System.out.print(" 浙菜 " + order.getSweetness() + " 不甜");
break;
case 1:
System.out.print(" 浙菜 " + order.getSweetness() + " 微甜");
break;
case 2:
System.out.print(" 浙菜 " + order.getSweetness() + " 稍甜");
break;
case 3:
System.out.print(" 浙菜 " + order.getSweetness() + " 甜");
break;
default:
break;
}
}
System.out.println();
}
}
Manage manage = new Manage();
/*Guest guest = new Guest();
guest.money = order.getTotalPrice();
guest.phone = tables[tableNum-1].telenumber;
guest.name = tables[tableNum-1].name;*/
manage.show();
}
}
}

class Dish {// 菜品类
String name;// 菜品名称
String tasteA;//特色菜系名称
int unit_price; // 单价
int tasteNum;
char T;//特色菜
Dish() {

}
Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
Dish(String name,String tasteA,int unit_price){
this.name = name;
if(tasteA.equals("川菜")) {
this.tasteNum = 5;
}
if(tasteA.equals("晋菜")) {
this.tasteNum = 4;
}
if(tasteA.equals("浙菜")) {
this.tasteNum = 3;
}

this.unit_price = unit_price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnit_price() {
return unit_price;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
int getPrice(int portion) {// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
if (portion == 1) {
return unit_price;
} else if (portion == 2) {
return (int) Math.round(unit_price * 1.5);
} else
return unit_price * 2;
}
}


class Menu {
Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
Menu() {

}
Dish searchDish(String dishName) {// 根据菜名在菜谱中查找菜品信息,返回Dish对象
for(int i = 0;dishs[i]!=null;i++) {
if(dishName.equals(dishs[i].name)) {
return dishs[i];
}
}
return null;

}

Dish addDish(String dishName, int unit_price) {// 添加一道菜品信息
Dish dish = new Dish();
dish.name = dishName;
dish.unit_price = unit_price;
return dish;
}
Dish addDish(String dishName, String tastesA, int unit_price) {
Dish dish = new Dish();
dish.name = dishName;
dish.tasteA = tastesA;
dish.unit_price = unit_price;
return dish;
}
/*Dish addDish(String specilName) {
Dish dish = new Dish();
dish.name = specilName;
return dish;
}*/
}

class Record {// 点菜记录类
int orderNum;// 序号
Dish d = new Dish();// 菜品
int taste;//口味度
int portion;// 份额(1/2/3代表小/中/大份)

int num;//份数
boolean isDelete = false;
boolean isAdd = false;
Record() {

}
Record(int orderNum2, Dish d, int portion, int num) {
this.orderNum = orderNum2;
this.d = d;
this.portion = portion;
this.num = num;
}
Record(int orderNum2, Dish d,int taste, int portion, int num) {
this.orderNum = orderNum2;
this.d = d;
this.taste = taste;
this.portion = portion;
this.num = num;
}
public Dish getD() {
return d;
}
public void setD(Dish d) {
this.d = d;
}

public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getOrderNum() {
return this.orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public boolean isDelete() {
return isDelete;
}
public void setDelete(boolean delete) {
isDelete = delete;
}
public boolean isAdd() {
return isAdd;
}
public void setAdd(boolean add) {
isAdd = add;
}
public int getTaste() {
return taste;
}
public void setTaste(int taste) {
this.taste = taste;
}

public int getPrice() {// 计价,计算本条记录的价格
return d.getPrice(portion) * this.num;
}
}

class Order {// 订单类
/*Menu menu;*/
Table[] tables = new Table[10];
Record[] records = new Record[10];// 保存订单上每一道的记录
String dishName;
int count = 0;
int spicyTaste;//辣度
int acidityTaste;//酸度
int sweetnessTaste;//甜度
Order() {

}
public int getSpicy() {//获取平均辣度
int spicyTaste = 0;
int count = 0;
for(int i = 0;records[i]!=null;i++) {
if(records[i].d.tasteA.equals("川菜")) {
spicyTaste +=records[i].taste*records[i].num;
count +=records[i].num;
}

}
return Math.round(spicyTaste/count);
}
public int getAcidity() {//获取平均酸度
int acidityTaste = 0;
int count = 0;
for(int i = 0;records[i]!=null;i++) {
if(records[i].d.tasteA.equals("晋菜")) {
acidityTaste +=records[i].taste*records[i].num;
count +=records[i].num;
}
}
return Math.round(acidityTaste/count);
}
public int getSweetness() {
int sweetnessTaste = 0;
int count = 0;
for(int i = 0;records[i]!=null;i++) {

if(records[i].d.tasteA.equals("浙菜")) {
sweetnessTaste +=records[i].taste*records[i].num;
count +=records[i].num;
}
}
return Math.round(sweetnessTaste/count);
}
public int getTotalPrice() {// 计算订单的总价
int sum = 0,i;
for(i=0;records[i]!=null;i++) {
if (!records[i].isDelete() && records[i].isAdd()) {
sum += records[i].getPrice();
count++;
}
}
return sum;
}
public int getdiscount() {
// int sum;
Order order = new Order();
Table table = new Table();
if(table.getSale()==10) {
return order.getTotalPrice();
}
if(table.getSale() == 8) {
return (int) Math.round(order.getTotalPrice()*0.8);
}
if(table.getSale() == 6) {
return (int)Math.round(order.getTotalPrice()*0.6);
}
return 0;

}
Record addARecord(int orderNum, Dish dish, int portion, int num) {// 添加一条菜品信息到订单中

records[count] = new Record();
records[count].orderNum = orderNum;
records[count].d = dish;
records[count].portion = portion;
records[count].num = num;
count++;
return records[count];
}
void addARecord(int orderNum, Dish dish,int taste, int portion, int num) {
records[count] = new Record();
records[count].orderNum = orderNum;
records[count].d = dish;
records[count].taste = taste;
records[count].portion = portion;
records[count].num = num;
count++;
}
/*Record addARecord(int orderNum, String dishName,int taste, int portion, int num) {// 添加一条菜品信息到订单中
records[count] = new Record();
records[count].orderNum = orderNum;
records[count].d.name = (dishName);
records[count].taste = (taste);
records[count].portion = portion;
records[count].num = num;
count++;
return records[count];
Record record = new Record();
record.orderNum = orderNum;
record.d.name = dishName;
record.taste = taste;
record.portion = portion;
record.num = num;
count ++;
return record;
}*/

boolean delARecordByOrderNum(int orderNum) {// 根据序号删除一条记录
for(int i = 0;records[i]!=null;i++) {
if(records[i].orderNum==orderNum) {
return true;
}
}
return false;

}
Record findRecordByNum(int orderNum) {//根据序号查找一条记录
for(int i = 0;records[i]!=null;i++) {
if(records[i].orderNum==orderNum) {
return records[i];
}
}
return null;
}
}

class Table{//输入table类
int tableNum;
String name;
String telenumber;
String time;
String dayTime;
public int getTableNum() {
return tableNum;
}
public void setTableNum(int tableNum) {
this.tableNum = tableNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelenumber() {
return telenumber;
}
public void setTelenumber(String telenumber) {
this.telenumber = telenumber;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDayTime() {
return dayTime;
}
public void setDayTime(String dayTime) {
this.dayTime = dayTime;
}
private Calendar getTimeCalendar(String time) {
String[] arr = time.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE,Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
public int getSale() {
//if (time != null)
String[] dayArray = time.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
calendar.set(Calendar.YEAR,Integer.parseInt(dayArray[0]));
calendar.set(Calendar.MONTH, Integer.parseInt(dayArray[1]) - 1);
calendar.set(Calendar.DATE,Integer.parseInt(dayArray[2]));
// calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));

Calendar now = getTimeCalendar(dayTime);

int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;

if (weekDay == 0 || weekDay == 6) {//当日期为周末的时候
if (!now.before(getTimeCalendar("9/30/00")) && !now.after(getTimeCalendar("21/30/00"))) {
return 10;
}
else
return 0;
} else {
if (!now.before(getTimeCalendar("17/00/00")) && !now.after(getTimeCalendar("20/30/00"))) {
return 8;
}
else if (!now.before(getTimeCalendar("10/30/00")) && !now.after(getTimeCalendar("14/30/00"))) {
return 6;
}
else
return 0;
}
// 表示不营业
}
}
class Guest{
String name;
String phone;
int money = 0;
}
class Manage{
Guest[] guests = new Guest[10];

public void addGuest(Guest guest) {
if(searchGuest(guest.name) == null) {//没有该客户
Guest[] Guests = new Guest[guests.length +1];
for(int i=1;i<guests.length;i++) {
Guests[i] = guests[i];
}
Guests[Guests.length -1] = guest;
guests = Guests;
}
else {
for(int i=1;i<guests.length;i++) {
if(guests[i].name.matches(guest.name)) {
guests[i].money = guests[i].money + guest.money;
}
}
}
}
public void show() {
for(int i=1;i<guests.length;i++) {
for(int j=i;j<guests.length;j++) {
if(guests[j].name.compareToIgnoreCase(guests[j+1].name) > 0) {//j在后面
Guest guest;
guest = guests[j];
guests[j] = guests[j+1];
guests[j+1] = guest;
}
}
}
for(int i=1;i<guests.length;i++) {
System.out.println(guests[i].name +" "+ guests[i].phone +" "+ guests[i].money);
}
}
Guest searchGuest(String name) {
for(int i=1;i<guests.length;i++) {
if(name.matches(guests[i].name)) {
return guests[i];
}
}
return null;
}
}


类图:

 

类图展示的是各种类之间的关系,点菜记录之间的关系

复杂度图:

 

import java.io.UnsupportedEncodingException;
import java.text.Collator;
import java.util.*;


public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
HashMap<String, Student> students = new HashMap<>();
HashMap<String, Course> courses = new HashMap<>();
HashMap<String, Class> classes = new HashMap<>();
HashSet<ChoiceCourse> cc = new HashSet<>();
Grade grade = null;
int flag = 0;
while (true) {
String line = input.nextLine();
String[] strs = line.split(" ");
CheckInput checkInput = new CheckInput(line, strs);
if (line.equals("end")) {
break;
}
if (strs.length <= 3) {
if (checkInput.checkCourse()) {
courses.put(strs[0], new Course(strs[0], strs[1], strs[2]));
}
}
if (strs.length > 3) {
flag = 1;
if (checkInput.checkCourseGrade()) {
students.put(strs[0], new Student(strs[0], strs[1]));//存入学生的学号,姓名
courses.put(strs[2], new Course(strs[2]));//存入课程名称
classes.put(strs[0].substring(0,6),new Class(strs[0].substring(0,6)));
classes.get(strs[0].substring(0,6)).addStudent(students.get(strs[0]));
classes.get(strs[0].substring(0,6)).getClassGrade();
if (strs.length == 4) {
grade = new CheckGrade(Integer.parseInt(strs[3]));
}
if (strs.length == 5) {
grade = new TestGrade(Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
}
if(checkInput.isCount(courses)){
if(checkInput.containCourse(courses)) {//检查是否包含这门课程
ChoiceCourse choiceCourse = new ChoiceCourse(courses.get(strs[2]), students.get(strs[0]),grade );
cc.add(choiceCourse);
choiceCourse.setCourseGrade();
choiceCourse.setStudentGrade();

}
}
}

}
}
//对学生进行排序
List<Map.Entry<String, Student>> list = new ArrayList<>(students.entrySet());
list.sort(Map.Entry.comparingByKey());
ArrayList<String> score1 = new ArrayList<>();
//输出学生信息
for (int i = 0; i < students.size(); i++) {
Student s = list.get(i).getValue();
if (list.get(i).getValue().courseNumber != 0) //判断学生是否有成绩
System.out.println(s.number + " " + s.name + " " + (s.sumGrade / s.courseNumber));
else
System.out.println(s.number + " " + s.name + " " + "did not take any exams");
}
//输出单科成绩
for (Map.Entry<String, Course> entry : courses.entrySet()) {
Course course = entry.getValue();
if (flag == 1) //判断该科目下是否有学生成绩
score1.add(course.courseName + course.getGrade(cc) + " " + (course.sumGrade / course.studentNumber));
else
score1.add(course.courseName + " has no grades yet");
}
//对输出的课程信息进行排序

Collections.sort(score1, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
try {
String str1 = new String(o1.toString().getBytes("GB2312"), "ISO-8859-1");
String str2 = new String(o2.toString().getBytes("GB2312"), "ISO-8859-1");
return str1.compareTo(str2);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return 0;
}
});
for (String s : score1)
System.out.println(s);

ArrayList<String> score = new ArrayList<>();
//输出班级成绩
for (Map.Entry<String, Class> entry : classes.entrySet()) {
HashMap<String, Student> map = entry.getValue().map;
int count = 0;
for (Map.Entry<String, Student> sd : map.entrySet()) {
count += sd.getValue().courseNumber;
}
if (count == 0)
score.add(entry.getValue().classNum + " has no grades yet");
else
score.add(entry.getValue().classNum + " " + entry.getValue().getClassGrade());
}
Collections.sort(score, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
try {
String str1 = new String(o1.toString().getBytes("GB2312"), "ISO-8859-1");
String str2 = new String(o2.toString().getBytes("GB2312"), "ISO-8859-1");
return str1.compareTo(str2);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return 0;
}
});
for (String str : score)
System.out.println(str);
}
}
class CheckInput {
String line;
String[] strs;

public CheckInput(String line, String[] strs) {
this.line = line;
this.strs = strs;
}

public boolean checkCourse() {//检查输入的课程是否正确
String regax1 = "(\\S{1,10} 必修)|(\\S{1,10} 必修 考试)|(\\S{1,10} 选修 (考试|考察))";
if (line.matches(regax1)) {
return true;
} else if (line.matches("\\S{1,10} 必修 考察")) {
System.out.println(strs[0] + " : course type & access mode mismatch");
return false;
} else {
System.out.println("wrong format");
return false;
}
}

public boolean checkCourseGrade() {//检查输入的课程成绩是否正确
String regax = "((\\d{8} \\S{1,10} \\S{1,10} ([0-9]|[1-9][0-9]|100) ([0-9]|[1-9][0-9]|100))|(\\d{8} \\S{1,10} \\S{1,10} ([0-9]|[1-9][0-9]|100)))";
if (line.matches(regax)) {
return true;
} else {
System.out.println("wrong format");
return false;
}
}

public boolean containCourse(HashMap<String, Course> map) {//判断课程中是否存在此课程
if (map.containsKey(strs[2])) {
return true;
} else {
System.out.println(strs[2] + " does not exist");
return false;
}
}

public boolean isCount(HashMap<String, Course> map) {//输入的成绩数量和课程的考核方式是否匹配

if (map.get(strs[2]).equals("考察") && strs.length == 5 || map.get(strs[2]).equals("考试") && strs.length == 4) {
System.out.println(strs[0] + " " + strs[1] + " " + ": access mode mismatch");
return false;
}
return true;
}
}
class Student{
int courseNumber;//课程数量
String number;
String name;
int sumGrade;//学生总成绩

public Student() {
}

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

public String getNumber() {
return number;
}

public String getName() {
return name;
}

public void setNumber(String number) {
this.number = number;
}

public void setName(String name) {
this.name = name;
}
}
class Class{
String classNum;
HashMap<String,Student> map = new HashMap<>();
public Class(String classNum) {
this.classNum = classNum;
}

public String getClassNum() {
return classNum;
}

public void setClassNum(String classNum) {
this.classNum = classNum;
}
public int getClassGrade(){//计算班级平均成绩
double averageGrade = 0;
Collection<Student> value = map.values();
for(Student s : value){
if(s.courseNumber!=0){
averageGrade += s.sumGrade/s.courseNumber;
}

}
return (int) (averageGrade/ map.size());
}
public void addStudent(Student s){

map.put(s.number,s);
}
}
class Course{//课程
String courseName;//课程名字
String courseType;//课程性质
String courseWay;//考核方法
int sumGrade;//总成绩
int studentNumber;//学生数量

public Course(String courseName, String courseType, String courseWay) {
this.courseName = courseName;
this.courseType = courseType;
this.courseWay = courseWay;
}

public Course(String courseName) {
this.courseName = courseName;
}

public String getCourseName() {
return courseName;
}

public void setCourseName(String courseName) {
this.courseName = courseName;
}

public String getCourseType() {
return courseType;
}

public void setCourseType(String courseType) {
this.courseType = courseType;
}

public String getCourseWay() {
return courseWay;
}

public void setCourseWay(String courseWay) {
this.courseWay = courseWay;
}
public String getGrade(HashSet<ChoiceCourse> set){//计算学科平时成绩和期末成绩平均分
int usualGrade = 0,finalGrade = 0;
for(ChoiceCourse cc:set){
if(cc.grade instanceof TestGrade){
usualGrade+=((TestGrade) cc.grade).usualGrade;
finalGrade += ((TestGrade) cc.grade).finalGrade;
}
if(cc.grade instanceof CheckGrade){
finalGrade += ((CheckGrade) cc.grade).finalGrade;
}
}
if(usualGrade == 0){
return " "+finalGrade/studentNumber;
}
else
return " "+usualGrade/studentNumber+" "+finalGrade/studentNumber;
}
}
abstract class Grade{//成绩
TestGrade testGrade;
CheckGrade checkGrade;

public TestGrade getTestGrade() {
return testGrade;
}

public void setTestGrade(TestGrade testGrade) {
this.testGrade = testGrade;
}

public CheckGrade getCheckGrade() {
return checkGrade;
}

public void setCheckGrade(CheckGrade checkGrade) {
this.checkGrade = checkGrade;
}

abstract int getAllGrade();
}
class TestGrade extends Grade{//考试成绩
int usualGrade;
int finalGrade;

public TestGrade(int usualGrade, int finalGrade) {
this.usualGrade = usualGrade;
this.finalGrade = finalGrade;
}
@Override
int getAllGrade() {
return (int)(usualGrade*0.3+finalGrade*0.7);
}
}
class CheckGrade extends Grade{//考察成绩
int finalGrade;

public CheckGrade(int finalGrade) {
this.finalGrade = finalGrade;
}

@Override
int getAllGrade() {
return (int)(finalGrade);
}//考查成绩

}
class ChoiceCourse{//选课类
Course course;
Student student;
Grade grade;

public ChoiceCourse(Course course, Student student, Grade grade) {
this.course = course;
this.student = student;
this.grade = grade;
}
public void setStudentGrade(){//将每科的成绩添加至学生总成绩中
student.sumGrade+=grade.getAllGrade();
student.courseNumber++;
}
public void setCourseGrade(){//获得单科总成绩
course.sumGrade+=grade.getAllGrade();
course.studentNumber++;
}

}

 

 

 

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Map<String,Integer> map = new HashMap<String,Integer>();
String[] keyWord = {"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"};
StringBuilder a = new StringBuilder();
String line = input.nextLine();
int flag = 0;
while(!line.equals("exit")){
// a.append(line.replaceAll("//.*"," ").replaceAll("\".*\""," "));
a.append(line.replaceAll("//.*","\\s+").replaceAll("\".*?\"","\\s+"));//去掉注释及双引号后面的内容

line = input.nextLine();
flag = 1;
}
if(flag == 0){
System.out.println("Wrong Format");
}
String s = a.toString();
s = s.replace("\\[\\]","\\s+");
s = s.replace(",","\\s+");
String b = s.replaceAll("/\\*.*?\\*/", "\\s+");//去掉/* */里的内容
for(int i = 0;i< keyWord.length;i++){
Pattern pattern = Pattern.compile("\\b"+keyWord[i]+"\\b");//创建关键词的正则表达式
Matcher matcher = pattern.matcher(b);//字符串与关键词匹配
int n = 0;
while(matcher.find()){//找到关键字,次数加一
n++;
}
if(n != 0){
map.put(keyWord[i],n);
}
}
Object[] arr = map.keySet().toArray();
Arrays.sort(arr);
for (Object k : arr) {
System.out.println(map.get(k) + "\t" + k);
}
}
}

关键字中需要用到的是Hashset来进行查找

 

 

(3)踩坑心得:

菜单的题对我来说还是挺困难的,一直做不出来,做了这么久也只得了6分;

 

 

 这个期末考试的测试相对于菜单来说还是计较好写的,能够做出的测试点挺多的,但是还是有点小问题,不知道怎么过;

 

 第二次期末考试的题相对来说比第一次做的更加顺手,得的分也比第一次高;

 

 

(4)改进建议:

 期末考试成绩统计的改进建议:

主要目的是管理学生、课程和班级的成绩信息,并进行相应的统计和输出。以下是对该代码的一些建议改进:

优化代码结构:建议将代码按照功能进行模块化,例如创建不同的类文件来管理学生、课程和班级的数据,并使用合适的方法和属性来操作和存储数据。这样可以提高代码的可读性和可维护性。使用面向对象的设计原则:通过使用类、对象、继承和多态等特性,可以更好地组织和管理代码。例如可以创建学生、课程和班级的类,并使用它们的对象来表示相应的数据和行为。使用更具有语义化的变量和方法名:使用更加清晰和易于理解的变量和方法名可以提高代码的可读性和可维护性。例如,可以将变量名改为具有描述性的名字,以更好地反映其用途。同时,建议使用驼峰命名法来命名变量和方法。引入异常处理机制:在代码中引入适当的异常处理机制,以捕获可能出现的错误并采取相应的处理措施。这有助于增强代码的健壮性和容错性。使用更加简洁和高效的数据结构和算法:根据具体的需求,选择合适的数据结构和算法来管理和操作数据。例如,可能可以使用List替代HashMap来存储学生和课程数据,以便更方便地进行排序和遍历。添加注释和文档说明:为代码添加适当的注释和文档说明,以帮助其他开发人员理解代码的逻辑和功能。

(5)总结:

 

通过这几次测试样例,学会了使用正则表达式来检查输入信息的格式是否符合要求,对于不符合要求的格式给出错误提示。
数据存储和管理:代码使用HashMap和ArrayList等数据结构来存储和管理学生、课程、班级等实体的信息,通过键值对的方式进行数据的存取和查找。

如何设计和实现一个简单的学生成绩管理系统:这段代码展示了如何通过合理的类设计和数据结构选择来实现一个学生成绩管理系统,并通过输入、处理和展示信息的方式完成相应的功能。
正则表达式的应用:代码中使用了正则表达式来检查输入信息的格式是否符合要求,这让我意识到正则表达式在处理字符串匹配和验证方面的重要性。
数据结构的选择和使用:代码中使用了HashMap和ArrayList等数据结构来存储和管理数据,这让我了解了不同数据结构的特点和使用场景,以及如何通过合适的数据结构来提高代码的效率和可读性。
错误处理和异常处理:代码中对输入信息的格式错误进行了处理,并给出相应的错误提示,这让我意识到在实际开发中,对错误和异常的处理是非常重要的,可以提高代码的健壮性和用户体验。
需要进一步学习和研究的地方包括:
设计模式和架构:学习如何使用设计模式和良好的架构来设计和实现更复杂的系统,提高代码的可维护性和可扩展性。
数据库和持久化:了解如何使用数据库来存储和管理大量的数据,并学习数据库相关的知识和技术,如SQL语言、ORM框架等。
用户界面和交互设计:学习如何设计和实现用户友好的界面和交互,提高系统的易用性和用户体验。
单元测试和自动化测试:了解如何编写有效的测试用例并进行单元测试和自动化测试,提高代码的质量和可靠性。

对老师没有意见,特别好;

 

posted @ 2023-06-27 23:54  菜要多练丫  阅读(6)  评论(0编辑  收藏  举报