一、前言

本次博客是針對面向?qū)ο蟪绦蛟O(shè)計(jì)課程布置的PTA第4、5、6次作業(yè)以及期中考試的編程題而寫的總結(jié)分析,重點(diǎn)分析了菜單計(jì)價系列題目、期中考試的編程題等具有一定難度和特色的問題。

 

二、PTA第四次作業(yè)

這次作業(yè)有四道題目,其中難度最大且分值最高的題目就是菜單計(jì)價程序-3,該題在菜單計(jì)價程序-2的基礎(chǔ)上添加了時間折扣,判斷代點(diǎn)菜信息等功能。

設(shè)計(jì)點(diǎn)菜計(jì)價程序,根據(jù)輸入的信息,計(jì)算并輸出總價格。

輸入內(nèi)容按先后順序包括兩部分:菜單、訂單,最后以"end"結(jié)束。

菜單由一條或多條菜品記錄組成,每條記錄一行

每條菜品記錄包含:菜名、基礎(chǔ)價格 兩個信息。

訂單分:桌號標(biāo)識、點(diǎn)菜記錄和刪除信息、代點(diǎn)菜信息。每一類信息都可包含一條或多條記錄,每條記錄一行或多行。

桌號標(biāo)識獨(dú)占一行,包含兩個信息:桌號、時間。

桌號以下的所有記錄都是本桌的記錄,直至下一個桌號標(biāo)識。

點(diǎn)菜記錄包含:序號、菜名、份額、份數(shù)。份額可選項(xiàng)包括:1、2、3,分別代表小、中、大份。

不同份額菜價的計(jì)算方法:小份菜的價格=菜品的基礎(chǔ)價格。中份菜的價格=菜品的基礎(chǔ)價格1.5。小份菜的價格=菜品的基礎(chǔ)價格2。如果計(jì)算出現(xiàn)小數(shù),按四舍五入的規(guī)則進(jìn)行處理。

刪除記錄格式:序號 delete

標(biāo)識刪除對應(yīng)序號的那條點(diǎn)菜記錄。

如果序號不對,輸出"delete error"

代點(diǎn)菜信息包含:桌號 序號 菜品名稱 份額 分?jǐn)?shù)

代點(diǎn)菜是當(dāng)前桌為另外一桌點(diǎn)菜,信息中的桌號是另一桌的桌號,帶點(diǎn)菜的價格計(jì)算在當(dāng)前這一桌。

程序最后按輸入的先后順序依次輸出每一桌的總價(注意:由于有代點(diǎn)菜的功能,總價不一定等于當(dāng)前桌上的菜的價格之和)。

每桌的總價等于那一桌所有菜的價格之和乘以折扣。如存在小數(shù),按四舍五入規(guī)則計(jì)算,保留整數(shù)。

折扣的計(jì)算方法(注:以下時間段均按閉區(qū)間計(jì)算):

周一至周五營業(yè)時間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余時間不營業(yè)。

周末全價,營業(yè)時間:9:30-21:30

如果下單時間不在營業(yè)范圍內(nèi),輸出"table " + t.tableNum + " out of opening hours"

參考以下類的模板進(jìn)行設(shè)計(jì):菜品類:對應(yīng)菜譜上一道菜的信息。

Dish {

String name;//菜品名稱

int unit_price; //單價

int getPrice(int portion)//計(jì)算菜品價格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份) }

菜譜類:對應(yīng)菜譜,包含飯店提供的所有菜的信息。

Menu {

Dish\[\] dishs ;//菜品數(shù)組,保存所有菜品信息

Dish searthDish(String dishName)//根據(jù)菜名在菜譜中查找菜品信息,返回Dish對象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

點(diǎn)菜記錄類:保存訂單上的一道菜品記錄

Record {

int orderNum;//序號\\

Dish d;//菜品\\

int portion;//份額(1/2/3代表小/中/大份)\\

int getPrice()//計(jì)價,計(jì)算本條記錄的價格\\

}

訂單類:保存用戶點(diǎn)的所有菜的信息。

Order {

Record\[\] records;//保存訂單上每一道的記錄

int getTotalPrice()//計(jì)算訂單的總價

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單中。

delARecordByOrderNum(int orderNum)//根據(jù)序號刪除一條記錄

findRecordByNum(int orderNum)//根據(jù)序號查找一條記錄

}

### 輸入格式:

桌號標(biāo)識格式:table + 序號 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時間(24小時制格式: HH/MM/SS)

菜品記錄格式:

菜名+英文空格+基礎(chǔ)價格

如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價格以最后一條記錄為準(zhǔn)。

點(diǎn)菜記錄格式:序號+英文空格+菜名+英文空格+份額+英文空格+份數(shù)注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。

刪除記錄格式:序號 +英文空格+delete

代點(diǎn)菜信息包含:桌號+英文空格+序號+英文空格+菜品名稱+英文空格+份額+英文空格+分?jǐn)?shù)

最后一條記錄以“end”結(jié)束。

### 輸出格式:

按輸入順序輸出每一桌的訂單記錄處理信息,包括:

1、桌號,格式:table+英文空格+桌號+”:”

2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,

每條點(diǎn)菜記錄輸出:序號+英文空格+菜名+英文空格+價格。其中的價格等于對應(yīng)記錄的菜品\*份數(shù),序號是之前輸入的訂單記錄的序號。如果訂單中包含不能識別的菜名,則輸出“\*\* does not exist”,\*\*是不能識別的菜名

如果刪除記錄的序號不存在,則輸出“delete error”

最后按輸入順序一次輸出每一桌所有菜品的總價(整數(shù)數(shù)值)格式:table+英文空格+桌號+“:”+英文空格+當(dāng)前桌的總價

本次題目不考慮其他錯誤情況,如:桌號、菜單訂單順序顛倒、不符合格式的輸入、序號重復(fù)等,在本系列的后續(xù)作業(yè)中會做要求。

