我是Java的初学者,正在尝试使用从数据库中提取的数据制作一个简单的餐厅计费系统
这是我的数据库提取
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/restaurant?useTimezone=true&serverTimezone=UTC";
String username ="root";
String password = "";
Scanner sc=new Scanner(System.in);
Class.forName(driver);
boolean again = true;
boolean again1 = true;
String yes;
ArrayList<String> name=new ArrayList<String>();
ArrayList<Double> price=new ArrayList<Double>();
ArrayList<String> materials=new ArrayList<String>();
ArrayList<Double> cost=new ArrayList<Double>();
HashMap<Integer,Integer> Oders=new HashMap<Integer,Integer>();
Connection conn = DriverManager.getConnection(url,username,password);
Statement myStmt = conn.createStatement();
ResultSet rs = myStmt.executeQuery("select * from food");
while (rs.next()) {
String nam= rs.getString("name");
double pric=rs.getDouble("price");
String mat= rs.getString("materials");
double cos=rs.getDouble("cost");
name.add(nam);
price.add(pric);
materials.add(mat);
cost.add(cos);
}
这是我的计算
while(again = true){
System.out.println("Please press y to diplay menu");
String c=sc.next();
char ch=c.charAt(0);
if(ch=='y'||ch=='Y'){
System.out.println("--------------BEMS Resturant Menu Card----------------");
for(int i=0;i<name.size();i++){
System.out.print(i+"."+name.get(i));
for(int j=0;j<price.size();j++){
System.out.println(":Rm"+price.get(i));
break;
}
}
System.out.println("");
System.out.println("Enter your choice");
System.out.println("--------------------------------------------------");
while(again1 = true){
int choice=sc.nextInt();
System.out.println("Enter the quantity");
int quantity=sc.nextInt();
if(choice<=name.size()){
if(Oders.containsKey(choice)){
System.out.println("Choice already present increasing the quantity for "+name.get(choice));
System.out.println("");
Oders.put(choice, Oders.get(choice)+quantity);
}else{
Oders.put(choice, quantity);
}
}else{
System.out.println("The pressed choice does not exist in menu,enter valic choice");
System.out.println("");
}
System.out.println("Wanna order something more");
System.out.println("press Y to add more or any random key to Check Out!");
String c1=sc.next();
char ch1=c1.charAt(0);
if(ch1=='y'||ch1=='Y'){
System.out.println("Enter your choice");
}else{
String bill ="";
String Total_Materials ="";
double Total_Cost = 0;
double Total_Bill= 0;
double Total_Profit= 0;
double pre =0;
System.out.println("no more items to be ordered---->preparing final bill");
System.out.println("--------------------BEMS Restaurant-------------------"
+ "");
System.out.println("Items ordered are");
System.out.println("--------------------------------------------------");
int count=1;
for (int key : Oders.keySet()) {
System.out.println(count+"."+name.get(key)+" : "+price.get(key)+" x "+Oders.get(key));
Total_Materials = Total_Materials+materials.get(key);
Total_Cost = Total_Cost+cost.get(key);
Total_Bill=Total_Bill+(price.get(key))*Oders.get(key);
bill =bill+ name.get(key);
pre = Total_Bill * 0.06;
Total_Bill = Total_Bill + pre;
Total_Profit =Total_Bill - Total_Cost;
count++;
}
System.out.println("--------------------------------------------------");
System.out.println("Total Bill : Rm."+Total_Bill);
System.out.println("Total SST : Rm."+ pre);
System.out.println("Materials Used: "+Total_Materials );
System.out.println("Total Cost: "+Total_Cost );
System.out.println("Total Profit: "+Total_Profit );
}
}
} else{
System.out.println("Exiting, that is invalid input,please try again");
sc.close();
}
}
我可以获取每笔交易的结果,但是如何获得每天的结果
任何答案都将非常有帮助,谢谢。