Java面向对象 --- 吃货联盟订餐系统(完整版)

为什么使用面向对象

Java面向对象版本的吃货联盟订餐系统:
因为基础版本都在一个类中实现的功能,太过于麻烦,不清楚
,所以说我们利用面向对象制作吃货联盟订餐系统。
使用面向对象制作的优点:
1.更加具体化,把具体的事物抽象成类
2.易于理解,方便编写代码
3.安全性较高,经过封装代码后安全性提高
4.后期便于维护

划分类

我们进入代码编写,面向对象显著的特点就是将一个事物划分成为一类事物,从而更便捷的编写,我们思考:
首先要有订单,订单中有顾客的信息,那么我们将顾客的信息放在订单类中,顾客可以操作订餐等功能。
其次应该有餐厅,餐厅中由许多食物,我们继续划分
大概分为2类,菜品和饮品,那么我们抽象出具体的父类,菜品和饮品都在菜单上展示,所以,我们将菜单定为父类。

代码编写

菜单类:

package com.oop.demo;

public abstract class Menu {

	public abstract void show();//打印供选择的菜品/饮品信息
	
	public abstract void add(int index, int num);//添加菜品/饮品
	
	public abstract void check();//打印选择的菜品/饮品信息
}

菜品类(继承于菜单类):

package com.oop.demo;


/**
 * 菜品类,继承于菜单类
 *
 */
public class Cuisine extends Menu{

	/*菜品名称*/
	private String[] cuisine_Names = {"鱼香肉丝", "宫保鸡丁", "糖醋里脊", "糖醋排骨", "小鸡炖蘑菇", "红烧鱼"};
	/*菜品单价*/
	private double[] cuisine_Prices = {29, 30, 40, 45, 50, 60};
	
	/*选择菜品的名称*/
	private String[] choice_Cui_Names = new String[cuisine_Names.length];
	
	/*选择菜品的单价*/
	private double[] choice_Cui_Prices = new double[cuisine_Prices.length];
	
	/*选择菜品的份数*/
	private int[] num = new int[choice_Cui_Names.length];
	
	/**
	 * 打印供选择菜品的信息
	 */
	public void show() {
		System.out.println("菜品序号\t菜品名称\t菜品单价");
		for (int i = 0; i < cuisine_Names.length; i++) {
			System.out.println((i + 1) + "\t" + cuisine_Names[i]
					+ "\t" + cuisine_Prices[i]);
		}
	}

	/**
	 * 添加菜品
	 */
	public void add(int index, int num) {
		for (int i = 0; i < choice_Cui_Names.length; i++) {
			if (choice_Cui_Names[i] == null) {
				choice_Cui_Names[i] =  cuisine_Names[index - 1];
				choice_Cui_Prices[i] =  cuisine_Prices[index - 1];
				this.num[i] = num;
				break;
			}
		}
	}

	/**
	 * 打印选择的菜品信息
	 */
	public void check() {
		for (int i = 0; i < choice_Cui_Names.length; i++) {
			if (choice_Cui_Names[i] != null) {
				System.out.print("\t" + choice_Cui_Names[i]
						+ "\t¥" + choice_Cui_Prices[i] + "\t  " + num[i]);
			}
		}
	}

	
}

饮品类(继承于菜单类):

package com.oop.demo;

/**
 * 饮品类,继承于菜单类
 *
 */
public class Beverage extends Menu{

	/*饮料名称*/
	private String[] beverage_Names = {"冰茶", "红茶", "绿茶", "蜜雪冰城", "矿泉水", "牛奶"};
	/*饮料单价*/
	private double[] beverage_Prices = {1.5, 2.5, 2.5, 10.0, 9.0, 12.0};
	
	
	/*选择的饮品名称*/
	private String[] choice_Bvg_Names = new String[beverage_Names.length];
	/*选择的饮品单价*/
	private double[] choice_Bvg_Prices = {beverage_Prices.length};
	/*选择的饮品份数*/
	private int[] num = new int[choice_Bvg_Names.length];
	
	/**
	 * 打印供选择饮品的信息
	 */
	public void show() {
		System.out.println("菜单如下:");
		System.out.println("饮品序号\t饮品名称\t饮品单价");
		for (int i = 0; i < beverage_Names.length; i++) {
			System.out.println((i + 1) + "\t" + beverage_Names[i] + "\t¥"
					+ beverage_Prices[i]);
		}
	}
	
	/**
	 * 添加饮品
	 */
	public void add(int index, int num) {
		for (int i = 0; i < choice_Bvg_Names.length; i++) {
			if (choice_Bvg_Names[i] == null) {
				choice_Bvg_Names[i] = beverage_Names[index - 1];
				choice_Bvg_Prices[i] = beverage_Prices[index - 1];
				this.num[i] = num;
				break;
			}
		}
	}
	