輸入格式:

桌號標(biāo)識格式:table + 序號 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時間(24小時制格式: HH/MM/SS)

菜品記錄格式:

菜名+英文空格+基礎(chǔ)價格

如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價格以最后一條記錄為準(zhǔn)。

點(diǎn)菜記錄格式:序號+英文空格+菜名+英文空格+份額+英文空格+份數(shù)注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。

刪除記錄格式:序號 +英文空格+delete

代點(diǎn)菜信息包含:桌號+英文空格+序號+英文空格+菜品名稱+英文空格+份額+英文空格+分?jǐn)?shù)

最后一條記錄以“end”結(jié)束。

輸出格式:

按輸入順序輸出每一桌的訂單記錄處理信息,包括:

1、桌號,格式:table+英文空格+桌號+“:”+英文空格

2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,

每條點(diǎn)菜記錄輸出:序號+英文空格+菜名+英文空格+價格。其中的價格等于對應(yīng)記錄的菜品\*份數(shù),序號是之前輸入的訂單記錄的序號。如果訂單中包含不能識別的菜名,則輸出“\*\* does not exist”,\*\*是不能識別的菜名

如果刪除記錄的序號不存在,則輸出“delete error”

最后按輸入順序一次輸出每一桌所有菜品的總價(整數(shù)數(shù)值)格式:table+英文空格+桌號+“:”+英文空格+當(dāng)前桌的總價

本次題目不考慮其他錯誤情況,如:桌號、菜單訂單順序顛倒、不符合格式的輸入、序號重復(fù)等,在本系列的后續(xù)作業(yè)中會做要求。

我的代碼如下:

package  num1;
import java.util.Calendar;
import java.util.Scanner;
class Dish {
    String name;//菜品名稱
    int unit_price; //單價
//int num;

    int getPrice(int portion) {
        int peic = 0;
        if (portion == 1) {
            peic = unit_price ;
        } else if (portion == 2) {
            peic = Math.round((float) (unit_price * 1.5)) ;
        } else if (portion == 3) {
            peic = (unit_price * 2) ;
        }
        return peic;//計(jì)算菜品價格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份)
    }
}
class Menu {
    Dish[] dishs = new Dish[10];//菜品數(shù)組,保存所有菜品信息
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd==null){
            System.out.println(dishName+" does not exist");
        }
        return temd;
    }//根據(jù)菜名在菜譜中查找菜品信息,返回Dish對象。
    Dish addDish(String dishName,int unit_price){
        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
        count++;
        return dh;
    }//添加一道菜品信息
}
class Record {
    int orderNum;//序號\
    //int AntherOrderNum;
    Dish d = new Dish();//菜品\
    int num = 0;
    int portion;//份額(1/2/3代表小/中/大份)\
    //int exist = 1;
    int getPrice(){
        return d.getPrice(portion)*num;
    }//計(jì)價,計(jì)算本條記錄的價格\
}
class Order {
    Record[] records = new Record[10];//保存訂單上每一道的記錄
    int count = 0;//訂單數(shù)量
    //int forcount = 0;//代點(diǎn)菜的數(shù)量
/*int getTotalPrice(){
int sum=0;
for(int i=0;i<count;i++){
if(records[i].exist==0)
continue;
sum=sum+records[i].getPrice();
}
return sum;
}//計(jì)算訂單的總價*/
    void addARecord(int orderNum,String dishName,int portion,int num){
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].portion = portion;
        records[count].num = num;
        count++;
    }//添加一條菜品信息到訂單中。
    /*Record TakeOdfor(int AnotherNUm,int orderNum,String dishName,int portion,int num){
    Record rd2 = new Record();
    rd2.d.name = dishName;
    rd2.orderNum = orderNum;
    rd2.portion = portion;
    rd2.d.num = num;
    rd2.AntherOrderNum = AnotherNUm;
    //forcount++;
    return rd2;
    }*/
    int delARecordByOrderNum(int orderNum){
        if(orderNum>count||orderNum<=0){
            System.out.println("delete error;");
            return 0;
        }else {
            return records[orderNum - 1].getPrice();
        }
    }//根據(jù)序號刪除一條記錄
}
class Table {
    int tableNum;
    String tableDtime;
    int year,month,day,week,hh,mm,ss;
    int sum = 0;//一桌價格 ;
    // boolean f = true;
    Order odt = new Order();
    //Order odre = new Order();
    float discnt = -1;
    void Gettottalprice(){
        if(discnt>0){
            sum = Math.round(sum*discnt);
            System.out.println("table " + tableNum + ": " + sum);
        }else {
            System.out.println("table " + tableNum + " out of opening hours");
        }
    }
    void AheadProcess(String tableDtime){
        this.tableDtime = tableDtime;
        processTime();
        discount();
//CheckAtime();
    }


    void processTime(){//處理時間
        String[] temp = tableDtime.split(" ");
        tableNum = Integer.parseInt(temp[1]);
        String[] temp1 = temp[2].split("/");
        String[] temp2 = temp[3].split("/");

        year = Integer.parseInt(temp1[0]);
        month = Integer.parseInt(temp1[1]);
        day = Integer.parseInt(temp1[2]);

        Calendar c = Calendar.getInstance();
        c.set(year, (month-1), day);
        week = c.get(Calendar.DAY_OF_WEEK);
        if(week==1)
            week = 7;
        else
            week--;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);

    }
    //void CheckAtime(){
// f= !(discnt < 0);
// }
    void discount(){
        if(week>=1&&week<=5)
        {
            if(hh>=17&&hh<20)
                discnt=0.8F;
            else if(hh==20&&mm<30)
                discnt=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discnt=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discnt=0.6F;
            else if(hh==14&&mm<30)
                discnt=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discnt=0.6F;
        }
        else
        {
            if(hh>=10&&hh<=20)
                discnt= 1.0F;
            else if(hh==9&&mm>=30)
                discnt= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discnt= 1.0F;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Menu mu = new Menu();
        Table[] tablemes = new Table[10];
        int j = 0;//菜單數(shù)
        int l = 0;//訂單數(shù)
        int k = 0;//代點(diǎn)菜數(shù)
        Dish tt;
//int sum = 0;
        int cntTable = 0;//桌號
        int count;
        String[] temp;
        int a1,a2,a3,a4,a5;

        while (true) {
            String st = sc.nextLine();
            temp = st.split(" ");
            if(st.equals("end"))
                break;
            count = temp.length;
            if (count == 2) {//一個空格
//String[] temp1 = st.split(" ");
                if (temp[1].equals("delete")) {//第二個為delete
                    a1 = Integer.parseInt(temp[0]);
                    int c = tablemes[cntTable].odt.delARecordByOrderNum(a1);
                    tablemes[cntTable].sum-=c;
                } else {//菜單添加
                    a2 = Integer.parseInt(temp[1]);
                    mu.dishs[j] = mu.addDish(temp[0], a2);
                    j++;
                }
//continue;
            }
            else if (count == 4) {//三個空格
//String[] temp2 = st.split(" ");
                if (temp[0].equals("table")) {//桌號
                    cntTable++;//跳過0;
                    l = 0;
                    tablemes[cntTable] = new Table();
//tablemes[cntTable].tableDtime = st;
                    tablemes[cntTable].AheadProcess(st);

                    System.out.println("table " + cntTable + ": ");
                } else {//增加訂單的情況;
                    a3 =Integer.parseInt(temp[0]);
                    a4 = Integer.parseInt(temp[2]);
                    a5=Integer.parseInt(temp[3]);
                    tablemes[cntTable].odt.addARecord(a3, temp[1],a4 , a5);
                    tt = mu.searthDish(temp[1]);
                    if (tt != null) {
                        tablemes[cntTable].odt.records[l].d = tt;
                        int a = tablemes[cntTable].odt.records[l].getPrice();
                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );
                        tablemes[cntTable].sum +=a;
                    }
                    l++;
                }
//continue;
            }
            else if (count == 5) {//代點(diǎn)菜
//String[] temp3 = st.split(" ");
                a1 = Integer.parseInt(temp[1]);
                a2 = Integer.parseInt(temp[3]);
                a3 = Integer.parseInt(temp[4]);
                tablemes[cntTable].odt.addARecord( a1, temp[2], a2, a3);
                tt = mu.searthDish(temp[2]);
                if (tt != null) {
                    tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price;
                    int b = tablemes[cntTable].odt.records[l].getPrice();
                    System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
                    tablemes[cntTable].sum += b;
                }
                l++;
            }
//st = sc.nextLine();
        }
        for (int i = 1; i < cntTable + 1; i++) {
            tablemes[i].Gettottalprice();
        }
    }
}

 

  • Dish 類表示菜品,包含菜品名稱和單價,并提供計(jì)算菜品價格的方法。
  • Menu 類用于管理菜單,包含菜品數(shù)組和菜品數(shù)量,提供查找菜品和添加菜品的方法。
  • Record 類表示訂單中的記錄,包含序號、菜品信息、數(shù)量和份額,并提供計(jì)算記錄價格的方法。
  • Order 類用于管理訂單,包含記錄數(shù)組和訂單數(shù)量,提供添加記錄和刪除記錄的方法。
  • Table 類表示餐桌,包含桌號、時間信息、總價格和訂單對象,提供計(jì)算總價格和處理時間的方法。
  • Main 類是程序的入口點(diǎn),通過命令行輸入來模擬添加菜品、點(diǎn)菜、刪除訂單等操作。

對該代碼進(jìn)行類圖分析和復(fù)雜度分析如下,結(jié)果如下:

sourcemonitor代碼分析結(jié)果如下:

 

這次題目對于對java語言及面向?qū)ο笏枷氩惶煜さ奈襾碚f頗具挑戰(zhàn),在一個星期的時間內(nèi),我不斷調(diào)試和優(yōu)化,還是沒能通過全部測試點(diǎn)。

三、PTA第五次作業(yè)

本次作業(yè)只有一道題目即菜單計(jì)價程序-4,通過對菜單計(jì)價程序-3的優(yōu)化和檢錯,主要針對錯誤輸入的檢測和拋出,來顯示相應(yīng)的信息。

本次課題比菜單計(jì)價系列-3增加的異常情況:

1、菜譜信息與訂單信息混合,應(yīng)忽略夾在訂單信息中的菜譜信息。輸出:"invalid dish"

2、桌號所帶時間格式合法(格式見輸入格式部分說明,其中年必須是4位數(shù)字,月、日、時、分、秒可以是1位或2位數(shù)),數(shù)據(jù)非法,比如:2023/15/16 ,輸出桌號+" date error"

3、同一桌菜名、份額相同的點(diǎn)菜記錄要合并成一條進(jìn)行計(jì)算,否則可能會出現(xiàn)四舍五入的誤差。

4、重復(fù)刪除,重復(fù)的刪除記錄輸出"deduplication :"+序號。

5、代點(diǎn)菜時,桌號不存在,輸出"Table number :"+被點(diǎn)菜桌號+" does not exist";本次作業(yè)不考慮兩桌記錄時間不匹配的情況。

6、菜譜信息中出現(xiàn)重復(fù)的菜品名,以最后一條記錄為準(zhǔn)。

7、如果有重復(fù)的桌號信息,如果兩條信息的時間不在同一時間段,(時段的認(rèn)定:周一到周五的中午或晚上是同一時段,或者周末時間間隔1小時(不含一小時整,精確到秒)以內(nèi)算統(tǒng)一時段),此時輸出結(jié)果按不同的記錄分別計(jì)價。

