第一本书 第九章 使用对象吃货联盟
如果有一天,能做到让程序自学习就好了;发呆发呆(`・ω・´);
这个小项目我写了5个小时;
优化了好多天;
希望大家珍惜;
谢谢 by 阿飞~ ~
package com.opp.opp.bookone.ordering;
public class EattingHappyMain {
public static void main(String[] args) {
EattingHappy eh = new EattingHappy();
eh.initial(); //初始化2例
eh.startMenu(); //主菜单先调用
eh.add(); //增
eh.delete(); //删
eh.display(); //查
eh.sign(); //签
eh.prices(); //赞
}
}
package com.opp.opp.bookone.ordering;
import java.util.Scanner;
public class EattingHappy {
/*
* 订餐人姓名、选择菜品、送餐时间、送餐地址、订单状态、需支付金额
* 1.创建对应的数组
* 2.数组的初始化
* 3.显示菜单
* 4.根据用户的选择进去指定的模块
*/
Scanner scanner = new Scanner(System.in);
// 初始化用户可以选择的菜
String[] menuNames = {"兰花熊掌", "八仙过海", "红扒熊掌", "八宝熊掌", "龙虾霸王"};
// 菜的单价
double[] prices = {300.5, 500.8, 200.0, 400.5, 880.8};
// 点赞数
//实例化对象
EattingHappySet ehs = new EattingHappySet();
public void initial() {
// 初始化2个订单
ehs.names[0] = "淅凤";
ehs.menus[0] = "兰花熊掌";
ehs.numbers[0] = 1;
ehs.times[0] = 8;
ehs.addresses[0] = "暂安处北大青鸟楼";
ehs.states[0] = 1;
ehs.money[0] = 50;
ehs.pNums[0] = 66;
ehs.names[1] = "艾迪";
ehs.menus[1] = "八仙过海";
ehs.numbers[1] = 10;
ehs.times[1] = 12;
ehs.addresses[1] = "暂安处2号楼";
ehs.states[1] = 1;
ehs.money[1] = 500;
ehs.pNums[1] = 188;
}
// 一级主菜单
public void startMenu() {
System.out.println("*****************欢迎使用吃货联盟订餐系统*****************");
// 如果用户输入的是0,默认返回主餐单
int number;
boolean isExit = false;
do {
// 显示菜单供用户选择
System.out.println("1、我要订餐");
System.out.println("2、查看餐带");
System.out.println("3、签收订单");
System.out.println("4、删除订单");
System.out.println("5、我要点赞");
System.out.println("6、退出系统");
System.out.println("********************************************************************");
System.out.println("请选择:");
// 从控制台 接收选择的信息
int choose = scanner.nextInt();
switch (choose)
{
case 1:
System.out.println("*****************我要订餐*****************");
add();
break;
case 2:
System.out.println("*****************查看订单*****************");
display();
break;
case 3:
System.out.println("*****************签收订单*****************");
sign();
break;
case 4:
System.out.println("*****************删除订单*****************");
delete();
break;
case 5:
System.out.println("*****************我要点赞*****************");
prices();
break;
default:
// 退出系统
System.out.println("谢谢惠顾,欢迎下次光临!");
isExit = true;
break;
}
if (!isExit) {
System.out.println("输入0返回菜单");
number = scanner.nextInt();
} else {
break;
}
} while (number == 0);
}
// 添加订单
public boolean add() {
boolean flag = false;
// 遍历订餐人
for (int i = 0; i < ehs.names.length; i++) {
if (ehs.names[i] == null) {
//未达到上限
flag = true;
System.out.println("请输入您的姓名:");
// 订餐人姓名
ehs.names[i] = scanner.next();
System.out.println("请选择您想订购的美食:");
System.out.println("编号\t\t\t餐名\t\t\t\t单价\t\t\t点赞数");
// 遍历
for (int j = 0; j < menuNames.length; j++) {
String n = ehs.pNums[j] > 0 ? ehs.pNums[j]
+ "个赞"
: "";
System.out.println(j + 1 + "\t\t\t" + menuNames[j]
+ "\t\t\t" + prices[j] + "\t\t\t" + n);
}
// 用户开始点餐
System.out.println("请您点餐:(选择菜单号)");
int chooseMenuName = scanner.nextInt();
System.out.println("请您输入点餐的份数:");
int Number = scanner.nextInt();
System.out.println("请输入您的地址:");
String address = scanner.next();
System.out.println("请输入您需要的送餐时间:(请输入整点,小时)");
int time = scanner.nextInt();
// 判断订单的金额是否满50元,如果不满足,则送餐费5元,满足则免运费
double sumPrice = prices[chooseMenuName - 1] * Number;
double price = (sumPrice > 50) ? 0 : 5;
System.out.print("订餐成功,");
System.out.println("您的订单为:" + menuNames[chooseMenuName - 1]
+ "\t" + Number + "份");
System.out.println("您的费用为:" + sumPrice);
System.out.println("您的运费为:" + price);
System.out.println("您的订单费用为:" + (price + sumPrice));
// 将数据带入数组
ehs.times[i] = time;
ehs.addresses[i] = address;
ehs.money[i] = (sumPrice + price);
ehs.menus[i] = menuNames[chooseMenuName - 1];
ehs.states[i] = 0;
ehs.numbers[i] = Number;
break;
}
}
if (!flag) {
System.out.println("购物车已经满了");
return false;
}
return true;
}
// 查看购物篮
public void display() {
// 查看购物 显示 序号 订餐人 餐品信息 送餐时间 送餐地址 总金额 订单状态
System.out.println("编号\t\t\t订餐人\t\t菜品(份数)\t\t\t送餐时间 " +
"\t\t\t送餐地址\t\t\t总金额\t\t\t状态\t\t\t点赞数量");
for (
int i = 0;
i < ehs.names.length; i++) {
if (ehs.names[i] != null) {
String state = (ehs.states[i] == 0) ? "正在配送" : "已经送达";
int time = ehs.times[i];
String sumPrice = ehs.money[i] + "元";
String address = ehs.addresses[i];
String menu = ehs.menus[i];
int count = ehs.numbers[i];
System.out.println(i + 1 + "\t\t\t" + ehs.names[i] + "\t\t\t" + menu + "\t"
+ count + "\t\t\t" + time + "\t\t\t" + address
+ "\t\t\t" + sumPrice + "\t\t\t" + state + "\t\t\t" + ehs.pNums[i]);
}
}
}
public void sign() {
boolean flagA = false;
// 三种状态 1 已经签收的 不需要重复签收 2 在配送中 即将达到 可以签收 3 已经送达签收完毕 不能重复签收
System.out.println("请您输入需要签收的订单编号:");
int chooseNum = scanner.nextInt();
for (int i = 0; i < ehs.names.length; i++) {
if (ehs.names[i] != null && ehs.states[i] == 1 && i == chooseNum - 1) {
// 状态值设定为已完成
ehs.states[i] = 1;
System.out.println("订单签收成功");
flagA = true;//找到
break;
} else if (ehs.names[i] != null && ehs.states[i] == 1 && i == chooseNum - 1) {
System.out.println("您的订单已经完成签收,无法再次签收");
flagA = true;//找到
}
}
if (!flagA) {//未找到
System.out.println("您选择的订单不存在");
}
}
public void delete() {
// 定标记 判断是否被签 是否存在
boolean flagB = false;
System.out.println("请您输入订单的编号");
int chooseNum = scanner.nextInt();
for (int i = 0; i < ehs.names.length; i++) {
if (ehs.names[i] != null && ehs.states[i] == 1 && i == chooseNum - 1) {
// 删除后,数组元素依次移位
for (int j = i; j < ehs.names.length - 1; j++) {
ehs.names[j] = ehs.names[j + 1];
ehs.menus[j] = ehs.menus[j + 1];
ehs.times[j] = ehs.times[j + 1];
ehs.addresses[j] = ehs.addresses[j + 1];
ehs.states[j] = ehs.states[j + 1];
ehs.numbers[j] = ehs.numbers[j + 1];
ehs.money[j] = ehs.money[j + 1];
ehs.pNums[j] = ehs.pNums[j + 1];
}
int endChooseNum = ehs.names.length - 1;
ehs.names[endChooseNum] = null;
ehs.menus[endChooseNum] = null;
ehs.times[endChooseNum] = 0;
ehs.addresses[endChooseNum] = null;
ehs.states[endChooseNum] = 0;
ehs.money[endChooseNum] = 0;
ehs.pNums[endChooseNum] = 0;
System.out.println("删除订单成功");
flagB = true;
break;
} else if (ehs.names[i] != null && ehs.states[i] == 0 && i == chooseNum - 1) {
System.out.println("订单未签收,无法删除");
flagB = true;//找到
break;
}
}
if (!flagB) {
System.out.println("订单不存在");
}
}
public void prices() {
// 显示菜品信息
System.out.println("序号\t\t\t餐名\t\t\t价格");
for (int i = 0; i < menuNames.length; i++) {
String price = prices[i] + "元";
String priaiseNum = (ehs.pNums[i] > 0 ? ehs.pNums[i] + "赞" : " ");
System.out.println((i + 1) + "\t" + menuNames[i] + "\t" + prices[i]);
}
System.out.println("请您输入点赞菜的序号:");
int pNum = scanner.nextInt();
ehs.pNums[pNum - 1]++;
System.out.println("点赞成功");
}
}
package com.opp.opp.bookone.ordering;
public class EattingHappySet {
// 订餐人姓名
String[] names = new String[5];
// 菜品
String[] menus = new String[5];
// 份数
int[] numbers = new int[5];
// 送餐日期
int[] times = new int[5];
// 送餐地址
String[] addresses = new String[5];
// 送餐状态 true已完成订单 false 订单配送中
int[] states = new int[5];
// 支付金额
double[] money = new double[5];
// 点赞数
int[] pNums = new int[5];
// 签单
int[] signs = new int[5];
}