	/**
	 * 打印选择的饮品信息
	 */
	public void check() {
		for (int i = 0; i < choice_Bvg_Names.length; i++) {
			if (choice_Bvg_Names[i] != null) {
				System.out.print("\t" + choice_Bvg_Names[i]
						+ "\t¥" + choice_Bvg_Prices[i] + "\t  " + num[i]);
			}
		}
	}
	
	
}

订单类:

package com.oop.demo;

public class OrderForm {

	private String name;//顾客昵称
	private String tel;//顾客电话
	private String addr;//顾客地址
	
	Menu[] menu = new Menu[3];//初始化5个菜单信息
	
	int[] states = new int[3];//订单状态
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	
	
}

顾客类:

package com.oop.demo;

import java.util.Scanner;

/**
 * 顾客类
 *
 */
public class Customer {

	Scanner sca = new Scanner(System.in);
	OrderForm order_Form;
	
	/**
	 * 修改信息
	 * @param name	昵称
	 * @param tel	电话号码
	 * @param addr	地址
	 */
	public void modify() {
		if (order_Form.getName() == null) {
			System.out.println("您还没有注册信息!");
		} else {
			System.out.println("请输入您的旧昵称:");
			String name = sca.next();
			System.out.println("请输入您的旧电话号码:");
			String tel = sca.next();
			System.out.println("请输入您的旧送餐地址:");
			String addr = sca.next();
			if (order_Form.getName().equals(name) && order_Form.getTel()
					.equals(tel) && order_Form.getAddr().equals(addr)) {
				System.out.println("请输入新的昵称:");
				order_Form.setName(sca.next());
				System.out.println("请输入新的电话号码:");
				order_Form.setTel(sca.next());
				System.out.println("请输入新的送餐地址:");
				order_Form.setAddr(sca.next());
				System.out.println("修改成功~");
			} else {
				System.out.println("输入信息于原有信息不符,修改失败!");
			}
		}
	}
	
	
	/**
	 * 删除订单
	 */
	public void del() {
		get();
		if (order_Form.menu[0] != null) {
			System.out.println("请输入要删除的订单序号:");
			int num = sca.nextInt();
			if (order_Form.states[num - 1] == 0) {
				System.out.println("您选择的订单未签收,不可删除!");
			} else {
				for (int i = num - 1; i < this.order_Form.menu.length - 1; i++) {
					this.order_Form.menu[i] = this.order_Form.menu[i + 1];
					this.order_Form.states[i + 1] = this.order_Form.states[num - 1];
				}
				this.order_Form.menu[num - 1] = null;
				System.out.println("删除成功~");
			}
		}
	}
	
	
	/**
	 * 签收订单
	 */
	public void signFor() {
		get();
		if (order_Form.menu[0] != null) {
			System.out.println("请输入要签收的订单序号:");
			int num = sca.nextInt();
			if (order_Form.states[num - 1] == 0) {
				order_Form.states[num - 1] = 1;
				System.out.println("签收成功~");
			} else if (order_Form.states[num - 1] == 1) {
				System.out.println("订单已签收无需重复签收!");
			}
		}
	}
	
	
	/**
	 * 查看订单信息
	 */
	public void get() {
		int count = 0;
		if (order_Form.getName() != null) {
			System.out.println("订单人:" + order_Form.getName()
						+ "\n送餐电话:" + order_Form.getTel()
						+ "\n送餐地址:" + order_Form.getAddr());
		}
		if (order_Form.menu[0] != null) {
			System.out.println("订单序号\t所点食品\t食品价格\t所点数量\t订单状态");
		}
		for (int i = 0; i < order_Form.menu.length; i++) {
			if (order_Form.menu[i] != null) {
				System.out.print("  " + (i + 1));
				order_Form.menu[i].check();
				if (order_Form.states[i] == 0) {
					System.out.println("\t 待签收");
				} else {
					System.out.println("\t 已签收");
				}
				count ++;
			}
		}
		if (count == 0) {
			System.out.println("您没有任何订单记录!");
		}
	}
	
	
	/**
	 * 点餐方法
	 */
	public void order(OrderForm orderForm) {
		if (order_Form.getName() == null) {
			System.out.println("请输入您的昵称:");
			order_Form.setName(sca.next());
		}
		if (order_Form.getTel() == null) {
			System.out.println("请输入您的电话号码:");
			order_Form.setTel(sca.next());
		}
		if (order_Form.getAddr() == null) {
			System.out.println("请输入您的地址:");
			order_Form.setAddr(sca.next());
		}
		if (orderForm.menu[orderForm.menu.length - 1] != null) {
			System.out.println("存储空间已满,无法存储!");
		} else {
			String choice = "";
			Menu menu = null;
			do {
				System.out.println("请选择您想要点的食品类型(1.菜品  2.饮品)");
				choice = sca.next();
				if (choice.equals("1")) {
					menu = new Cuisine();
				} else if (choice.equals("2")) {
					menu = new Beverage();
				} else {
					System.out.println("您的输入有误,请您重新输入:");
				}
			} while(!(choice.equals("1") || choice.equals("2")));
			menu.show();
			System.out.println("请根据编号选择食品:");
			int food = sca.nextInt();
			System.out.println("请输入要点餐的份数:");
			int num = sca.nextInt();
			menu.add(food, num);
			for (int i = 0; i < this.order_Form.menu.length; i++) {
				if (this.order_Form.menu[i] == null) {
					this.order_Form.menu[i] = menu;
					System.out.println("订餐成功~");
					break;
				}
			}
		}
	}
}

