java基础练习项目:吃货联盟

最开始看到题目是懵的,不知道怎么下手。等到后面理清的思维逻辑后,基本上也就直接填代码了。

大概流程是:先初始化这个题目里面所需要的变量,然后是用面向过程里的选择语句switch来提供用户选择的选项,在每个选项里设置方法,运用面向对象学到的基础内容来调用方法,具体代码如下:

public class FoodList {//初始化食品清单
String name;// 菜名
double price;// 菜品价格
int like;// 点赞数
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getLike() {
return like;
}
public void setLike(int like) {
this.like = like;
}
public FoodList(String name, double price,  int like) {
this.name = name;
this.price = price;
this.like = like;
}

}

public class ClientOrder {//订餐人订餐时输入的信息
String name;// 订餐人名称


String dishMegs;// 所选菜品信息


String times;// 送餐时间


String addresses;// 送餐地址


String states;// 订单状态


double sumPrices;// 订单总金额


public ClientOrder(String name, String dishMegs, String times, String addresses, String states, double sumPrices) {
super();
this.name = name;
this.dishMegs = dishMegs;
this.times = times;
this.addresses = addresses;
this.states = states;
this.sumPrices = sumPrices;
}

public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getDishMegs() {
return dishMegs;
}


public void setDishMegs(String dishMegs) {
this.dishMegs = dishMegs;
}


public String getTimes() {
return times;
}


public void setTimes(String times) {
this.times = times;
}


public String getAddresses() {
return addresses;
}


public void setAddresses(String addresses) {
this.addresses = addresses;
}


public String getStates() {
return states;
}


public void setStates(String states) {
this.states = states;
}


public double getSumPrices() {
return sumPrices;
}


public void setSumPrices(double sumPrices) {
this.sumPrices = sumPrices;
}

}


public class OrderingMgr {//执行程序的具体方法
Scanner input = new Scanner(System.in);
FoodList[] food = new FoodList[3];
ClientOrder[] orders=new ClientOrder[4];
public void init() {
food[0] = new FoodList("红烧带鱼", 38.0, 0);
food[1] = new FoodList("鱼香肉丝", 20.0, 0);
food[2] = new FoodList("时令鲜蔬", 10.0, 0);// 初始化菜单


}


public void orderMenu() {
System.out.println("欢迎使用“吃货联盟订餐系统”");
System.out.println("**********************************");
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=input.nextInt();
switch (choose) {
case 1:
// 点餐
toOrder();
back();
break;
case 2:
// 查看
lookOrder();
back();
break;
case 3:
// 签收订单
sign();
back();
break;
case 4:
// 删除订单
delectOrder();
back();
break;
case 5:
// 我要点赞
like();
back();
break;
case 6:
System.out.println("谢谢使用,欢迎下次光临!");
break;
default:
System.out.println("输入错误!");
break;


}



}
private void lookOrder() {
System.out.println("****查看餐袋****");
System.out.println("序号\t订餐人\t餐品信息\t\t送餐日期\t\t送餐地址\t\t总金额\t订单状态");
for (int i = 0; i < orders.length; i++) {
if (orders[i] != null) {
                String states;
                if (orders[i].states.equals("0")) {
states="已预订";
}else if (orders[i].states.equals("1")) {
states="已完成";
}else{
states="异常";
}
System.out.println(i + 1 + "\t" + orders[i].name + "\t" + orders[i].dishMegs + "\t" + orders[i].times+"日"
+ "\t\t" + orders[i].addresses + "\t" + orders[i].sumPrices+"元" + "\t" +states + "\t");
}
}

}








private void like() {
// TODO Auto-generated method stub
System.out.println("****我要点赞****");
System.out.println("序号\t菜名\t\t单价\t\t点赞数");
for (int i = 0; i < food.length; i++) {
if (food[i] != null) {
System.out.println(i + 1 + "\t" + food[i].name + "\t\t" + food[i].price + "元\t\t" + food[i].like);
}
}
System.out.println("请选择要点赞的商品序号:");
int like = input.nextInt();
if (like
food[like - 1].like += 1;
System.out.println("点赞成功~");
} else {
System.out.println("您要点赞的商品不存在!");
}
}


private void delectOrder() {
// TODO Auto-generated method stub
System.out.println("****删除订单****");
System.out.print("请输入要删除的订单序号:");
int b = input.nextInt();
if (b <= orders.length && orders[b - 1] != null && orders[b - 1].name != null) {
if (orders[b - 1].states.equals("1")) {
for (int i = b - 1; i < orders.length - 1; i++) {
orders[i] = orders[i + 1];
}
orders[orders.length - 1] = null;
System.out.print("删除成功!");
} else {
System.out.print("您选择的订单未签收,不能删除!");
}
} else {
System.out.print("该订单不存在!");
}
}


private void sign() {
// TODO Auto-generated method stub
System.out.println("****签收订单****");
System.out.print("请选择要签收的订单序号:");
int xuhao = input.nextInt();
if (orders[xuhao - 1] != null && orders[xuhao - 1].name != null && orders[xuhao - 1].states.equals("0")) {
orders[xuhao - 1].states = "1";
System.out.println("订单签收成功");
} else {
System.out.print("该订单已签收或者不存在该订单");
}
}


private void back() {
// TODO Auto-generated method stub
System.out.println("按0返回主菜单:");
int back = input.nextInt();
if(back==0){
orderMenu();
}else{
System.out.println("谢谢使用,已退出!");
}
}


private void toOrder() {

System.out.println("****我要订餐****");
System.out.print("请输入订餐人姓名:");
String name = input.next();
System.out.println("序号\t菜名\t\t单价\t\t点赞数");
for (int i = 0; i < food.length; i++) {
if (food[i] != null) {
System.out.println(i + 1 + "\t" + food[i].name + "\t\t" + food[i].price + "元\t\t" + food[i].like);
}
}
System.out.print("请选择您要点的菜品编号:");
int bianhao = input.nextInt();
System.out.print("请选择您需要的份数:");
int fenshu = input.nextInt();
System.out.print("请选择送餐的时间(送餐时间是10点至20点间整点送餐):");
String shijian = input.next();
System.out.print("请选择送餐地址:");
String dizhi = input.next();
for (int i = 0; i < orders.length; i++) {
if (orders[i] == null) {
double canfei = food[bianhao - 1].price * fenshu;
double song = 0;
if (canfei < 50.00) {
song = 10;
}
double zongfei = canfei + song;
orders[i] = new ClientOrder(name, food[bianhao - 1].name+fenshu+"份", shijian, dizhi, "0", zongfei);
System.out.println("订餐成功!");
System.out.println("您定的是:" + food[bianhao - 1].name + fenshu + "份");
System.out.println("送餐时间:" + shijian + "点");
System.out.println("餐费:" + canfei + "元," + "送餐费:" + song + "元,总计" + zongfei + "元。");
break;
}
}

}
}


public class OrderTest {//最后是测试这个项目
public static void main(String[] args) {
OrderingMgr eat=new OrderingMgr();
    eat.init();
    eat.orderMenu();    
}
}

你可能感兴趣的:(java学习)