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.length&&food[like - 1] != null) {
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();
}
}