FC第二次博客作业

Posted Felix-C

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FC第二次博客作业相关的知识,希望对你有一定的参考价值。

一、前言

oop训练集四中主要考察了字符串的操作,array数组,排序方法,一些自带方法,封装等。

oop训练集五中主要考察了正则表达式,字符串的基本操作和面对对象中的聚合等。

oop训练集六中综合考察了面对对象编程和字符串处理等能力。

 

二、设计与分析

1、oop4中的7-1

菜单计价程序-3

这道题当时我没有做出来,我认为有以下原因:

1、没有认真仔细的阅读题目要求,很多细节出错导致程序运行出错并且找不到错误所在

2、没有成熟的字符串处理方式,算法累赘

之后的学习我会注重这两方面,冷静阅读题目要求,增强字符串的处理能力

 

2、oop5中7-5

日期问题面向对象设计(聚合一)

题目要求如下:

 这道题的求n天的几个方法在前面的oop训练集中写过,这里不做赘述,在前一次blog中有分析过,这次题目主要考察聚合关系,在DateUtil类中new Day类,在Day类中new Month类,在Month类中new Year类,在每个类中写相应的需要的方法,在DateUtil中用方法实现功能。

代码如下:

 

import java.util.*;

public class Main
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
int year = 0;
int month = 0;
int day = 0;
if(choice==1) // test getNextNDays method
int m = 0;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
DateUtil date = new DateUtil(year, month, day);
DateUtil dateF = new DateUtil(year, month, day);
if (!date.checkInputValidity())
System.out.println("Wrong Format");
System.exit(0);

m = sc.nextInt();
if (m < 0)
System.out.println("Wrong Format");
System.exit(0);

if(m==0)
System.out.println(dateF.showDate()+" next "+m+" days is:"+dateF.showDate());
System.exit(0);

DateUtil dateNextN = date.getNextNDays(m);
System.out.println(dateF.showDate()+" next "+m+" days is:"+dateNextN.showDate());
else if (choice==2)
int m = 0;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
DateUtil date = new DateUtil(year, month, day);
DateUtil dateF = new DateUtil(year, month, day);
if (!date.checkInputValidity())
System.out.println("Wrong Format");
System.exit(0);

m = sc.nextInt();
if (m < 0)
System.out.println("Wrong Format");
System.exit(0);

if(m==0)
System.out.println(dateF.showDate()+" next "+m+" days is:"+dateF.showDate());
System.exit(0);

DateUtil datePreviousN = date.getPreviousNDays(m);
System.out.println(dateF.showDate()+" previous "+m+" days is:"+datePreviousN.showDate());
else if (choice==3)
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
DateUtil date = new DateUtil(year, month, day);
int year1 = sc.nextInt();
int month1 = sc.nextInt();
int day1 = sc.nextInt();
DateUtil date1 = new DateUtil(year1, month1, day1);
if (date.checkInputValidity() && date1.checkInputValidity())
int n = date.getDaysofDates(date1);
System.out.println("The days between "+date.showDate()+" and "+date1.showDate()+" are:"+n);
else
System.out.println("Wrong Format");
System.exit(0);

else
System.out.println("Wrong Format");
System.exit(0);



class Day
private int value;

public Day()

public Day(int value)
this.value = value;

public int getValue()
return this.value;

public void setValue(int value)
this.value = value;

public void dayIncrement()
this.value+=1;

public void dayReduction()
this.value-=1;

class Year
private int value;

public Year(int value)
this.value = value;

public int getValue()
return this.value;

public void setValue(int value)
this.value = value;

public boolean isLeapYear()
if(this.value%400==0||(this.value%4==0&&this.value%100!=0))
return true;
else
return false;

public boolean validate()
if(this.value<1820||this.value>2020)
return false;
else
return true;

public void yearIncrement()
this.value+=1;

public void yearReduction()
this.value-=1;

class Month
private int value;

public Month(int value)
this.value = value;

public int getValue()
return this.value;

public void setValue(int value)
this.value = value;

public void resetMin()
this.value = 1;

public void resetMax()
this.value = 12;

public boolean validate()
if(this.value<1||this.value>12)
return false;
else
return true;

public void monthIncrement()
this.value+=1;

public void monthReduction()
this.value-=1;

class DateUtil
private Year year;
private Month month;
private Day day;
private int[] mon_maxnum = new int[]31,28,31,30,31,30,31,31,30,31,30,31;

public DateUtil(int y,int m,int d)
year = new Year(y);
month = new Month(m);
day = new Day(d);

public Year getYear()
return this.year;

public void setYear(Year year)
this.year = year;

public Month getMonth()
return this.month;

public void setMonth(Month month)
this.month = month;

public Day getDay()
return this.day;

public void setDay(Day day)
this.day = day;

public void setDayMin()
this.day.setValue(1);

public void setDayMax()
this.day.setValue(12);

public boolean checkInputValidity()
if(this.year.isLeapYear())
mon_maxnum[1]=29;
else
mon_maxnum[1]=28;

if (this.year.validate() && this.month.validate())
if (this.day.getValue() >= 0 && this.day.getValue() <= mon_maxnum[this.month.getValue() - 1])
return true;
else
return false;

else
return false;

public DateUtil getNextNDays(int n)
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

if ((long)n + this.day.getValue() <= mon_maxnum[this.month.getValue() - 1])
this.day.setValue(this.day.getValue() + n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;
else
while ((long)n + this.day.getValue() > mon_maxnum[this.month.getValue() - 1])
n = n - mon_maxnum[this.month.getValue() - 1] + this.day.getValue();
this.day.setValue(0);
if (this.month.getValue() == 12)
this.year.setValue(this.year.getValue() + 1);
this.month.resetMin();
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

else
this.month.setValue(this.month.getValue() + 1);


this.day.setValue(n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;

public DateUtil getPreviousNDays(int n)
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

if (this.day.getValue() - (long)n> 0)
this.day.setValue(this.day.getValue() - n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;
else
while (this.day.getValue() - (long)n <= 0)
n = n - this.day.getValue();
if (this.month.getValue() != 1)
this.month.setValue(this.month.getValue() - 1);
this.day.setValue(mon_maxnum[this.month.getValue() - 1]);
else
this.month.resetMax();
this.year.setValue(this.year.getValue() - 1);
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

this.day.setValue(mon_maxnum[this.month.getValue() - 1]);


this.day.setValue(this.day.getValue() - n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;

public boolean compareDates(DateUtil date)
if (this.year.getValue() > date.year.getValue())
return true;
else if (this.year.getValue() < date.year.getValue())
return false;
else
if (this.month.getValue() > date.month.getValue())
return true;
else if (this.month.getValue() < date.month.getValue())
return false;
else
if (this.day.getValue() > date.day.getValue())
return true;
else
return false;



public boolean equalTwoDates(DateUtil date)
if(this.day.getValue()==date.day.getValue()&&this.month.getValue()==date.month.getValue()&&this.year.getValue()==date.year.getValue())
return true;
else
return false;

public int getDaysofDates(DateUtil date)
DateUtil young;
DateUtil old;
if(this.compareDates(date))
young = new DateUtil(date.year.getValue(),date.month.getValue(),date.day.getValue());
old = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
else
young = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
old = new DateUtil(date.year.getValue(),date.month.getValue(),date.day.getValue());

int days = 0;
for (int y = young.year.getValue(); y < old.year.getValue(); y++)
if (y%400==0||(y%4==0&&y%100!=0))
days += 366;
else
days += 365;


for(int i = 0;i<old.month.getValue()-1;i++)
if(old.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

days+=mon_maxnum[i];

days+=old.day.getValue();
for(int i = 0;i<young.month.getValue()-1;i++)
if(young.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

days-=mon_maxnum[i];

days-=young.day.getValue();
return days;

public String showDate()
String str = String.valueOf(this.year.getValue()+"-"+this.month.getValue()+"-"+this.day.getValue());
return str;

 

3、oop5中7-6

日期问题面向对象设计(聚合二)

题目要求如下:

 

 分析:这题与上题不同的地方在于Year、Month和Day之间关系减弱,在DateUtil中调用所有类并实现所需功能,这样做简单方便,容易后期修改。

代码如下:


import java.util.*;

public class Main
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
int year = 0;
int month = 0;
int day = 0;
if(choice==1) // test getNextNDays method
int m = 0;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
DateUtil date = new DateUtil(year, month, day);
DateUtil dateF = new DateUtil(year, month, day);
if (!date.checkInputValidity())
System.out.println("Wrong Format");
System.exit(0);

m = sc.nextInt();
if (m < 0)
System.out.println("Wrong Format");
System.exit(0);

if(m==0)
System.out.println(dateF.showDate()+" next "+m+" days is:"+dateF.showDate());
System.exit(0);

DateUtil dateNextN = date.getNextNDays(m);
System.out.println(dateF.showDate()+" next "+m+" days is:"+dateNextN.showDate());
else if (choice==2)
int m = 0;
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
DateUtil date = new DateUtil(year, month, day);
DateUtil dateF = new DateUtil(year, month, day);
if (!date.checkInputValidity())
System.out.println("Wrong Format");
System.exit(0);

m = sc.nextInt();
if (m < 0)
System.out.println("Wrong Format");
System.exit(0);

if(m==0)
System.out.println(dateF.showDate()+" next "+m+" days is:"+dateF.showDate());
System.exit(0);

DateUtil datePreviousN = date.getPreviousNDays(m);
System.out.println(dateF.showDate()+" previous "+m+" days is:"+datePreviousN.showDate());
else if (choice==3)
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
DateUtil date = new DateUtil(year, month, day);
int year1 = sc.nextInt();
int month1 = sc.nextInt();
int day1 = sc.nextInt();
DateUtil date1 = new DateUtil(year1, month1, day1);
if (date.checkInputValidity() && date1.checkInputValidity())
int n = date.getDaysofDates(date1);
System.out.println("The days between "+date.showDate()+" and "+date1.showDate()+" are:"+n);
else
System.out.println("Wrong Format");
System.exit(0);

else
System.out.println("Wrong Format");
System.exit(0);





class Day
private int value;

public Day()


public Day(int value)
this.value = value;


public int getValue()
return this.value;


public void setValue(int value)
this.value = value;


public void dayIncrement()
this.value+=1;


public void dayReduction()
this.value-=1;



class Year
private int value;

public Year(int value)
this.value = value;


public int getValue()
return this.value;


public void setValue(int value)
this.value = value;


public boolean isLeapYear()
if(this.value%400==0||(this.value%4==0&&this.value%100!=0))
return true;
else
return false;



public boolean validate()
if(this.value<1820||this.value>2020)
return false;
else
return true;



public void yearIncrement()
this.value+=1;


public void yearReduction()
this.value-=1;



class Month
private int value;

public Month(int value)
this.value = value;


public int getValue()
return this.value;


public void setValue(int value)
this.value = value;


public void resetMin()
this.value = 1;


public void resetMax()
this.value = 12;


public boolean validate()
if(this.value<1||this.value>12)
return false;
else
return true;



public void monthIncrement()
this.value+=1;


public void monthReduction()
this.value-=1;



class DateUtil
private Year year;
private Month month;
private Day day;
private int[] mon_maxnum = new int[]31,28,31,30,31,30,31,31,30,31,30,31;

public DateUtil(int y,int m,int d)
year = new Year(y);
month = new Month(m);
day = new Day(d);


public Year getYear()
return this.year;


public void setYear(Year year)
this.year = year;


public Month getMonth()
return this.month;


public void setMonth(Month month)
this.month = month;


public Day getDay()
return this.day;


public void setDay(Day day)
this.day = day;


public void setDayMin()
this.day.setValue(1);


public void setDayMax()
this.day.setValue(12);


public boolean checkInputValidity()
if(this.year.isLeapYear())
mon_maxnum[1]=29;
else
mon_maxnum[1]=28;

if (this.year.validate() && this.month.validate())
if (this.day.getValue() >= 0 && this.day.getValue() <= mon_maxnum[this.month.getValue() - 1])
return true;
else
return false;

else
return false;



public DateUtil getNextNDays(int n)
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;


if ((long)n + this.day.getValue() <= mon_maxnum[this.month.getValue() - 1])
this.day.setValue(this.day.getValue() + n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;
else
while ((long)n + this.day.getValue() > mon_maxnum[this.month.getValue() - 1])
n = n - mon_maxnum[this.month.getValue() - 1] + this.day.getValue();
this.day.setValue(0);
if (this.month.getValue() == 12)
this.year.setValue(this.year.getValue() + 1);
this.month.resetMin();
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

else
this.month.setValue(this.month.getValue() + 1);


this.day.setValue(n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;



public DateUtil getPreviousNDays(int n)
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;


if (this.day.getValue() - (long)n> 0)
this.day.setValue(this.day.getValue() - n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;
else
while (this.day.getValue() - (long)n <= 0)
n = n - this.day.getValue();
if (this.month.getValue() != 1)
this.month.setValue(this.month.getValue() - 1);
this.day.setValue(mon_maxnum[this.month.getValue() - 1]);
else
this.month.resetMax();
this.year.setValue(this.year.getValue() - 1);
if (this.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

this.day.setValue(mon_maxnum[this.month.getValue() - 1]);


this.day.setValue(this.day.getValue() - n);
DateUtil dateUtil = new DateUtil(this.year.getValue(), this.month.getValue(), this.day.getValue());
return dateUtil;



public boolean compareDates(DateUtil date)
if (this.year.getValue() > date.year.getValue())
return true;
else if (this.year.getValue() < date.year.getValue())
return false;
else
if (this.month.getValue() > date.month.getValue())
return true;
else if (this.month.getValue() < date.month.getValue())
return false;
else
if (this.day.getValue() > date.day.getValue())
return true;
else
return false;





public boolean equalTwoDates(DateUtil date)
if(this.day.getValue()==date.day.getValue()&&this.month.getValue()==date.month.getValue()&&this.year.getValue()==date.year.getValue())
return true;
else
return false;



public int getDaysofDates(DateUtil date)
DateUtil young;
DateUtil old;
if(this.compareDates(date))
young = new DateUtil(date.year.getValue(),date.month.getValue(),date.day.getValue());
old = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
else
young = new DateUtil(this.year.getValue(),this.month.getValue(),this.day.getValue());
old = new DateUtil(date.year.getValue(),date.month.getValue(),date.day.getValue());

int days = 0;
for (int y = young.year.getValue(); y < old.year.getValue(); y++)
if (y%400==0||(y%4==0&&y%100!=0))
days += 366;
else
days += 365;


for(int i = 0;i<old.month.getValue()-1;i++)
if(old.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

days+=mon_maxnum[i];

days+=old.day.getValue();
for(int i = 0;i<young.month.getValue()-1;i++)
if(young.year.isLeapYear())
mon_maxnum[1] = 29;
else
mon_maxnum[1] = 28;

days-=mon_maxnum[i];

days-=young.day.getValue();
return days;


public String showDate()
String str = String.valueOf(this.year.getValue()+"-"+this.month.getValue()+"-"+this.day.getValue());
return str;

 

4、oop6中7-1

菜单计价程序-4

分析:在这道题中,我认为最主要考察的就是处理字符串的能力。在我的思路中,我认为首先要利用ArrayList将所有用户输入的字符串储存起来,再使用while循环一个一个处理,而在处理的时候,字符串是有固定格式进行输入,否则报错,因此我使用正则表达式进行字符串的分类处理。我认为这题需要注意的细节有:在删除订单的时候,不能覆盖删除,应该将订单中的num数量改为0,这样能在之后重复删除检测出来,同时不影响计算金额;在有两桌时,若第一桌桌号字符串输入错误,到第二桌号之前的所有字符串无效,这时我使用了recordRight进行判断,并在while循环的第一句中加入该判断,使得中间部分无效字符串不进行处理;在时间处理中会用到星期,而我使用了Calendar类进行时间处理,并判断星期。

我的不足:我没有写出多个桌号的相关代码,我当时没有想出用hashmap进行桌号和时间的同时存储,代码不够精炼,算法不够成熟,有很多累赘。

代码如下:

 

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

public class Main
public static void main(String[] args)
Scanner sc = new Scanner(System.in);

ArrayList<String> allLine = new ArrayList<>(); //存储所有输入语句
while (true)
String line = sc.nextLine();
if (line.equals("end"))
break;

allLine.add(line);

Dish[] dishes = new Dish[0];
Menu menu = new Menu(dishes);

Record[] records = new Record[0];
Order order = new Order(records);

String table = new String();

int tableIndex = 0; //储存桌数
int recordIndex = 0; //储存订单数
boolean menuRight = true;
boolean recordRight = true;

for (String str : allLine)
if (!str.matches("^table \\\\d+ \\\\d4/\\\\d1,2/\\\\d1,2 \\\\d1,2/\\\\d1,2/\\\\d1,2$") && !recordRight)
continue;

//处理菜品标识格式: 麻婆豆腐 12 T||null
if (str.matches("^[\\\\u4E00-\\\\u9FFF]+ [1-9]\\\\d* T$"))
if (menuRight)
String[] segments = str.split(" ");
if (Integer.parseInt(segments[1]) <= 0 || Integer.parseInt(segments[1]) >= 300)
System.out.println(segments[0] + " price out of range " + segments[1]);
continue;

menu.addDish(segments[0], Integer.parseInt(segments[1]), \'T\');
else
System.out.println("invalid dish");

else if (str.matches("^[\\\\u4E00-\\\\u9FFF]+ [1-9]\\\\d*$"))
if (menuRight)
String[] segments = str.split(" ");
if (Integer.parseInt(segments[1]) < 0 || Integer.parseInt(segments[1]) > 300)
System.out.println(segments[0] + " price out of range " + segments[1]);
continue;

Dish dish = menu.addDish(segments[0], Integer.parseInt(segments[1]), \'F\');
else
System.out.println("invalid dish");


//处理桌号标识格式:table 55 2023/3/31 12/00/00
else if (str.matches("^table [1-9]\\\\d* \\\\d4/\\\\d1,2/\\\\d1,2 \\\\d1,2/\\\\d1,2/\\\\d1,2$"))
if (tableIndex != 0)
String[] segments = table.split(" ");
System.out.println("table " + segments[0] + ": " + order.getTotalPrice() + " " + order.lastPrice(segments[1], segments[2]));

recordRight = true;
menuRight = false;
String[] segments = str.split(" ");

if(Integer.parseInt(segments[1])<1||Integer.parseInt(segments[1])>55)
System.out.println(segments[1]+" table num out of range");
recordRight = false;
continue;

if (!isDateDayRight(segments[2], segments[3]))
System.out.println(segments[1] + " date error");
recordRight = false;
continue;

if (!isDateRight(segments[2]))
System.out.println("not a valid time period");
recordRight = false;
continue;

if (!isInRun(segments[2], segments[3]))
System.out.println("table " + segments[1] + " out of opening hours");
recordRight = false;
continue;

if (recordRight)
table = String.valueOf(segments[1] + " " + segments[2] + " " + segments[3]);
tableIndex++;
System.out.println("table " + segments[1] + ": ");

else if (!str.matches("^table \\\\d+ \\\\d4/\\\\d1,2/\\\\d1,2 \\\\d1,2/\\\\d1,2/\\\\d1,2$") && str.startsWith("table"))
System.out.println("wrong format");
recordRight = false;

// 处理点菜记录格式:1 麻婆豆腐 1 1 0-9\\\\d*
else if (str.matches("^\\\\d+ [\\\\u4E00-\\\\u9FFF]+ \\\\d [1-9]\\\\d*$"))
String[] segments = str.split(" ");
if (Integer.parseInt(segments[0]) <= recordIndex)
System.out.println("record serial number sequence error");
else
boolean rightLine = true;
if (menu.searchDish(segments[1]) != null)
if (Integer.parseInt(segments[2]) > 3)
System.out.println(segments[0] + " portion out of range " + segments[2]);
rightLine = false;
recordIndex++;
continue;

if (Integer.parseInt(segments[3]) > 15)
System.out.println(segments[0] + " num out of range " + segments[3]);
rightLine = false;
recordIndex++;
continue;

if (rightLine)
Record record = order.addARecord(Integer.parseInt(segments[0]), segments[1], Integer.parseInt(segments[2]), Integer.parseInt(segments[3]), menu);
System.out.println(segments[0] + " " + segments[1] + " " + record.getPrice());
recordIndex++;

else
System.out.println(segments[1] + " does not exist");



// 处理删除记录格式:2 delete
else if (str.matches("^\\\\d+ delete$"))
String[] segments = str.split(" ");
boolean rightDNum = false;
if (Integer.parseInt(segments[0]) <= recordIndex)
for (int i = 0; i < order.records.length; i++)
if (Integer.parseInt(segments[0]) == order.records[i].orderNum)
if (order.records[i].num != 0)
order.delARecordByOrderNum(Integer.parseInt(segments[0]));

else
System.out.println("deduplication " + segments[0]);

rightDNum = true;


if (!rightDNum)
System.out.println("delete error");

else
System.out.println("wrong format");

// 处理代点菜信息格式:桌号+ +序号+ +菜品名称+ +份额+ +分数
else if (str.matches("^\\\\d+ \\\\d+ [\\\\u4E00-\\\\u9FFF]+ \\\\d [1-9]\\\\d*$"))
String[] segments = str.split(" ");
if (Integer.parseInt(segments[1]) <= recordIndex)
System.out.println("record serial number sequence error");
else
boolean rightLine = true;
if (menu.searchDish(segments[2]) != null)
if (Integer.parseInt(segments[3]) > 3)
System.out.println(segments[1] + " portion out of range " + segments[3]);
rightLine = false;
recordIndex++;

if (menu.searchDish(segments[2]).special == \'T\' && Integer.parseInt(segments[3]) == 2)
System.out.println(segments[1] + " portion out of range " + segments[3]);
rightLine = false;
recordIndex++;

if (Integer.parseInt(segments[4]) > 15)
System.out.println(segments[1] + " num out of range " + segments[4]);
rightLine = false;
recordIndex++;

if (rightLine)
String tableN = table.split(" ")[0];
Record record = order.addARecord(Integer.parseInt(segments[1]), segments[2], Integer.parseInt(segments[3]), Integer.parseInt(segments[4]), menu);
System.out.println(segments[1] + " table " + tableN + " pay for table " + segments[0] + " " + record.getPrice()); //4 table 2 pay for table 1 12
recordIndex++;

else
System.out.println(segments[2] + " does not exist");



//输入错误
else
System.out.println("wrong format");


if (recordRight)
String[] segments = table.split(" ");
System.out.println("table " + segments[0] + ": " + order.getTotalPrice() + " " + order.lastPrice(segments[1], segments[2]));


public static boolean isDateRight(String date)
String[] segments = date.split("/");
if (Integer.parseInt(segments[0]) >= 2022 && Integer.parseInt(segments[0]) <= 2023 && Integer.parseInt(segments[1]) >= 1 && Integer.parseInt(segments[1]) <= 12 && isDayRight(Integer.parseInt(segments[1]), Integer.parseInt(segments[2])))
return true;
else
return false;

public static boolean isDayRight(int month, int day)
int[] mon_max = new int[]0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31;
if (day > mon_max[month] || day < 1)
return false;
else
return true;

public static boolean isDateDayRight(String date, String time)
String[] segments1 = date.split("/");

if (Integer.parseInt(segments1[1]) > 12 || Integer.parseInt(segments1[1]) < 1 || !isDayRight(Integer.parseInt(segments1[1]), Integer.parseInt(segments1[2])))
return false;

if (!time.matches("^([01]?[0-9]|2[0-3])/([0-5]?[0-9])/([0-5]?[0-9])$"))
return false;

return true;

public static int weekNum(String date)
String[] segments = date.split("/");
int year = Integer.parseInt(segments[0]);
int month = Integer.parseInt(segments[1]);
int day = Integer.parseInt(segments[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
return dayOfWeek;

public static boolean isInRun(String date, String time)
int weekNum = weekNum(date);
String[] segments = time.split("/");
int timeNow = Integer.parseInt(segments[0]) * 60 * 60 + Integer.parseInt(segments[1]) * 60 + Integer.parseInt(segments[2]);
int time11 = 17 * 60 * 60;
int time12 = 20 * 3600 + 30 * 60;
int time21 = 10 * 3600 + 30 * 60;
int time22 = 14 * 3600 + 30 * 60;
int time31 = 9 * 3600 + 30 * 60;
int time32 = 21 * 3600 + 30 * 60;
if (weekNum > 1 && weekNum < 7)
if ((timeNow >= time11 && timeNow <= time12) || (timeNow >= time21 && timeNow <= time22))
return true;
else
return false;

else
if (timeNow >= time31 && timeNow <= time32)
return true;
else
return false;



class Dish
String name;
int unit_price;
char special;

public Dish(String name, int unit_price, char special)
this.name = name;
this.unit_price = unit_price;
this.special = special;

public String getName()
return this.name;

public void setName(String name)
this.name = name;

public int getUnit_price()
return this.unit_price;

public void setUnit_price(int unit_price)
this.unit_price = unit_price;

public int getPrice(int portion) //return 0 则为无匹配对象
switch (portion)
case 1:
return this.unit_price;

case 2:
return (int) (this.unit_price * 1.5 + 0.5);

case 3:
return (int) (this.unit_price * 2);


return 0;

class Menu
Dish[] dish;

public Menu(Dish[] dish)
this.dish = dish;

public Dish searchDish(String dishName) //return null 则为无匹配对象
for (int i = 0; i < this.dish.length; i++)
if (this.dish[i].name.equals(dishName))
return this.dish[i];


return null;

public Dish addDish(String dishName, int unit_price, char special)
Dish newDish = new Dish(dishName, unit_price, special);
if (searchDish(dishName) != null)
for (int i = 0; i < this.dish.length; i++)
if (this.dish[i].name.equals(dishName))
this.dish[i].special = special;
this.dish[i].unit_price = unit_price;


return newDish;
else
Dish[] newDishArray = new Dish[this.dish.length + 1];
for (int i = 0; i < this.dish.length; i++)
newDishArray[i] = this.dish[i];

newDishArray[newDishArray.length - 1] = newDish;
this.dish = newDishArray;
return newDish;


class Record
int orderNum;
Dish d;
int portion;
int num;

public Record(int orderNum, Dish d, int portion, int num)
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = num;

public int getPrice()
return this.d.getPrice(this.portion) * this.num;

class Order
Record[] records;

public Order(Record[] records)
this.records = records;

public int getTotalPrice()
int totalPrice = 0;
for (int i = 0; i < this.records.length; i++)
totalPrice += this.records[i].getPrice();

return totalPrice;

public Record addARecord(int orderNum, String dishName, int portion, int num, Menu menu)
Dish d = menu.searchDish(dishName);
Record newRecord = new Record(orderNum, d, portion, num);
Record[] newRecordsArray = new Record[this.records.length + 1];
for (int i = 0; i < this.records.length; i++)
newRecordsArray[i] = this.records[i];

newRecordsArray[newRecordsArray.length - 1] = newRecord;
this.records = newRecordsArray;
return newRecord;

public void delARecordByOrderNum(int orderNum)
Record[] newRecordsArray = new Record[this.records.length];
for (int i = 0, j = 0; i < this.records.length; i++, j++)
if (this.records[i].orderNum == orderNum)
this.records[i].num = 0;

newRecordsArray[j] = this.records[i];

this.records = newRecordsArray;

public void findRecordByNum(int orderNum)
if (orderNum < this.records.length && orderNum >= 0)
System.out.println(this.records[orderNum].orderNum + this.records[orderNum].d.name + "portion:" + this.records[orderNum].portion + "num:" + this.records[orderNum].num);
else
System.out.println("Wrong OrderNum!");

public int lastPrice(String date, String time)
int total = 0;
String[] date1 = date.split("/");
int year = Integer.parseInt(date1[0]);
int month = Integer.parseInt(date1[1]);
int day = Integer.parseInt(date1[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String[] time1 = time.split("/");
int timeNow = Integer.parseInt(time1[0]) * 60 * 60 + Integer.parseInt(time1[1]) * 60 + Integer.parseInt(time1[2]);
int timeNight1 = 17 * 60 * 60;
int timeNight2 = 20 * 60 * 60 + 30 * 60;
int timeNoon1 = 10 * 60 * 60 + 30 * 60;
int timeNoon2 = 14 * 60 * 60 + 30 * 60;

for (int i = 0; i < this.records.length; i++)
if (this.records[i].d.special == \'T\' && dayOfWeek > 1 && dayOfWeek < 7)
total += (int) (this.records[i].getPrice() * 0.7 + 0.5);
else
if (dayOfWeek > 1 && dayOfWeek < 7 && timeNow >= timeNight1 && timeNow <= timeNight2)
total += this.records[i].getPrice() * 0.8;
else if (dayOfWeek > 1 && dayOfWeek < 7 && timeNow >= timeNoon1 && timeNow <= timeNoon2)
total += this.records[i].getPrice() * 0.6;
else
total += this.records[i].getPrice();



return total;

 

 

 

 

三、踩坑心得

 1、粗心大意,没有注意细节,oop6中错误输出

 next应为previous

 

2、正则表达式在java中应为\\\\

3、类与类的关系尽量放小

 

四、改进建议

在oop6中7-1中

我应该使用hashmap进行table语句的储存,储存两个值 桌号 和 时间

以此写多个桌号的处理

 

五、总结

在这三次训练集当中,我认为我学会了很多以前遗漏的知识点,例如正则应该使用\\\\,类中属性应该尽量使用封装,同时我巩固了字符串的处理方法,类的聚合,正则表达式的使用和规则。同时,我也发现了我的很多错误和不足,我还是没有习惯随手写注释,代码依旧不够精练,很多方法例如哈希表等都不能熟练使用。

 

OO第二次博客作业

第五次作业:多线程电梯

多线程的协同和同步控制:

本次作业一共有以下几个线程:读入处理线程inputHandler,单个电梯运行线程elevatorRun*3,任务分派线程newNewDispatch。

inputHandler线程用来读入并判断是否合法,提取指令并将其放入总指令队列。

每个elevatorRun线程负责调度一个电梯,有自己的一个指令队列,操纵这台电梯按照指令队列的要求运行,有点类似上一次的dispatch。

newNewDispatch负责根据floor亮灯情况和每个电梯的状态等从总指令队列取出指令并放进每个电梯的指令队列。

需要进行处理的是inputHandler与newNewDispatch共同操作中指令队列;newNewDispatch和exevatorRun操作floor和elevator

对于前一个,简单的锁总指令队列即可。

对于后一个,为了避免死锁,我请求状态时按照电梯顺序分别锁电梯123,其他情况下锁需要使用的电梯。

度量分析

技术分享图片

 技术分享图片

主要问题是在调度类中为了省事大量使用了重复的代码。此外在使用前几次的代码时修补bug也增加了许多补丁式的代码,增加了长度与复杂度。

BUG分析

我在电梯调度分配上出了些奇怪的问题,导致了有些情况下捎带指令没有被捎带。

我拿到的那位是一个巨佬,洋洋洒洒写了10个类,快2k行代码,测了一下并没有任何bug

第六次作业:IFTTT

多线程的协同和同步控制:

本次作业一共有以下几个线程:读入处理线程inputHandler,监控线程*n

这次的相对比较简单,主要冲突在输出问价上。对summary和detail输出分别加锁就好了。

度量分析

技术分享图片

技术分享图片

问题在于监控时把四种方法写在一起用了一个而没有分开。我使用了switch+一段代码的模式,看上去也比较清晰,但是会导致代码过长。

BUG分析

对方并没有给我公测,根据我自己测试结果来看过公测没有什么问题,但是互测中应该是能发现一些小问题的。

我拿到的那一份写的比较一般,基本随手就能找到bug,公测挂了好几个点。测完公测后简单又测了一下,发现甚至不能定位错误原因,搞得我也不知道是不是有一个问题导致的错误,最后挂了一个点以后放弃了进一步测试。

 

第七次作业:出租车1

多线程的协同和同步控制:

本次作业一共有以下几个线程:出租车线程*100,调度分配线程*运行的指令个数,读入处理线程

产生冲突的主要是出租车选单,出租车当前状态,任务派单时对出租车的访问

度量分析

技术分享图片

技术分享图片

在处理接单等任务时处理逻辑还是有点复杂。本来写的想法有漏洞,后来在修补时把那里的代码搞得很乱。

BUG分析

直接使用了助教提供的GUI中的bfs算法计算最短距离,现在发现提供的实现运算太慢了,单次误差有几十毫秒,一次性输入几百条时直接卡死了。下次作业时要把这段重写了。

此外,接到乘客时忘记sleep了。

心得体会

写代码前要认真规划,防止写到一半发现问题再重新添加的情况。虽然修改代码可以解决大部分的bug,可是这会导致自己的代码越来越长,检查时会十分不爽。

以上是关于FC第二次博客作业的主要内容,如果未能解决你的问题,请参考以下文章

OO第二次博客作业

OO第二次博客作业

OO第二次博客作业

OO第二次博客作业

OO第二次博客作业

oo第二次博客作业