测试类:

package com.oop.demo;

import java.util.Scanner;

/**
 * 测试类
 *
 */
public class Test {

	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		Customer customer = new Customer();
		customer.order_Form = new OrderForm();
		System.err.println("欢迎使用吃货联盟订餐系统");
		boolean isExit = false;//	true:退出系统
		do {
			System.out.println("\n************************\n"
					+ "1.我要订餐\n"
					+ "2.查看餐袋\n"
					+ "3.签收订单\n"
					+ "4.删除订单\n"
					+ "5.修改信息\n"
					+ "6.退出系统\n"
					+ "************************\n"
					+ "请根据序号进行选择:");
			switch(sca.next()) {
				case "1":
					System.out.println("***我要订餐***");
					customer.order(customer.order_Form);
					break;
				case "2":
					System.out.println("***查看餐袋***");
					customer.get();
					break;
				case "3":
					System.out.println("***签收订单***");
					customer.signFor();
					break;
				case "4":
					System.out.println("***删除订单***");
					customer.del();
					break;
				case "5":
					System.out.println("***修改订单***");
					customer.modify();
					break;
				case "6":
					System.err.println("确定退出吗?(1;退出 2.继续)");
					if (sca.next().equals("1")) {
						isExit = true;
					}
					break;
				default:
					System.out.println("您的输入有误,请您重新输入:");
			}
		} while (!isExit);
		if (isExit) {
			System.out.println("感谢使用,再见!");
		}
		sca.close();
	}
}

ok,到此结束!
如有我未发现的bug,望各位大佬留言再评论区,双手感谢!!!🤞

  • 20
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
基于uml的网上订餐系统的开发文档 第1章 绪 论 - 4 - 1.1 系统开发的背景和意义 - 4 - 1.2 国内外研究发展现状 - 4 - 1.2.1 面向对象技术的发展与现状 - 4 - 1.2.2 UML的建模语言 - 5 - 1.2.3 UML的应用领域 - 6 - 1.2.4 网上订餐的发展与现状 - 6 - 第2章 业务建模 - 7 - 2.1 RUP软件开发过程 - 7 - 2.2 业务术语表 - 8 - 2.3 主业务用例图 - 9 - 第3章 分析与设计 - 10 - 3.1 业务流程调查 - 10 - 3.1.1 订餐系统业务流程调查 - 10 - 3.1.2 岗位职责 - 11 - 3.2 业务用例分析 - 11 - 3.2.2 订餐系统活动图 - 15 - 3.3 顺序图 - 18 - 餐厅订餐系统的顺序图 - 19 - 3.3.1 CancelBooking - 19 - 3.3.2 DeleteMember - 20 - 3.3.3 DisplayBooking - 20 - 3.3.4DisplayMember - 21 - 3.3.5 ModifyBooking - 22 - 3.3.6 ModifyMember - 23 - 3.3.7 RecordArrival - 23 - 3.3.8 RecordBooking - 24 - 3.3.9 RecordLeft - 25 - 3.3.10 RecordWalkIn - 26 - 3.3.11 RegisterMember - 27 - 3.3.12 RemindBooking - 28 - 3.3.13 SearchBooking - 28 - 3.4 协作图 - 29 - 订餐系统协作图 - 29 - 3.4.1 CancelBooking - 30 - 3.4.2 DisplayMember - 30 - 3.4.3 ModifyBooking - 31 - 3.4.4 ModifyMember - 31 - 3.4.5 RecordArrival - 32 - 3.4.6 RecordBooking - 33 - 3.4.7 RecordLeft - 33 - 3.4.8 RecordWalkIn - 34 - 3.4.6 RegisterMember - 35 - 3.4.9 RemindBooking - 35 - 3.4.10 SearchBooking - 36 - 3.5 活动图 - 36 - 3.6 业务类图 - 37 - 3.6.1 餐厅订餐系统业务类图 - 37 - 3.6.2 餐厅订餐系统业务类描述 - 38 - 3.6.3 数据库详细设计 - 39 - 第4章 系统实现 - 39 - 4.1 系统构件图 - 39 - 4.5 部署图 - 39 - 4.5.1 网络结构图 - 39 - 4.5.2 系统部署图 - 39 - 4.6 界面设计 - 39 - 4.6.1 本系统用户界面程序设计遵循的原则 - 39 - 4.6.2 输入输出设计

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bug 终结者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值