0
点赞
收藏
分享

微信扫一扫

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,望各位大佬留言再评论区,双手感谢!!!????



举报

相关推荐

0 条评论