代码都写得很简单,用的都是一些通俗易懂的方法:
Main.java
import java.io.IOException;
ParseException;
import java.util.Scanner;public class Main {static Scanner sc = new Scanner(System.in);public static void main(String[] args) throws IOException, InterruptedException, ParseException {Order o = new Order();Menu();o.HaveOrder();o.Print();}public static void Menu() { //KFC主菜单System.out.println("--------------欢迎光临KFC---------------");System.out.println("-------------以下是菜单列表-------------");System.out.println("------1.快乐儿童餐 -> 价格:9.99RMB----");System.out.println("------2.吮指原味鸡 -> 价格:14.99RMB----");System.out.println("------3.香辣炸鸡餐 -> 价格:19.99RMB----");System.out.println("------4.可乐(大) -> 价格:4.99RMB----");System.out.println("------5.超值双人餐 -> 价格:39.99RMB----");System.out.println("---------------0.完成点餐--------------");System.out.println("---------------8.退出菜单--------------");System.out.println("---------------9.会员通道--------------");System.out.println("---------------------------------------");System.out.println("----------请输入你的菜单序号:----------");}
}
Order.java
import java.io.*;
import java.math.BigDecimal;
*;
import java.util.*;
public class Order {static ArrayList<String> arr = new ArrayList<String>();static Scanner sc = new Scanner(System.in);public float money = 0; //商品价格public float payMoney = 0; //支付钱数public float backMoney = 0; //找零public String String_backMoney; //正常支付时的找零的字符串表示,用以解决货币显示错误的问题public int OffNumber = 0; //使用优惠券的代码public boolean weekendOff = false; //是否周末折扣日public boolean paywithcard = false; //是否用储值卡支付Card card = new Card();public void HaveOrder() throws IOException, InterruptedException, ParseException { //点餐boolean flag = true;while (flag) {int num = sc.nextInt();if (num != 0) {switch(num) {case(1):System.out.println("你点了" + num + "号餐品 -> 快乐儿童餐,如果继续点餐请输入序号,结束点餐请输0!");money = money + 9.9f;arr.add(num + "号餐品 -> 快乐儿童餐 价格:9.9RMB");break;case(2):System.out.println("你点了" + num + "号餐品 -> 吮指原味鸡,如果继续点餐请输入序号,结束点餐请输0!");money = money + 14.9f;arr.add(num + "号餐品 -> 吮指原味鸡 价格:14.9RMB");break;case(3):System.out.println("你点了" + num + "号餐品 -> 香辣炸鸡餐,如果继续点餐请输入序号,结束点餐请输0!");money = money + 19.9f;arr.add(num + "号餐品 -> 香辣炸鸡餐 价格:19.9RMB");break;case(4):System.out.println("你点了" + num + "号餐品 -> 可乐(大),如果继续点餐请输入序号,结束点餐请输0!");money = money + 4.9f;arr.add(num + "号餐品 -> 可乐(大) 价格:49.9RMB");break;case(5):System.out.println("你点了" + num + "号餐品 -> 超值双人餐,如果继续点餐请输入序号,结束点餐请输0!");money = money + 39.9f;arr.add(num + "号餐品 -> 超值双人餐 价格:39.9RMB");break;case(8):it(1);case(9):card.Menu(); //进入会员通道card.HaveSelect();break;default:System.out.println("没有这个餐品!请重新输入!结束点餐请输 0 或其他!");break;}}else {flag = false;}}money = Rounding(money); //正常显示货币数System.out.println("你一共点了" + money + "RMB的餐品!");System.out.println("是否使用优惠券?(Y/N)");String ifUseOff = sc.next();if (ifUseOff.equals("Y") || ifUseOff.equals("y")) {Off(); //使用优惠券}else {System.out.println("不使用优惠券!");}System.out.println("请选择你的支付方式:n0.取消支付 1.现金支付 2.储值卡支付");boolean flag2 = true;while (flag2) {int selectPayWay = sc.nextInt();if (selectPayWay != 0 ) {switch(selectPayWay) {case(1):PayWithCash(); //现金支付flag2 = false;break;case(2):PayWithCard(); //储值卡支付flag2 = false;break;default:System.out.println("输入错误!请重新输入!");break;}}else {flag2 = false;}}}public void PayWithCard() throws IOException, InterruptedException, ParseException {paywithcard = true;SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //输出日期float restMoney = card.PayLogin(); //储值卡登陆int index; //用户在ArrayList中的index值将会传给它String str_restMoney; //用户余额的string方式表达if (isWeekend(time.format(new Date()).toString()) == true) { //如果是周末将享受8折优惠weekendOff = true;System.out.println("今天是周末!将享受八折折扣!"); //只有使用储值卡才会有周末优惠System.out.println("折前价为:" + money + "RMB");money *= 0.8;money = Rounding(money);System.out.println("折后价为:" + money + "RMB");}System.out.println("您当前的余额为:" + restMoney + "RMB");System.out.println("正在支付!请稍后!");Thread.sleep(2000);if (restMoney - money < 0) { //如果余额不足将直接退出程序Thread.sleep(2000);System.out.println("对不起!您的余额不足!");it(1);}restMoney = restMoney - money;restMoney = Rounding(restMoney);Thread.sleep(2000);System.out.println("支付成功!");System.out.println("您现在的余额为:" + restMoney + "RMB");str_restMoney = String(restMoney); //将余额转换成string型index = card.ReturnStaticIndex(); //index参数传递card.arr_money.set(index, str_restMoney); //更新ArrayList指定的index的用户余额情况card.Rewrite(); //重写用户列表(card)}public void PayWithCash() { //现金支付System.out.println("你选择使用现金支付!请输入你要付的钱数:");payMoney = sc.nextFloat();System.out.println("你支付了" + payMoney + "RMB,正在处理中请稍等!");if (payMoney >= money) {try {Thread.sleep(2000);backMoney = payMoney - money;DecimalFormat df = new DecimalFormat("0.0"); //对找零的数值进行合理化String_backMoney = df.format(backMoney);System.out.println("支付成功!找您" + String_backMoney + "RMB");//it(1);} catch (InterruptedException e) {e.printStackTrace();}}else {System.out.println("对不起!您支付的钱数不够!");it(1);}}public void Off() { //优惠券函数boolean flag = true;System.out.println("使用优惠券!");System.out.println("您目前可用的优惠券有:");System.out.println("0.取消使用优惠券 1.满10减2 2.满20减5 3.满50减20 4.满100减50");System.out.println("你选择你要使用的优惠券:");money = Rounding(money); //对money进行数值合理化while(flag) {Int()) {case(0):System.out.println("取消使用优惠券!");flag = false;break;case(1):if (money >= 10) {System.out.println("经系统检测,你可以使用该优惠券!");money -= 2;System.out.println("优惠已执行!目前合计:" + money + "RMB");OffNumber = 1; //设置使用的优惠券编号用于小票输出}else {System.out.println("对不起!你的金额暂不支持使用该优惠券!");}flag = false;break;case(2):if (money >= 20) {System.out.println("经系统检测,你可以使用该优惠券!");money -= 5;System.out.println("优惠已执行!目前合计:" + money + "RMB");OffNumber = 2;}else {System.out.println("对不起!你的金额暂不支持使用该优惠券!");}flag = false;break;case(3):if (money >= 50) {System.out.println("经系统检测,你可以使用该优惠券!");money -= 20;System.out.println("优惠已执行!目前合计:" + money + "RMB");OffNumber = 3;}else {System.out.println("对不起!你的金额暂不支持使用该优惠券!");}flag = false;break;case(4):if (money >= 100) {System.out.println("经系统检测,你可以使用该优惠券!");money -= 50;System.out.println("优惠已执行!目前合计:" + money + "RMB");OffNumber = 4;}else {System.out.println("对不起!你的金额暂不支持使用该优惠券!");}flag = false;break;default:System.out.println("输入错误!取消使用优惠券请输入0!");break;}}}public void Print() throws IOException {float beforeOff; //打折前的价格BufferedWriter write = new BufferedWriter((new FileWriter(new File("src/小票"))));SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); write.write(time.format(new Date())); //输出时间戳wLine();wLine();for (int i = 0; i < arr.size(); i++) {write.(i));wLine();} //输出商品列表wLine();if(weekendOff == true) {write.write("本次交易使用了周末优惠!");wLine();}if (paywithcard) {write.write("本次交易使用了储值卡支付!");wLine();}if (OffNumber != 0) {write.write("本次交易中使用了优惠券!");wLine();if (OffNumber == 1) {write.write("使用了 满10减2 优惠券!");wLine();beforeOff = money + 2;write.write("优惠前合计:" + beforeOff + "RMB");wLine();}else if (OffNumber == 2) {write.write("使用了 满20减5 优惠券!");wLine();beforeOff = money + 5;write.write("优惠前合计:" + beforeOff + "RMB");wLine();}else if (OffNumber == 3) {write.write("使用了 满50减20 优惠券!");wLine();beforeOff = money + 20;write.write("优惠前合计:" + beforeOff + "RMB");wLine();}else if (OffNumber == 4) {write.write("使用了 满100减50 优惠券!");wLine();beforeOff = money + 50;write.write("优惠前合计:" + beforeOff + "RMB");wLine();}}write.write("合计:" + money + "RMB");wLine();if (paywithcard == true) {write.write("支付:" + money + "RMB"); //如果使用储值卡支付,支付即为合计wLine();}else {write.write("支付:" + payMoney + "RMB");wLine();}if (paywithcard == true) { //使用储值卡支付,不会出现找零}else {write.write("找零:" + String_backMoney + "RMB");wLine();}write.write("祝您用餐愉快!");write.flush();write.close();}public float Rounding(float money) { //四舍五入BigDecimal b = new BigDecimal(money);return b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); }public boolean isWeekend(String date) throws ParseException { //判断是否为周末DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); Date bdate = format1.parse(date);Calendar cal = Instance();cal.setTime(bdate);(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){return true;}else{return false;}}
}
Card.java
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;public class Card {ArrayList<String> arr_id = new ArrayList<String>(); //存放会员信息的三个ArrayListArrayList<String> arr_password = new ArrayList<String>();ArrayList<String> arr_money = new ArrayList<String>();static Scanner sc = new Scanner(System.in);public int static_index;public String id;public String password;public float restMoney;public void Menu() {System.out.println("********欢迎进入会员通道********");System.out.println("*************0.注册*************");System.out.println("*************1.登录*************");System.out.println("*************2.返回*************");}public void AfterLoginMenu() {System.out.println("********你已经成功登录!*********");System.out.println("*************1.充值*************");System.out.println("*************2.余额*************");System.out.println("*************3.退出*************");}public void HaveSelect() throws IOException, InterruptedException {System.out.println("请输入您的操作序号:");boolean flag = true;while(flag) {int num = sc.nextInt();if (num != 2) {switch(num) {case(0):CreatCard();break;case(1):Login();break;default:System.out.println("没有这个选项!请重新输入!");break;}}else {flag = false;Main.Menu();}}}public void AfterLoginSelect() throws InterruptedException, IOException {System.out.println("请输入您的操作序号:");boolean flag = true;while(flag) {int num = sc.nextInt();if (num != 3) {switch(num) {case(1):Charge();break;case(2):Show();break;default:System.out.println("没有这个选项!请重新输入!");break;}}else {flag = false;Menu();HaveSelect();}}}public void CreatCard() throws IOException, InterruptedException {boolean flag = true;String secondPassword;System.out.println("请输入你的卡号:");this.id = sc.next();System.out.println("请输入你的密码:");this.password = sc.next();System.out.println("请再次输入你的密码:");while(flag) {secondPassword = sc.next();if (secondPassword.equals(password)) {Thread.sleep(2000);System.out.println("账号创建成功!");flag = false;}else {System.out.println("输入错误!请重新输入!");}}Thread.sleep(2000);System.out.println("ID:" + this.id);System.out.println("PassWord:" + this.password);System.out.println("为了您的账户安全!请不要将你的密码告诉其他人!");BufferedWriter write = new BufferedWriter((new FileWriter(new File("src/Card"),true)));write.write("ID:" + this.id); //输出信息到会员列表wLine();write.write("PassWord:" + this.password);wLine();write.write("Money:" + stMoney);wLine();System.out.println("储值卡创建完毕!");write.flush();write.close();}public void Login() throws IOException, InterruptedException { //在会员通道中登录BufferedReader read = new BufferedReader((new FileReader(new File("src/Card"))));String str_id,str_password;String str = "";boolean F = true;int flag = 1;int index;while((str = adLine()) != null) { //将会员的信息读入到ArrayList中if (flag % 3 == 1) {arr_id.add(str.substring(3));flag ++;}else if (flag % 3 == 2) {arr_password.add(str.substring(9));flag ++;}else{arr_money.add(str.substring(6));flag ++;}}read.close();while(F) {System.out.println("请输入你的卡号:");str_id = sc.next();if (ains(str_id)) {static_index = index = arr_id.indexOf(str_id);System.out.println("情输入你的密码:");str_password = sc.next();if ((index).equals(str_password)) {System.out.println("登录成功!");F = false;AfterLoginMenu();AfterLoginSelect();}else {System.out.println("密码错误!");}}else {System.out.println("未找到该用户!");}} }public float PayLogin() throws IOException, InterruptedException { //用于支付时登录BufferedReader read = new BufferedReader((new FileReader(new File("src/Card"))));String str_id,str_password;String str = "";boolean F = true;int flag = 1;int index;while((str = adLine()) != null) { //读取会员的信息if (flag % 3 == 1) {arr_id.add(str.substring(3));flag ++;}else if (flag % 3 == 2) {arr_password.add(str.substring(9));flag ++;}else{arr_money.add(str.substring(6));flag ++;}}read.close();while(F) {System.out.println("请输入你的卡号:");str_id = sc.next();if (ains(str_id)) {static_index = index = arr_id.indexOf(str_id);System.out.println("情输入你的密码:");str_password = sc.next();if ((index).equals(str_password)) {Thread.sleep(3000);System.out.println("登录成功!");F = false;}else {System.out.println("密码错误!");}}else {System.out.println("未找到该用户!");}}return restMoney = Float.parseFloat((static_index));}public int ReturnStaticIndex() {return static_index;}public void Charge() throws InterruptedException, IOException { //充值String money;float chargeMoney;float restMoney;System.out.println("请输入充值的数目:");chargeMoney = sc.nextFloat();restMoney = Float.parseFloat((static_index)); //先读取用户的余额到内存中,并转换为floatrestMoney = restMoney + chargeMoney;Thread.sleep(2000);System.out.println("充值成功!");System.out.println("现在的余额为:" + restMoney);money = String(restMoney); //充值完成后将float转换为stringarr_money.set(static_index, money); //将转换好的string更新到ArrayList中Rewrite();}public void Rewrite() throws IOException { //重写用户列表BufferedWriter write = new BufferedWriter((new FileWriter(new File("src/Card"))));for (int i = 0; i < arr_id.size(); i++) {write.write("ID:" + (i));wLine();write.write("PassWord:" + (i));wLine();write.write("Money:" + (i));wLine();}write.flush();write.close();}public void Show() throws InterruptedException { //查询用户余额System.out.println("查询中!请稍后!");Thread.sleep(2000);System.out.println("Money:" + (static_index));}}
前前后后花了大概两天时间,写的比较粗糙,代码复用性也有点差,实际上可以再优化。
主要涉及到一些问题:
T-T 我的作业为什么这么多…
本文发布于:2024-02-05 04:50:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170724528963188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |