第二次博客作業
前言
第四次大作業:本次大作業有四道題,其中有兩道是菜單計價程序,相較于后兩次的菜單計價程序來說還是相對簡單不少的
第五次大作業:本次作業只有《菜單計價程序-4》一道題,因為涉及到錯誤判斷,比起《菜單計價程序-3》,代碼量極速上升,大概是《菜單計價程序-3》的兩倍,一開始沒上課的時候,老師還沒給我們講關于正則表達式的用法,所以在判斷菜單4新增的錯誤輸入的時候顯得及其的復雜。當學會如何使用正則表達式時候,代碼量下降了許多,但是還是十分復雜,測試點也及其得多,最后我還是沒有做到滿分
第六次大作業:本次作業也是只有《 菜單計價程序-5》一道題,本次作業也是根據《 菜單計價程序-3》迭代而來,雖然是根據根據菜單3迭代而來,但是我的初始代碼仍是在菜單4的基礎上刪除錯誤判斷而來,因為在做菜單4的過程中,發現了菜單3代碼的很多不足,所以初始代碼選擇了在菜單4的基礎上刪減錯誤判斷。本次作業增加了特殊菜的概念,輸入輸出的內容更加豐富,但是比較第四次大作業,還是要更簡單一點的
期中考試:相比我們的PTA大作業,這次期中考試倒是相當簡單,考的基本是基礎知識,,基本按題目寫就可以全部過,但是最后一題編程題由于時間的問題,沒有來得及寫完。但是在考試題目里還有一些選擇題,是一些java里的概念題,有好多道題目不清楚,所以在這方面我還得加強。
設計與分析
菜單計價程序-4
本體大部分內容與菜單計價程序-3相同,增加的部分用加粗文字進行了標注。
設計點菜計價程序,根據輸入的信息,計算并輸出總價格。
輸入內容按先后順序包括兩部分:菜單、訂單,最后以"end"結束。
菜單由一條或多條菜品記錄組成,每條記錄一行
每條菜品記錄包含:菜名、基礎價格 兩個信息。
訂單分:桌號標識、點菜記錄和刪除信息、代點菜信息。每一類信息都可包含一條或多條記錄,每條記錄一行或多行。
桌號標識獨占一行,包含兩個信息:桌號、時間。
桌號以下的所有記錄都是本桌的記錄,直至下一個桌號標識。
點菜記錄包含:序號、菜名、份額、份數。份額可選項包括:1、2、3,分別代表小、中、大份。
不同份額菜價的計算方法:小份菜的價格=菜品的基礎價格。中份菜的價格=菜品的基礎價格1.5。小份菜的價格=菜品的基礎價格2。如果計算出現小數,按四舍五入的規則進行處理。
刪除記錄格式:序號 delete
標識刪除對應序號的那條點菜記錄。
如果序號不對,輸出"delete error"
代點菜信息包含:桌號 序號 菜品名稱 份額 分數
代點菜是當前桌為另外一桌點菜,信息中的桌號是另一桌的桌號,帶點菜的價格計算在當前這一桌。
程序最后按輸入的桌號從小到大的順序依次輸出每一桌的總價(注意:由于有代點菜的功能,總價不一定等于當前桌上的菜的價格之和)。
每桌的總價等于那一桌所有菜的價格之和乘以折扣。如存在小數,按四舍五入規則計算,保留整數。
折扣的計算方法(注:以下時間段均按閉區間計算):
周一至周五營業時間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余時間不營業。
周末全價,營業時間:9:30-21:30
如果下單時間不在營業范圍內,輸出"table " + t.tableNum + " out of opening hours"
參考以下類的模板進行設計(本內容與計價程序之前相同,其他類根據需要自行定義):
菜品類:對應菜譜上一道菜的信息。
Dish {
String name;//菜品名稱
int unit_price; //單價
int getPrice(int portion)//計算菜品價格的方法,輸入參數是點菜的份額(輸入數據只能是1/2/3,代表小/中/大份) }
菜譜類:對應菜譜,包含飯店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品數組,保存所有菜品信息
Dish searthDish(String dishName)//根據菜名在菜譜中查找菜品信息,返回Dish對象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
點菜記錄類:保存訂單上的一道菜品記錄
Record {
int orderNum;//序號
Dish d;//菜品\\
int portion;//份額(1/2/3代表小/中/大份)
int getPrice()//計價,計算本條記錄的價格
}
訂單類:保存用戶點的所有菜的信息。
Order {
Record[] records;//保存訂單上每一道的記錄
int getTotalPrice()//計算訂單的總價
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單中。
delARecordByOrderNum(int orderNum)//根據序號刪除一條記錄
findRecordByNum(int orderNum)//根據序號查找一條記錄
}
本次課題比菜單計價系列-3增加的異常情況:
1、菜譜信息與訂單信息混合,應忽略夾在訂單信息中的菜譜信息。輸出:"invalid dish"
2、桌號所帶時間格式合法(格式見輸入格式部分說明,其中年必須是4位數字,月、日、時、分、秒可以是1位或2位數),數據非法,比如:2023/15/16 ,輸出桌號+" date error"
3、同一桌菜名、份額相同的點菜記錄要合并成一條進行計算,否則可能會出現四舍五入的誤差。
4、重復刪除,重復的刪除記錄輸出"deduplication :"+序號。
5、代點菜時,桌號不存在,輸出"Table number :"+被點菜桌號+" does not exist";本次作業不考慮兩桌記錄時間不匹配的情況。
6、菜譜信息中出現重復的菜品名,以最后一條記錄為準。
7、如果有重復的桌號信息,如果兩條信息的時間不在同一時間段,(時段的認定:周一到周五的中午或晚上是同一時段,或者周末時間間隔1小時(不含一小時整,精確到秒)以內算統一時段),此時輸出結果按不同的記錄分別計價。
8、重復的桌號信息如果兩條信息的時間在同一時間段,此時輸出結果時合并點菜記錄統一計價。前提:兩個的桌號信息的時間都在有效時間段以內。計算每一桌總價要先合并符合本條件的飯桌的點菜記錄,統一計價輸出。
9、份額超出范圍(1、2、3)輸出:序號+" portion out of range "+份額,份額不能超過1位,否則為非法格式,參照第13條輸出。
10、份數超出范圍,每桌不超過15份,超出范圍輸出:序號+" num out of range "+份數。份數必須為數值,最高位不能為0,否則按非法格式參照第16條輸出。
11、桌號超出范圍[1,55]。輸出:桌號 +" table num out of range",桌號必須為1位或多位數值,最高位不能為0,否則按非法格式參照第16條輸出。
12、菜譜信息中菜價超出范圍(區間(0,300)),輸出:菜品名+" price out of range "+價格,菜價必須為數值,最高位不能為0,否則按非法格式參照第16條輸出。
13、時間輸入有效但超出范圍[2022.1.1-2023.12.31],輸出:"not a valid time period"
14、一條點菜記錄中若格式正確,但數據出現問題,如:菜名不存在、份額超出范圍、份數超出范圍,按記錄中從左到右的次序優先級由高到低,輸出時只提示優先級最高的那個錯誤。
15、每桌的點菜記錄的序號必須按從小到大的順序排列(可以不連續,也可以不從1開始),未按序排列序號的輸出:"record serial number sequence error"。當前記錄忽略。(代點菜信息的序號除外)
16、所有記錄其它非法格式輸入,統一輸出"wrong format"
17、如果記錄以“table”開頭,對應記錄的格式或者數據不符合桌號的要求,那一桌下面定義的所有信息無論正確或錯誤均忽略,不做處理。如果記錄不是以“table”開頭,比如“tab le 55 2023/3/2 12/00/00”,該條記錄認為是錯誤記錄,后面所有的信息并入上一桌一起計算。
本次作業比菜單計價系列-3增加的功能:
菜單輸入時增加特色菜,特色菜的輸入格式:菜品名+英文空格+基礎價格+"T"
例如:麻婆豆腐 9 T
菜價的計算方法:
周一至周五 7折, 周末全價。
注意:不同的四舍五入順序可能會造成誤差,請按以下步驟累計一桌菜的菜價:
計算每條記錄的菜價:將每份菜的單價按份額進行四舍五入運算后,乘以份數計算多份的價格,然后乘以折扣,再進行四舍五入,得到本條記錄的最終支付價格。
最后將所有記錄的菜價累加得到整桌菜的價格。
代碼如下:
import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalTime; import java.time.temporal.ChronoField; import java.util.Scanner; class Table { private int num; private Order order; private LocalDate date; private LocalTime time; private double total; public Table(int num,String date,String time) { this.num = num; if(check(date)) { String[] DATE = date.split("/"); this.date = LocalDate.of(Integer.parseInt(DATE[0]),Integer.parseInt(DATE[1]),Integer.parseInt(DATE[2])); } else this.date = LocalDate.of(1,1,1); String[] TIME = time.split("/"); this.time = LocalTime.of(Integer.parseInt(TIME[0]),Integer.parseInt(TIME[1]),Integer.parseInt(TIME[2])); this.total = 0.0; } public LocalTime getTime() { return time; } public double getTotal() { return total; } public Order getOrder() { return order; } public LocalDate getDate() { return date; } public void setOrder(Order order) { this.order = order; } public int getNum() { return num; } public int getWeek() { return date.get(ChronoField.DAY_OF_WEEK); } public int OpeningHours(int week,LocalTime time,boolean T) { if(week <= 5) { if(T) { return 7; } else { if(time.isAfter(LocalTime.of(16,59,59))&&time.isBefore(LocalTime.of(20,30,1))) return 8; else if(time.isAfter(LocalTime.of(10,29,59))&&time.isBefore(LocalTime.of(14,30,1))) return 6; else return -1; } } else { if(time.isAfter(LocalTime.of(9,30,0))&&time.isBefore(LocalTime.of(21,30,0))) return 10; else return -1; } } public int getDiscountedPrice() { int discountedPrice = 0; Record[] records = order.getRecords(); double[] price = new double[records.length]; for(int i = 0; i < records.length; i++) { price[i] = records[i].getPrice()*OpeningHours(getWeek(),getTime(),records[i].getDish().getT())*0.1; if(price[i] - (int)price[i] >= 0.5) price[i] = (int)price[i] + 1; else price[i] = (int)price[i]; } for(int i = 0; i < price.length; i++) discountedPrice += (int)price[i]; return discountedPrice; } public void showTotal() { int judge = OpeningHours(getWeek(),getTime(),false); if(judge == -1) System.out.println("table"+" "+num+"out of opening hours"); else { if(date.isAfter(LocalDate.of(2020,1,1))&&date.isBefore(LocalDate.of(2023,12,31))) System.out.println("table"+" "+num+": "+order.GETALLPrice()+" "+getDiscountedPrice()); else System.out.println("not a valid time period"); } } public static boolean check (String str) { SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd"); try { sd.setLenient(false); sd.parse(str); } catch (Exception e) { return false; } return true; } } public class Main { public static boolean isNumer(String STR1) { if (STR1 == null) { return false; } int SZ = STR1.length(); for (int i = 0; i < SZ; i++) { if (Character.isDigit(STR1.charAt(i)) == false) { return false; } } return true; } public static boolean isTable(String[] table) { if (table.length < 5) { if (table[0].equals("table")) { if (isNumer(table[1])) { if (table[3].length() == 8) return true; else return false; } else return false; } else return false; } else return false; } public static void main(String[] args) { @SuppressWarnings("resource") Scanner input = new Scanner(System.in); String[] newrecord = input.nextLine().split(" "); Dish[] dish = new Dish[10]; for (int i = 0; !newrecord[0].equals("table"); i++) { if (isNumer(newrecord[1])) { if (newrecord.length > 2) { if (newrecord.length < 4) dish[i] = new Dish(newrecord[0], Integer.parseInt(newrecord[1]), true); else { System.out.println("wrong format"); System.exit(0); } } else dish[i] = new Dish(newrecord[0], Integer.parseInt(newrecord[1]), false); if (!(dish[i].getUtilPrice() > 0 && dish[i].getUtilPrice() < 300)) { System.out.println(dish[i].getName() + " price out of range " + dish[i].getUtilPrice()); dish[i] = new Dish(); } } else { dish[i] = new Dish(); System.out.println("wrong format"); } newrecord = input.nextLine().split(" "); } Menu menu = new Menu(dish); if (isTable(newrecord)) { Table[] table = new Table[10]; int TABLENUM = 0; for (TABLENUM = 0; newrecord[0].equals("table"); TABLENUM++) { table[TABLENUM] = new Table(Integer.parseInt(newrecord[1]), newrecord[2], newrecord[3]); System.out.println("table " + table[TABLENUM].getNum() + ": "); Record[] RECORD = new Record[10]; newrecord = input.nextLine().split(" "); for (int j = 0; !(newrecord[1].equals("delete") || isTable(newrecord)); j++) { if (newrecord[0].length() > 2) { if (newrecord.length > 3) RECORD[j] = new Record(1, new Dish(), 0, 0); else System.out.println("invalid dish"); } else { if (menu.SEARCHDish(newrecord[1]).getName().equals("notexist")) { System.out.println(newrecord[1] + " does not exist"); RECORD[j] = new Record(); } else { RECORD[j] = new Record(Integer.parseInt(newrecord[0]), menu.SEARCHDish(newrecord[1]),Integer.parseInt(newrecord[2]), Integer.parseInt(newrecord[3])); } } newrecord = input.nextLine().split(" "); if (newrecord[0].equals("end")) break; } Order order = new Order(RECORD, menu); table[TABLENUM].setOrder(order); order.showOrder(); if (!newrecord[0].equals("end")) { while (newrecord[1].equals("delete")) { order.delARecordOrderNum(Integer.parseInt(newrecord[0])); newrecord = input.nextLine().split(" "); if (newrecord[0].equals("end")) break; } } } for (int i = 0; i < TABLENUM; i++) { table[i].showTotal(); } } else System.out.println("wrong format"); } } class Record { private int ORDERNum; private Dish NEWDISH; private int portion; private int num; public Record() { ORDERNum = 0; NEWDISH = new Dish(); portion = 0; num = 0; } public Record(int orderNum,Dish d,int portion,int num) { this.ORDERNum = orderNum; this.NEWDISH = d; this.portion = portion; this.num = num; } public Dish getDish() { return NEWDISH; } public int getOrderNum(){ return ORDERNum; } public int getNum() { return num; } public int getPortion() { return portion; } public void Priceshow() { if(portion < 10) { if(!NEWDISH.getT()&&(portion < 0||portion > 3)) System.out.println(ORDERNum+" num out of range "+portion); else if(NEWDISH.getT()&&(portion < 0||portion > 3)) System.out.println(ORDERNum+" portion out of range "+portion); else { if(num > 15) System.out.println(ORDERNum+" num out of range "+num); else System.out.println(ORDERNum+" "+NEWDISH.getName()+" "+getPrice()); } } else System.out.println("wrong format"); } public int getPrice() { if(!NEWDISH.getT()&&(portion < 0||portion > 3)) return 0; else if(NEWDISH.getT()&&(portion < 0||portion > 3)) return 0; else { if(num > 15) return 0; else return NEWDISH.getPrice(portion)*num; } } } class Menu { private Dish[] dishs; public Menu(Dish[] dishs) { this.dishs = new Dish[dishs.length]; for (int i = 0; i < dishs.length; i++) { if (dishs[i] == null) this.dishs[i] = new Dish(); else this.dishs[i] = dishs[i]; } } public Dish ADDDish(String dishName, int unit_price, boolean FLAG) { return new Dish(dishName, unit_price, FLAG); } public Dish SEARCHDish(String DISHNAME) { int num = 0; Dish flag = new Dish("notexist", 0, false); for (int i = 0; i < dishs.length; i++) { if (dishs[i].getName().equals(DISHNAME)) { num = i; flag = new Dish("exist", 0, false); } } if (flag.getName().equals("exist")) return dishs[num]; else return flag; } } class Dish { private String NAME; private int unit_price; private boolean FLAG; public Dish() { NAME = ""; unit_price = 0; FLAG = false; } public Dish(String name, int util_price, boolean FLAG) { this.NAME = name; this.unit_price = util_price; this.FLAG = FLAG; } public int getUtilPrice() { return unit_price; } public String getName() { return NAME; } public boolean getT() { return FLAG; } public int getPrice(int portion) { double price = 0; if (portion == 1) { price = unit_price; } else if (portion == 2) { price = Math.round((float) (unit_price * 1.5)); } else if (portion == 3) { price = (unit_price * 2); } return (int) price; } } class Order { private Record[] RECORDS; private Menu MENU; public Order(Record[] records,Menu menu) { this.RECORDS = new Record[records.length]; for(int i = 0; i < records.length; i++) if(records[i] == null) this.RECORDS[i] = new Record(); else this.RECORDS[i] = records[i]; this.MENU = menu; } public Record[] getRecords() { return RECORDS; } public Record addARecord(int orderNum,String dishName,int portion,int num) { Dish d = MENU.SEARCHDish(dishName); Record error = new Record(); if(d.getName().equals("")) return error; else return new Record(orderNum,d,portion,num); } public int GETALLPrice() { int TotalPrice = 0; for(int i = 0; i < RECORDS.length; i++) { TotalPrice += RECORDS[i].getPrice(); } return TotalPrice; } public int findRecordNum(int orderNum) { for(int i = 0; i < RECORDS.length; i++) { if(RECORDS[i].getOrderNum() == orderNum) { return i; } } return -1; } public void delARecordOrderNum(int orderNum) { int flag = findRecordNum(orderNum); if(flag != -1) { if(RECORDS[flag].getNum() == 0) System.out.println("deduplication "+ orderNum); RECORDS[flag] = new Record(orderNum,new Dish(),0,0); } else System.out.println("delete error"); } public void showOrder() { for(int i = 0; i < RECORDS.length; i++) { if(RECORDS[i].getNum() != 0) { int m = i; for(m = i; m > 0; m--) { if(RECORDS[m-1].getOrderNum() >= RECORDS[i].getOrderNum()) { System.out.println("record serial number sequence error"); RECORDS[i] = new Record(); break; } } if(m == 0) RECORDS[i].Priceshow(); } else { if(RECORDS[i].getOrderNum() == 1) System.out.println("wrong format"); } } } }
類圖如下:

代碼分析:
這次菜單4的題目是前面菜單計價程序的迭代,增加了很多關于錯誤輸入的判斷,在程序設計過程中如果能夠合理運用正則表達式的處理方法,就可以使代碼變得簡潔許多
菜單計價程序-5
本題在菜單計價程序-3的基礎上增加了部分內容,增加的內容用加粗字體標識。
注意不是菜單計價程序-4,本題和菜單計價程序-4同屬菜單計價程序-3的兩個不同迭代分支。
設計點菜計價程序,根據輸入的信息,計算并輸出總價格。
輸入內容按先后順序包括兩部分:菜單、訂單,最后以"end"結束。
菜單由一條或多條菜品記錄組成,每條記錄一行
每條菜品記錄包含:菜名、基礎價格 三個信息。
訂單分:桌號標識、點菜記錄和刪除信息、代點菜信息。每一類信息都可包含一條或多條記錄,每條記錄一行或多行。
桌號標識獨占一行,包含兩個信息:桌號、時間。
桌號以下的所有記錄都是本桌的記錄,直至下一個桌號標識。
點菜記錄包含:序號、菜名、份額、份數。份額可選項包括:1、2、3,分別代表小、中、大份。
不同份額菜價的計算方法:小份菜的價格=菜品的基礎價格。中份菜的價格=菜品的基礎價格1.5。小份菜的價格=菜品的基礎價格2。如果計算出現小數,按四舍五入的規則進行處理。
刪除記錄格式:序號 delete
標識刪除對應序號的那條點菜記錄。
如果序號不對,輸出"delete error"
代點菜信息包含:桌號 序號 菜品名稱 口味度 份額 份數
代點菜是當前桌為另外一桌點菜,信息中的桌號是另一桌的桌號,帶點菜的價格計算在當前這一桌。
程序最后按輸入的先后順序依次輸出每一桌的總價(注意:由于有代點菜的功能,總價不一定等于當前桌上的菜的價格之和)。
每桌的總價等于那一桌所有菜的價格之和乘以折扣。如存在小數,按四舍五入規則計算,保留整數。
折扣的計算方法(注:以下時間段均按閉區間計算):
周一至周五營業時間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余時間不營業。
周末全價,營業時間:9:30-21:30
如果下單時間不在營業范圍內,輸出"table " + t.tableNum + " out of opening hours"
參考以下類的模板進行設計:菜品類:對應菜譜上一道菜的信息。
Dish {
String name;//菜品名稱
int unit_price; //單價
int getPrice(int portion)//計算菜品價格的方法,輸入參數是點菜的份額(輸入數據只能是1/2/3,代表小/中/大份) }
菜譜類:對應菜譜,包含飯店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品數組,保存所有菜品信息
Dish searthDish(String dishName)//根據菜名在菜譜中查找菜品信息,返回Dish對象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
點菜記錄類:保存訂單上的一道菜品記錄
Record {
int orderNum;//序號\\
Dish d;//菜品\\
int portion;//份額(1/2/3代表小/中/大份)\\
int getPrice()//計價,計算本條記錄的價格\\
}
訂單類:保存用戶點的所有菜的信息。
Order {
Record[] records;//保存訂單上每一道的記錄
int getTotalPrice()//計算訂單的總價
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單
delARecordByOrderNum(int orderNum)//根據序號刪除一條記錄
findRecordByNum(int orderNum)//根據序號查找一條記錄
}
### 輸入格式:
桌號標識格式:table + 序號 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時間(24小時制格式: HH/MM/SS)
菜品記錄格式:
菜名+英文空格+基礎價格
如果有多條相同的菜名的記錄,菜品的基礎價格以最后一條記錄為準。
點菜記錄格式:序號+英文空格+菜名+英文空格+份額+英文空格+份數注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。
刪除記錄格式:序號 +英文空格+delete
代點菜信息包含:桌號+英文空格+序號+英文空格+菜品名稱+英文空格+份額+英文空格+分數
最后一條記錄以“end”結束。
### 輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號,格式:table+英文空格+桌號+”:”
2、按順序輸出當前這一桌每條訂單記錄的處理信息,
每條點菜記錄輸出:序號+英文空格+菜名+英文空格+價格。其中的價格等于對應記錄的菜品\*份數,序號是之前輸入的訂單記錄的序號。如果訂單中包含不能識別的菜名,則輸出“\*\* does not exist”,\*\*是不能識別的菜名
如果刪除記錄的序號不存在,則輸出“delete error”
最后按輸入順序一次輸出每一桌所有菜品的總價(整數數值)格式:table+英文空格+桌號+“:”+英文空格+當前桌的總價
以上為菜單計價系列-3的題目要求,加粗的部分是有調整的內容。本次課題相比菜單計價系列-3新增要求如下:
1、菜單輸入時增加特色菜,特色菜的輸入格式:菜品名+英文空格+口味類型+英文空格+基礎價格+"T"
例如:麻婆豆腐 川菜 9 T
菜價的計算方法:
周一至周五 7折, 周末全價。
特色菜的口味類型:川菜、晉菜、浙菜
川菜增加辣度值:辣度0-5級;對應辣度水平為:不辣、微辣、稍辣、辣、很辣、爆辣;
晉菜增加酸度值,酸度0-4級;對應酸度水平為:不酸、微酸、稍酸、酸、很酸;
浙菜增加甜度值,甜度0-3級;對應酸度水平為:不甜、微甜、稍甜、甜;
例如:麻婆豆腐 川菜 9 T
輸入訂單記錄時如果是特色菜,添加口味度(辣/酸/甜度)值,格式為:序號+英文空格+菜名+英文空格+口味度值+英文空格+份額+英文空格+份數
例如:1 麻婆豆腐 4 1 9
單條信息在處理時,如果口味度超過正常范圍,輸出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根據菜品類型擇一輸出,例如:
acidity num out of range : 5
輸出一桌的信息時,按辣、酸、甜度的順序依次輸出本桌菜各種口味的口味度水平,如果沒有某個類型的菜,對應的口味(辣/酸/甜)度不輸出,只輸出已點的菜的口味度??谖抖人接煽谖抖绕骄荡_定,口味度平均值只綜合對應口味菜系的菜計算,不做所有菜的平均。比如,某桌菜點了3份川菜,辣度分別是1、3、5;還有4份晉菜,酸度分別是,1、1、2、2,辣度平均值為3、酸度平均值四舍五入為2,甜度沒有,不輸出。
一桌信息的輸出格式:table+英文空格+桌號+:+英文空格+當前桌的原始總價+英文空格+當前桌的計算折扣后總價+英文空格+"川菜"+數量+辣度+英文空格+"晉菜"+數量+酸度+英文空格+"浙菜"+數量+甜度。
如果整桌菜沒有特色菜,則只輸出table的基本信息,格式如下,注意最后加一個英文空格:
table+英文空格+桌號+:+英文空格+當前桌的原始總價+英文空格+當前桌的計算折扣后總價+英文空格
例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜
計算口味度時要累計本桌各類菜系所有記錄的口味度總和(每條記錄的口味度乘以菜的份數),再除以對應菜系菜的總份數,最后四舍五入。
注:本題要考慮代點菜的情況,當前桌點的菜要加上被其他桌代點的菜綜合計算口味度平均值。
2、考慮客戶訂多桌菜的情況,輸入時桌號時,增加用戶的信息:
格式:table+英文空格+桌號+英文空格+":"+英文空格+客戶姓名+英文空格+手機號+日期(格式:YYYY/MM/DD)+英文空格+ 時間(24小時制格式: HH/MM/SS)
例如:table 1 : tom 13670008181 2023/5/1 21/30/00
約束條件:客戶姓名不超過10個字符,手機號11位,前三位必須是180、181、189、133、135、136其中之一。
輸出結果時,先按要求輸出每一桌的信息,最后按字母順序依次輸出每位客戶需要支付的金額。不考慮各桌時間段的問題,同一個客戶的所有table金額都要累加。
輸出用戶支付金額格式:
用戶姓名+英文空格+手機號+英文空格+支付金額
注意:不同的四舍五入順序可能會造成誤差,請按以下步驟累計一桌菜的菜價:
計算每條記錄的菜價:將每份菜的單價按份額進行四舍五入運算后,乘以份數計算多份的價格,然后乘以折扣,再進行四舍五入,得到本條記錄的最終支付價格。
將所有記錄的菜價累加得到整桌菜的價格。
輸入格式:
桌號標識格式:table + 序號 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時間(24小時制格式: HH/MM/SS)
菜品記錄格式:
菜名+口味類型+英文空格+基礎價格
如果有多條相同的菜名的記錄,菜品的基礎價格以最后一條記錄為準。
點菜記錄格式:序號+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份額+英文空格+份數 注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范圍見題目中說明。
刪除記錄格式:序號 +英文空格+delete
代點菜信息包含:桌號+英文空格+序號+英文空格+菜品名稱**+英文空格+辣/酸/甜度值+**英文空格+份額+英文空格+分數
最后一條記錄以“end”結束。
輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號,格式:table+英文空格+桌號+“:”+英文空格
2、按順序輸出當前這一桌每條訂單記錄的處理信息,
每條點菜記錄輸出:序號+英文空格+菜名+英文空格+價格。其中的價格等于對應記錄的菜品\*份數,序號是之前輸入的訂單記錄的序號。如果訂單中包含不能識別的菜名,則輸出“\*\* does not exist”,\*\*是不能識別的菜名
如果刪除記錄的序號不存在,則輸出“delete error”
之后按輸入順序一次輸出每一桌所有菜品的價格(整數數值),
格式:table+英文空格+桌號+“:”+英文空格+當前桌的計算折扣后總價+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格
最后按拼音順序輸出每位客戶(不考慮客戶同名或拼音相同的情況)的支付金額,格式: 用戶姓名+英文空格+手機號+英文空格+支付總金額,按輸入順序排列。
代碼如下
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.time.LocalDate; import java.util.ArrayList; class lastPrint{ ArrayList<Table> tables = new ArrayList<>(); PersonPay[] personPays; int p; public void payPrint(){ for(int i=0;i<p;i++){ for(Table table:tables){ if(table.tableName.equals(personPays[i].PERSONname)){ personPays[i].payPrice+=table.tablePrice(); } } } for(int i=0;i<p-1;i++){ for(int l=i+1;l<p;l++){ if(personPays[i].PERSONname.toLowerCase().compareTo(personPays[l].PERSONname.toLowerCase())>0) { PersonPay informmation; informmation=personPays[l]; personPays[l]=personPays[i]; personPays[i]=informmation; } } } for(int k=0;k<p;k++){ System.out.println(personPays[k].PERSONname+" "+personPays[k].TelephoneNum+" "+personPays[k].payPrice); } } public void print(){ for (Table table : tables) { table.order.getTotalPrice(); table.getTaste(); if(table.SpicyNum==0&&table.aciNum==0&&table.SWEETNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice()+" "); if(table.SpicyNum!=0&&table.aciNum==0&&table.SWEETNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()); if(table.SpicyNum==0&&table.aciNum!=0&&table.SWEETNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 晉菜 " +(int)table.aciNum+" "+table.acidityLevel()); if(table.SpicyNum==0&&table.aciNum==0&&table.SWEETNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 浙菜 " +(int)table.SWEETNum+" "+table.sweetnessLevel()); if(table.SpicyNum!=0&&table.aciNum!=0&&table.SWEETNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()+" 晉菜 "+(int)table.aciNum+" "+table.acidityLevel()); if(table.SpicyNum!=0&&table.aciNum==0&&table.SWEETNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()+" 浙菜 "+(int)table.SWEETNum+" "+table.sweetnessLevel()); if(table.SpicyNum==0&&table.aciNum!=0&&table.SWEETNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 晉菜 " +(int)table.aciNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.SWEETNum+" "+table.sweetnessLevel()); if(table.SpicyNum!=0&&table.aciNum!=0&&table.SWEETNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.uaualPrice + table.order.SpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.SpicyNum+" "+table.spicyLevel()+" 晉菜 "+(int)table.aciNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.SWEETNum+" "+table.sweetnessLevel()); } } } public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Table table = new Table(); Record[] records = new Record[20]; Dish[] dishes = new Dish[20]; SpecialDish_taste[] specialDishes = new SpecialDish_taste[20]; Order order = new Order(); Menu menu = new Menu(); ArrayList<Table> tables = new ArrayList<>(); PersonPay[] personPays = new PersonPay[10]; lastPrint PrintMESS = new lastPrint(); int X = 0 , z1 = 0 , per = 0 , Y = 0 , z = 0 , tableNum , portion , num , unit_price ,dishNum, delNum , tNum; String meName , DTime , HTime , name , tabName , telNum , TASTE; boolean havingTable = false , Othertable = false; for(;;){ String input = in.readLine(); if(input.equals("end")){ if(X!=0){ table.order = order; tables.add(X-1 , table); } PrintMESS.personPays = personPays; PrintMESS.tables = tables; PrintMESS.print(); PrintMESS.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); table = new Table(); Y = 0; } havingTable = true; tableNum = Integer.parseInt(getInput[1]); DTime = getInput[5]; HTime = getInput[6]; tabName = getInput[3]; telNum = getInput[4]; table.tableNum = tableNum; table.NORMALTime = DTime; table.tableName = tabName; table.telephoneNum = telNum; table.remainTime = HTime; if(!table.timeJudgement1()){ havingTable = false; System.out.println("table " +table.tableNum + " out of opening hours"); continue; } order = new Order(); records = new Record[10]; if(X==0){ personPays[per] = new PersonPay(); personPays[per].TelephoneNum = telNum; personPays[per].PERSONname = tabName; per++; PrintMESS.p = per; } else{ for(int i=0;i<per;i++){ if(personPays[i].PERSONname.equals(tabName)&&personPays[i].TelephoneNum.equals(telNum)) break; if(i==per-1){ personPays[per] = new PersonPay(); personPays[per].TelephoneNum = telNum; personPays[per].PERSONname = tabName; per++; PrintMESS.p = per; } } } System.out.println("table "+tableNum+": "); X++; } else if(input.matches("^(\\S+)( )(\\S+)( )([1-9][0-9]*)( )(T)$")){ int i = 0; meName = getInput[0]; TASTE = getInput[1]; unit_price = Integer.parseInt(getInput[2]); if(z1 == 0) { specialDishes[0] = menu.addSpecialDish(meName , unit_price , TASTE); z1++; } else{ for(;i < z1 ; i++) { if(meName.equalsIgnoreCase(specialDishes[i].name)) { specialDishes[i].unit_price = unit_price; break; } } } if(i == z1){ specialDishes[z1] = menu.addSpecialDish(meName , 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(!havingTable) continue; dishNum = Integer.parseInt(getInput[0]); name = getInput[1]; portion = Integer.parseInt(getInput[2]); num = Integer.parseInt(getInput[3]); 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(records[Y].hasorder+" "+records[Y].d.name+" "+records[Y].getPrice()); Y++; order.records = records; order.dishNum = Y; } else{ System.out.println(name+" does not exist"); } } else if(input.matches("([1-9][0-9]*)( )(delete)")) { if(!havingTable) continue; delNum = Integer.parseInt(getInput[0]); if(order.FindRcord(delNum) == 1){ order.delARecordByOrderNum(delNum); } else System.out.println("delete error;"); } else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([0-9]+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){ if(!havingTable) continue; dishNum = Integer.parseInt(getInput[0]); name = getInput[1]; tNum = Integer.parseInt(getInput[2]); num = Integer.parseInt(getInput[4]); portion = Integer.parseInt(getInput[3]); if(menu.searthSpecialDish(name) != null) { if(((menu.searthSpecialDish(name).taste.equals("川菜") && tNum >= 0 && tNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晉菜") && tNum>=0 && tNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tNum>=0 && tNum <= 3))){ records[Y] = new Record(); records[Y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tNum , Othertable); records[Y].ISSPECIALDISH = true; System.out.println(records[Y].hasorder+" "+records[Y].d.name+" "+records[Y].getPrice()); Y++; order.records = records; order.dishNum = Y; } } else{ System.out.println(name+" does not exist"); continue; } if(menu.searthSpecialDish(name).taste.equals("浙菜")){ if(tNum<0||tNum>3) System.out.println("sweetness num out of range :"+tNum); } if(menu.searthSpecialDish(name).taste.equals("川菜")){ if(tNum<0||tNum>5) System.out.println("spicy num out of range :"+tNum); } if(menu.searthSpecialDish(name).taste.equals("晉菜")){ if(tNum<0||tNum>4) System.out.println("acidity num out of range :"+tNum); } } else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([0-9]*)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) { if(!havingTable) continue; int t = Integer.parseInt(getInput[0]); dishNum = Integer.parseInt(getInput[1]); name = getInput[2]; tNum = 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("川菜") && tNum >= 1 && tNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晉菜") && tNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tNum <= 3))){ records[Y] = new Record(); records[Y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tNum , !Othertable); records[Y].ISSPECIALDISH = true; System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[Y].getPrice()); tables.get(i).giveTaste(name,num,tNum,menu); Y++; order.records = records; order.dishNum = Y; } } break; } } } else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) { if(!havingTable) 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("^(\\S+)( )([1-9][0-9]*)$")){ int i = 0; meName = getInput[0]; unit_price = Integer.parseInt(getInput[1]); if(z == 0) { dishes[0] = menu.addDish(meName , unit_price); z++; } else{ for(;i < z ; i++) { if(meName.equalsIgnoreCase(dishes[i].name)) { dishes[i].unit_price = unit_price; break; } } } if(i == z){ dishes[z] = menu.addDish(meName , unit_price); z++; } menu.dishes = dishes; menu.dishNum2 = z; } else { System.out.println("wrong format"); } } } } class SpecialDish_taste extends Dish{ String taste; } class Dish{ int unit_price; String name; public int GETPRICE(int portion){ double get_Price=0; if(portion==1) get_Price=unit_price; else if(portion==2) get_Price=unit_price*1.5; else if(portion==3) get_Price=unit_price*2; return (int) Math.round(get_Price); } } class Record{ int aci; int SWEET; boolean ISSPECIALDISH; int hasorder; Dish d; int portion; int Spicy; int num; public int getPrice() { return d.GETPRICE(portion)*num; } } class PersonPay{ String PERSONname; String TelephoneNum; int payPrice; } class Table{ double SpicyNum; double aciNum; double SWEETNum; int tableNum; String tableName; String telephoneNum; String NORMALTime; String remainTime; Order order; double cDiscount; double sDiscount; int aveSpicy; int aveAcidity; int aveSweetness; public boolean timeJudgement1(){ String[] x = remainTime.split("/"); String[] y = NORMALTime.split("/"); double hour = Double.parseDouble(x[0]); double minute = Double.parseDouble(x[1]); double second = Double.parseDouble(x[2]); int year = Integer.parseInt(y[0]); int month = Integer.parseInt(y[1]); int day = Integer.parseInt(y[2]); LocalDate date = LocalDate.of(year,month,day); int weekOfDay = date.getDayOfWeek().getValue(); if (weekOfDay >= 1 && weekOfDay <= 5) { if (hour > 17 && hour < 20 || hour == 17 && minute >= 0 || hour == 20 && minute < 30 || hour == 20 && minute == 30 && second == 0) { cDiscount = 0.8; sDiscount = 0.7; return true; } else if (hour > 10 && hour < 14 || hour == 10 && minute >= 30 || hour == 14 && minute < 30 || hour == 14 && minute == 30 && second == 0) { cDiscount = 0.6; sDiscount = 0.7; return true; } else { return false; } } if (weekOfDay == 6 || weekOfDay == 7) { if (hour >= 10 && hour < 21 || hour == 9 && minute >= 30 || hour == 21 && minute < 30 ||hour == 21 && minute == 30 && second == 0) { sDiscount = cDiscount = 1; return true; } else { return false; } } return false; } public int tablePrice(){ return this.order.getTotalPrice(cDiscount,sDiscount); } public void getTaste(){ for(int i = 0;i<this.order.dishNum;i++){ if(order.records[i].ISSPECIALDISH){ if(order.records[i].SWEET!=-1){ aveSweetness+=order.records[i].SWEET*order.records[i].num; SWEETNum=order.records[i].num+SWEETNum; } if(order.records[i].Spicy!=-1){ aveSpicy+=order.records[i].Spicy*order.records[i].num; SpicyNum=order.records[i].num+SpicyNum; } if(order.records[i].aci!=-1){ aveAcidity+=order.records[i].aci*order.records[i].num; aciNum=order.records[i].num+aciNum; } } } if(SpicyNum!=0){ aveSpicy=(int)Math.round(aveSpicy/SpicyNum); } if(SWEETNum!=0){ aveSweetness=(int)Math.round(aveSweetness/SWEETNum); } if(aciNum!=0){ aveAcidity=(int)Math.round(aveAcidity/aciNum); } } public String acidityLevel(){ if(aveAcidity==0) return "不酸"; if(aveAcidity==1) return "微酸"; if(aveAcidity==2) return "稍酸"; if(aveAcidity==3) return "酸"; if(aveAcidity==4) return "很酸"; return null; } public String spicyLevel(){ if(aveSpicy==0) return "不辣"; if(aveSpicy==1) return "微辣"; if(aveSpicy==2) return "稍辣"; if(aveSpicy==3) return "辣"; if(aveSpicy==4) return "很辣"; if(aveSpicy==5) return "爆辣"; return null; } public String sweetnessLevel(){ if(aveSweetness==0) return "不甜"; if(aveSweetness==1) return "微甜"; if(aveSweetness==2) return "稍甜"; if(aveSweetness==3) return "甜"; return null; } public void giveTaste(String name , int num , int tLevel , Menu menu){ if(menu.searthSpecialDish(name).taste.equals("浙菜")){ aveSweetness+=tLevel*num; SWEETNum+=num; } if(menu.searthSpecialDish(name).taste.equals("川菜")){ aveSpicy+=tLevel*num; SpicyNum+=num; } if(menu.searthSpecialDish(name).taste.equals("晉菜")){ aveAcidity+=tLevel*num; aciNum+=num; } } } class Order { int dishNum; int uaualPrice; int SpecialPrice; Record[] records; public void addPortion() { for(int i = 0;i < dishNum-1;i++){ for(int l = i+1;l<dishNum;l++){ if(records[i].d.name.equals(records[l].d.name)&&records[i].portion == records[l].portion){ records[i].num+=records[l].num; records[l].num = 0; } } } } public int getTotalPrice(double commonDiscount,double specialDiscount){ int allmoney = 0; for(int i=0;i<dishNum;i++) { if(!records[i].ISSPECIALDISH) allmoney+=Math.round(records[i].getPrice()*commonDiscount); else allmoney+=Math.round(records[i].getPrice()*specialDiscount); } return allmoney; } public void getTotalPrice(){ for(int k=0;k<dishNum;k++) { if(!records[k].ISSPECIALDISH) uaualPrice+=records[k].getPrice(); else SpecialPrice+=records[k].getPrice(); } } public Record addARecord(int orderNum,String dishName,int portion,int num , Menu menu){ Record newrecord=new Record(); newrecord.portion=portion; newrecord.num=num; newrecord.d=menu.searthDish(dishName); newrecord.hasorder=orderNum; return newrecord; } public Record addASpecialRecord(int orderNum,String dishName,int portion,int num ,Menu menu,int tasteNum , boolean forOther){ Record newrecord=new Record(); newrecord.d=menu.searthSpecialDish(dishName); newrecord.hasorder=orderNum; newrecord.portion=portion; newrecord.num=num; if(!forOther){ if(menu.searthSpecialDish(dishName).taste.equals("浙菜")){ newrecord.SWEET = tasteNum; newrecord.Spicy = -1; newrecord.aci = -1; } if(menu.searthSpecialDish(dishName).taste.equals("晉菜")){ newrecord.aci = tasteNum; newrecord.Spicy = -1; newrecord.SWEET = -1; } if(menu.searthSpecialDish(dishName).taste.equals("川菜")){ newrecord.Spicy = tasteNum; newrecord.aci = -1; newrecord.SWEET = -1; } } else{ newrecord.aci = -1; newrecord.Spicy = -1; newrecord.SWEET = -1; } return newrecord; } public int FindRcord(int orderNum){ for(int i = 0; i<dishNum;i++) { if(records[i].hasorder==orderNum) { return 1; } } return 0; } public void delARecordByOrderNum(int orderNum){ for(int i=0;i<dishNum;i++) { if(orderNum==records[i].hasorder) { records[i].num=0; } } } } class Menu{ int dishNum2; int dishNum1; Dish[] dishes; SpecialDish_taste[] specialDishes; public SpecialDish_taste searthSpecialDish(String dishName){ for(int k=0;k<dishNum1;k++) { if(dishName.equals(specialDishes[k].name)){ return specialDishes[k]; } } return null; } public Dish searthDish(String dishName){ for(int k=0;k<dishNum2;k++) { if(dishName.equals(dishes[k].name)){ return dishes[k]; } } return null; } public SpecialDish_taste addSpecialDish(String dishName,int unit_price,String taste){ SpecialDish_taste addDish=new SpecialDish_taste(); addDish.name=dishName; addDish.unit_price=unit_price; addDish.taste=taste; return addDish; } public Dish addDish(String dishName,int unit_price){ Dish addDish=new Dish(); addDish.name=dishName; addDish.unit_price=unit_price; return addDish; } }
類圖如下:

期中考試
7-1 圓類設計
創建一個圓形類(Circle),私有屬性為圓的半徑,從控制臺輸入圓的半徑,輸出圓的面積
輸入格式:
輸入圓的半徑,取值范圍為(0,+∞),輸入數據非法,則程序輸出Wrong Format,注意:只考慮從控制臺輸入數值的情況
輸出格式:
輸出圓的面積(保留兩位小數,可以使用String.format(“%.2f”,輸出數值)控制精度)
代碼如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { double r; Scanner input = new Scanner(System.in); r = input.nextDouble(); if(r>0) { Circle circle = new Circle(r); double Area; Area = circle.getArea(); System.out.println(String.format("%.2f", Area)); } else System.out.println("Wrong Format"); } } class Circle { double r; public Circle (double r) { this.r = r; } public double getradius() { return r; } public void setradius() { this.r = r; } public double getArea() { return r*r*Math.PI; } }
此題較為簡單,根據題目的要求就能完成
7-2 類結構設計
設計一個矩形類,其屬性由矩形左上角坐標點(x1,y1)及右下角坐標點(x2,y2)組成,其中,坐標點屬性包括該坐標點的X軸及Y軸的坐標值(實型數),求得該矩形的面積。類設計如下圖:

輸入格式:
分別輸入兩個坐標點的坐標值x1,y1,x2,y2。
輸出格式:
輸出該矩形的面積值(保留兩位小數)。
代碼如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double x1; double x2; double y1; double y2; double Area; x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); Area = Math.abs((x1-x2)) * Math.abs((y1-y2)); System.out.println(String.format("%.2f", Area)); } }
這題根據題目里面提供的類圖,設計出相對應的類,就能完成題目的要求
7-3 繼承與多態
將測驗1與測驗2的類設計進行合并設計,抽象出Shape父類(抽象類),Circle及Rectangle作為子類,類圖如下所示:

試編程完成如上類圖設計,主方法源碼如下(可直接拷貝使用)
public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int choice = input.nextInt(); switch(choice) { case 1://Circle double radiums = input.nextDouble(); Shape circle = new Circle(radiums); printArea(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); printArea(rectangle); break; } }
其中,printArea(Shape shape)方法為定義在Main類中的靜態方法,體現程序設計的多態性。
輸入格式:
輸入類型選擇(1或2,不考慮無效輸入)
對應圖形的參數(圓或矩形)
輸出格式:
圖形的面積(保留兩位小數)
代碼如下:
import java.util.Scanner; public class Main { public static void printArea(Shape shape) { System.out.printf("%.2f", shape.getArea()); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int choice = input.nextInt(); switch (choice) { case 1:// Circle double radiums = input.nextDouble(); if (radiums > 0) { Shape circle = new Circle(radiums); printArea(circle); } else System.out.println("Wrong Format"); 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); printArea(rectangle); break; } } } abstract class Shape { public abstract double getArea(); } class Point { double x; double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX() { this.x = x; } public double getY() { return y; } public void setY() { this.y = y; } } class Circle extends Shape{ double r; public Circle (double r) { this.r = r; } public double getradius() { return r; } public void setradius() { this.r = r; } public double getArea() { return r*r*Math.PI; } } class Rectangle extends Shape{ Point topleftpoint; Point lowerrightpoint; public Rectangle(Point topleftpoint,Point lowerrightpoint) { this.topleftpoint = topleftpoint; this.lowerrightpoint = lowerrightpoint; } public double getArea() { double hight = Math.abs(topleftpoint.getY()-lowerrightpoint.getY()); double width = Math.abs(topleftpoint.getX()-lowerrightpoint.getX()); double area = hight * width; return area; } }
踩坑心得
在pta作業中,幾次菜單計價程序的迭代題目比較復雜,尤其是菜單四里面的檢測錯誤輸入尤為繁瑣,但是在使用正則表達式之后就簡便了許多
期中考試的題目相比較大作業來說是較為簡單的。在做圓類設計的那道題的時候,在計算圓的面積的時候,我用了3.14導致測試點過不去,后來才知道圓周率在Java里面有表達的公式為Math.PI
總結
大作業的難度逐漸上升,需要熟練的掌握Java中類的使用及Object和String類中一些方法的使用。其中,最主要的是對類的使用,如類的繼承、封裝和多態性等。更加深入地理解面向對象的編程思想,熟悉Java編程語言的類和對象的使用方法。重點是對Javabean的基本使用,類的廣泛應用。通過這些練習,我們可以更深入地掌握Java編程語言的常用技術,如數據結構、輸入輸出、異常處理等。在今后的學習中,我要更加注重編程思想的學習,這樣才能更快更好的設計出代碼。
浙公網安備 33010602011771號