java点外卖系统(无界面)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DT_Zhangshuo/article/details/81224350

把这几天学的做一个综合性应用,做了一个小型订餐的系统,用到了面向对象的思想、静态的特性、集合框架中的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();
    }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页