題目集4、5、6
題量中等,難度中等
設(shè)計(jì)點(diǎn)菜計(jì)價(jià)程序,根據(jù)輸入的信息,計(jì)算并輸出總價(jià)格。
輸入內(nèi)容按先后順序包括兩部分:菜單、訂單,最后以"end"結(jié)束。
菜單由一條或多條菜品記錄組成,每條記錄一行
每條菜品記錄包含:菜名、基礎(chǔ)價(jià)格 兩個(gè)信息。
訂單分:桌號(hào)標(biāo)識(shí)、點(diǎn)菜記錄和刪除信息、代點(diǎn)菜信息。每一類信息都可包含一條或多條記錄,每條記錄一行或多行。
桌號(hào)標(biāo)識(shí)獨(dú)占一行,包含兩個(gè)信息:桌號(hào)、時(shí)間。
桌號(hào)以下的所有記錄都是本桌的記錄,直至下一個(gè)桌號(hào)標(biāo)識(shí)。
點(diǎn)菜記錄包含:序號(hào)、菜名、份額、份數(shù)。份額可選項(xiàng)包括:1、2、3,分別代表小、中、大份。
不同份額菜價(jià)的計(jì)算方法:小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格。中份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格1.5。小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格2。如果計(jì)算出現(xiàn)小數(shù),按四舍五入的規(guī)則進(jìn)行處理。
刪除記錄格式:序號(hào) delete
標(biāo)識(shí)刪除對(duì)應(yīng)序號(hào)的那條點(diǎn)菜記錄。
如果序號(hào)不對(duì),輸出"delete error"
代點(diǎn)菜信息包含:桌號(hào) 序號(hào) 菜品名稱 份額 分?jǐn)?shù)
代點(diǎn)菜是當(dāng)前桌為另外一桌點(diǎn)菜,信息中的桌號(hào)是另一桌的桌號(hào),帶點(diǎn)菜的價(jià)格計(jì)算在當(dāng)前這一桌。
程序最后按輸入的先后順序依次輸出每一桌的總價(jià)(注意:由于有代點(diǎn)菜的功能,總價(jià)不一定等于當(dāng)前桌上的菜的價(jià)格之和)。
每桌的總價(jià)等于那一桌所有菜的價(jià)格之和乘以折扣。如存在小數(shù),按四舍五入規(guī)則計(jì)算,保留整數(shù)。
折扣的計(jì)算方法(注:以下時(shí)間段均按閉區(qū)間計(jì)算):
周一至周五營(yíng)業(yè)時(shí)間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余時(shí)間不營(yíng)業(yè)。
周末全價(jià),營(yíng)業(yè)時(shí)間:9:30-21:30
如果下單時(shí)間不在營(yíng)業(yè)范圍內(nèi),輸出"table " + t.tableNum + " out of opening hours"
參考以下類的模板進(jìn)行設(shè)計(jì):菜品類:對(duì)應(yīng)菜譜上一道菜的信息。
Dish {
String name;//菜品名稱
int unit_price; //單價(jià)
int getPrice(int portion)//計(jì)算菜品價(jià)格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份) }
菜譜類:對(duì)應(yīng)菜譜,包含飯店提供的所有菜的信息。
Menu {
Dish\[\] dishs ;//菜品數(shù)組,保存所有菜品信息
Dish searthDish(String dishName)//根據(jù)菜名在菜譜中查找菜品信息,返回Dish對(duì)象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
點(diǎn)菜記錄類:保存訂單上的一道菜品記錄
Record {
int orderNum;//序號(hào)\\
Dish d;//菜品\\
int portion;//份額(1/2/3代表小/中/大份)\\
int getPrice()//計(jì)價(jià),計(jì)算本條記錄的價(jià)格\\
}
訂單類:保存用戶點(diǎn)的所有菜的信息。
Order {
Record\[\] records;//保存訂單上每一道的記錄
int getTotalPrice()//計(jì)算訂單的總價(jià)
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單中。
delARecordByOrderNum(int orderNum)//根據(jù)序號(hào)刪除一條記錄
findRecordByNum(int orderNum)//根據(jù)序號(hào)查找一條記錄
}
### 輸入格式:
桌號(hào)標(biāo)識(shí)格式:table + 序號(hào) +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時(shí)間(24小時(shí)制格式: HH/MM/SS)
菜品記錄格式:
菜名+英文空格+基礎(chǔ)價(jià)格
如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價(jià)格以最后一條記錄為準(zhǔn)。
點(diǎn)菜記錄格式:序號(hào)+英文空格+菜名+英文空格+份額+英文空格+份數(shù)注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。
刪除記錄格式:序號(hào) +英文空格+delete
代點(diǎn)菜信息包含:桌號(hào)+英文空格+序號(hào)+英文空格+菜品名稱+英文空格+份額+英文空格+分?jǐn)?shù)
最后一條記錄以“end”結(jié)束。
### 輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號(hào),格式:table+英文空格+桌號(hào)+”:”
2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,
每條點(diǎn)菜記錄輸出:序號(hào)+英文空格+菜名+英文空格+價(jià)格。其中的價(jià)格等于對(duì)應(yīng)記錄的菜品\*份數(shù),序號(hào)是之前輸入的訂單記錄的序號(hào)。如果訂單中包含不能識(shí)別的菜名,則輸出“\*\* does not exist”,\*\*是不能識(shí)別的菜名
如果刪除記錄的序號(hào)不存在,則輸出“delete error”
最后按輸入順序一次輸出每一桌所有菜品的總價(jià)(整數(shù)數(shù)值)格式:table+英文空格+桌號(hào)+“:”+英文空格+當(dāng)前桌的總價(jià)
本次題目不考慮其他錯(cuò)誤情況,如:桌號(hào)、菜單訂單順序顛倒、不符合格式的輸入、序號(hào)重復(fù)等,在本系列的后續(xù)作業(yè)中會(huì)做要求。
輸入格式:
桌號(hào)標(biāo)識(shí)格式:table + 序號(hào) +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時(shí)間(24小時(shí)制格式: HH/MM/SS)
菜品記錄格式:
菜名+英文空格+基礎(chǔ)價(jià)格
如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價(jià)格以最后一條記錄為準(zhǔn)。
點(diǎn)菜記錄格式:序號(hào)+英文空格+菜名+英文空格+份額+英文空格+份數(shù)注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。
刪除記錄格式:序號(hào) +英文空格+delete
代點(diǎn)菜信息包含:桌號(hào)+英文空格+序號(hào)+英文空格+菜品名稱+英文空格+份額+英文空格+分?jǐn)?shù)
最后一條記錄以“end”結(jié)束。
輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號(hào),格式:table+英文空格+桌號(hào)+“:”+英文空格
2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,
每條點(diǎn)菜記錄輸出:序號(hào)+英文空格+菜名+英文空格+價(jià)格。其中的價(jià)格等于對(duì)應(yīng)記錄的菜品\*份數(shù),序號(hào)是之前輸入的訂單記錄的序號(hào)。如果訂單中包含不能識(shí)別的菜名,則輸出“\*\* does not exist”,\*\*是不能識(shí)別的菜名
如果刪除記錄的序號(hào)不存在,則輸出“delete error”
最后按輸入順序一次輸出每一桌所有菜品的總價(jià)(整數(shù)數(shù)值)格式:table+英文空格+桌號(hào)+“:”+英文空格+當(dāng)前桌的總價(jià)
本次題目不考慮其他錯(cuò)誤情況,如:桌號(hào)、菜單訂單順序顛倒、不符合格式的輸入、序號(hào)重復(fù)等,在本系列的后續(xù)作業(yè)中會(huì)做要求。
import java.util.*; // 菜品類 class Dish { private String name; // 菜名 private int price; // 價(jià)格 public Dish(String name, int price) { this.name = name; this.price = price; } public String getName() { return name; } public int getPrice() { return price; } } // 訂單記錄類 class Record { private Dish dish; // 菜品 private boolean isSubstitute; // 是否代點(diǎn)菜 public Record(Dish dish, boolean isSubstitute) { this.dish = dish; this.isSubstitute = isSubstitute; } public Dish getDish() { return dish; } public boolean isSubstitute() { return isSubstitute; } } // 訂單類 class Order { private List<Record> records; // 訂單記錄列表 public Order() { this.records = new ArrayList<>(); } public void addARecord(Record record) { records.add(record); } public List<Record> getRecords() { return records; } } // 菜單類 class Menu { private Map<String, Dish> dishes; // 菜品列表 public Menu() { this.dishes = new HashMap<>(); } public void addDish(String name, int price) { Dish dish = new Dish(name, price); dishes.put(name, dish); } public Dish getDishByName(String name) { return dishes.getOrDefault(name, null); } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Menu menu = new Menu(); // 讀入菜單信息 int dishNum = scanner.nextInt(); for (int i = 0; i < dishNum; i++) { String name = scanner.next(); int price = scanner.nextInt(); menu.addDish(name, price); } int tableNum = 0; Map<Integer, Order> orders = new HashMap<>(); while (scanner.hasNext()) { String command = scanner.next(); if (command.equals("checkout")) { // 結(jié)賬 int tableId = scanner.nextInt(); Order order = orders.get(tableId); if (order == null) { System.out.println("tableId " + tableId + " does not exist."); } else { int totalPrice = 0; for (Record record : order.getRecords()) { Dish dish = record.getDish(); int price = dish.getPrice(); if (record.isSubstitute()) { price *= 2; // 代點(diǎn)菜價(jià)格翻倍 } totalPrice += price; } // 根據(jù)營(yíng)業(yè)時(shí)間計(jì)算并輸出打折后的總價(jià) int hour = scanner.nextInt(); // 當(dāng)前小時(shí)數(shù) double discount = 1.0; if (hour >= 8 && hour <= 10) { discount = 0.8; } else if (hour >= 14 && hour <= 17) { discount = 0.9; } System.out.println((int) (totalPrice * discount)); } } else { // 點(diǎn)餐或刪除記錄 int tableId = scanner.nextInt(); Order order = orders.get(tableId); if (order == null) { order = new Order(); orders.put(tableId, order); } if (command.equals("delete")) { // 刪除記錄 int recordIndex = scanner.nextInt(); List<Record> records = order.getRecords(); if (recordIndex >= 0 && recordIndex < records.size()) { records.remove(recordIndex); } else { System.out.println("delete error"); } } else { // 點(diǎn)餐 String dishName = command; Dish dish = menu.getDishByName(dishName); if (dish != null) { boolean isSubstitute = false; if (scanner.hasNextInt()) { isSubstitute = true; tableId = scanner.nextInt(); } Record record = new Record(dish, isSubstitute); order.addARecord(record); } else { System.out.println("菜名" + dishName + "不存在"); } } } } // 輸出每一桌的總價(jià) for (int tableId : orders.keySet()) { System.out.println(tableId + ":"); Order order = orders.get(tableId); int totalPrice = 0; for (Record record : order.getRecords()) { Dish dish = record.getDish(); int price = dish.getPrice(); if (record.isSubstitute()) { price *= 2; // 代點(diǎn)菜價(jià)格翻倍 } totalPrice += price; } System.out.println(totalPrice); } } }
從鍵盤錄入一段英文文本(句子之間的標(biāo)點(diǎn)符號(hào)只包括“,”或“.”,單詞之間、單詞與標(biāo)點(diǎn)之間都以" "分割。
要求:按照每個(gè)單詞的長(zhǎng)度由高到低輸出各個(gè)單詞(重復(fù)單詞只輸出一次),如果單詞長(zhǎng)度相同,則按照單詞的首字母順序(不區(qū)分大小寫,首字母相同的比較第二個(gè)字母,以此類推)升序輸出。
輸入格式:
一段英文文本。
輸出格式:
按照題目要求輸出的各個(gè)單詞(每個(gè)單詞一行)。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); wordCountSort(text); scanner.close(); } public static void wordCountSort(String text) { // 將文本按照標(biāo)點(diǎn)符號(hào)和空格分割成單詞數(shù)組 String[] words = text.replaceAll("[.,]", "").split("\\s+"); // 使用Set去除重復(fù)單詞,并按照長(zhǎng)度和字母順序排序 Set<String> uniqueWords = new HashSet<>(); Collections.addAll(uniqueWords, words); List<String> sortedWords = new ArrayList<>(uniqueWords); Collections.sort(sortedWords, new Comparator<String>() { @Override public int compare(String s1, String s2) { // 首先比較長(zhǎng)度 int lengthComparison = Integer.compare(s2.length(), s1.length()); if (lengthComparison == 0) { // 如果長(zhǎng)度相同,則按照字母順序比較 return s1.compareToIgnoreCase(s2); } else { return lengthComparison; } } }); // 輸出排序后的單詞 for (String word : sortedWords) { System.out.println(word); } } }
從鍵盤輸入兩個(gè)日期,格式如:2022-06-18。判斷兩個(gè)日期的先后,并輸出它們之間間隔的天數(shù)、周數(shù)(不足一周按0計(jì)算)。
預(yù)備知識(shí):通過查詢Java API文檔,了解Scanner類中nextLine()等方法、String類中split()等方法、Integer類中parseInt()等方法的用法,了解LocalDate類中of()、isAfter()、isBefore()、until()等方法的使用規(guī)則,了解ChronoUnit類中DAYS、WEEKS、MONTHS等單位的用法。
輸入格式:
輸入兩行,每行輸入一個(gè)日期,日期格式如:2022-06-18
輸出格式:
第一行輸出:第一個(gè)日期比第二個(gè)日期更早(晚)
第二行輸出:兩個(gè)日期間隔XX天
第三行輸出:兩個(gè)日期間隔XX周
import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String s2 = in.nextLine(); String [] dateStr1 = s1.split("-"); String [] dateStr2 = s2.split("-"); int year1 = Integer.parseInt( dateStr1[0] ); int month1 = Integer.parseInt( dateStr1[1] ); int day1 = Integer.parseInt( dateStr1[2] ); int year2 = Integer.parseInt( dateStr2[0] ); int month2 = Integer.parseInt( dateStr2[1] ); int day2 = Integer.parseInt( dateStr2[2] ); LocalDate date1 = LocalDate.of(year1, month1, day1); LocalDate date2 = LocalDate.of(year2, month2, day2); if(date1.isBefore(date2)) System.out.println("第一個(gè)日期比第二個(gè)日期更早"); else System.out.println("第一個(gè)日期比第二個(gè)日期更晚"); long x = date1.until(date2, ChronoUnit.DAYS); long y = date1.until(date2, ChronoUnit.WEEKS); System.out.println("兩個(gè)日期間隔" + Math.abs(x) + "天" ); System.out.println("兩個(gè)日期間隔" + Math.abs(y) + "周" ); } }
設(shè)計(jì)點(diǎn)菜計(jì)價(jià)程序,根據(jù)輸入的信息,計(jì)算并輸出總價(jià)格。
輸入內(nèi)容按先后順序包括兩部分:菜單、訂單,最后以"end"結(jié)束。
菜單由一條或多條菜品記錄組成,每條記錄一行
每條菜品記錄包含:菜名、基礎(chǔ)價(jià)格 兩個(gè)信息。
訂單分:點(diǎn)菜記錄和刪除信息。每一類信息都可包含一條或多條記錄,每條記錄一行。
點(diǎn)菜記錄包含:序號(hào)、菜名、份額、份數(shù)。
份額可選項(xiàng)包括:1、2、3,分別代表小、中、大份。
刪除記錄格式:序號(hào) delete
標(biāo)識(shí)刪除對(duì)應(yīng)序號(hào)的那條點(diǎn)菜記錄。
不同份額菜價(jià)的計(jì)算方法:
小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格。
中份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格1.5。
小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格2。
如果計(jì)算出現(xiàn)小數(shù),按四舍五入的規(guī)則進(jìn)行處理。
參考以下類的模板進(jìn)行設(shè)計(jì):
菜品類:對(duì)應(yīng)菜譜上一道菜的信息。
Dish {
String name;//菜品名稱
int unit_price; //單價(jià)
int getPrice(int portion)//計(jì)算菜品價(jià)格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份) }
菜譜類:對(duì)應(yīng)菜譜,包含飯店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品數(shù)組,保存所有菜品信息
Dish searthDish(String dishName)//根據(jù)菜名在菜譜中查找菜品信息,返回Dish對(duì)象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
點(diǎn)菜記錄類:保存訂單上的一道菜品記錄
Record {
int orderNum;//序號(hào)\
Dish d;//菜品\
int portion;//份額(1/2/3代表小/中/大份)\
int getPrice()//計(jì)價(jià),計(jì)算本條記錄的價(jià)格\
}
訂單類:保存用戶點(diǎn)的所有菜的信息。
Order {
Record[] records;//保存訂單上每一道的記錄
int getTotalPrice()//計(jì)算訂單的總價(jià)
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單中。
delARecordByOrderNum(int orderNum)//根據(jù)序號(hào)刪除一條記錄
findRecordByNum(int orderNum)//根據(jù)序號(hào)查找一條記錄
}
import java.util.Scanner;
class Main{
public static void main(String[] args){
int j=0,k,sum=0,i;
String s;
String s1;
Scanner scanner=new Scanner(System.in);
Order order=new Order();
Menu menu=new Menu();
while(true){
s=scanner.next();//zuopanduan
if(s.equals("end"))
break;
if(s.charAt(0)-'0'<=100){
int orderNum=0,num=0,portion=0;
orderNum=s.charAt(0)-'0';
s1=scanner.next();
if(!(s1.charAt(0)!='d')) { //后續(xù)可能會(huì)有bug
if (order.findRecordByNum(order,orderNum, j) != 1)
System.out.println("delete error;");
else
order.delARecordByOrderNum(order,orderNum);
}
else {
int portion1=scanner.nextInt();
int num1=scanner.nextInt();
order.records[j]=order.addaRecord(menu,orderNum,s1,portion1,num1);
if(!(order.records[j].d!=null))
System.out.println(s1+" "+"does not exist");
else
System.out.println(order.records[j].orderNum+" "+order.records[j].d.name+" "+order.records[j].getPrice);
j++;
}
}
else {
int p=scanner.nextInt();
menu.addDish(s,p);
}
}
for(i=0;i<j;i++)
sum+=order.records[i].getPrice;
System.out.print(sum);
}
}
class Dish {
String name;
int unit_price;
public Dish(String name,int unit_price){
this.name = name;
this.unit_price = unit_price;
}
public Dish(){
}
public int getPrice(int portion){
double unit_price1=unit_price;
if(portion==2)
unit_price1=1.5*unit_price;
if(portion==3)
unit_price1=2*unit_price;
return (int) Math.round(unit_price1);
}
}
class Menu {
public Dish[] dishs=new Dish[20];
int f=0;
Dish searthDish(String dishName){
int i;
int flag=0;
for(i=f-1;i>=0;i--){
flag=0;
if(dishs[i].name.equals(dishName)){
flag=1;break;
}
}
if(!(flag != 1))
return dishs[i];
else
return null;
}
public void addDish(String dishName,int unit_price){
dishs[f]=new Dish(dishName,unit_price);
f++;
}
}
class Record {
int orderNum;//序號(hào)\
Dish d;//菜品\
int portion;//份額(1/2/3代表小/中/大份)\
int getPrice;
int num;
}
class Order {
Record[] records=new Record[20];
public Record addaRecord(Menu menu,int orderNum,String dishName,int portion,int num){
Record record=new Record();
record.orderNum=orderNum;
record.num=num;
record.d= menu.searthDish(dishName);
record.portion=portion;
if((record.d!=null))
record.getPrice=record.d.getPrice(portion)*num;
return record;
}
public void delARecordByOrderNum(Order order,int orderNum){
if (true) {} else {} order.records[orderNum-1].getPrice=0;
return ;
}
public int findRecordByNum(Order order,int orderNum,int j){
for(int i=0;i<j;i++)
if(!(order.records[i].orderNum!=orderNum))
return 1;
return 0;
}
}
本體大部分內(nèi)容與菜單計(jì)價(jià)程序-3相同,增加的部分用加粗文字進(jìn)行了標(biāo)注。
設(shè)計(jì)點(diǎn)菜計(jì)價(jià)程序,根據(jù)輸入的信息,計(jì)算并輸出總價(jià)格。
輸入內(nèi)容按先后順序包括兩部分:菜單、訂單,最后以"end"結(jié)束。
菜單由一條或多條菜品記錄組成,每條記錄一行
每條菜品記錄包含:菜名、基礎(chǔ)價(jià)格 兩個(gè)信息。
訂單分:桌號(hào)標(biāo)識(shí)、點(diǎn)菜記錄和刪除信息、代點(diǎn)菜信息。每一類信息都可包含一條或多條記錄,每條記錄一行或多行。
桌號(hào)標(biāo)識(shí)獨(dú)占一行,包含兩個(gè)信息:桌號(hào)、時(shí)間。
桌號(hào)以下的所有記錄都是本桌的記錄,直至下一個(gè)桌號(hào)標(biāo)識(shí)。
點(diǎn)菜記錄包含:序號(hào)、菜名、份額、份數(shù)。份額可選項(xiàng)包括:1、2、3,分別代表小、中、大份。
不同份額菜價(jià)的計(jì)算方法:小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格。中份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格1.5。小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格2。如果計(jì)算出現(xiàn)小數(shù),按四舍五入的規(guī)則進(jìn)行處理。
刪除記錄格式:序號(hào) delete
標(biāo)識(shí)刪除對(duì)應(yīng)序號(hào)的那條點(diǎn)菜記錄。
如果序號(hào)不對(duì),輸出"delete error"
代點(diǎn)菜信息包含:桌號(hào) 序號(hào) 菜品名稱 份額 分?jǐn)?shù)
代點(diǎn)菜是當(dāng)前桌為另外一桌點(diǎn)菜,信息中的桌號(hào)是另一桌的桌號(hào),帶點(diǎn)菜的價(jià)格計(jì)算在當(dāng)前這一桌。
程序最后按輸入的桌號(hào)從小到大的順序依次輸出每一桌的總價(jià)(注意:由于有代點(diǎn)菜的功能,總價(jià)不一定等于當(dāng)前桌上的菜的價(jià)格之和)。
每桌的總價(jià)等于那一桌所有菜的價(jià)格之和乘以折扣。如存在小數(shù),按四舍五入規(guī)則計(jì)算,保留整數(shù)。
折扣的計(jì)算方法(注:以下時(shí)間段均按閉區(qū)間計(jì)算):
周一至周五營(yíng)業(yè)時(shí)間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余時(shí)間不營(yíng)業(yè)。
周末全價(jià),營(yíng)業(yè)時(shí)間:9:30-21:30
如果下單時(shí)間不在營(yíng)業(yè)范圍內(nèi),輸出"table " + t.tableNum + " out of opening hours"
參考以下類的模板進(jìn)行設(shè)計(jì)(本內(nèi)容與計(jì)價(jià)程序之前相同,其他類根據(jù)需要自行定義):
菜品類:對(duì)應(yīng)菜譜上一道菜的信息。
Dish {
String name;//菜品名稱
int unit_price; //單價(jià)
int getPrice(int portion)//計(jì)算菜品價(jià)格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份) }
菜譜類:對(duì)應(yīng)菜譜,包含飯店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品數(shù)組,保存所有菜品信息
Dish searthDish(String dishName)//根據(jù)菜名在菜譜中查找菜品信息,返回Dish對(duì)象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
點(diǎn)菜記錄類:保存訂單上的一道菜品記錄
Record {
int orderNum;//序號(hào)
Dish d;//菜品\\
int portion;//份額(1/2/3代表小/中/大份)
int getPrice()//計(jì)價(jià),計(jì)算本條記錄的價(jià)格
}
訂單類:保存用戶點(diǎn)的所有菜的信息。
Order {
Record[] records;//保存訂單上每一道的記錄
int getTotalPrice()//計(jì)算訂單的總價(jià)
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單中。
delARecordByOrderNum(int orderNum)//根據(jù)序號(hào)刪除一條記錄
findRecordByNum(int orderNum)//根據(jù)序號(hào)查找一條記錄
}
本次課題比菜單計(jì)價(jià)系列-3增加的異常情況:
1、菜譜信息與訂單信息混合,應(yīng)忽略?shī)A在訂單信息中的菜譜信息。輸出:"invalid dish"
2、桌號(hào)所帶時(shí)間格式合法(格式見輸入格式部分說明,其中年必須是4位數(shù)字,月、日、時(shí)、分、秒可以是1位或2位數(shù)),數(shù)據(jù)非法,比如:2023/15/16 ,輸出桌號(hào)+" date error"
3、同一桌菜名、份額相同的點(diǎn)菜記錄要合并成一條進(jìn)行計(jì)算,否則可能會(huì)出現(xiàn)四舍五入的誤差。
4、重復(fù)刪除,重復(fù)的刪除記錄輸出"deduplication :"+序號(hào)。
5、代點(diǎn)菜時(shí),桌號(hào)不存在,輸出"Table number :"+被點(diǎn)菜桌號(hào)+" does not exist";本次作業(yè)不考慮兩桌記錄時(shí)間不匹配的情況。
6、菜譜信息中出現(xiàn)重復(fù)的菜品名,以最后一條記錄為準(zhǔn)。
7、如果有重復(fù)的桌號(hào)信息,如果兩條信息的時(shí)間不在同一時(shí)間段,(時(shí)段的認(rèn)定:周一到周五的中午或晚上是同一時(shí)段,或者周末時(shí)間間隔1小時(shí)(不含一小時(shí)整,精確到秒)以內(nèi)算統(tǒng)一時(shí)段),此時(shí)輸出結(jié)果按不同的記錄分別計(jì)價(jià)。
8、重復(fù)的桌號(hào)信息如果兩條信息的時(shí)間在同一時(shí)間段,此時(shí)輸出結(jié)果時(shí)合并點(diǎn)菜記錄統(tǒng)一計(jì)價(jià)。前提:兩個(gè)的桌號(hào)信息的時(shí)間都在有效時(shí)間段以內(nèi)。計(jì)算每一桌總價(jià)要先合并符合本條件的飯桌的點(diǎn)菜記錄,統(tǒng)一計(jì)價(jià)輸出。
9、份額超出范圍(1、2、3)輸出:序號(hào)+" portion out of range "+份額,份額不能超過1位,否則為非法格式,參照第13條輸出。
10、份數(shù)超出范圍,每桌不超過15份,超出范圍輸出:序號(hào)+" num out of range "+份數(shù)。份數(shù)必須為數(shù)值,最高位不能為0,否則按非法格式參照第16條輸出。
11、桌號(hào)超出范圍[1,55]。輸出:桌號(hào) +" table num out of range",桌號(hào)必須為1位或多位數(shù)值,最高位不能為0,否則按非法格式參照第16條輸出。
12、菜譜信息中菜價(jià)超出范圍(區(qū)間(0,300)),輸出:菜品名+" price out of range "+價(jià)格,菜價(jià)必須為數(shù)值,最高位不能為0,否則按非法格式參照第16條輸出。
13、時(shí)間輸入有效但超出范圍[2022.1.1-2023.12.31],輸出:"not a valid time period"
14、一條點(diǎn)菜記錄中若格式正確,但數(shù)據(jù)出現(xiàn)問題,如:菜名不存在、份額超出范圍、份數(shù)超出范圍,按記錄中從左到右的次序優(yōu)先級(jí)由高到低,輸出時(shí)只提示優(yōu)先級(jí)最高的那個(gè)錯(cuò)誤。
15、每桌的點(diǎn)菜記錄的序號(hào)必須按從小到大的順序排列(可以不連續(xù),也可以不從1開始),未按序排列序號(hào)的輸出:"record serial number sequence error"。當(dāng)前記錄忽略。(代點(diǎn)菜信息的序號(hào)除外)
16、所有記錄其它非法格式輸入,統(tǒng)一輸出"wrong format"
17、如果記錄以“table”開頭,對(duì)應(yīng)記錄的格式或者數(shù)據(jù)不符合桌號(hào)的要求,那一桌下面定義的所有信息無論正確或錯(cuò)誤均忽略,不做處理。如果記錄不是以“table”開頭,比如“tab le 55 2023/3/2 12/00/00”,該條記錄認(rèn)為是錯(cuò)誤記錄,后面所有的信息并入上一桌一起計(jì)算。
本次作業(yè)比菜單計(jì)價(jià)系列-3增加的功能:
菜單輸入時(shí)增加特色菜,特色菜的輸入格式:菜品名+英文空格+基礎(chǔ)價(jià)格+"T"
例如:麻婆豆腐 9 T
菜價(jià)的計(jì)算方法:
周一至周五 7折, 周末全價(jià)。
注意:不同的四舍五入順序可能會(huì)造成誤差,請(qǐng)按以下步驟累計(jì)一桌菜的菜價(jià):
計(jì)算每條記錄的菜價(jià):將每份菜的單價(jià)按份額進(jìn)行四舍五入運(yùn)算后,乘以份數(shù)計(jì)算多份的價(jià)格,然后乘以折扣,再進(jìn)行四舍五入,得到本條記錄的最終支付價(jià)格。
最后將所有記錄的菜價(jià)累加得到整桌菜的價(jià)格。
輸入格式:
桌號(hào)標(biāo)識(shí)格式:table + 序號(hào) +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時(shí)間(24小時(shí)制格式: HH/MM/SS)
菜品記錄格式:
菜名+英文空格+基礎(chǔ)價(jià)格
如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價(jià)格以最后一條記錄為準(zhǔn)。
點(diǎn)菜記錄格式:序號(hào)+英文空格+菜名+英文空格+份額+英文空格+份數(shù)注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。
刪除記錄格式:序號(hào) +英文空格+delete
代點(diǎn)菜信息包含:桌號(hào)+英文空格+序號(hào)+英文空格+菜品名稱+英文空格+份額+英文空格+分?jǐn)?shù)
最后一條記錄以“end”結(jié)束。
輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號(hào),格式:table+英文空格+桌號(hào)+”:”+英文空格
2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,
每條點(diǎn)菜記錄輸出:序號(hào)+英文空格+菜名+英文空格+價(jià)格。其中的價(jià)格等于對(duì)應(yīng)記錄的菜品*份數(shù),序號(hào)是之前輸入的訂單記錄的序號(hào)。如果訂單中包含不能識(shí)別的菜名,則輸出“** does not exist”,**是不能識(shí)別的菜名
如果刪除記錄的序號(hào)不存在,則輸出“delete error”
最后按輸入順序一次輸出每一桌所有菜品的總價(jià)(整數(shù)數(shù)值)格式:table+英文空格+桌號(hào)+“:”+英文空格+當(dāng)前桌的原始總價(jià)+英文空格+當(dāng)前桌的計(jì)算折扣后總價(jià)
import java.text.ParseException; import java.time.DateTimeException; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Scanner; public class Main { public static boolean isNumeric(String s) { int i; try { i = Integer.parseInt(s); return true; } catch (NumberFormatException e) { return false; } } public static void main(String[] args) throws ParseException { Menu menu = new Menu(); ArrayList<Table> tables = new ArrayList<Table>(); Scanner input = new Scanner(System.in); String str1 = new String(); int i = 0; int portion = 0, quota = 0; while (true) {// 輸入菜單 Dish temp = new Dish(); int isRepeat = -1; str1 = input.nextLine(); if (str1.matches("[\\S]* [1-9][\\d]*")) { String[] token = str1.split(" "); temp.name = token[0]; temp.unit_price = Integer.parseInt(token[1]); if (temp.unit_price > 300) { System.out.println(temp.name + " price out of range " + temp.unit_price); continue; } temp.isT = false; isRepeat = menu.searchDish(temp.name); if (isRepeat != -1) { menu.dishs.remove(isRepeat); } menu.dishs.add(temp); } else if (str1.matches("[\\S]* [\\d]* T")) { String[] token = str1.split(" "); temp.name = token[0]; temp.unit_price = Integer.parseInt(token[1]); if (temp.unit_price > 300) { System.out.println(temp.name + " price out of range " + temp.unit_price); continue; } temp.isT = true; if (isRepeat != -1) { menu.dishs.remove(isRepeat); } menu.dishs.add(temp); } else if (str1.equals("end")) { break; } else if (str1.matches("tab.*")) { break; } else { System.out.println("wrong format"); continue; } } while (!str1.equals("end")) { Table temp = new Table(); boolean isRepeat = false; int repeatNum = 0; if (str1.matches("table.*")) { if (str1.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) { String[] token = str1.split(" "); String[] Date = token[2].split("/"); String[] Time = token[3].split("/"); int[] intDate = new int[3]; int[] intTime = new int[3]; for (i = 0; i < 3; i++) { intDate[i] = Integer.parseInt(Date[i]); intTime[i] = Integer.parseInt(Time[i]); } temp.num = Integer.parseInt(token[1]); if (temp.num > 55) { System.out.println(temp.num + " table num out of range"); str1 = input.nextLine(); continue; } try { temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1], intTime[2]); temp.getWeekDay(); } catch (DateTimeException e) { System.out.println(temp.num + " date error"); str1 = input.nextLine(); continue; } if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0)) && temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) { System.out.println("not a valid time period"); str1 = input.nextLine(); continue; } // 判斷桌號(hào)是否重復(fù) if (temp.isOpen()) { for (i = 0; i < tables.size(); i++) { // 有重復(fù)的桌號(hào) if (temp.num == tables.get(i).num && tables.get(i).isOpen()) { Duration duration = Duration.between(temp.time, tables.get(i).time); // 同一天 if (duration.toDays() == 0) { // 在周一到周五 if (temp.weekday > 0 && temp.weekday < 6) { // 在同一時(shí)間段 if (temp.time.getHour() < 15 && tables.get(i).time.getHour() < 15) { temp = tables.get(i); isRepeat = true; repeatNum = i; break; } } // 在周末 else { // 時(shí)間相差小于一小時(shí) if (duration.toHours() < 3600) { temp = tables.get(i); repeatNum = i; isRepeat = true; break; } } } } } } if(!isRepeat) { System.out.println("table " + temp.num + ": "); } } else { System.out.println("wrong format"); str1 = input.nextLine(); continue; } // 本桌開始點(diǎn)菜 while (true) { str1 = input.nextLine(); if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); portion = Integer.parseInt(token[2]); quota = Integer.parseInt(token[3]); if (temp.order.records.size() > 0) { if (Integer.parseInt( token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) { System.out.println("record serial number sequence error"); continue; } } if (menu.searchDish(token[1]) == -1) { System.out.println(token[1] + " does not exist"); continue; } if (portion > 3 || portion < 1) { System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion); continue; } if (quota > 15) { System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota); continue; } temp.od(menu, token[0], token[1], portion, quota); } // 判斷是否為刪除訂單 else if (str1.matches("[1-9][\\d]* delete")) { String[] token = str1.split(" "); temp.order.delARecordByOrderNum(Integer.parseInt(token[0])); } // 判斷是否為夾雜菜單 else if (str1.matches("[\\S]* [\\d]*")) { System.out.println("invalid dish"); continue; } else if (str1.matches("[\\S]* [\\d]* T")) { System.out.println("invalid dish"); continue; } // 判斷是否為代點(diǎn) else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); // 判斷代點(diǎn)桌號(hào)是否存在 boolean exist = false; for (int j = 0; j < tables.size(); j++) { if (tables.get(j).num == Integer.parseInt(token[0])) { exist = true; break; } } if (exist) { System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table " + Integer.parseInt(token[0]) + " "); Record treat = new Record(); treat.d = menu.dishs.get(menu.searchDish(token[2])); portion = Integer.parseInt(token[3]); quota = Integer.parseInt(token[4]); treat.portion = portion; treat.quota = quota; System.out.print(treat.getPrice() + "\n"); temp.sum += treat.getPrice(); } // 若不存在則輸出內(nèi)容 else { System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist"); } } else if (str1.equals("end")) { break; } else if(str1.matches("ta.*")){ break; } else { System.out.println("wrong format"); continue; } } } else if (str1.matches("t.*")) { isRepeat = true; temp = tables.get(tables.size()); while (true) { str1 = input.nextLine(); if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); portion = Integer.parseInt(token[2]); quota = Integer.parseInt(token[3]); // 判斷訂單號(hào)是否由小到大排列 if (temp.order.records.size() > 0) { if (Integer.parseInt( token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) { System.out.println("record serial number sequence error"); continue; } } if (menu.searchDish(token[1]) == -1) { System.out.println(token[1] + " does not exist"); continue; } if (portion > 3 || portion < 1) { System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion); continue; } if (quota > 15) { System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota); continue; } temp.od(menu, token[0], token[1], portion, quota); } // 判斷是否為刪除訂單 else if (str1.matches("[1-9][\\d]* delete")) { String[] token = str1.split(" "); temp.order.delARecordByOrderNum(Integer.parseInt(token[0])); } // 判斷是否為夾雜菜單 else if (str1.matches("[\\S]* [\\d]*")) { System.out.println("invalid dish"); continue; } else if (str1.matches("[\\S]* [\\d]* T")) { System.out.println("invalid dish"); continue; } // 判斷是否為代點(diǎn) else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); // 判斷代點(diǎn)桌號(hào)是否存在 boolean exist = false; for (int j = 0; j < tables.size(); j++) { if (tables.get(j).num == Integer.parseInt(token[0])) { exist = true; break; } } if (exist) { System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table " + Integer.parseInt(token[0]) + " "); Record treat = new Record(); treat.d = menu.dishs.get(menu.searchDish(token[2])); portion = Integer.parseInt(token[3]); quota = Integer.parseInt(token[4]); treat.portion = portion; treat.quota = quota; System.out.print(treat.getPrice() + "\n"); temp.sum += treat.getPrice(); } // 若不存在則輸出內(nèi)容 else { System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist"); } } else if (str1.equals("end")) { break; } else { System.out.println("wrong format"); continue; } } if (tables.size() != 0) { tables.get(tables.size() - 1).order.records.addAll(temp.order.records); } } else { str1 = input.nextLine(); continue; } // 本桌點(diǎn)菜結(jié)束,進(jìn)入下一桌 if (isRepeat) { tables.remove(repeatNum); } temp.getSum(); tables.add(temp); } // 最終輸出桌號(hào)訂單信息 for (i = 0; i < tables.size(); i++) { if (tables.get(i).isOpen()) { System.out .println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum); } else System.out.println("table " + tables.get(i).num + " out of opening hours"); } } static class Dish { String name; int unit_price; boolean isT = false; } static class Record { int orderNum; Dish d; int portion; int quota; boolean isDeleted = false; int getPrice() { if (portion == 2) return (int) Math.round(1.5 * d.unit_price) * quota; else if (portion == 3) return 2 * d.unit_price * quota; else return d.unit_price * quota; } } static class Menu { ArrayList<Dish> dishs = new ArrayList<Dish>(); int searchDish(String dishName) { for (int i = 0; i < dishs.size(); i++) { if (dishName.equals(dishs.get(i).name)) { return i; } } return -1; } Dish addDish(String dishName, int unit_price) { Dish newDish = new Dish(); newDish.name = dishName; newDish.unit_price = unit_price; return newDish; } } static class Order { // Record[] records = new Record[20]; ArrayList<Record> records = new ArrayList<Record>(); Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) { Record newRecord = new Record(); newRecord.orderNum = orderNum; newRecord.d = menu.dishs.get(menu.searchDish(dishName)); newRecord.portion = portion; newRecord.quota = quota; System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice()); return newRecord; } int searchReocrd(String name) { for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 1; i <= 1; ++ i) {if (true) {}else {}} for (int i = 0; i < records.size(); i++) { if (records.get(i).d.name == name) { return i; } } return -1; } boolean delARecordByOrderNum(int orderNum) { int flag = 0; for (int i = 0; i < records.size(); ++ i) { if (records.get(i).orderNum == orderNum) { if (records.get(i).isDeleted == false) { records.get(i).isDeleted = true; } else { System.out.println("deduplication " + orderNum); } flag++; } } if (flag == 0) { System.out.println("delete error;"); return false; } return true; } } static class Table { Order order = new Order(); int num; LocalDateTime time; int weekday; long sum = 0; long origSum = 0; void od(Menu menu, String str1, String str2, int portion, int quota) { { order.records.add(order.addARecord(Integer.parseInt(str1), str2, portion, quota, menu)); } } void getWeekDay() { weekday = time.getDayOfWeek().getValue(); } void getSum() { for (int i = 0; i < order.records.size(); i++) { if (!order.records.get(i).isDeleted) { origSum += order.records.get(i).getPrice(); if (order.records.get(i).d.isT) { if (weekday > 0 && weekday < 6) { sum += Math.round(order.records.get(i).getPrice() * 0.7); } else { sum += order.records.get(i).getPrice(); } } else { if (weekday > 0 && weekday < 6) { if (time.getHour() >= 17 && time.getHour() < 20) sum += Math.round(order.records.get(i).getPrice() * 0.8); if (time.getHour() == 20) { if (time.getMinute() <= 30) sum += Math.round(order.records.get(i).getPrice() * 0.8); } if (time.getHour() >= 10 && time.getHour() < 14) sum += Math.round(order.records.get(i).getPrice() * 0.6); if (time.getHour() == 14) { if (time.getMinute() <= 30) sum += Math.round(order.records.get(i).getPrice() * 0.6); } } else sum+=order.records.get(i).getPrice(); } } } } boolean isOpen() { if (weekday > 0 && weekday < 6) { if (time.getHour() >= 17 && time.getHour() < 20) return true; if (time.getHour() == 20) { if (time.getMinute() <= 30) return true; } if (time.getHour() > 10 && time.getHour() < 14) return true; if (time.getHour() == 10) { if (time.getMinute() >= 30) return true; } if (time.getHour() == 14) { if (time.getMinute() <= 30) return true; } } else { if (time.getHour() > 9 && time.getHour() < 21) return true; if (time.getHour() == 9) { if (time.getMinute() >= 30) return true; } if (time.getHour() == 21) { if (time.getMinute() <= 30) return true; } } return false; } } }
本題在菜單計(jì)價(jià)程序-3的基礎(chǔ)上增加了部分內(nèi)容,增加的內(nèi)容用加粗字體標(biāo)識(shí)。
注意不是菜單計(jì)價(jià)程序-4,本題和菜單計(jì)價(jià)程序-4同屬菜單計(jì)價(jià)程序-3的兩個(gè)不同迭代分支。
設(shè)計(jì)點(diǎn)菜計(jì)價(jià)程序,根據(jù)輸入的信息,計(jì)算并輸出總價(jià)格。
輸入內(nèi)容按先后順序包括兩部分:菜單、訂單,最后以"end"結(jié)束。
菜單由一條或多條菜品記錄組成,每條記錄一行
每條菜品記錄包含:菜名、基礎(chǔ)價(jià)格 三個(gè)信息。
訂單分:桌號(hào)標(biāo)識(shí)、點(diǎn)菜記錄和刪除信息、代點(diǎn)菜信息。每一類信息都可包含一條或多條記錄,每條記錄一行或多行。
桌號(hào)標(biāo)識(shí)獨(dú)占一行,包含兩個(gè)信息:桌號(hào)、時(shí)間。
桌號(hào)以下的所有記錄都是本桌的記錄,直至下一個(gè)桌號(hào)標(biāo)識(shí)。
點(diǎn)菜記錄包含:序號(hào)、菜名、份額、份數(shù)。份額可選項(xiàng)包括:1、2、3,分別代表小、中、大份。
不同份額菜價(jià)的計(jì)算方法:小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格。中份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格1.5。小份菜的價(jià)格=菜品的基礎(chǔ)價(jià)格2。如果計(jì)算出現(xiàn)小數(shù),按四舍五入的規(guī)則進(jìn)行處理。
刪除記錄格式:序號(hào) delete
標(biāo)識(shí)刪除對(duì)應(yīng)序號(hào)的那條點(diǎn)菜記錄。
如果序號(hào)不對(duì),輸出"delete error"
代點(diǎn)菜信息包含:桌號(hào) 序號(hào) 菜品名稱 口味度 份額 份數(shù)
代點(diǎn)菜是當(dāng)前桌為另外一桌點(diǎn)菜,信息中的桌號(hào)是另一桌的桌號(hào),帶點(diǎn)菜的價(jià)格計(jì)算在當(dāng)前這一桌。
程序最后按輸入的先后順序依次輸出每一桌的總價(jià)(注意:由于有代點(diǎn)菜的功能,總價(jià)不一定等于當(dāng)前桌上的菜的價(jià)格之和)。
每桌的總價(jià)等于那一桌所有菜的價(jià)格之和乘以折扣。如存在小數(shù),按四舍五入規(guī)則計(jì)算,保留整數(shù)。
折扣的計(jì)算方法(注:以下時(shí)間段均按閉區(qū)間計(jì)算):
周一至周五營(yíng)業(yè)時(shí)間與折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余時(shí)間不營(yíng)業(yè)。
周末全價(jià),營(yíng)業(yè)時(shí)間:9:30-21:30
如果下單時(shí)間不在營(yíng)業(yè)范圍內(nèi),輸出"table " + t.tableNum + " out of opening hours"
參考以下類的模板進(jìn)行設(shè)計(jì):菜品類:對(duì)應(yīng)菜譜上一道菜的信息。
Dish {
String name;//菜品名稱
int unit_price; //單價(jià)
int getPrice(int portion)//計(jì)算菜品價(jià)格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份) }
菜譜類:對(duì)應(yīng)菜譜,包含飯店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品數(shù)組,保存所有菜品信息
Dish searthDish(String dishName)//根據(jù)菜名在菜譜中查找菜品信息,返回Dish對(duì)象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
點(diǎn)菜記錄類:保存訂單上的一道菜品記錄
Record {
int orderNum;//序號(hào)\\
Dish d;//菜品\\
int portion;//份額(1/2/3代表小/中/大份)\\
int getPrice()//計(jì)價(jià),計(jì)算本條記錄的價(jià)格\\
}
訂單類:保存用戶點(diǎn)的所有菜的信息。
Order {
Record[] records;//保存訂單上每一道的記錄
int getTotalPrice()//計(jì)算訂單的總價(jià)
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一條菜品信息到訂單中。
delARecordByOrderNum(int orderNum)//根據(jù)序號(hào)刪除一條記錄
findRecordByNum(int orderNum)//根據(jù)序號(hào)查找一條記錄
}
### 輸入格式:
桌號(hào)標(biāo)識(shí)格式:table + 序號(hào) +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時(shí)間(24小時(shí)制格式: HH/MM/SS)
菜品記錄格式:
菜名+英文空格+基礎(chǔ)價(jià)格
如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價(jià)格以最后一條記錄為準(zhǔn)。
點(diǎn)菜記錄格式:序號(hào)+英文空格+菜名+英文空格+份額+英文空格+份數(shù)注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。
刪除記錄格式:序號(hào) +英文空格+delete
代點(diǎn)菜信息包含:桌號(hào)+英文空格+序號(hào)+英文空格+菜品名稱+英文空格+份額+英文空格+分?jǐn)?shù)
最后一條記錄以“end”結(jié)束。
### 輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號(hào),格式:table+英文空格+桌號(hào)+”:”
2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,
每條點(diǎn)菜記錄輸出:序號(hào)+英文空格+菜名+英文空格+價(jià)格。其中的價(jià)格等于對(duì)應(yīng)記錄的菜品\*份數(shù),序號(hào)是之前輸入的訂單記錄的序號(hào)。如果訂單中包含不能識(shí)別的菜名,則輸出“\*\* does not exist”,\*\*是不能識(shí)別的菜名
如果刪除記錄的序號(hào)不存在,則輸出“delete error”
最后按輸入順序一次輸出每一桌所有菜品的總價(jià)(整數(shù)數(shù)值)格式:table+英文空格+桌號(hào)+“:”+英文空格+當(dāng)前桌的總價(jià)
以上為菜單計(jì)價(jià)系列-3的題目要求,加粗的部分是有調(diào)整的內(nèi)容。本次課題相比菜單計(jì)價(jià)系列-3新增要求如下:
1、菜單輸入時(shí)增加特色菜,特色菜的輸入格式:菜品名+英文空格+口味類型+英文空格+基礎(chǔ)價(jià)格+"T"
例如:麻婆豆腐 川菜 9 T
菜價(jià)的計(jì)算方法:
周一至周五 7折, 周末全價(jià)。
特色菜的口味類型:川菜、晉菜、浙菜
川菜增加辣度值:辣度0-5級(jí);對(duì)應(yīng)辣度水平為:不辣、微辣、稍辣、辣、很辣、爆辣;
晉菜增加酸度值,酸度0-4級(jí);對(duì)應(yīng)酸度水平為:不酸、微酸、稍酸、酸、很酸;
浙菜增加甜度值,甜度0-3級(jí);對(duì)應(yīng)酸度水平為:不甜、微甜、稍甜、甜;
例如:麻婆豆腐 川菜 9 T
輸入訂單記錄時(shí)如果是特色菜,添加口味度(辣/酸/甜度)值,格式為:序號(hào)+英文空格+菜名+英文空格+口味度值+英文空格+份額+英文空格+份數(shù)
例如:1 麻婆豆腐 4 1 9
單條信息在處理時(shí),如果口味度超過正常范圍,輸出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根據(jù)菜品類型擇一輸出,例如:
acidity num out of range : 5
輸出一桌的信息時(shí),按辣、酸、甜度的順序依次輸出本桌菜各種口味的口味度水平,如果沒有某個(gè)類型的菜,對(duì)應(yīng)的口味(辣/酸/甜)度不輸出,只輸出已點(diǎn)的菜的口味度。口味度水平由口味度平均值確定,口味度平均值只綜合對(duì)應(yīng)口味菜系的菜計(jì)算,不做所有菜的平均。比如,某桌菜點(diǎn)了3份川菜,辣度分別是1、3、5;還有4份晉菜,酸度分別是,1、1、2、2,辣度平均值為3、酸度平均值四舍五入為2,甜度沒有,不輸出。
一桌信息的輸出格式:table+英文空格+桌號(hào)+:+英文空格+當(dāng)前桌的原始總價(jià)+英文空格+當(dāng)前桌的計(jì)算折扣后總價(jià)+英文空格+"川菜"+數(shù)量+辣度+英文空格+"晉菜"+數(shù)量+酸度+英文空格+"浙菜"+數(shù)量+甜度。
如果整桌菜沒有特色菜,則只輸出table的基本信息,格式如下,注意最后加一個(gè)英文空格:
table+英文空格+桌號(hào)+:+英文空格+當(dāng)前桌的原始總價(jià)+英文空格+當(dāng)前桌的計(jì)算折扣后總價(jià)+英文空格
例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜
計(jì)算口味度時(shí)要累計(jì)本桌各類菜系所有記錄的口味度總和(每條記錄的口味度乘以菜的份數(shù)),再除以對(duì)應(yīng)菜系菜的總份數(shù),最后四舍五入。
注:本題要考慮代點(diǎn)菜的情況,當(dāng)前桌點(diǎn)的菜要加上被其他桌代點(diǎn)的菜綜合計(jì)算口味度平均值。
2、考慮客戶訂多桌菜的情況,輸入時(shí)桌號(hào)時(shí),增加用戶的信息:
格式:table+英文空格+桌號(hào)+英文空格+":"+英文空格+客戶姓名+英文空格+手機(jī)號(hào)+日期(格式:YYYY/MM/DD)+英文空格+ 時(shí)間(24小時(shí)制格式: HH/MM/SS)
例如:table 1 : tom 13670008181 2023/5/1 21/30/00
約束條件:客戶姓名不超過10個(gè)字符,手機(jī)號(hào)11位,前三位必須是180、181、189、133、135、136其中之一。
輸出結(jié)果時(shí),先按要求輸出每一桌的信息,最后按字母順序依次輸出每位客戶需要支付的金額。不考慮各桌時(shí)間段的問題,同一個(gè)客戶的所有table金額都要累加。
輸出用戶支付金額格式:
用戶姓名+英文空格+手機(jī)號(hào)+英文空格+支付金額
注意:不同的四舍五入順序可能會(huì)造成誤差,請(qǐng)按以下步驟累計(jì)一桌菜的菜價(jià):
計(jì)算每條記錄的菜價(jià):將每份菜的單價(jià)按份額進(jìn)行四舍五入運(yùn)算后,乘以份數(shù)計(jì)算多份的價(jià)格,然后乘以折扣,再進(jìn)行四舍五入,得到本條記錄的最終支付價(jià)格。
將所有記錄的菜價(jià)累加得到整桌菜的價(jià)格。
輸入格式:
桌號(hào)標(biāo)識(shí)格式:table + 序號(hào) +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 時(shí)間(24小時(shí)制格式: HH/MM/SS)
菜品記錄格式:
菜名+口味類型+英文空格+基礎(chǔ)價(jià)格
如果有多條相同的菜名的記錄,菜品的基礎(chǔ)價(jià)格以最后一條記錄為準(zhǔn)。
點(diǎn)菜記錄格式:序號(hào)+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份額+英文空格+份數(shù) 注:份額可輸入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范圍見題目中說明。
刪除記錄格式:序號(hào) +英文空格+delete
代點(diǎn)菜信息包含:桌號(hào)+英文空格+序號(hào)+英文空格+菜品名稱**+英文空格+辣/酸/甜度值+**英文空格+份額+英文空格+分?jǐn)?shù)
最后一條記錄以“end”結(jié)束。
輸出格式:
按輸入順序輸出每一桌的訂單記錄處理信息,包括:
1、桌號(hào),格式:table+英文空格+桌號(hào)+“:”+英文空格
2、按順序輸出當(dāng)前這一桌每條訂單記錄的處理信息,
每條點(diǎn)菜記錄輸出:序號(hào)+英文空格+菜名+英文空格+價(jià)格。其中的價(jià)格等于對(duì)應(yīng)記錄的菜品\*份數(shù),序號(hào)是之前輸入的訂單記錄的序號(hào)。如果訂單中包含不能識(shí)別的菜名,則輸出“\*\* does not exist”,\*\*是不能識(shí)別的菜名
如果刪除記錄的序號(hào)不存在,則輸出“delete error”
之后按輸入順序一次輸出每一桌所有菜品的價(jià)格(整數(shù)數(shù)值),
格式:table+英文空格+桌號(hào)+“:”+英文空格+當(dāng)前桌的計(jì)算折扣后總價(jià)+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格
最后按拼音順序輸出每位客戶(不考慮客戶同名或拼音相同的情況)的支付金額,格式: 用戶姓名+英文空格+手機(jī)號(hào)+英文空格+支付總金額,按輸入順序排列。
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.time.LocalDateTime; public class Main { public static void main(String[] args) { Table[] table=new Table[10]; Menu menu = new Menu(); Scanner input = new Scanner(System.in); String nextLine = input.nextLine(); int i=0; int num=0; int flag=0; int temp=0; int sametime=0; while (!nextLine.equals("end")) { String[] lineArray = nextLine.split(" "); if(nextLine.equals("")) { nextLine = input.nextLine(); System.out.println("wrong format"); continue; } else if(lineArray.length == 7&&lineArray[0].equals("table")==true&&canParseInt(lineArray[1])==true &&isopen(lineArray[5],lineArray[6])==true&&judge(lineArray[4])){ i++; flag=1; num=0; sametime=0; table[i]=new Table(); table[i].order=new Order(menu); table[i].num=Integer.parseInt(lineArray[1]); table[i].name=lineArray[3]; table[i].phoneNum=lineArray[4]; table[i].time=new Time(); table[i].time.time1=lineArray[5]; table[i].time.time2=lineArray[6]; System.out.println("table "+Integer.parseInt(lineArray[1])+": "); temp=0; } else if (lineArray.length == 7&&lineArray[0].equals("table")==true&&(!judge(lineArray[4])||canParseInt(lineArray[1])==false)) { System.out.println("wrong format"); temp=1; } else if(lineArray.length == 7&&lineArray[0].equals("table")==true&&(canParseInt(lineArray[1])==false||Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0||isopen(lineArray[5],lineArray[6])==false)) { temp=1; } else if(lineArray.length >7&&lineArray[0].equals("table")==true) { System.out.println("wrong format"); temp=1; } else if ((lineArray.length == 4||lineArray.length == 5)&&lineArray[0].equals("table")==false&&temp==0&&canParseInt(lineArray[0])) { int orderNum = Integer.parseInt(lineArray[0]); String dishName = lineArray[1]; int parseInt =0; int parseInt1 =0; int parseInt2 =0; if(lineArray.length == 4){ parseInt1 = Integer.parseInt(lineArray[2]); parseInt2 = Integer.parseInt(lineArray[3]); }else { parseInt = Integer.parseInt(lineArray[2]); parseInt1 = Integer.parseInt(lineArray[3]); parseInt2 = Integer.parseInt(lineArray[4]); } // if(lineArray[0].length()>1&&Integer.parseInt(lineArray[0])<10) // System.out.println("wrong format"); // else { if(sametime==0&&table[i].order.addARecord(orderNum, dishName, parseInt, parseInt1,parseInt2,i,false,false)!=null) num=orderNum; else if(sametime==1){ num=0; table[i-1].order.addARecord(orderNum, dishName, parseInt, parseInt1,parseInt2,i-1,false,false); table[i].sametime=1; } // } } else if ("delete".equals(lineArray[1])&&temp==0) { table[i].order.delARecordByOrderNum(Integer.parseInt(lineArray[0]),i); } else if((lineArray.length ==5||lineArray.length ==6)&&canParseInt(lineArray[0])==true&&canParseInt(lineArray[1])==true){ int a=0; if(i>1){ for(int j=1;j<=i;j++){ if(table[j].num==Integer.parseInt(lineArray[0])){ Dish dish = menu.searthDish(lineArray[2]); int price=0; if(lineArray.length ==5) price= dish.getPrice(Integer.parseInt(lineArray[3]))*Integer.parseInt(lineArray[4]); else price= dish.getPrice(Integer.parseInt(lineArray[4]))*Integer.parseInt(lineArray[5]); System.out.println(lineArray[1]+" table "+table[i].num+" pay for table "+table[j].num+" "+price); table[Integer.parseInt(lineArray[0])].order.addARecord(Integer.parseInt(lineArray[1]),lineArray[2],Integer.parseInt(lineArray[3]),Integer.parseInt(lineArray[4]),Integer.parseInt(lineArray[5]),Integer.parseInt(lineArray[0]),true,false); table[i].order.addARecord(Integer.parseInt(lineArray[1]),lineArray[2],Integer.parseInt(lineArray[3]),Integer.parseInt(lineArray[4]),Integer.parseInt(lineArray[5]),i,false,true); a=1; } } if(a==0) System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist"); } else System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist"); } else { if((lineArray.length == 3)&&canParseInt(lineArray[0])==false&&lineArray[1].equals("delete")==false) { System.out.println("wrong format"); } if(lineArray.length == 4&&canParseInt(lineArray[2])==true&&lineArray[3].equals("T")) menu.addDish(lineArray[0], lineArray[1],Integer.parseInt(lineArray[2]),true); if(lineArray.length == 2&&canParseInt(lineArray[1])==true&&flag==0) menu.addDish(lineArray[0], null,Integer.parseInt(lineArray[1]),false); } if(lineArray.length == 7&&lineArray[0].equals("table")==true&&canParseInt(lineArray[1])==true&&isopen(lineArray[5], lineArray[6]) == false) { if (isopen(lineArray[5], lineArray[6]) == false ) System.out.println("table " + Integer.parseInt(lineArray[1]) + " out of opening hours"); } nextLine = input.nextLine(); } input.close(); for(int j=1;j<=i;j++){ table[j].getprice(j); } for(int j=1;j<=i;j++){ for(int k=j+1;k<=i;k++) { if (table[j].name!=null&&table[k].name!=null&&table[j].name.compareTo(table[k].name) == 0){ table[k].name=null; table[j].Tableprice+=table[k].Tableprice; } if(table[j].name!=null&&table[k].name!=null&&table[j].name.compareTo(table[k].name)>0){ table[9]=table[j]; table[j]=table[k]; table[k]=table[9]; } } } for(int j=1;j<=i;j++){ if(table[j].name!=null) System.out.println(table[j].name+" "+table[j].phoneNum+" "+table[j].Tableprice); } } public static boolean canParseInt(String str) { if(str==null) { return false; } return str.matches("\\d+"); } public static boolean judge(String str){ if(str.length()!=11)return false; String[] strs=str.split(""); if(strs[0].equals("1")&&strs[1].equals("8")&&strs[2].equals("0"))return true; if(strs[0].equals("1")&&strs[1].equals("8")&&strs[2].equals("1"))return true; if(strs[0].equals("1")&&strs[1].equals("8")&&strs[2].equals("9"))return true; if(strs[0].equals("1")&&strs[1].equals("3")&&strs[2].equals("3"))return true; if(strs[0].equals("1")&&strs[1].equals("3")&&strs[2].equals("5"))return true; if(strs[0].equals("1")&&strs[1].equals("3")&&strs[2].equals("6"))return true; return false; } public static boolean isopen(String str ,String str2){ Time time = new Time(); time.time1=str; time.time2=str2; time.getDay(); time.getYear(); time.getweekOfDay(); if (time.weekday<=5&&time.weekday>=1&&((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30)||(time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))) { return true; } else if((time. weekday==6|| time . weekday==7)&&((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30))) { return true; }else { return false; } } } class Menu { private List<Dish> dishs = new ArrayList<>();//菜品數(shù)組,保存所有菜品信息 Dish searthDish(String dishName) { for (Dish dish : dishs) { if (dish.getDishname().equals(dishName)) { return dish; } } return null; } //添加一道菜品信息 Dish addDish(String dishName,String kindOfDish, int unit_price,boolean t) { for (Dish dish : dishs) { if (dish.getDishname().equals(dishName)) { dish.setUnit_price(unit_price); dish.setKindOfDish(kindOfDish); dish.setT(t); return dish; } } Dish dish = new Dish(dishName,kindOfDish, unit_price,t); dishs.add(dish); return dish; } } class Dish { String dishname;//菜品名稱 int unit_price; //單價(jià) String kindOfDish; int tastenum; public void setKindOfDish(String kindOfDish) { this.kindOfDish = kindOfDish; } boolean T; public void setT(boolean t) { T = t; } public String getDishname() { return dishname; } public void setUnit_price(int unit_price) { this.unit_price = unit_price; } public Dish(String name, String kindOfDish,int unit_price,boolean t) { this.dishname = name; this.kindOfDish=kindOfDish; this.unit_price = unit_price; this.T=t; } //計(jì)算菜品價(jià)格的方法,輸入?yún)?shù)是點(diǎn)菜的份額(輸入數(shù)據(jù)只能是1/2/3,代表小/中/大份) int getPrice(int portion) { if (portion == 2) return (int) Math.round(1.5 *unit_price); else if (portion == 3) return 2 * unit_price ; else return unit_price ; } } class Record { private int table; private int numOrder;//序號(hào)\ private Dish d;//菜品\ private int portion;//份額(1/2/3代表小/中/大份)\ public int num; public int degree; public boolean isreplace; public boolean bereplace; private boolean isDelete = false; private int deleteNum=0; public int getDeleteNum() { return deleteNum; } public void setDeleteNum(int deleteNum) { this.deleteNum = deleteNum; } public boolean isNotFound() { return notFound; } private boolean notFound = false; public Record(int orderNum, Dish d,int degree, int portion, int num) { this.numOrder = orderNum; this.d = d; d.tastenum=degree; this.degree = degree; this.portion = portion; this.num = num; } //計(jì)價(jià),計(jì)算本條記錄的價(jià)格 int getPrice() { return d.getPrice(portion) * this.num; } public int getNumOrder() { return numOrder; } public Dish getD() { return d; } public void setDelete(boolean delete) { isDelete = delete; } public boolean isDelete() { return isDelete; } } class Order { private Menu menu; public boolean iscc; public boolean isjc; public boolean iszc; public int ccNum=0; public int jcNum=0; public int zcNum=0; public static Record[][] getRecords() { return records; } public int ccDegree=0; public int jcDegree=0; public int zcDegree=0; static Record[][] records=new Record[10][40]; public Order(Menu menu) { this.menu = menu; } //計(jì)算訂單的總價(jià) int getTotalPrice(int i) { int sum = 0; for (int j=1;j<=records[i].length;j++) { if(records[i][j]==null)break; int price = records[i][j].getPrice(); if (!records[i][j].isDelete()&&records[i][j].getD().T==false&&records[i][j].isreplace==false) { sum = sum + price; } } return sum; } int getTotalPrice2(int i) { int sum = 0; for (int j=1;j<=records[i].length;j++) { if(records[i][j]==null)break; int price = records[i][j].getPrice(); if (!records[i][j].isDelete()&&records[i][j].getD().T==true&&records[i][j].isreplace==false) { sum = sum + price; } } return sum; } int getTotalPrice3(int i,double a) { int sum = 0; for (int j=1;j<=records[i].length;j++) { if(records[i][j]==null)break; int price = (int)Math.round(records[i][j].getPrice()*a); if (!records[i][j].isDelete()&&records[i][j].getD().T==false&&records[i][j].isreplace==false) { sum = sum + price; } } return sum; } int getTotalPrice4(int i,double a) { int sum = 0; for (int j=1;j<=records[i].length;j++) { if(records[i][j]==null)break; int price = (int)Math.round(records[i][j].getPrice()*a); if (!records[i][j].isDelete()&&records[i][j].getD().T==true&&records[i][j].isreplace==false) { sum = sum + price; } } return sum; } //添加一條菜品信息到訂單中。 Record addARecord(int orderNum, String dishName, int degree,int portion, int num,int i,boolean isreplace,boolean bereplace) { Dish dish = menu.searthDish(dishName); if (dish == null) { System.out.println(dishName + " does not exist"); return null; } if(/*(dish.isT()==true&&portion==2)||*/portion>3) { System.out.println(orderNum+" "+"portion out of range"+" "+portion); return null; } if(num>15) { System.out.println(orderNum+" "+"num out of range"+" "+num); return null; } if(dish.kindOfDish!=null&&dish.kindOfDish.equals("川菜")&&(degree>5||degree<0)) { System.out.println("spicy num out of range :" + degree); return null; } if(dish.kindOfDish!=null&&dish.kindOfDish.equals("晉菜")&&(degree>4||degree<0)) { System.out.println("acidity num out of range :" + degree); return null; } if(dish.kindOfDish!=null&&dish.kindOfDish.equals("浙菜")&&(degree>3||degree<0)) { System.out.println("sweetness num out of range :" + degree); return null; } if(dish.kindOfDish!=null&&dish.kindOfDish.equals("川菜")&&(degree<=5&°ree>=0)&&bereplace==false) { this.iscc=true; this.ccDegree+=degree*num; this.ccNum+=num; } if(dish.kindOfDish!=null&&dish.kindOfDish.equals("晉菜")&&(degree<=4&°ree>=0)&&bereplace==false) { this.isjc=true; this.jcDegree+=degree*num; this.jcNum+=num; } if(dish.kindOfDish!=null&&dish.kindOfDish.equals("浙菜")&&(degree<=3&°ree>=0)&&bereplace==false) { this.iszc=true; this.zcDegree+=degree*num; this.zcNum+=num; } int t = 0; for (int j=1;j<=records[i].length;j++) { if(records[i][j]==null) { t=j; break; } } records[i][t]= new Record(orderNum, dish,degree, portion, num); int price = records[i][t].getPrice(); records[i][t].isreplace=isreplace; if(records[i][t].isreplace==false&&bereplace==false) System.out.println(records[i][t].getNumOrder() + " " + records[i][t].getD().getDishname() + " " + price); return records[i][t]; } public boolean delARecordByOrderNum(int orderNum,int i) { int t=0; for (int j=1;j<=20;j++) { if (records[i][j]!=null&&!records[i][j].isNotFound() &&records[i][j].getNumOrder() == orderNum) { records[i][j].setDelete(true); records[i][j].setDeleteNum(records[i][j].getDeleteNum()+1); if(records[i][j].getD().kindOfDish!=null&&records[i][j].getD().kindOfDish.equals("川菜")&&this.iscc) { this.ccDegree-=records[i][j].degree*records[i][j].num; this.ccNum-=records[i][j].num; if(this.ccDegree==0&&this.ccNum==0) this.iscc=false; } if(records[i][j].getD().kindOfDish!=null&&records[i][j].getD().kindOfDish.equals("晉菜")&&this.isjc) { this.jcDegree-=records[i][j].degree*records[i][j].num; this.jcNum-=records[i][j].num; if(this.jcDegree==0&&this.jcNum==0) this.isjc=false; } if(records[i][j].getD().kindOfDish!=null&&records[i][j].getD().kindOfDish.equals("浙菜")&&this.iszc) { this.zcDegree-=records[i][j].degree*records[i][j].num; this.zcNum-=records[i][j].num; if(this.zcDegree==0&&this.zcNum==0) this.iszc=false; } t=records[i][j].getDeleteNum(); if(t>1) { System.out.println("deduplication "+orderNum); } return true; } } System.out.println("delete error;"); return false; } } class Table{ int num; Time time ; Order order; long Tableprice; String name; String phoneNum; int sametime=0; int price=0; void getprice(int i) { time.getDay(); time.getYear(); time.getweekOfDay(); if (time.weekday<=5&&time.weekday>=1) { if((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30) ) { this.Tableprice=this.order.getTotalPrice3(i,0.8)+this.order.getTotalPrice4(i,0.7); System.out.print("table "+this.num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.Tableprice+price)); } else if((time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30)) {this.Tableprice=this.order.getTotalPrice3(i,0.6)+this.order.getTotalPrice4(i,0.7); System. out. print("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.Tableprice+price)) ; } } if(time. weekday==6|| time . weekday==7) { if((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30) ) { Tableprice=Math. round ((order.getTotalPrice(i)+order.getTotalPrice2(i))); System. out. print ("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+(this.Tableprice+price)) ; } } if(this.order.iscc) { System.out.print(" 川菜 " + this.order.ccNum ); int a=(int)Math.round(this.order.ccDegree/(this.order.ccNum*1.0) ); if(a==0) System.out.print(" 不辣"); if(a==1) System.out.print(" 微辣"); if(a==2) System.out.print(" 稍辣"); if(a==3) System.out.print(" 辣"); if(a==4) System.out.print(" 很辣"); if(a==5) System.out.print(" 爆辣"); } if(this.order.isjc) { System.out.print(" 晉菜 " + this.order.jcNum ); int a=(int)Math.round(this.order.jcDegree/(this.order.jcNum*1.0) ); if(a==0) System.out.print(" 不酸"); if(a==1) System.out.print(" 微酸"); if(a==2) System.out.print(" 稍酸"); if(a==3) System.out.print(" 酸"); if(a==4) System.out.print(" 很酸"); } if(this.order.iszc) { System.out.print(" 浙菜 " + this.order.zcNum ); int a=(int)Math.round(this.order.zcDegree/(this.order.zcNum*1.0) ); if(a==0) System.out.print(" 不甜"); if(a==1) System.out.print(" 微甜"); if(a==2) System.out.print(" 稍甜"); if(a==3) System.out.print(" 甜"); } if(!this.order.iszc&&!this.order.isjc&&!this.order.iscc) System.out.print(" "); System.out.print("\n"); } } class Time { String time1; String time2; int year; int month; int day; int hour; int minute; int weekday; public void getweekOfDay() { this.weekday=LocalDateTime.of(this.year, this.month, this.day, this.hour, this.minute).getDayOfWeek().getValue(); } public void getYear() { String Date1[] = time1.split("\\/"); year = Integer.parseInt(Date1[0]); month = Integer.parseInt(Date1[1]); day = Integer.parseInt(Date1[2]); } public void getDay() { String Date2[] = time2.split("\\/"); hour = Integer.parseInt(Date2[0]); minute = Integer.parseInt(Date2[1]); } }
期中考試:
創(chuàng)建一個(gè)圓形類(Circle),私有屬性為圓的半徑,從控制臺(tái)輸入圓的半徑,輸出圓的面積
輸入格式:
輸入圓的半徑,取值范圍為(0,+∞),輸入數(shù)據(jù)非法,則程序輸出Wrong Format,注意:只考慮從控制臺(tái)輸入數(shù)值的情況
輸出格式:
輸出圓的面積(保留兩位小數(shù),可以使用String.format(“%.2f”,輸出數(shù)值)控制精度)
輸入樣例:
在這里給出一組輸入。例如:
2.35
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
17.35
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double c = sc.nextDouble(); if (c <= 0) { System.out.println("Wrong Format"); } else { System.out.print(String.format("%.2f",Math.acos(-1) * c * c)); } } }
設(shè)計(jì)一個(gè)矩形類,其屬性由矩形左上角坐標(biāo)點(diǎn)(x1,y1)及右下角坐標(biāo)點(diǎn)(x2,y2)組成,其中,坐標(biāo)點(diǎn)屬性包括該坐標(biāo)點(diǎn)的X軸及Y軸的坐標(biāo)值(實(shí)型數(shù)),求得該矩形的面積。類設(shè)計(jì)如下圖:

輸入格式:
分別輸入兩個(gè)坐標(biāo)點(diǎn)的坐標(biāo)值x1,y1,x2,y2。
輸出格式:
輸出該矩形的面積值(保留兩位小數(shù))。
輸入樣例:
在這里給出一組輸入。例如:
6 5.8 -7 8.9
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
40.30
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double x1 = sc.nextDouble(); double y1 = sc.nextDouble(); double x2 = sc.nextDouble(); double y2 = sc.nextDouble(); double area = Math.abs(x1 - x2) * Math.abs(y1 - y2); System.out.println(String.format("%.2f",area)); } }
將測(cè)驗(yàn)1與測(cè)驗(yàn)2的類設(shè)計(jì)進(jìn)行合并設(shè)計(jì),抽象出Shape父類(抽象類),Circle及Rectangle作為子類,類圖如下所示:

試編程完成如上類圖設(shè)計(jì),主方法源碼如下(可直接拷貝使用):
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類中的靜態(tài)方法,體現(xiàn)程序設(shè)計(jì)的多態(tài)性。
輸入格式:
輸入類型選擇(1或2,不考慮無效輸入)
對(duì)應(yīng)圖形的參數(shù)(圓或矩形)
輸出格式:
圖形的面積(保留兩位小數(shù))
輸入樣例1:
1
5.6
輸出樣例1:
在這里給出相應(yīng)的輸出。例如:
98.52
輸入樣例2:
2
5.6
-32.5
9.4
-5.6
輸出樣例2:
在這里給出相應(yīng)的輸出。例如:
102.22
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if (choice == 1) { double c = sc.nextDouble(); if (c <= 0) { System.out.println("Wrong Format"); } else { System.out.print(String.format("%.2f",Math.acos(-1) * c * c)); } } else { double x1 = sc.nextDouble(); double y1 = sc.nextDouble(); double x2 = sc.nextDouble(); double y2 = sc.nextDouble(); double area = Math.abs(x1 - x2) * Math.abs(y1 - y2); System.out.println(String.format("%.2f",area)); } } }
在測(cè)驗(yàn)3的題目基礎(chǔ)上,重構(gòu)類設(shè)計(jì),實(shí)現(xiàn)列表內(nèi)圖形的排序功能(按照?qǐng)D形的面積進(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()) + " ");
}
}
}
輸入格式:
輸入圖形類型(1:圓形;2:矩形;0:結(jié)束輸入)
輸入圖形所需參數(shù)
輸出格式:
按升序排序輸出列表中各圖形的面積(保留兩位小數(shù)),各圖形面積之間用空格分隔。
輸入樣例:
在這里給出一組輸入。例如:
1
2.3
2
3.2
3
6
5
1
2.3
0
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
5.60 16.62 16.62
#include <bits/stdc++.h> using namespace std; int main() { int choice; vector<double> v; while (cin >> choice) { if (choice == 1) { double c; cin >> c; v.push_back(acos(-1) * c * c); } else if (choice == 2) { double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; v.push_back(abs(x1 - x2) * abs(y1 - y2)); } else { break; } } sort(v.begin(), v.end()); for (auto val : v) { printf("%.2lf ", val); } }
總結(jié):
學(xué)習(xí)了java的基本語法。
浙公網(wǎng)安備 33010602011771號(hào)