import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
class UserInformation implements Comparable<UserInformation>{
public String name;
public String phone;
public int sum;
@Override
public int compareTo(UserInformation o) {
return this.name.compareTo(o.name);
}
}
class Tool {
public static Select FormatJudgment(String s) {
Select select = Select.ILLEGAL_FORMAT;
if (s.matches("[\\u4e00-\\u9fa5]+ [1-9][0-9]?[0-9]?[0-9]?"))
select = Select.MENU;//普通菜
if (s.matches("[\\u4e00-\\u9fa5]+ [\\u4e00-\\u9fa5]+ [1-9][0-9]?[0-9]?[0-9]? T"))
select = Select.SPE_MENU;//特殊菜
if (s.matches("table [1-9]+[0-9]?[0-9]? : [a-zA-Z]{1,10} (180|181|189|133|135|136)\\d{8} [0-9]{4}/[0-9]{1,2}/[0-9]{1,2} [0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}"))
select = Select.TABLE;//桌信息
if (s.matches("[1-9][0-9]? [\\u4e00-\\u9fa5]+ [0-9] [0-9]+ [1-9][0-9]?[0-9]?"))
select = Select.ORDER_SPECIAL;
if (s.matches("[1-9][0-9]? [\\u4e00-\\u9fa5]+ [0-9] [1-9][0-9]?[0-9]?"))
select = Select.ORDER;//点菜
if (s.matches("[1-9][0-9]{0,2} delete"))
select = Select.DELETE;//删除
if (s.matches("[0-9][0-9]{0,10} [1-9][0-9]{0,2} [\\u4e00-\\u9fa5]+ [0-9] [1-9][0-9]{0,2}"))
select = Select.ORDER_TO_OTHER;//代点
if (s.matches("[0-9][0-9]{0,10} [1-9][0-9]{0,2} [\\u4e00-\\u9fa5]+ [0-9]+ [0-9] [1-9][0-9]{0,2}"))
select = Select.ORDER_SPECIAL_TO_OTHER;
return select;
}
public static void panDuan(String[] t, ArrayList<Table> tables) {
String dateTime = t[2] + " " + t[3];
String pattern = "yyyy/M/d H/m/s";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime currentDateTime;
if (ifBeyond(t[1], 1, 55)) {
System.out.println(t[1] + "table num out of range");
Main.dishFlag = false;
return;
}
try {
formatter = DateTimeFormatter.ofPattern(pattern);
currentDateTime = LocalDateTime.parse(dateTime, formatter);
} catch (Exception e) {
System.out.println(t[1] + " date error");
Main.dishFlag = false;
return;
}
LocalDate currentDate = currentDateTime.toLocalDate();
LocalDate beginDate = LocalDate.of(2022, 1, 1);
LocalDate endDate = LocalDate.of(2023, 12, 31);
if (currentDate.isBefore(beginDate) || currentDate.isAfter(endDate)) {
System.out.println("not a valid time period");
Main.dishFlag = false;
return;
}
Main.dishFlag = true;
tables.add(new Table());
tables.get(tables.size() - 1).recordTableInformation(t);
}
public static int sToInt(String a) {
return Integer.parseInt(a);
}
public static boolean ifBeyond(String a, int begin, int end) {
if (sToInt(a) < begin || sToInt(a) > end)
return true;
return false;
}
public static boolean kouWeiPanDuanChaoChu(String s, int range, Menu m) {
String t = m.specialMenu.get(s).getValue();
/*Main.kouWei.forEach((key, value) ->
System.out.println("Key: " + key + ", Value: (" + value.getKey() + ", " + value.getValue() + ")"));*/
if (range < 0 || range > Main.kouWei.get(t).getKey()) {
System.out.println(Main.kouWei.get(t).getValue() + " num out of range :" + range);
return true;
}
return false;
}
public static int duiYingXuHao(String s, Menu m) {
String t = m.specialMenu.get(s).getValue();
if (t.equals("川菜"))
return 0;
if (t.equals("晋菜"))
return 1;
if (t.equals("浙菜"))
return 2;
return 0;
}
public static String lanweipandaun(String s, int fenShu, int sum) {
int dengij = (int) ((sum) * 1.0 / fenShu + 0.5);
String res="";
if (s.equals("川菜"))//不辣、微辣、稍辣、辣、很辣、爆辣
{
switch (dengij) {
case 0:
res = "不辣";
break;
case 1:
res = "微辣";
break;
case 2:
res = "稍辣";
break;
case 3:
res = "辣";
break;
case 4:
res = "很辣";
break;
case 5:
res = "爆辣";
break;
}
} else if (s.equals("晋菜"))//不酸、微酸、稍酸、酸、很酸;
{
switch (dengij) {
case 0:
res = "不酸";
break;
case 1:
res = "微酸";
break;
case 2:
res = "稍酸";
break;
case 3:
res = "酸";
break;
case 4:
res = "很酸";
break;
}
} else {
switch (dengij)//不甜、微甜、稍甜、甜;
{
case 0:
res = "不甜";
break;
case 1:
res = "微甜";
break;
case 2:
res = "稍甜";
break;
case 3:
res = "甜";
break;
}
}
return res;
}
public static void init() {
AbstractMap.SimpleEntry<Integer, String> stringStringSimpleEntry = new AbstractMap.SimpleEntry<Integer, String>(5, "spicy");
Main.kouWei.put("川菜", stringStringSimpleEntry);
AbstractMap.SimpleEntry<Integer, String> stringStringSimpleEntry1 = new AbstractMap.SimpleEntry<Integer, String>(4, "acidity");
Main.kouWei.put("晋菜", stringStringSimpleEntry1);
AbstractMap.SimpleEntry<Integer, String> stringStringSimpleEntry2 = new AbstractMap.SimpleEntry<Integer, String>(3, "sweetness");
Main.kouWei.put("浙菜", stringStringSimpleEntry2);
}
}
class Table {
public int tableNumber;
public String userName;
public String phoneNumber;
public String orderTime;
public ArrayList<Dish> dishes = new ArrayList<>();
public ArrayList<Integer> deletes = new ArrayList<>();
public int sum;
public KouWeiJiLu[] jiLus=new KouWeiJiLu[3];
public String[] caiName={"川菜","晋菜","浙菜"};
// public AbstractMap.SimpleEntry<String, String>[] teSeCaiKouWeiJiLu=new AbstractMap.SimpleEntry[3];
public Table(){
for (int i = 0; i < jiLus.length; i++) {
jiLus[i]=new KouWeiJiLu();
}
}
void delete(String[] t) {
if(!Main.dishFlag)
return;
int a = 0;
int number = Integer.parseInt(t[0]);
if (deletes.contains(number)) {
System.out.println("deduplication :" + number);
} else {
for(int i=dishes.size()-1;i>=0;i--)//反向循环删除
{
if(dishes.get(i).orderNumber==number)
{
Dish dish=dishes.get(i);
jiLus[Tool.duiYingXuHao(dish.name,Main.menu)].fenShu-=dish.number;
jiLus[Tool.duiYingXuHao(dish.name,Main.menu)].sum-=dish.number*dish.kouWei;
dishes.remove(i);
deletes.add(number);
a=1;
}
}
if (a == 0)
System.out.println("delete error;");
}
}
void recordTableInformation(String[] t) {
tableNumber = Integer.parseInt(t[1]);
orderTime = t[5] + " " + t[6];
userName =t[3];
phoneNumber =t[4];
System.out.println("table " + tableNumber + ": ");
Main.dishFlag=true;
Main.inFlag=false;
}
void order(String[] t, Menu m) {
if(!Main.dishFlag)return ;
String dishName = t[1];
if (!m.hasDish(dishName))
System.out.println(dishName + " does not exist");
else {
int aa=0;
for (int i = 0; i < dishes.size(); i++) {
if(dishes.get(i).name==t[1]&&(dishes.get(i).portion==Tool.sToInt(t[2])))
{
dishes.get(i).number+=Tool.sToInt(t[3]);
aa=1;
break;
}
}
if(aa==0)
{
Dish dish = new Dish();
dish.orderNumber = Integer.parseInt(t[0]);
dish.kouWei=Tool.sToInt(t[2]);
dish.name = t[1];
dish.price = m.getPrice(dish.name);
dish.portion = Integer.parseInt(t[2]);
dish.number = Integer.parseInt(t[3]);
dish.ifSpecial=false;
dishes.add(dish);
System.out.println(dish);
}
}
}
public void orderSpecial(String[] t, Menu m) {
if(!Main.dishFlag)return ;
String dishName = t[1];
if (!m.hasDish(dishName))
{
System.out.println(dishName + " does not exist");
}
else {
int aa=0;
for (int i = 0; i < dishes.size(); i++) {
if(dishes.get(i).name==t[1]&&(dishes.get(i).portion==Tool.sToInt(t[2])))
{
dishes.get(i).number+=Tool.sToInt(t[3]);
aa=1;
break;
}
}
if(aa==0)
{
int range=Tool.sToInt(t[2]);
if(Tool.kouWeiPanDuanChaoChu(t[1],range,m))
{
return ;
}
Dish dish = new Dish();
dish.orderNumber = Integer.parseInt(t[0]);
dish.name = t[1];
dish.price = m.getPrice(dish.name);
dish.portion = Integer.parseInt(t[3]);
dish.number = Integer.parseInt(t[4]);
dish.ifSpecial=true;
dishes.add(dish);
System.out.println(dish);
int xuHao=Tool.duiYingXuHao(t[1],m);
if( jiLus[xuHao].fenShu==-1)
jiLus[xuHao].fenShu=0;
jiLus[xuHao].fenShu+=dish.number;
jiLus[xuHao].sum+=dish.number*range;
}
}
}
void orderToOther(String[] t, Menu m) {
if(!Main.dishFlag)
return;
String dishName = t[2];
if (!m.hasDish(dishName))
System.out.println(dishName + " doer not exist");
else {
Dish dish = new Dish();
dish.orderNumber = Integer.parseInt(t[1]);
dish.name = t[2];
dish.price = m.getPrice(dish.name);
dish.portion = Integer.parseInt(t[3]);
dish.number = Integer.parseInt(t[4]);
dish.ifSpecial=false;
dishes.add(dish);
System.out.println(dish.orderNumber + " table " + tableNumber + " pay for table " + t[0] + " " + dish.totalPrice());
}
}
public void orderSpecialToOther(String[] t, Menu m) {
if(!Main.dishFlag)
return;
String dishName = t[2];
if (!m.hasDish(dishName))
System.out.println(dishName + " doer not exist");
else {
int range=Tool.sToInt(t[3]);
if(Tool.kouWeiPanDuanChaoChu(t[2],range,m))
{
return ;
}
Dish dish = new Dish();
dish.orderNumber = Integer.parseInt(t[1]);
dish.name = t[2];
dish.price = m.getPrice(dish.name);
dish.portion = Integer.parseInt(t[4]);
dish.number = Integer.parseInt(t[5]);
dish.ifSpecial=true;
dishes.add(dish);
System.out.println(dish.orderNumber + " table " + tableNumber + " pay for table " + t[0] + " " + dish.totalPrice());
int muBiaoTable=Tool.sToInt(t[0])-1;
int xuHao=Tool.duiYingXuHao(t[2],m);
if( Main.tables.get(muBiaoTable).jiLus[xuHao].fenShu==-1)
Main.tables.get(muBiaoTable).jiLus[xuHao].fenShu=0;
Main.tables.get(muBiaoTable).jiLus[xuHao].fenShu+=dish.number;
Main.tables.get(muBiaoTable).jiLus[xuHao].sum+=dish.number*range;
}
}
void getSum() {
int ans = 0;
int ans1 = 0;
double v = BusinessHours.getDiscount(orderTime);
double v1 = BusinessHours.getSpecialDiscount(orderTime);
for (Dish dish : dishes) {
ans1 += dish.total;
if (dish.ifSpecial)
{
ans += (int) (dish.total * v1 + 0.5);
}
else
ans += (int) (dish.total * v + 0.5);
}
sum = ans;
System.out.print("table " + tableNumber + ": " + ans1 + " " + sum+" ");
int aaa=0;
for (int i = 0; i < jiLus.length; i++) {
if(jiLus[i].fenShu!=-1&&jiLus[i].fenShu!=0)
{
if(aaa==0)
aaa=1;
else
{
System.out.print(" ");
}
System.out.print(caiName[i] + " " + jiLus[i].fenShu + " " + Tool.lanweipandaun(caiName[i], jiLus[i].fenShu, jiLus[i].sum));
}
}
System.out.println();
for (UserInformation user : Main.users) {
if(user.name.equals(userName)&&user.phone.equals(phoneNumber))
{
user.sum+=sum;
return;
}
}
UserInformation userInformation=new UserInformation();
userInformation.name=userName;
userInformation.phone=phoneNumber;
userInformation.sum=sum;
Main.users.add(userInformation);
}
}
enum Select {
MENU(1),
SPE_MENU(2),
TABLE(3),
ORDER(4),
ORDER_SPECIAL(7),
DELETE(5),
ORDER_TO_OTHER(6),
ORDER_SPECIAL_TO_OTHER(7),
ILLEGAL_FORMAT(0);
private final int value;
private Select(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
class Menu {
public Map<String, Integer> menu = new HashMap<>();
public Map<String, AbstractMap.SimpleEntry<Integer, String>> specialMenu = new HashMap<>();
public void addDish(String[] t) {
if(!Main.menuFlag&&Main.dishFlag)
{
System.out.println("invalid dish");
return ;
}
if(Main.menuFlag)
{
String a=t[1];
if(Tool.ifBeyond(a,1,299))
{
System.out.println(t[0] + " price out of range " + t[1]);
}
else
{
String dishName = t[0];
int price = Integer.parseInt(t[1]);
menu.put(dishName, price);
}
}
}
public void addDish1(String[] t) {//特色菜加菜
if(!Main.menuFlag&&Main.dishFlag)
{
System.out.println("invalid dish");
return ;
}
if(Main.menuFlag)
{
String a=t[2];
if(Tool.ifBeyond(a,1,299))
{
System.out.println(t[0] + " price out of range " + t[1]);
}
else {
String dishName = t[0];
int price = Integer.parseInt(t[2]);
AbstractMap.SimpleEntry<Integer, String> entry = new AbstractMap.SimpleEntry<>(price, t[1]);
specialMenu.put(dishName, entry);
}
}
}
public boolean hasDish(String dishName) {
return menu.containsKey(dishName) || specialMenu.containsKey(dishName);
}
public boolean ifSpecial(String dishName) {
if (specialMenu.containsKey(dishName))
return true;
return false;
}
public int getPrice(String dishName) {
if(ifSpecial(dishName))
return specialMenu.get(dishName).getKey();
return menu.get(dishName);
}
}
class KouWeiJiLu {
public int fenShu=-1;
public int sum;
}
class Dish {
public int orderNumber;
public String name;
public int price;
public int portion;
public int number;
public int total;
public boolean ifSpecial;
public int kouWei;
public int totalPrice() {
total=(int) ((1.0 * price * (portion + 1) / 2 * number)+0.5);
return total;
}
// public int discountedPrice(){
// }
@Override
public String toString() {
return orderNumber+" "+name+" "+totalPrice();
}
}
class BusinessHours {
private static final int startDay = 1;
private static final int endDay = 5;
private static final LocalTime startTime = LocalTime.of(17, 0);
private static final LocalTime endTime = LocalTime.of(20, 30);
private static final LocalTime startTime1 = LocalTime.of(10, 30);
private static final LocalTime endTime1 = LocalTime.of(14, 30);
private static final int Day = 7;
private static final int Day1=6;
private static final LocalTime startTime2 = LocalTime.of(9, 30);
private static final LocalTime endTime2 = LocalTime.of(21, 30);
public static double getDiscount(String dateTime) {
String pattern = "yyyy/M/d H/m/s";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime currentDateTime= LocalDateTime.parse(dateTime, formatter);
LocalDate currentDate = currentDateTime.toLocalDate();
LocalTime currentTime = currentDateTime.toLocalTime();
// 获取星期几
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
int week = dayOfWeek.getValue();
// 判断是否在周一到周五之间
if (week >= startDay && week <= endDay) {
if (myIsAfter(currentTime,startTime) && myIsBefore(currentTime,endTime)) {
return 0.8;
} else if (myIsAfter(currentTime,startTime1) && myIsBefore(currentTime,endTime1)) {
return 0.6;
}
}
if (week == Day||week==Day1) {
if (myIsAfter(currentTime,startTime2) && myIsBefore(currentTime,endTime2)) {
return 1;
}
}
return 0;
}
public static double getSpecialDiscount(String dateTime) {
String pattern = "yyyy/M/d H/m/s";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime currentDateTime= LocalDateTime.parse(dateTime, formatter);
LocalDate currentDate = currentDateTime.toLocalDate();
LocalTime currentTime = currentDateTime.toLocalTime();
// 获取星期几
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
int week = dayOfWeek.getValue();
// 判断是否在周一到周五之间
if (week >= startDay && week <= endDay) {
return 0.7;
}
if (week == Day||week==Day1) {
return 1;
}
return 0;
}
private static boolean myIsAfter(LocalTime a,LocalTime b)
{
if(a.isAfter(b)||a.equals(b))
return true;
return false;
}
private static boolean myIsBefore(LocalTime a,LocalTime b)
{
if(a.isBefore(b)||a.equals(b))
return true;
return false;
}
}
public class Main {
static boolean menuFlag = true;
static boolean dishFlag = false;
static boolean inFlag = false;
public static HashMap<String, AbstractMap.SimpleEntry<Integer, String>> kouWei = new HashMap<>();
public static Scanner sc = new Scanner(System.in);
public static ArrayList<Table> tables = new ArrayList<>();
public static Menu menu = new Menu();
public static ArrayList<UserInformation> users=new ArrayList<>();
public static void main(String[] args) {
Tool.init();
while (true) {
String a;
a = sc.nextLine();
if(a.equals("end"))
break;
String[] t = a.split("\\s");
Select select = Tool.FormatJudgment(a);
switch (select) {
case MENU:
menu.addDish(t);
break;
case SPE_MENU:
menu.addDish1(t);
break;
case TABLE:
menuFlag = false;
//Tool.panDuan(t, tables);//在table的格式合法的情况下进一步判断数据是否合法
String aa = t[5] + " " + t[6];
double v = BusinessHours.getDiscount(aa);
if (Double.compare(v, 0) == 0) {
System.out.println("table " + t[1] + " out of opening hours");
Main.dishFlag = false;
Main.inFlag = true;
break;
}
tables.add(new Table());
tables.get(tables.size() - 1).recordTableInformation(t);
break;
case ORDER_SPECIAL:
if (tables.size() > 0) {
tables.get(tables.size() - 1).orderSpecial(t, menu);
}
break;
case DELETE:
if (tables.size() > 0) {
tables.get(tables.size() - 1).delete(t);
}
break;
case ORDER:
if (tables.size() > 0) {
tables.get(tables.size() - 1).order(t, menu);
}
break;
case ORDER_SPECIAL_TO_OTHER:
if (tables.size() > 0) {
tables.get(tables.size() - 1).orderSpecialToOther(t, menu);
}
break;
case ORDER_TO_OTHER:
if (tables.size() > 0) {
tables.get(tables.size() - 1).orderToOther(t, menu);
}
break;
case ILLEGAL_FORMAT:
System.out.println("wrong format");
break;
}
}
for (Table table : tables) {
table.getSum();
}
Collections.sort(users);
for (int i = 0; i < users.size(); i++) {
System.out.print(users.get(i).name + " " + users.get(i).phone + " " + users.get(i).sum);
if(i<users.size()-1)
System.out.println();
}
/*for (UserInformation user : users) {
System.out.println(user.name + " " + user.phone + " " + user.sum);
}*/
}
}
-
菜品类(Dish):
- 属性:菜品编号、菜名、价格、描述、图片等。
- 功能:获取菜品信息、修改菜品信息等。
-
订单类(Order):
- 属性:订单号、用户ID、下单时间、订单状态、订单总价等。
- 功能:添加、修改、查询订单、获取订单详情、获取订单状态、确认订单等。
-
用户类(User):
- 属性:用户ID、用户名、密码、地址、电话等。
- 功能:注册、登录、修改个人信息、查看历史订单等。
-
菜单类(Menu):
- 属性:菜品列表。
- 功能:获取全部菜品、按类别查询菜品等。
以上是一些基本的类设计,可以根据实际情况进行调整扩展。这些类之间的交互及数据流程如下:
- 用户浏览菜单时,菜单类提供全部菜品。用户选择菜品后,将菜品信息添加到购物车类中。
- 用户确认订单时,购物车类将菜品列表和总价传递给订单类,生成订单号、记录订单状态和提交时间等信息。
- 用户选择支付方式并进行支付操作时,支付类调用支付接口进行支付。
- 一旦订单支付成功,支付类将支付结果传递给订单类,更新订单状态。
- 用户可以通过订单类查看历史订单信息,包括订单状态、菜品信息和支付状态等。
综上所述,良好的类设计和分析可以使得点菜系统的功能更为清晰明了,易于开发、维护和扩展。
标签:return,String,int,BLOG,dish,public,out From: https://www.cnblogs.com/nchu-wu/p/17842764.html