8、重復(fù)的桌號信息如果兩條信息的時間在同一時間段,此時輸出結(jié)果時合并點(diǎn)菜記錄統(tǒng)一計(jì)價。前提:兩個的桌號信息的時間都在有效時間段以內(nèi)。計(jì)算每一桌總價要先合并符合本條件的飯桌的點(diǎn)菜記錄,統(tǒng)一計(jì)價輸出。

9、份額超出范圍(1、2、3)輸出:序號+" portion out of range "+份額,份額不能超過1位,否則為非法格式,參照第13條輸出。

10、份數(shù)超出范圍,每桌不超過15份,超出范圍輸出:序號+" num out of range "+份數(shù)。份數(shù)必須為數(shù)值,最高位不能為0,否則按非法格式參照第16條輸出。

11、桌號超出范圍[1,55]。輸出:桌號 +" table num out of range",桌號必須為1位或多位數(shù)值,最高位不能為0,否則按非法格式參照第16條輸出。

12、菜譜信息中菜價超出范圍(區(qū)間(0,300)),輸出:菜品名+" price out of range "+價格,菜價必須為數(shù)值,最高位不能為0,否則按非法格式參照第16條輸出。

13、時間輸入有效但超出范圍[2022.1.1-2023.12.31],輸出:"not a valid time period"

14、一條點(diǎn)菜記錄中若格式正確,但數(shù)據(jù)出現(xiàn)問題,如:菜名不存在、份額超出范圍、份數(shù)超出范圍,按記錄中從左到右的次序優(yōu)先級由高到低,輸出時只提示優(yōu)先級最高的那個錯誤。

15、每桌的點(diǎn)菜記錄的序號必須按從小到大的順序排列(可以不連續(xù),也可以不從1開始),未按序排列序號的輸出:"record serial number sequence error"。當(dāng)前記錄忽略。(代點(diǎn)菜信息的序號除外)

16、所有記錄其它非法格式輸入,統(tǒng)一輸出"wrong format"

17、如果記錄以“table”開頭,對應(yīng)記錄的格式或者數(shù)據(jù)不符合桌號的要求,那一桌下面定義的所有信息無論正確或錯誤均忽略,不做處理。如果記錄不是以“table”開頭,比如“tab le 55 2023/3/2 12/00/00”,該條記錄認(rèn)為是錯誤記錄,后面所有的信息并入上一桌一起計(jì)算。

本次作業(yè)比菜單計(jì)價系列-3增加的功能:

菜單輸入時增加特色菜,特色菜的輸入格式:菜品名+英文空格+基礎(chǔ)價格+"T"

例如:麻婆豆腐 9 T

菜價的計(jì)算方法:

周一至周五 7折, 周末全價。

注意:不同的四舍五入順序可能會造成誤差,請按以下步驟累計(jì)一桌菜的菜價:

計(jì)算每條記錄的菜價:將每份菜的單價按份額進(jìn)行四舍五入運(yùn)算后,乘以份數(shù)計(jì)算多份的價格,然后乘以折扣,再進(jìn)行四舍五入,得到本條記錄的最終支付價格。

最后將所有記錄的菜價累加得到整桌菜的價格。

 

我的代碼如下:

class Worn
{
    void worninput(String[]a,int num,int[]b)
    {
        for(int i=0;i<num;i++)
        {
            String[] arr=a[i].split(" ");
            boolean flag=false;
            //麻婆豆腐 12
            if(arr.length>5)
            {
                b[i]=1;
            }
            if(a[i].matches("^[\u4e00-\u9fa5]{1,} ([1-9][0-9]{0,2})$")==true)//普通菜存入
            {
                flag=true;
            }
            //油淋生菜 9 T
            else if(a[i].matches("^[\u4e00-\u9fa5]{1,4} ([1-9][0-9]{0,2}) [T]$")==true)//特色菜存入
            {
                flag=true;
            }
            //table 31 2023/2/1 14/20/00
            else if(arr[0].equals("table")==true)//
            {
                int blank=0;
                for(int bk=0;bk<a[i].length();bk++)
                {
                    if(a[i].charAt(bk)==' ')
                    {
                        blank++;
                    }
                }
                if(arr[1].matches("^([1-9][0-9]{0,})$")&&blank==3)
                    flag=true;
                //int blank=0;

            }
            //1 麻婆豆腐 1 16
            else if(a[i].matches("^[1-9][0-9]{0,2} [\u4e00-\u9fa5]{1,} [1-9] ([1-9][0-9]{0,1})$")==true)
            {
                flag=true;
            }
            //1 1 麻婆豆腐 1 16
            else if(a[i].matches("^([1-9][0-9]{0,1}) [1-9][0-9]{0,2} [\u4e00-\u9fa5]{1,} [1-9] ([1-9][0-9]{0,1})$")==true)
            {
                flag=true;
            }
            //2 delete
            else if(a[i].matches("^([1-9][0-9]{0,1}) delete$")==true)
            {
                flag=true;
            }
            else if(a[i].matches("^end$")==true)
            {
                flag=true;
            }
            if(flag==false)
            {
                b[i]=1;
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            String[] a=new String[50];
            int[]b=new int[50];
            Library li = new Library();
            Menu c=new Menu();
            table[] t=new table[50];
            Dish g=new Dish();
            int i=0;//桌號
            int j=0;//菜數(shù)
            int p=0;//當(dāng)前處理桌號
            int lastdish=0;
            int dc1=0,dc2=0,dc3=0,dd1=0,dd2=0,dd3=0,dd4=0;
            int inputnum=0,h;
            a[0] = in.nextLine();
            b[0] = 0;
            while (!a[inputnum].equals("end")) {
                inputnum++;
                a[inputnum] = in.nextLine();
                b[inputnum] = 0;
            }
            Worn w=new Worn();
            w.worninput(a,inputnum,b);
            for(h=0;h<inputnum;h++) {
                if (a[h].isEmpty()) {
                    System.out.println("wrong format");
                } else {
                    boolean flagerror = false;
                    //分割字符
                    String[] arr = a[h].split(" ");
                    if (arr.length == 4 || arr[0].charAt(0) == 't') {
                        if (arr[0].equals("table"))//加桌
                        {
                            flagerror = true;
                            lastdish = 0;
                            table x = new table();
                            x.input(a[h], b[h]);
                            if (i != 0)
                                p = t[i - 1].searchtime(t, x, i);
                            if (p == i)//加新桌
                            {
                                t[i] = new table();
                                t[p] = x;
                                i++;
                                lastdish = 0;//初始化
                                if (t[p].flag)
                                    System.out.println("table " + t[p].tablenum + ": ");
                            } else {
                                if (t[p].flag)
                                    System.out.println("table " + t[p].tablenum + ": ");
                                lastdish = 0;
                            }
                        } else if (arr[0].matches("^[1-9][0-9]{0,1}$"))//自己點(diǎn)菜
                        {
                            if (b[h] == 1) {
                                flagerror = true;
                                if (i != 0 && t[p].flag) {
                                    System.out.println("wrong format");
                                }
                                continue;
                            }
                            dc1 = Integer.parseInt(arr[0]);
                            dc2 = Integer.parseInt(arr[2]);
                            dc3 = Integer.parseInt(arr[3]);
                            if (i != 0 && t[p].flag) {

                                if (i != 0) {
                                    if (dc1 <= lastdish) {
                                        System.out.println("record serial number sequence error");
                                    } else {
                                        t[p].selforder.addARecord(0, dc1, arr[1], dc2, dc3, true);
                                        g = c.searthDish(arr[1]);
                                        if (g != null && t[p].flag) {
                                            if (dc2 > 3 && dc2 < 10 || dc2 <= 0 && dc2 > -10)//情況9
                                            {
                                                //序號+" portion out of range "+份額
                                                System.out.println(dc1 + " portion out of range " + dc2);
                                            } else if (dc3 > 15) {
                                                System.out.println(dc1 + " num out of range " + dc3);
                                            } else {
                                                t[p].selforder.records[t[p].k].d = g;
                                                //System.out.println(t[p].selforder.records[]);
                                                int x = t[p].selforder.records[t[p].k].getPrice();
                                                //2 油淋生菜 27
                                                lastdish = dc1;
                                                System.out.println(dc1 + " " + arr[1] + " " + x);
                                            }
                                        }
                                        t[p].k++;

                                    }
                                }

                            }
                        } else {
                            System.out.println("wrong format");
                            flagerror = true;
                        }
                    } else if (arr.length == 5) {
                        if (b[h] == 1) {
                            flagerror = true;
                            if (i != 0 && t[p].flag) {
                                System.out.println("wrong format");
                            }
                            continue;
                        }
                        dd1 = Integer.parseInt(arr[0]);
                        dd2 = Integer.parseInt(arr[1]);
                        dd3 = Integer.parseInt(arr[3]);
                        dd4 = Integer.parseInt(arr[4]);
                        if (t[p].searthtable(t, dd1, p) == true) {
                            t[p].selforder.addARecord(dd2, dd1, arr[2], dd3, dd4, false);
                            g = c.searthDish(arr[2]);
                            if (g != null) {
                                t[p].selforder.records[t[p].k].d = g;
                                int x = t[p].selforder.records[t[p].k].getPrice();
                                System.out.println(dd2 + " table " + t[p].tablenum + " pay for table " + dd1 + " " + x);
                            }
                            //k++;
                            t[p].k++;
                        }
                    } else if (arr.length == 2) {
                        if (arr[1].equals("delete")) {

                            if (i != 0) {
                                if (b[h] == 1) {
                                    flagerror = true;
                                    if (i != 0) {
                                        System.out.println("wrong format");
                                    }
                                    continue;
                                }
                                if (t[p].flag)
                                    t[p].selforder.delARecordByOrderNum(Integer.parseInt(arr[0]), t[p]);
                            }
                        } else//加菜
                        {
                            if (b[h] == 1) {
                                flagerror = true;
                                System.out.println("wrong format");
                                continue;
                            }
                            if (i != 0 && t[p].flag)//情況1
                            {
                                System.out.println("invalid dish");
                            } else {
                                if (Integer.parseInt(arr[1]) > 0 && Integer.parseInt(arr[1]) < 300) {
                                    c.addDish(arr[0], Integer.parseInt(arr[1]), false);
                                    j++;
                                } else {
                                    System.out.println(arr[0] + " price out of range " + arr[1]);
                                }
                            }
                        }
                    } else if (arr.length == 3) {
                        if (b[h] == 1) {
                            flagerror = true;
                            System.out.println("wrong format");
                            continue;
                        }
                        if (i != 0 && t[p].flag) {
                            System.out.println("invalid dish");
                        } else {
                            if (arr[2].equals("T")) {
                                if (Integer.parseInt(arr[1]) > 0 && Integer.parseInt(arr[1]) < 300) {
                                    c.addDish(arr[0], Integer.parseInt(arr[1]), true);
                                    j++;
                                } else {
                                    System.out.println(arr[0] + " price out of range " + arr[1]);
                                }
                            }
                        }
                    }
                }
            }
            for(int l=0;l<i;l++)
            {
                t[l].getTotalPrice();
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}
因代碼過長,在此只截取了新增的wron類以及MAIN類的代碼,try 塊中包含了主要的代碼邏輯,用于接收用戶的輸入并進(jìn)行處理。catch 塊用于捕獲 NumberFormatException 異常,該異常通常在將字符串轉(zhuǎn)換為數(shù)字時發(fā)生,例如使用 Integer.parseInt() 方法時。

在 catch 塊中,異常對象 e 被打印出來,以便調(diào)試和定位錯誤。


該代碼的類圖以及代碼復(fù)雜度分析如下:

 

 這次題目我成功的完成了所有功能,通過所有測試點(diǎn)。

四、PTA第六次作業(yè)

這次作業(yè)是菜單計(jì)價程序5,菜單計(jì)價系列的最后一道題目。

以上為菜單計(jì)價系列-3的題目要求,加粗的部分是有調(diào)整的內(nèi)容。本次課題相比菜單計(jì)價系列-3新增要求如下:

 

1、菜單輸入時增加特色菜,特色菜的輸入格式:菜品名+英文空格+口味類型+英文空格+基礎(chǔ)價格+"T"

例如:麻婆豆腐 川菜 9 T

菜價的計(jì)算方法:

周一至周五 7折, 周末全價。

特色菜的口味類型:川菜、晉菜、浙菜

川菜增加辣度值:辣度0-5級;對應(yīng)辣度水平為:不辣、微辣、稍辣、辣、很辣、爆辣;

晉菜增加酸度值,酸度0-4級;對應(yīng)酸度水平為:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3級;對應(yīng)酸度水平為:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

輸入訂單記錄時如果是特色菜,添加口味度(辣/酸/甜度)值,格式為:序號+英文空格+菜名+英文空格+口味度值+英文空格+份額+英文空格+份數(shù)

例如:1 麻婆豆腐 4 1 9

單條信息在處理時,如果口味度超過正常范圍,輸出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根據(jù)菜品類型擇一輸出,例如:

acidity num out of range : 5

輸出一桌的信息時,按辣、酸、甜度的順序依次輸出本桌菜各種口味的口味度水平,如果沒有某個類型的菜,對應(yīng)的口味(辣/酸/甜)度不輸出,只輸出已點(diǎn)的菜的口味度。口味度水平由口味度平均值確定,口味度平均值只綜合對應(yīng)口味菜系的菜計(jì)算,不做所有菜的平均。比如,某桌菜點(diǎn)了3份川菜,辣度分別是1、3、5;還有4份晉菜,酸度分別是,1、1、2、2,辣度平均值為3、酸度平均值四舍五入為2,甜度沒有,不輸出。

一桌信息的輸出格式:table+英文空格+桌號+:+英文空格+當(dāng)前桌的原始總價+英文空格+當(dāng)前桌的計(jì)算折扣后總價+英文空格+"川菜"+數(shù)量+辣度+英文空格+"晉菜"+數(shù)量+酸度+英文空格+"浙菜"+數(shù)量+甜度。

如果整桌菜沒有特色菜,則只輸出table的基本信息,格式如下,注意最后加一個英文空格:

table+英文空格+桌號+:+英文空格+當(dāng)前桌的原始總價+英文空格+當(dāng)前桌的計(jì)算折扣后總價+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

計(jì)算口味度時要累計(jì)本桌各類菜系所有記錄的口味度總和(每條記錄的口味度乘以菜的份數(shù)),再除以對應(yīng)菜系菜的總份數(shù),最后四舍五入。

注:本題要考慮代點(diǎn)菜的情況,當(dāng)前桌點(diǎn)的菜要加上被其他桌代點(diǎn)的菜綜合計(jì)算口味度平均值。

 

 

2、考慮客戶訂多桌菜的情況,輸入時桌號時,增加用戶的信息:

格式:table+英文空格+桌號+英文空格+":"+英文空格+客戶姓名+英文空格+手機(jī)號+日期(格式:YYYY/MM/DD)+英文空格+ 時間(24小時制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

約束條件:客戶姓名不超過10個字符,手機(jī)號11位,前三位必須是180、181、189、133、135、136其中之一。

輸出結(jié)果時,先按要求輸出每一桌的信息,最后按字母順序依次輸出每位客戶需要支付的金額。不考慮各桌時間段的問題,同一個客戶的所有table金額都要累加。

輸出用戶支付金額格式:

用戶姓名+英文空格+手機(jī)號+英文空格+支付金額

 

 

注意:不同的四舍五入順序可能會造成誤差,請按以下步驟累計(jì)一桌菜的菜價:

 

計(jì)算每條記錄的菜價:將每份菜的單價按份額進(jìn)行四舍五入運(yùn)算后,乘以份數(shù)計(jì)算多份的價格,然后乘以折扣,再進(jìn)行四舍五入,得到本條記錄的最終支付價格。

將所有記錄的菜價累加得到整桌菜的價本次題目的代碼可根據(jù)菜單計(jì)價程序3的代碼來修改,加上口味特色菜等功能,我在菜單計(jì)價程序3的代碼基礎(chǔ)上進(jìn)行了優(yōu)化,使得最后的代碼行數(shù)很少。

在這里貼出我的代碼的Main部分:

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Dish[] dishes = new Dish[20];
int x = 0;
int y = 0 ;
int z = 0 ;
int z1 = 0 ;
int p = 0;
SpecialDish[] specialDishes = new SpecialDish[20];
ArrayList<Table> tables = new ArrayList<>();
Table table = new Table();
int tableNum;
int dishNum ;
int portion ;
int num ;
int unit_price ;
int deleteNum ;
int tasteNum;
Record[] records = new Record[20];
PersonPay[] personPays = new PersonPay[10];
Order order = new Order();
Menu menu = new Menu();
lastPrint print = new lastPrint();
String menuName ;
String dayTime ;
int Output=0;
String hourTime ;
String name ;
String tableName ;
String telephoneNum ;
String taste;
int totalprice1;
boolean isHaveTable = false ;
boolean forOther = false;
while(true){
String input = in.readLine();

if(input.equals("end")){
if(x!=0){
table.order = order;
tables.add(x-1 , table);
Output ++;
}
print.tables = tables;
print.personPays = personPays;
print.print();
print.payPrint();
break;
}
String[] getInput = input.split(" ");
if(input.matches("^(table)( )([1-9][0-9]*)( )(:)( )(\\S+)( )((136|133|135|180|181|189)[0-9]{8})( )([0-9]{4})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})( )([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})$")) {
if(x != 0) {
table.order = order;
tables.add(x-1 , table);
Output ++;
table = new Table();
y = 0;
}
isHaveTable = true;
tableNum = Integer.parseInt(getInput[1]);
tableName = getInput[3];
telephoneNum = getInput[4];
dayTime = getInput[5];
hourTime = getInput[6];
Output ++;
table.tableNum = tableNum;
table.tableName = tableName;
table.telephoneNum = telephoneNum;
table.mainTime = dayTime;
table.remainTime = hourTime;
if (table.timeJudgement1()) {
Output ++;
} else {
isHaveTable = false;
System.out.println("table " + table.tableNum + " out of opening hours");
continue;
}
order = new Order();
records = new Record[10];
if(x==0){
personPays[p] = new PersonPay();
personPays[p].name = tableName;
personPays[p].telephoneNum = telephoneNum;
p++;
Output++;
print.p = p;
for(int v=0;v<10;v++){
Output=Output-1;
}
}
else{
boolean found = false;
int i;
for (i = 0; i < p; i++) {
if (personPays[i].name.equals(tableName) && personPays[i].telephoneNum.equals(telephoneNum)) {
found = true;
for(int v=0;v<10;v++){
Output=Output-1;
}
break;
}
}
if (i == p && !found) {
personPays[p] = new PersonPay();
personPays[p].name = tableName;
personPays[p].telephoneNum = telephoneNum;
for(int v=0;v<10;v++){
Output=Output-1;

}
p++;
print.p = p;
}
}
System.out.println("table "+tableNum+": ");
x++;
}
else if(input.matches("^(\\S+)( )([1-9][0-9]*)$")){
int i = 0;
menuName = getInput[0];
unit_price = Integer.parseInt(getInput[1]);
for(int v=0;v<10;v++){
Output=Output-1;
}
if(z == 0) {
dishes[0] = menu.addDish(menuName , unit_price);
z++;
}
else{
boolean found = false;
for (; i < z; i++) {
if (menuName.equalsIgnoreCase(dishes[i].name)) {
dishes[i].unit_price = unit_price;
found = true;
break;
}
}

}
if(i == z){
dishes[z] = menu.addDish(menuName , unit_price);
z++;
}
menu.dishes = dishes;
menu.dishNum = z;
}
else if(input.matches("^(\\S+)( )(\\S+)( )([1-9][0-9]*)( )(T)$")){
int i = 0;
menuName = getInput[0];
taste = getInput[1];
unit_price = Integer.parseInt(getInput[2]);
if(z1 == 0){
specialDishes[0] = menu.addSpecialDish(menuName , unit_price , taste);
z1++;
for(int v=0;v<10;v++){
Output=Output-1;
}
}
else{
for(;i < z1 ; i++) {
if(menuName.equalsIgnoreCase(specialDishes[i].name)) {
specialDishes[i].unit_price = unit_price;
break;
}
}
}
if(i == z1){
specialDishes[z1] = menu.addSpecialDish(menuName , unit_price , taste);
z1++;
}
menu.specialDishes = specialDishes;
menu.dishNum1 = z1;
}
else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){
if(!isHaveTable)
continue;
dishNum = Integer.parseInt(getInput[0]);
name = getInput[1];
portion = Integer.parseInt(getInput[2]);
num = Integer.parseInt(getInput[3]);
Dish searchedDish = menu.searthDish(name);
if (searchedDish != null) {
records[y] = order.addARecord(dishNum, name, portion, num, menu);
records[y].isSpecialDish = false;
System.out.println(records[y].orderNum + " " + records[y].d.name + " " + records[y].getPrice());
y++;
for(int v=0;v<10;v++){
Output=Output-1;
}
order.records = records;
order.dishNum = y;
} else {
System.out.println(name + " does not exist");
}
}
else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([0-9]+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){
if(!isHaveTable)
continue;
dishNum = Integer.parseInt(getInput[0]);
name = getInput[1];
tasteNum = Integer.parseInt(getInput[2]);
portion = Integer.parseInt(getInput[3]);
num = Integer.parseInt(getInput[4]);
if(menu.searthSpecialDish(name) != null) {
if(((menu.searthSpecialDish(name).taste.equals("川菜") && tasteNum >= 0 && tasteNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晉菜") && tasteNum>=0 && tasteNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tasteNum>=0 && tasteNum <= 3))){
records[y] = new Record();
records[y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tasteNum , forOther);
records[y].isSpecialDish = true;
System.out.println(records[y].orderNum+" "+records[y].d.name+" "+records[y].getPrice());
y++;
order.records = records;
order.dishNum = y;
}
}
else{
System.out.println(name+" does not exist");
continue;
}
SpecialDish specialDish = menu.searthSpecialDish(name);
if (specialDish != null) {
switch (specialDish.taste) {
case "川菜":
if (tasteNum < 0 || tasteNum > 5) {
System.out.println("spicy num out of range: " + tasteNum);
}
break;
case "晉菜":
if (tasteNum < 0 || tasteNum > 4) {
System.out.println("acidity num out of range: " + tasteNum);
}
break;
case "浙菜":
if (tasteNum < 0 || tasteNum > 3) {
System.out.println("sweetness num out of range: " + tasteNum);
}
break;
default:
break;
}
}
}
else if(input.matches("([1-9][0-9]*)( )(delete)")) {
if(!isHaveTable)
continue;
deleteNum = Integer.parseInt(getInput[0]);
if(order.findRecordByNum(deleteNum) == 1){
order.delARecordByOrderNum(deleteNum);
}
else
System.out.println("delete error;");
}
else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) {
if(!isHaveTable)
continue;
int t = Integer.parseInt(getInput[0]);
dishNum = Integer.parseInt(getInput[1]);
name = getInput[2];
portion = Integer.parseInt(getInput[3]);
num = Integer.parseInt(getInput[4]);
for(int i = 0;i<x-1;i++){
if(tables.get(i).tableNum == t){
if(menu.searthDish(name) != null) {
records[y] = new Record();
records[y] = order.addARecord(dishNum , name , portion , num , menu);
records[y].isSpecialDish = false;
System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[y].getPrice());
y++;
order.records = records;
order.dishNum = y;
}
break;
}
}
}
else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([0-9]*)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) {
if(!isHaveTable)
continue;
int t = Integer.parseInt(getInput[0]);
dishNum = Integer.parseInt(getInput[1]);
name = getInput[2];
tasteNum = Integer.parseInt(getInput[3]);
portion = Integer.parseInt(getInput[4]);
num = Integer.parseInt(getInput[5]);
for(int i = 0;i<x-1;i++){
if(tables.get(i).tableNum == t){
if(menu.searthSpecialDish(name) != null) {
if(((menu.searthSpecialDish(name).taste.equals("川菜") && tasteNum >= 1 && tasteNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晉菜") && tasteNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tasteNum <= 3))){
records[y] = new Record();
records[y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tasteNum , !forOther);
records[y].isSpecialDish = true;
System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[y].getPrice());
tables.get(i).giveTaste(name,num,tasteNum,menu);
y++;
order.records = records;
order.dishNum = y;
}
}

break;
}
}
}
else {
System.out.println("wrong format");
}
}
}
public String judgeTriangleType(double side1, double side2, double side3) {
if (side1 <= 0 || side2 <= 0 || side3 <= 0) {
return "does not exist";
}
if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) {
return "spicy num out of range:";
}
if (side1 == side2 && side2 == side3) {
return "acidity num out of range:";
} else if (side1 == side2 || side1 == side3 || side2 == side3) {
return "pay for table";
} else {
return "sweetness num out of range";
}
}
}

改后的部分類圖及代碼復(fù)雜度報圖分析如下:

 

 

六、期中考試題目分析

本次期中考試的編程題目為圓形和矩形的類結(jié)構(gòu)設(shè)計(jì)及其圖形類的繼承、多態(tài)和接口。

前三題較為簡單,我想著重介紹一下第四題,

在測驗(yàn)3的題目基礎(chǔ)上,重構(gòu)類設(shè)計(jì),實(shí)現(xiàn)列表內(nèi)圖形的排序功能(按照圖形的面積進(jìn)行排序)。
提示:題目中Shape類要實(shí)現(xiàn)Comparable接口。

其中,Main類源碼如下(可直接拷貝使用):

public class Main {
    public static void main(String\[\] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();    

        int choice = input.nextInt();

        while(choice != 0) {
            switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                list.add(circle);
                break;
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();            
                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);
                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                list.add(rectangle);
                break;
            }
            choice = input.nextInt();
        }    

