點餐系統
場景:注冊多個使用者,使用者登入點餐系統進行點餐,點餐結束,結賬扣減使用者錢包餘額
對象:
餐廳(名字,菜品數組,就餐人員,就餐選擇的菜品數組)【歡迎,點菜,結賬,走人】
菜(菜名,價錢)
人(姓名,錢包餘額)
運作類:提示功能:注冊,登入(上一步,進入餐廳),退出系統
代碼
//主類(運作類)
package com.fish.www.ordering;
import java.util.Scanner;
public class OrderingSystem {
static String[] userName = new String[50];
static String[] userPassword = new String[50];
static int register = 0;//登記是否登入了系統
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
People people = new People();
Food food = new Food();
menu(scanner);
Restaurant restaurant = new Restaurant();
restaurant.menu(food);
restaurant.order(scanner,food);
people.name = userName[register];
people.speak(restaurant,food,scanner);
people.leave();
checkOut(scanner,people,restaurant);
}
//主界面
public static void menu(Scanner scanner) {
System.out.println("-------> 歡迎來到X的餐廳 <-------");
System.out.println(" ---->1.注冊\n -->2.登入");
System.out.println("請輸入你的選擇:");
if (scanner.nextInt() == 1) {
register(scanner);
} else {
System.out.println("沒有使用者,重新開始!\n\n\n-----------------");
menu(scanner);
}
}
//注冊界面
public static void register(Scanner scanner) {
System.out.println("\n\n-----------------\n開始注冊");
for (int i = 0; i < userName.length; i++) {
System.out.print("請輸入姓名:");
userName[i] = scanner.next();
System.out.print("請輸入密碼:");
userPassword[i] = scanner.next();
System.out.println("是否繼續輸入:");
if (scanner.nextInt() != 1) {
break;
}
}
System.out.println("選擇:\n1.上一步\n2.進入餐廳");
if (scanner.nextInt() == 1) {
register(scanner);
} else {
signIn(scanner);
}
}
//登入判斷
public static void signIn(Scanner scanner) {
System.out.println("\n\n-----------------\n開始登入");
System.out.print("請輸入姓名:");
String user = scanner.next();
System.out.print("請輸入密碼:");
String password = scanner.next();
for (int i = 0; i < userName.length; i++) {
if (userName[i].equals(user) && userPassword[i].equals(password)) {
register = i;
System.out.print("登入成功!");
break;
}
if (i == userName.length - 1) {
System.out.println("輸入錯誤!重新登入!\n-------------------\n");
signIn(scanner);
}
}
}
//結賬走人
public static void checkOut(Scanner scanner,People people,Restaurant restaurant){
System.out.println(userName[register]+"您好,您今晚的消費為:"+restaurant.sumPrice);
System.out.println("請選擇支付方式:1\n1.支付寶\n2.微信\n");
if (scanner.nextInt()==1){
people.assetsAlipay = people.assetsAlipay - restaurant.sumPrice;
System.out.println("支付成功!\n餘額為:"+people.assetsAlipay);
}else {
people.assetsWeChat = people.assetsWeChat - restaurant.sumPrice;
System.out.println("支付成功!\n餘額為:"+people.assetsWeChat);
}
}
}
//餐廳類
package com.fish.www.ordering;
import java.util.Scanner;
public class Restaurant {
/**
* 餐廳(名字,菜品數組,就餐人員,就餐選擇的菜品數組)【歡迎,點菜,結賬,走人】
*/
String restaurantName = "肥仔快樂餐廳";
String[] userFood = new String[500];
float sumPrice = 0;
public Restaurant() {
System.out.println("歡迎來到" + restaurantName);
}
//菜單
public void menu(Food food) {
System.out.println("開始點餐:");
for (int i = 0; i < food.foodMenu.length; i++) {
System.out.println((i + 1) + "." + food.foodMenu[i] + "\t" + food.foodPrice[i] + "元");
}
}
//點餐
public void order(Scanner scanner, Food food) {
for (int i = 0; i < userFood.length; i++) {
System.out.println("請輸入你的選擇:");
int userChose = scanner.nextInt() - 1;
if (userChose < 9 && userChose >= 0) {
sumPrice = sumPrice + food.foodPrice[userChose];
userFood[i] = food.foodMenu[userChose];
System.out.println("你選擇了:" + food.foodMenu[userChose] + "\n目前消費:" + sumPrice);
} else if (userChose == 9) {
break;
} else {
System.out.println("沒有我喜歡的,走了");
break;
}
}
}
}
//食物與人
package com.fish.www.ordering;
public class Food {
/**
* 菜(菜名,價錢)
*/
String[] foodMenu = new String[]{"霸王蟹", "石斑魚", "羊肉串", "牛肉串", "烤全羊", "白斬雞", "檸檬鴨", "可樂", "雪碧", "結束", "直接走人"};
int[] foodPrice = new int[]{25, 26, 3, 5, 1000, 35, 40, 6, 6, 0, 0};
}
package com.fish.www.ordering;
import java.util.Random;
import java.util.Scanner;
public class People {
/**
* 人(姓名,錢包餘額)
*/
String name;
int assetsWeChat;
int assetsAlipay;
public People() {
Random random = new Random();
this.assetsWeChat = this.assetsAlipay = random.nextInt(10000000) + 500000;
}
public void speak(Restaurant restaurant, Food food, Scanner scanner) {
System.out.println("服務員,我加餐。");
restaurant.order(scanner, food);
}
public void leave(){
System.out.println("服務員,結賬!");
}
}
運作截圖
|