点餐系统

点餐系统

大家好!!!
这个点餐系统时我这个学期课设选的一个题目,参考了许多大神的代码后给我了莫大的提示,我自己写了这个代码,求大神们鞭策,我只是一只小菜鸟。
那么我介绍这个代码的作用:

  1. 进入点餐系统,开始点餐
  2. 查寻已经点过的餐品,同时确认是否要删除已经点过的餐品,进行删除。
  3. 对已经确认过的餐品的进行结账
  4. 退出点餐系统
    这个代码很简单,我的能力有限,没有用到数据库,就连最基本的javafx都没用到,希望各位大神 指点一二,后续若是继续学习的话,我会慢慢完善此代码的。所以仅限参考吧。。。
    废话不多说,开始上代码:

import java.util.Scanner;

public class DishOrder {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] dishes=new String[7];//用顺序表数组写菜单
		dishes[0]="烤地瓜,4元";
		dishes[1]="红烧猪蹄,42元";
		dishes[2]="麻婆豆腐,14元";
		dishes[3]="腐竹炒肉,18元";
		dishes[4]="鱼香肉丝,28元";
		dishes[5]="北京烤鸭,46元";
		dishes[6]="米饭,1元";
		Dishes<String> list=new Dishes<>(dishes);//调用Dishes类,用其方法;
		System.out.println("欢迎光临!客官,里面请!");	
		Scanner sc=new Scanner(System.in);
		Dishes[] dish=new Dishes[8];//将菜单信息传输过去
		chuancan(dish);
		System.out.println("请输入客人数量");
		int num=sc.nextInt();
		System.out.println("请输入客人桌号");
		int tableID=sc.nextInt();
		Customers guest=new Customers(num,tableID);
		int num1=0,adress=0,number=0;
		double total = 0;
		boolean breakout=true;
		boolean ifremove=true;
		Integer[] store=new Integer[10];//用来储存客人点餐单号
		Dishes<Integer> list1 =new Dishes<>(store);
		while(breakout) {
			System.out.println("如果想要点餐请按1\n如果想要查询已点请按2\n结账请按3\n结束点餐请按4");
			int choose=sc.nextInt();
			switch(choose) {
			case 1://点菜
				System.out.println("您已经进入点菜系统");
				showMenu(dishes);
				System.out.println("请问您需要点餐吗?");
				do{
					
					 boolean sure=true;
					for(int i=0;i<store.length&&sure;i++) { 
						System.out.println("请您输入你想点的序号");
						int c=sc.nextInt();
						store[i]=c;
						list1.insert(c);
						System.out.println("您点的菜品id为:"+dish[c].getDishID()+"  您点的菜品名字为:"+dish[c].getDishName());
						System.out.println("如果继续点餐请输入true,如果不想点餐请输入false");
						sure=sc.nextBoolean() == true;
						num1++;
						}if(!sure)
						break;
				}while(true);
				break;
			case 2://查看已点
				list1=new Dishes<>(store);
				System.out.println("您已经点了:");
				for(int i=0;i<num1;i++) {
					System.out.println(dish[list1.get(i)].getDishID()+dish[list1.get(i)].getDishName()+dish[list1.get(i)].getPrice());
				}
				System.out.println("请问还有什么其他需求吗?");
				System.out.println("如果想要删除已点餐品请按1\n若没有请按0");
				int request=sc.nextInt();
				int cc;
				if(request==1){
					while(ifremove) {
					System.out.println("请您输入你想删除的序号");
					cc=sc.nextInt();
					adress=list1.search(cc);
					list1.remove(adress);
					 number++;
					 System.out.println("请问是否还需要删除其他餐品吗?\n如果是请输入true,否则删除false");
					 ifremove=sc.nextBoolean();
					 }
					}			
				if(request==0){
					break;
				}
				System.out.println("您已经点了:");
				for(int i=0;i<num1-number;i++) {
					System.out.println(dish[list1.get(i)].getDishID()+dish[list1.get(i)].getDishName()+dish[list1.get(i)].getPrice());
				}
				break;
			case 3://结账	
				for(int i=0;i<num1-number;i++) {
					 total=total+dish[list1.get(i)].getPrice();}
				System.out.println(guest.getTableID()+"号餐桌共消费"+(total)+"元");
					break;
			case 4://退出点餐
				System.out.println("结束点餐请输入false");
				 breakout=sc.nextBoolean();
			}
		}
		System.out.println("欢迎下次光临!");
	}
	private static void showMenu(String[] dishes) {
		// TODO Auto-generated method stub
		System.out.println("---------主菜单-------");
		for(int i=0;i<dishes.length;i++)
			System.out.println(dishes[i]);	
		System.out.println("----------------");
	}
	public static void chuancan(Dishes[] dish) {
		dish[0]=new Dishes<Object>(0,"烤地瓜",4);
		dish[1]=new Dishes<Object>(1,"红烧猪蹄",42);
		dish[2]=new Dishes<Object>(2,"麻婆豆腐",14);
		dish[3]=new Dishes<Object>(3,"腐竹炒肉",18);
		dish[4]=new Dishes<Object>(4,"鱼香肉丝",28);
		dish[5]=new Dishes<Object>(5,"北京烤鸭",46);
		dish[6]=new Dishes<Object>(6,"米饭",1);
	}
	
}

这个就相当于点餐的主界面吧,一切程序都在这上面运行。这个菜单你们就忽略吧,太。。。你们懂得。
这个我运用到数据结构中顺序表的思想,对菜单进行插入,删除,查找操作。
以下代码是餐品类:


public class Dishes<T> {
	private int dishID;//菜品id
	private String dishName;//菜品名字
	private double price;//菜品价格
	Object[] element;// 使用一位数组作为存储结构,因为用的是泛型类,所以定义父类数组
	protected int n;// 元素个数(不是元素容量)
	public int num=1;
	public Dishes(int length) {
		this.element = new Object[length];
		this.n = 0;
	}
	public Dishes(T[] values) {
		this(values.length);//把调用本类的其他构造方法,把values传过去
		for (int i = 0; i < values.length; i++) 
		this.element[i] = values[i];
		this.n = element.length;
	}
	public Dishes() {// 自动生成构造方法
		this(64);
	}
	public Dishes(int dishID,String dishName,double price){
		this.dishID=dishID;
		this.dishName=dishName;
		this.price=price;
	}
	public int getDishID(){
		return dishID;
	}
	public String getDishName(){
		return dishName;
	}
	public void setDishName(String dishName){
		this.dishName=dishName;
	}
	public double getPrice(){
		return price;
	}
	public void setPrice(double price){
		this.price=price;
	}
	public int search(T key) {
		for(int i=0;i<this.n;i++) {
			if(key.equals(element[i]))
				return i;}
			return -1;
	}
	public T insert(int i, T x) {//在第i个位置插入元素x
		if (x == null)
			throw new NullPointerException("x=null");
		if (i < 0)
			i = 0;
		if (i > n)
			i = this.n;
		Object[] source = this.element;
		if (this.n == element.length) {
			this.element = new Object[source.length * 2];
		}
		for (int j = 0; j<i; j++) {
			this.element[j] = source[j];
		}//this.element[i] = x;
		//System.out.println("插入元素"+element[i]);
		for (int j = n - 1; j >= i; j--) {
			element[j + 1] = source[j];
			}this.element[i] = x;
			this.n++;//??
			//for(Object arr:element)
				//System.out.println(arr);
		return  (T)element[i];//return x
	}
	public T insert(T x) {
		return this.insert(this.n, x);
	}
	public T remove(int i) {
		if (i >=0 && i <this.n && this.n > 0) {
			T old = (T) element[i];
			for (int j = i; j <this.n -1; j++) 
				this.element[j] = this.element[j+1];
				this.element[this.n-1]=null;
				this.n--;
				return old;
		}
		return null;
	}
	public T get(int i) {
		if(i>=0&&i<this.n)
			return (T)this.element[i];
		return null;
		}
	
}

这个类基本上就是顺序表中插入,删除,查找方法所在
以下代码是顾客类:


public class Customers {
	private static int c;
	private static String dishName;
	private static double price;
	private int count;//人数
	private int tableID;//桌号
	private static Dishes[] dishes;//点的菜品
	public Customers(int count,int tableID){
		this.count=count;
		this.tableID=tableID;
		//dishs=new ArrayList<>();
	}
	public Customers(){
		this.count=count;
		this.tableID=tableID;
		this.dishes=dishes;
	}
	public Customers(int c, String dishName, double price) {
		this.c=c;
		this.dishName=dishName;
		this.price=price;
	}
	public int getCount(){
		return count;
	}
	public int getTableID(){
		return tableID;
	}
	public void setTableID(int tableID){
		this.tableID=tableID;
	}
	public static Dishes[] getDish(){
		return dishes;
	}
	public void setDishs(Dishes[] dishes){
		this.dishes=dishes;
	}
}

以上就是我的点餐系统
嗯。。。结果我就不写出来了,太长了。希望这会对跟我一样不知道如何开始写这个代码的你们有所帮助。
谢谢你们的观看和留言。。。

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值