        list.sort(Comparator.naturalOrder());//正向排序

        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }    
    }    
}

我的代碼如下:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;

abstract class Shape implements Comparable<Shape> {
public abstract double calculateArea();
@Override
public int compareTo(Shape s) {
if (this.calculateArea() < s.calculateArea()) {
return -1;
} else if (this.calculateArea() > s.calculateArea()) {
return 1;
} else {
return 0;
}
}
}

class Circle extends Shape {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}

class Rectangle extends Shape {
private Point topLeft;
private Point bottomRight;

public Rectangle(Point topLeft, Point bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}

@Override
public double calculateArea() {
double width = Math.abs(bottomRight.getX() - topLeft.getX());
double height = Math.abs(topLeft.getY() - bottomRight.getY());
return width * height;
}
}

class Point {
private double x;
private double y;

public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public double getY() {
return y;
}
}

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Shape> shapes = new ArrayList<>();

int choice = input.nextInt();

while (choice != 0) {
switch (choice) {
case 1: // Circle
double radius = input.nextDouble();
Shape circle = new Circle(radius);
shapes.add(circle);
break;
case 2: // Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point topLeft = new Point(x1, y1);
Point bottomRight = new Point(x2, y2);
Shape rectangle = new Rectangle(topLeft, bottomRight);
shapes.add(rectangle);
break;
}
choice = input.nextInt();
}

