java點外賣系統(無介面)
阿新 • • 發佈:2018-11-30
把這幾天學的做一個綜合性應用,做了一個小型訂餐的系統,用到了面向物件的思想、靜態的特性、集合框架中的ArrayList和HashMap對物件的儲存以及操作、異常處理。
專案總共包含四個檔案,分別是選單類、訂單類、操作類、Main
選單資訊儲存與操作
/**
* Created by zhangshuo on 2018/7/26.
* 外層類是選單列表
* 內層類儲存單品資訊
*/
import java.util.*;
public class Cookbook {
public Cookbook() {
setlist();
setmap();
}
public class Cook{
String name;
int type;
double price;
int like=0;
public Cook() {
}
public Cook(String name, int type, double price) {
this.name = name;
this.type = type;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getLike() {
return like;
}
public void setLike() {
like++;
}
}
public static List list=new ArrayList();
void setlist(){
Cook c1=new Cook("魚香肉絲",1,22);list.add(c1);
Cook c2=new Cook("麻辣雞絲",1,20);list.add(c2);
Cook c3=new Cook("京醬肉絲",1,26);list.add(c3);
Cook c4=new Cook("西葫蘆絲",2,13);list.add(c4);
Cook c5=new Cook("炒土豆絲",2,10);list.add(c5);
Cook c6=new Cook("牛肉餅",3,6);list.add(c6);
Cook c7=new Cook("豬肉包",3,2);list.add(c7);
Cook c8=new Cook("皮蛋湯",4,5);list.add(c8);
Cook c9=new Cook("百事可樂",5,5);list.add(c9);
Cook c10=new Cook("青島啤酒",5,6);list.add(c10);
}
public static Map hsmap=new HashMap();
public static void setmap(){ //把金額與名稱對映
Iterator it=list.iterator();
int i=0;
while(it.hasNext()){
Cook cook=(Cook)it.next();
hsmap.put(cook.getName(),i);
i++;
}
}
public static void showall(){
System.out.println("選單");
Iterator it=list.iterator();
System.out.println("菜名"+"\t\t\t\t"+"價格"+"\t\t"+"型別"+"\t\t"+"贊數");
while(it.hasNext()){
Cook c=(Cook)it.next();
System.out.print(c.name+"\t\t\t"+c.price+"\t\t");
switch(c.type){
case 1:System.out.print("葷");break;
case 2:System.out.print("素");break;
case 3:System.out.print("主");break;
case 4:System.out.print("湯");break;
case 5:System.out.print("飲");break;
}
System.out.println("\t\t"+c.like);
}
}
}
訂單的資訊儲存與操作
/**
* Created by zhangshuo on 2018/7/26.
* 外層類是訂單列表
* 內層類是單個訂單
*/
import java.util.*;
public class Orderlist {
public class order{
String name; //人名
String cookname; //菜名
int n; //購買數量
String time; //購買時間
String place; //收貨地點
double nprice=0; //總價
String state="未簽收"; //狀態
public order(String name, String cookname, int n, String time, String place) { //生成訂單
this.name = name;
this.cookname = cookname;
this.n = n;
this.time = time;
this.place = place;
Cookbook.Cook c=(Cookbook.Cook)Cookbook.list.get((int)Cookbook.hsmap.get(cookname));//到選單中查詢相應菜
this.nprice=n*c.price;//獲取名字所在行的價格
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCookname() {
return cookname;
}
public void setCookname(String cookname) {
this.cookname = cookname;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public double getNprice() {
return nprice;
}
public void setNprice(double nprice) {
this.nprice = nprice;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
List olist=new ArrayList();
public Orderlist() {
}
void setOlist(String name,String cookname,int n,String time,String place){ //建立訂單
try{
order ord=new order(name,cookname,n,time,place); //生成具體訂單
olist.add(ord);
}catch(Exception e){
System.out.print("查無此菜!\n");
return;
}
finally{}
System.out.print("已生成訂單,等待發貨\n");
}
void showOlist(){ //顯示所有訂單
Iterator it=olist.iterator();
while(it.hasNext()){
order o=(order)it.next();
System.out.println(o.name+"\t"+o.cookname+"\t\t"+o.n+"\t"+o.time+"\t"+o.place+"\t"+o.nprice+"\t"+o.state);
}
}
void setstate(int i){ //簽收第i個訂單
order ord=(order)olist.get(i);
ord.state="已簽收";
}
void dele(int i){ //刪除第i個訂單
olist.remove(i);
}
}
例項化前兩個類,並實現基本的增刪查改
import java.util.*;
/**
* Created by zhangshuo on 2018/7/26.
* 下單類主要是對訂單的一些動作
*
*/
public class Orderdinner {
Orderlist oderlist=new Orderlist();
public void setorder(){ //建立訂單
System.out.print("新建您的訂單,輸入您的名字、菜名、數量、時間、地點:");
Scanner input=new Scanner(System.in);
String name=input.next();
String cookname=input.next();
int n=input.nextInt();
String time=input.next();
String place=input.next();
oderlist.setOlist(name,cookname,n,time,place);
}
public void showorder(){ //檢視訂單
oderlist.showOlist();
}
public void orderok(){ //簽收訂單
int i;
Scanner input=new Scanner(System.in);
System.out.print("輸入要簽收的序號:");
i=input.nextInt();
try {
oderlist.setstate(i - 1);
}catch(Exception e){
System.out.print("訂單不存在!\n");
}
finally{}
}
public void dorporder(){ //刪除訂單
int i;
Scanner input=new Scanner(System.in);
System.out.print("輸入要刪除的序號:");
i=input.nextInt();
try{
oderlist.dele(i-1);
}catch(Exception e){
System.out.print("訂單不存在!\n");
}
finally{}
}
public void like(){ //點贊
String name;
System.out.print("輸入您喜歡的菜名:");
Scanner input=new Scanner(System.in);
name=input.next();
Cookbook.Cook c=(Cookbook.Cook)Cookbook.list.get((int)Cookbook.hsmap.get(name));//找到對應的行
c.setLike();
System.out.println("已點贊\n");
}
}
顯示主要的樣式,呼叫動作類中的動作
void run(){
Scanner input=new Scanner(System.in);
Orderdinner buyfood=new Orderdinner();
Cookbook cookbook=new Cookbook();
boolean bb=true;
while(bb){
System.out.printf("1.檢視選單\n2.我要訂餐\n3.檢視餐袋\n4.簽收訂單\n5.刪除訂單\n6.我要點贊\n7.退出系統\n");
int n=1;
try{
n=input.nextInt();
}catch (Exception e){
System.out.println("輸入選項不合法");
return;
}
switch(n){
case 1:
Cookbook.showall();
break;
case 2:
buyfood.setorder();
break;
case 3:
buyfood.showorder();
break;
case 4:
buyfood.orderok();
break;
case 5:
buyfood.dorporder();
break;
case 6:
buyfood.like();
break;
case 7:
System.out.println("退出系統");
bb=false;
break;
}
}
input.close();
}
public static void main(String[] args) {
Main m=new Main();
m.run();
}