shapes.sort(Comparator.naturalOrder());

for (Shape shape : shapes) {
System.out.print(String.format("%.2f", shape.calculateArea()) + " ");
}
}
}

 

 

 在抽象類shape中設(shè)計(jì)了一個comprable的接口,定義圖形比較方法,通過圓形類和矩形類繼承該抽象類,分別重寫計(jì)算面積的方法,在Main中,用該接口的比較方法對給出參數(shù)的圖形進(jìn)行自然排序并輸出。

該代碼復(fù)雜度及行數(shù)不多,在這里不贅述其這方面的分析圖報表。

六、問題分析

① 一開始在寫菜單計(jì)價程序3時,對類的多態(tài)和面向?qū)ο笏枷氩惶私猓瑢?dǎo)致在new一個新的record或者dish對象時,不能準(zhǔn)確的傳出或傳入?yún)?shù),導(dǎo)致最后的代碼依然存在問題。

② 菜單計(jì)價程序4、5代碼復(fù)雜度較高,可讀性較差且代碼的邏輯較為混亂,只顧著完成而未注意其代碼運(yùn)存及圈復(fù)雜度。

七、總結(jié)

這幾次的作業(yè)讓我對面向?qū)ο蟮乃枷牒秃锰幱辛烁钜徊降牧私猓ㄟ^這幾周的學(xué)習(xí),我進(jìn)一步掌握了java語言的語法以及面向?qū)ο缶幊蹋瑸槲医酉聛淼膶W(xué)習(xí)和生活打下了良好的基礎(chǔ),讓我受益良多。