水果店(库)管理系统
- 1.前言:
- 2.功能简介及部分测试视频:
- 3.本管理系统的构建原理(简介):
- (1).如何跳转页面:
- (2).如何让控制台能输出彩色字体:
- (3).如何稳定存储数据:
- (4).如何实现购物车:
- (5).如何让数据长久保存:
- 4.如何在eclipse上运行本代码:
- (1).让 eclipse 控制台能输出彩色字体。
- (2).分块复制到 eclipse 运行(条理清晰)。
- (3).直接在一个Main类运行(方便):
- 5.综上:
1.前言:
创作这个管理系统的初心是,大一的时候编程老师让我们写过一个超市管理系统(c++)当时不会,在网上借鉴别人的代码,大致了解了一下原理,更多的感受是神奇,埋下了自己实现的种子。这里大一编程老师的启蒙非常重要 ♥️
到了大二学习了数据结构,其中线性表,链表的储存数据的方式真是非常精妙,学完数据结构,我便构思着如何能让这些知识运用到实处,巧了本管理系统的核心就是数据结构的线性表。(这里必须要称赞我大二数据结构老师的课,条例非常清晰,以至于学完这个课半年之后,我依旧对数据结构有很深印象😍)
当然真正决定要实现是在大二下学期操作系统课上(我被顾客和服务之前巧妙的工作关系吸引)决定实现自己的系统。😂
由于是第一次写管理系统,本代码大概有1200行,是在3月2日开始写,中间写了一半但由于数据无法长久存储,被迫终止,重新构建底层逻辑。于4月10号在构建出新的底层逻辑后,重新开始写至今天4月26日完结,跨度大概55天。
本代码是由纯Java实现,以示我对这个语言的热爱。
2.功能简介及部分测试视频:
本管理系统一共有17个功能:
水果店管理系统,终极测试版
上述功能完美实现一个闭环,调用闭环内的函数的目的就是为了修改内部数据
3.本管理系统的构建原理(简介):
(1).如何跳转页面:
核心是清屏函数,及清掉本次操作的过程,打印下一次操作的页面,可以给人一种跳转效果。函数实现具体参考我的之前写的博客 ->用代码实现eclipse控制台清屏。
(2).如何让控制台能输出彩色字体:
这个实现起来也是非常的简单,-> eclipse让控制台能输出彩色字体,大概半分钟能实现。
(3).如何稳定存储数据:
解决这个问题我没有用list作为底层逻辑,1.是list本质是指针,后续那些函数必须要对储存的数据进行各种修改,主要还是担心list无法提供这些稳定性。2.每种水果要存储很多信息,名称、产地、保质期…如果用list实现的话要嵌套在另一个list内十分不方便。
解决方法:
我直接设立一个fruit类(类似int)
class fruit {//每种水果的信息
String name;//水果名字
String data;//进货日期
int time;//保质期(天)
String place;//产地
double enterprice;//进价
double leaveprice;//出口价
double fruitamount;//库存 (kg)
}
再设立一个 fru 数组存储每种水果的信息,即可。
接下来就直接根据数据结构的线性表知识为核心实现增添,删除,扩容操作即可
如果不知到 list 为什么不稳定,可以看我写的 对 List 函数底层逻辑的理解(简要概述)
如果不知道如何用创建的类构建数组可以看我写的 对 JAVA 中 “指针“ 的理解
(4).如何实现购物车:
购物车的实现是我最满意的想法之一😍真的爱了这个想法,太优雅了
最开始实现的时候我也是一筹莫展,最笨的方法是将库内的数据复制到购物车类里,但这样实在是太繁琐了,如果去实现的话那么购物车类的代码量应该是800行左右,太繁琐了。在沉淀10天之后
在计网课上我突然醒悟可以用java自带HashSet去存储每个水果其所对应下标,然后对Set内下标进行操作,那么购物车的修改、清除、增添全部迎刃而解,可见数组的下标是多么实用,用Set的另一个原因是内部存储的下标不会重复,那么根据下标找到的水果也不会重复。只能说妙,太妙了
😍
(5).如何让数据长久保存:
由于还没学习数据库,所以我们目前能做的就是能让数据在整个程序运行过程中数据不会丢失,只有重启程序,数据才会恢复默认。
那么具体应如何不让数据在运行中丢失呢:
首先我的数据是存储在一个类内的,数据恢复默认的原因是你在修改该这个类变量s内的数据后,又重新new了一个新变量s1,如果你要用变量s1进行后续操作,你很容易会发现s1内的数据是默认数据。所以为了避免出现这个问题,你只要一直用s进行操作即可。这里就需要在 Main 主方法内确保整个程序运行过程中一直是那一个s。
4.如何在eclipse上运行本代码:
这里给了几种方法确保本代码你能在你的eclipse上跑起来,毕竟再厉害的代码你的编译器不能运行都是白搭😂
(1).让 eclipse 控制台能输出彩色字体。
目的是为了有更好的观赏性。
(2).分块复制到 eclipse 运行(条理清晰)。
1.建立一个project工程。
2.建立一个shop包(package)。
3.分别建立五个class类,将下方代码分别装入,在Main里运行即可。
需要建立的五个类代码分别如下:
一,Expand类
package shop;
class Expand{
public String cipher = "123";
public boolean member = false;
public int icapacity = 7;//初始容量(当前容量)
public static int size = 6;//水果总类
public fruit fru[] = new fruit[icapacity];//初始容量
Expand(){//构造函数
initial(this.fru, icapacity);
//默认水果
fru[0].name = "富士苹果";
fru[0].data = "2023/3/4";
fru[0].enterprice = 23.45;
fru[0].leaveprice = 24.99;
fru[0].place = "许昌静9/215";
fru[0].time = 11;
fru[0].fruitamount = 100.45;
fru[1].name = "朴素香蕉";
fru[1].data = "2023/3/4";
fru[1].enterprice = 1.55;
fru[1].leaveprice = 1.99;
fru[1].place = "许昌静9/215";
fru[1].time = 11;
fru[1].fruitamount = 110.45;
fru[2].name = "沙糖桔";
fru[2].data = "2023/3/4";
fru[2].enterprice = 1.45;
fru[2].leaveprice = 1.99;
fru[2].place = "许昌静9/215";
fru[2].time = 11;
fru[2].fruitamount = 200.45;
fru[3].name = "朴素菠萝";
fru[3].data = "2023/4/26";
fru[3].enterprice = 16;
fru[3].leaveprice = 16.5;
fru[3].place = "许昌静9/215";
fru[3].time = 11;
fru[3].fruitamount = 200.45;
fru[4].name = "朴素西瓜";
fru[4].data = "2023/4/26";
fru[4].enterprice = 10;
fru[4].leaveprice = 11;
fru[4].place = "许昌静9/215";
fru[4].time = 11;
fru[4].fruitamount = 225.45;
fru[5].name = "芒果";
fru[5].data = "2023/4/26";
fru[5].enterprice = 13;
fru[5].leaveprice = 15;
fru[5].place = "许昌静9/215";
fru[5].time = 11;
fru[5].fruitamount = 110.45;
}
public void Exchage(fruit a, fruit b) {
a.name = b.name;
a.data = b.data;
a.enterprice = b.enterprice;
a.fruitamount = b.fruitamount;
a.leaveprice = b.leaveprice;
a.time = b.time;
a.place = b.place;
}
public void delete(int i) {
for(int j = i; j < size - 1; j ++)
Exchage(fru[j],fru[j + 1]);
size --;
}
public void initial(fruit fru[], int number) {//初始化
for(int i = 0; i < number; i ++) fru[i] = new fruit();
}
public void updatecapacity(int newcapacity) {//更新容量
fruit newdata[] = new fruit[newcapacity];
initial(newdata, newcapacity);
for(int i = 0; i < size; i ++) newdata[i] = fru[i];//挪过来
fru = newdata;//指向
this.icapacity = newcapacity;//最大容量更新
}
public void print() {
for(int i = 0; i < this.size; i ++) {
System.out.println("\033[35m" + "꧁༺๑๑༻꧂" + "\033[0m");
System.out.print("水果名称: " + "\033[36m" + fru[i].name + " ");
System.out.print("\033[0m" + "进货日期: " + "\033[36m" + fru[i].data + " ");
System.out.print("\033[0m" + "水果保质期(天): " + "\033[36m" + fru[i].time + " ");
System.out.println("\033[0m" + "产地:" + "\033[36m" + fru[i].place + " ");
System.out.print("\033[0m" + "\033[0m" + "进价(元/kg): " + "\033[36m" + fru[i].enterprice + " ");
System.out.print("\033[0m" + "出口价(元/kg): " + "\033[36m" + fru[i].leaveprice + " ");
System.out.print("\033[0m" + "库存 (kg): " + "\033[36m");
System.out.printf("%.2f\n", fru[i].fruitamount);
System.out.print("\033[0m");
}
System.out.println();
}
public boolean isDb_Number(String s) {
try {
Double.parseDouble(s);
return true;
}catch(Exception e) {
return false;
}
}
public boolean isIt_Number(String s) {
try {
Integer.parseInt(s);
return true;
}catch(Exception e) {
return false;
}
}
public void print_customer() {
System.out.printf("%-18s%-20s%-20s%-20s%-20s%-20s\n","水果名称 " , "进货日期 " , "水果保质期(天) " , "产地 " , "价格(元/kg) " , "库存 (kg) ");
for(int i = 0; i < this.size; i ++) {
System.out.println("\033[35m" + "꧁༺๑๑༻꧂" + "\033[0m");
System.out.print("\033[36m");
System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s\n", fru[i].name, fru[i].data , fru[i].time , fru[i].place , fru[i].leaveprice ,(float)fru[i].fruitamount);
System.out.print("\033[0m");
}
System.out.println();
}
public void print_someone(int i){
System.out.println("所选水果信息如下:");
System.out.println("\033[35m" + "꧁༺๑๑༻꧂" + "\033[0m");
System.out.println("水果名称: " + "\033[36m" + fru[i].name);
System.out.println("\033[0m" + "进货日期: " + "\033[36m" + fru[i].data);
System.out.println("\033[0m" + "水果保质期(天): " + "\033[36m" + fru[i].time);
System.out.println("\033[0m" + "产地:" + "\033[36m" + fru[i].place);
System.out.println("\033[0m" + "\033[0m" + "进价(元/kg): " + "\033[36m" + fru[i].enterprice);
System.out.println("\033[0m" + "出口价(元/kg): " + "\033[36m" + fru[i].leaveprice);
System.out.println("\033[0m" + "库存 (kg): " + "\033[36m" + (float)fru[i].fruitamount);
System.out.print("\033[0m");
}
}
此类主要存储水果信息,以及增删改查函数。
二,fruit类
package shop;
class fruit {//每种水果的信息
String name;//水果名字
String data;//进货日期
int time;//保质期(天)
String place;//产地
double enterprice;//进价
double leaveprice;//出口价
double fruitamount;//库存 (kg)
}
三,Function_admin类
package shop;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;
class Function_admin{//存储菜单,实用功能
public static void clear() throws AWTException{//清除函数
Robot ro = new Robot();
ro.delay(100);//延时
ro.mousePress(InputEvent.BUTTON3_MASK);//单机鼠标右键
ro.mouseRelease(InputEvent.BUTTON3_MASK);//松开右键
ro.keyPress(KeyEvent.VK_CONTROL);//按ctrl键
ro.keyPress(KeyEvent.VK_R);//按R
ro.keyRelease(KeyEvent.VK_R);//松开R
ro.keyRelease(KeyEvent.VK_CONTROL);//松开ctrl
ro.delay(250);
}
public static void memu1_print() {
System.out.println("\033[36m" + "☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃");
System.out.println("\033[33m" + " ❀❀❀欢迎来到Narnat的水果店(库)❀❀❀");
System.out.println("\033[36m" + "☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃");
System.out.println();
System.out.println("\033[0m" + "这里的水果价格几乎都是批发价哦,绝对实惠!");
}
public static void memu2_print() {
System.out.println("\033[36m" + " narnat的水果店 " + "\033[0m");
System.out.println("\033[30m" + " 模式选择 " + "\033[0m");
System.out.println("*************" + "\033[35m"+" 1.顾客模式 "+"\033[0m"+"*************");
System.out.println("*************" + "\033[36m"+" 2.管理员模式 "+"\033[0m"+"*************");
System.out.println("*************" + "\033[37m"+" 0.退出系统 "+"\033[0m"+"*************");
System.out.println("请选择您想要的服务模式 (0 ~ 2): ");
}
public static void enter_1() throws AWTException {
System.out.println("\033[0m" + "请在控制台内单击 enter 以继续:");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
clear();
}
public static int enter_2() throws AWTException {
while(true) {
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
if(n.length() != 1 || !(n.charAt(0) >= '0' && n.charAt(0) <= '2')) {
System.out.println("\033[31m" + "please enter between 0 ~ 2" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s = sc.nextLine();
clear();
}
else {
clear();
return Integer.parseInt(n);
}
}
}
public static char enter_3() throws AWTException {
while(true) {
System.out.print("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
clear();
}
else {
clear();
return s.charAt(0);
}
}
}
public static int enter_4() throws AWTException {
Robot r = new Robot();
Scanner sc = new Scanner(System.in);
while(true) {
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) >= '0' && s.charAt(0) <= '6')) {
System.out.println("\033[31m" + "please enter between 0 ~ 6" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s1 = sc.nextLine();
clear();
}
else {
clear();
System.out.print("\033[0m" + "输入成功正在跳转......");
r.delay(400);
clear();
return Integer.parseInt(s);
}
}
}
public static void view_profit(Function_customer fc){
System.out.println("\033[31m" + "净利润(yuan):" + "\033[0m");
System.out.println(fc.total_cost - fc.foundation_cost);
}
public static void leave_print() throws AWTException {
clear();
System.out.println("\033[34m" + "❀❀❀❀感谢您的光临,期待您的再次光临❀❀❀❀" + "\033[0m");
}
public static void check(Expand e) throws AWTException {
Robot r = new Robot();
Scanner sc = new Scanner(System.in);
int i = 3;
while(true) {
if(i <= 0) {
System.out.println("很抱歉验证次数已经用完");
System.out.println("您需要等待一段时间(15s)后才能再次输入");
System.out.println("若您忘记密码,您可以重启程序以恢复默认密码");
r.delay(10000); //10s
clear();
i = 2;
continue;
}
System.out.println("请输入" + e.cipher.length() + "位密码以进入管理员模式");
System.out.println("\033[32m" + "请在下方控制台内顶格输入:" + "\033[0m");
String s1 = "";
s1 = sc.nextLine();
if(s1.equals(e.cipher))
break;
else {
System.out.println("\033[31m" + "password error" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s2 = sc.nextLine();
clear();
}
i --;
}
e.member = true;
System.out.println("\033[31m" + "密码正确即将进入管理员模式!" + "\033[0m");
r.delay(1000);// 1s;
clear();
}
public static void memu3_print() {
System.out.println("\033[36m" + " 管理员模式" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 0.退至模式选择 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 1.密码修改 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 2.增添水果种类 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 3.删除水果种类 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 4.打印库存 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 5.增添/删除水果信息 " + "\033[32m" + "☆☆☆☆☆☆☆☆ " + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 6.查看利润 " + "\033[32m" + "☆☆☆☆☆☆☆☆ " + "\033[0m");
System.out.println("请输入您想要进行的操作选项编号(待更新):");
}
public static void remind_print() {
System.out.println("温馨提示:");
System.out.println("1.管理员模式,只有超市管理员才能进入,进入需要输入密码。");
System.out.println("2.若之前编辑过新密码,且忘记密码,可以重启程序,恢复默认密码。");
System.out.println("3.若选择 'n' 可以直接退回到服务模式选择界面。");
System.out.println("\033[36m" + "请确认您是否要打开管理员模式(y/n):" + "\033[0m");
}
public static void cipher_change(Expand e) throws AWTException {
System.out.println(" 密码修改");
Robot r = new Robot();
Scanner sc = new Scanner(System.in);
int i = 3;
while(i > 0) {
System.out.println("请再次输入" + e.cipher.length() + "位管理员密码");
System.out.println("\033[32m" + "请在下方控制台内顶格输入:" + "\033[0m");
String s1 = "";
s1 = sc.nextLine();
if(s1.equals(e.cipher))
break;
else {
System.out.println("\033[31m" + "password error" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s2 = sc.nextLine();
clear();
}
i --;
if(i <= 0) {
System.out.println("很抱歉验证次数已经用完");
System.out.println("您需要等待一段时间(15s)后才能再次输入");
System.out.println("若您忘记密码,您可以重启程序以恢复默认密码");
r.delay(10000); //10s
clear();
i = 2;
}
}
clear();
String s2 = "";
String s3 = "";
while(true) {
System.out.println("请输入新密码:");
s2 = sc.nextLine();
if(s2.equals(e.cipher)) {
System.out.println("\033[31m" + "输入的密码与原密码相同!" + "\033[0m");
enter_1();
continue;
}
clear();
for(int j = 0; j < s2.length(); j ++) System.out.print("*");
System.out.println();
System.out.println("请再次输入密码:");
s3 = sc.nextLine();
if(s2.equals(s3)) {
e.cipher = s3;
e.member = false;
System.out.println("更改成功!即将退至模式选择界面");
r.delay(300);
clear();
return;
}
else {
System.out.println("\033[31m" + "error! two password not equals" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s4 = sc.nextLine();
clear();
}
}
}
public static void add_fruit(Expand e) throws AWTException {//添加
clear();
System.out.println(" 水果种类添加");
Scanner sc = new Scanner(System.in);
int i = 0;
String s = "";
String s2 = "";
String s3 = "";
if(e.size == e.icapacity) e.updatecapacity(2*e.size);//原容量翻倍
while(true) {
System.out.print("\033[33m" + "请输入新水果名称:" + "\033[0m");
s = sc.nextLine();
for(i = 0; i < e.size; i ++) {
if(s.equals(e.fru[i].name)) {
System.out.println("\033[31m" +"error!"+ s + "aready have! " + "\033[0m");
break;
}
}
if(i!= e.size) {
System.out.println("请在控制台下输入enter以继续");
String s1 = sc.nextLine();
}
else break;
}
e.fru[e.size].name = s;
System.out.println("\033[31m"+"水果名称添加成功!"+"\033[0m");
System.out.println("请将以下信息填写完整:");
System.out.println("\033[33m" + "请输入进货日期:" + "\033[0m");
e.fru[e.size].data = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
while(true) {
System.out.println("\033[33m" + "请输入保质期(天):" + "\033[0m");
s2 = sc.nextLine();
if(e.isIt_Number(s2) && Integer.parseInt(s2) > 0) {
e.fru[e.size].time = Integer.parseInt(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数: " + "\033[0m");
}
System.out.println("\033[33m" + "请输入产地:" + "\033[0m");
e.fru[e.size].place = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
while(true) {
System.out.println("\033[33m" + "请输入进价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Integer.parseInt(s2) > 0) {
e.fru[e.size].enterprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
while(true) {
System.out.println("\033[33m" + "请输入出口价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2)&& Double.parseDouble(s2) > 0) {
e.fru[e.size].leaveprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
while(true) {
System.out.println("\033[33m" + "请输入库存 (kg):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[e.size].fruitamount = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
e.size ++;
System.out.println("新的水果基本信息已经储存完毕,新的水果库存如下:");
Robot r = new Robot();
r.delay(250);
clear();
e.print();
while(true) {
System.out.println("是否继续添加? y/n");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
clear();
continue;
}
else
break;
}
r.delay(250);
System.out.println("正在跳转...");
clear();
if(s3.charAt(0) == 'y') {
System.out.println("已有水果种类及信息如下:");
e.print();
add_fruit(e);
}
else
return;
}
public static void delete_fruit(Expand e) throws AWTException {
Robot r = new Robot();
String s3 = "";
System.out.println("已有水果总类及信息如下:") ;
e.print();
Scanner sc = new Scanner(System.in);
int index = 0;
while(true) {
System.out.println("请选择您要删除的水果名称");
String s = sc.nextLine();
int i = 0;
for(i = 0; i < e.size; i ++)
if(e.fru[i].name.equals(s)) {
index = i;
break;
}
if(i == e.size) {
System.out.println("\033[31m" + "error!" + s + "这种水果,并未找到!" + "\033[0m");
while(true) {
System.out.println("是否继续进行删除操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s1 = sc.nextLine();
}
else
break;
}
if(s3.charAt(0) == 'y') continue;
else {
clear();
return;
}
}
else {
while(true) {
System.out.println("查找成功!" + "删除后数据将无法挽回,是否继续进行删除操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
}
else
break;
}
if(s3.charAt(0) == 'y') {
clear();
System.out.println("删除成功!更新后的状态如下:");
e.delete(i);
System.out.println("请按enter键以继续:");
String s4 = sc.nextLine();
clear();
return;
}
else {
System.out.println("正在退出删除模式...");
r.delay(250);
clear();
return;
}
}
}
}
public static void revise_fruit_1(Expand e) throws AWTException {
Robot r = new Robot();
String s3 = "";
System.out.println("已有水果总类及信息如下:") ;
e.print();
Scanner sc = new Scanner(System.in);
int index= 0;
while(true) {
System.out.println("请选择您要修改的水果名称");
String s = sc.nextLine();
int i = 0;
for(i = 0; i < e.size; i ++)
if(e.fru[i].name.equals(s)) {
index = i;
break;
}
if(i == e.size) {
System.out.println("\033[31m" + "error!" + s + "这种水果,并未找到!" + "\033[0m");
while(true) {
System.out.println("是否继续进行修改操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s1 = sc.nextLine();
}
else
break;
}
if(s3.charAt(0) == 'y') continue;
else {
clear();
return;
}
}
else {
out :while(true) {
System.out.println("查找成功!");
r.delay(300);
clear();
e.print_someone(index);
revise_fruit_2(e, index);
while(true) {
System.out.println("是否继续进行修改操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s1 = sc.nextLine();
}
else {
clear();
break;
}
}
if(s3.charAt(0) == 'y') continue out ;
else {
clear();
return;
}
}
}
}
}
public static void revise_fruit_2(Expand e, int index) throws AWTException {
String revise[] = {"水果名称", "进货日期", "水果保质期", "产地", "进价", "出口价", "库存"};
Scanner sc = new Scanner(System.in);
System.out.println("请输出您想修改的信息所对应的编号:");
for(int i = 0; i < 7; i ++) {
System.out.print("\033[33m" + i +"."+ revise[i] + " " + "\033[0m");
}
System.out.println();
System.out.println("请输入您的选择:");
String n = sc.nextLine();
if(n.length() != 1 || !(n.charAt(0) >= '0' && n.charAt(0) <= '6')) {
System.out.println("\033[31m" + "please enter between 0 ~ 6" + "\033[0m");
enter_1();
revise_fruit_2(e, index);
}
else {
revise_fruit_3(e, Integer.parseInt(n), index);
}
}
public static void revise_fruit_3(Expand e, int n, int index) {
Scanner sc = new Scanner(System.in);
String s2 = "";
if(n == 0) {
System.out.println("\033[33m" + "请输入新水果名称:" + "\033[0m");
e.fru[index].name = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
}
else if(n == 1) {
System.out.println("\033[33m" + "请输入新进货日期:" + "\033[0m");
e.fru[index].data = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
}
else if(n == 2) {
while(true) {
System.out.println("\033[33m" + "请输入新保质期(天):" + "\033[0m");
s2 = sc.nextLine();
if(e.isIt_Number(s2) && Integer.parseInt(s2) > 0) {
e.fru[index].time = Integer.parseInt(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数: " + "\033[0m");
}
}
else if(n == 3) {
System.out.println("\033[33m" + "请输入新产地:" + "\033[0m");
e.fru[index].place = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
}
else if(n == 4) {
while(true) {
System.out.println("\033[33m" + "请输入进价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[index].enterprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}else if(n == 5) {
while(true) {
System.out.println("\033[33m" + "请输入出口价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[index].leaveprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}
else if(n == 6) {
while(true) {
System.out.println("\033[33m" + "请输入库存 (kg):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[index].fruitamount = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}
}
}
主要实现管理员模式的一系列功能
四,Function_customer类
package shop;
import java.awt.AWTException;
import java.awt.Robot;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Function_customer{
public static Set s = new HashSet<Integer>();
public static double price[];
public static double number[];
public static int fc_initail = 7;
public static int fc_size = 6;
public static double customer_sumcost = 0;
public static double total_cost = 0;
public static double foundation_cost = 0;
Function_customer(){
price = new double[fc_initail];
number = new double[fc_initail];
}
public static void update_message(int newfc_data) {
double new_price[] = new double[newfc_data];
double new_number[] = new double[newfc_data];
for(int i = 0; i < fc_size; i ++) {//挪过来
new_price[i] = price[i];
new_number[i] = number[i];
}
price = new_price;
number = new_number;
fc_initail = newfc_data;
}
public static void view_bill(){
System.out.println("\033[31m" + "总消费金额(yuan)" + "\033[0m");
System.out.println(total_cost);
}
public static void checkout(Function_admin f, Expand e) throws AWTException {
print_shop_cart(e, f);
Robot r = new Robot();
if(s.isEmpty()) {
System.out.println("购物车为空!");
f.enter_1();
return;
}
if(enter_8(f)) {
if(customer_sumcost < 0) customer_sumcost = 0;
total_cost = total_cost + customer_sumcost;
customer_sumcost = 0;
for(Object o : s) {
int n = (int)o;
foundation_cost = foundation_cost + e.fru[n].enterprice * number[n];
price[n] = 0;
number[n] = 0;
}
System.out.println("购买成功!感谢惠顾");
r.delay(300);
s.clear();
f.clear();
}
else
{
System.out.println("正在退出结账系统......");
r.delay(300);
f.clear();
}
}
public static void cart_clear(Function_admin f, Expand e) throws AWTException {
Robot r = new Robot();
print_shop_cart(e, f);
if(s.isEmpty()) {
System.out.println("购物车为空!");
f.enter_1();
return;
}
if(enter_7(f)) {
System.out.println("清除成功正在退出.....");
r.delay(200);
f.clear();
int n = 0;
for(Object o : s) {
n = (int)o;
e.fru[n].fruitamount = number[n] + e.fru[n].fruitamount;
number[n] = 0;
price[n] = 0;
}
}
else {
System.out.println("正在退出......");
r.delay(200);
f.clear();
}
}
public static void cart_change(Function_admin f, Expand e) throws AWTException {
print_shop_cart(e, f);
if(s.isEmpty()) {
System.out.println("购物车为空");
System.out.println();
f.enter_1();
return;
}
for(Object o : s) {
System.out.print("\033[33m" + o +"."+ e.fru[(int)o].name + " " + "\033[0m");
}
System.out.println();
System.out.println("\033[36m" + "请输出您想修改的水果其所对应的编号:"+ "\033[0m");
cart_change_fruit(input(f, e), f, e);
}
public static void cart_change_fruit(int n, Function_admin f, Expand e) throws AWTException {
Robot r = new Robot();
double sum1 = e.fru[n].fruitamount;
double sum2 = number[n];
System.out.println("\033[36m" + "请修改:" + "\033[0m");
System.out.println("\033[33m" + "请输入新的订单量:" + "\033[0m");
double new_number = 0;
while(true) {
new_number = input_new_number(f, e);
if(new_number < 0) {
System.out.println("\033[31m" + "请输入一个非负数!" + "请重新输入:" + "\033[0m");
continue;
}
if(sum1 + sum2 >= new_number) break;
System.out.println("\033[31m" + "抱歉库存不足,目前库存只有" + sum1 + "请重新输入:" + "\033[0m");
}
System.out.println("\033[31m" + "修改成功!" + "\033[0m");
if(new_number <= sum2) {
e.fru[n].fruitamount = sum2 - new_number + sum1;
}
else {
e.fru[n].fruitamount = sum1 - (new_number - sum2);
}
number[n] = new_number;
double bf_price = price[n];
price[n] = e.fru[n].leaveprice * number[n];
customer_sumcost = customer_sumcost - bf_price + price[n];
if(customer_sumcost < 0) customer_sumcost = 0;
if(new_number == 0) s.remove(n);
if(enter_6(f)) {
f.clear();
cart_change(f, e);
}
System.out.print("修改完成正在跳转......");
r.delay(300);
f.clear();
}
public static double input_new_number(Function_admin f, Expand e) throws AWTException {
Scanner sc = new Scanner(System.in);
while(true) {
String s = sc.nextLine();
if(!e.isDb_Number(s) && !(Double.parseDouble(s) >= 0)) {
System.out.println("\033[31m" + "请输入一个非负数" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s1 = sc.nextLine();
}
else {
return Double.parseDouble(s);
}
}
}
public static int input(Function_admin f, Expand e) throws AWTException {
Scanner sc = new Scanner(System.in);
while(true) {
String s = sc.nextLine();
if(!check(s, e)) {
System.out.println("\033[31m" + "No such order" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s1 = sc.nextLine();
}
else {
return Integer.parseInt(s);
}
}
}
public static boolean check(String number, Expand e) {//判断购物车是否右这个产品
if(!e.isIt_Number(number)) return false;
int int_number = 0;
for(Object o : s) {
int_number = (int) o;
if(int_number == number.charAt(0) - '0') return true;
}
return false;
}
public static void print_shop_cart(Expand e, Function_admin f) throws AWTException {
System.out.println("\033[36m" + " 购物车" + "\033[0m");
System.out.printf("%-20s%-20s%-20s\n","水果名称", "数量(kg)","花费(yuan)");
if(!s.isEmpty())
for(Object o : s) {
if(number[(int)o] != 0) {
System.out.print("\033[36m");
System.out.printf("%-20s%-20s%-20s\n",e.fru[(int) o].name, (float)number[(int)o], (float)price[(int)o]);
System.out.print("\033[0m");
}
}
System.out.println("\033[31m" + "总花费:" + (float)customer_sumcost + "\033[0m");
}
public static void memu4_print() {
System.out.println("\033[36m" + " 顾客模式" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 1.添加订单 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 2.修改订单 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 3.查看购物车 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 4.结账 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 5.清除购物车 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 6.查看总账单 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 0.退至模式选择 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("请输入您想要进行的操作选项编号(待更新):");
}
public static void shopping_cart(Expand e, Function_admin f) throws AWTException {
add_order(e, f);
}
public static void add_order(Expand e, Function_admin f) throws AWTException {
e.print_customer();
System.out.println("请选择您想要添加的水果序号:");
for(int i = 0; i < e.size; i ++) {
System.out.print("\033[33m" + i +"."+ e.fru[i].name + " " + "\033[0m");
}
System.out.println();
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
if(n.length() != 1 || !(n.charAt(0) >= '0' && n.charAt(0) - '0' < e.size)) {
System.out.println("\033[31m" + "please enter between 0 ~ " + (e.size - 1) + "\033[0m");
f.enter_1();
add_order(e, f);
}
else {
if(e.size >= fc_initail) {
update_message(2 * e.size);
}
s.add(Integer.parseInt(n));
add_remain(Integer.parseInt(n), e, f);
}
}
public static void add_remain(int n, Expand e,Function_admin f) throws AWTException {
Scanner sc = new Scanner(System.in);
String s2 = "";
out : while(true) {
System.out.println("\033[33m" + "请输入您想购买的数量(kg):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0 && Double.parseDouble(s2) <= e.fru[n].fruitamount) {
double bfnumber = number[n];
double bfprice = price[n];
double bfcost = customer_sumcost;
number[n] = number[n] + Double.parseDouble(s2);//更新
System.out.println("\033[31m" +"购买成功!" + "\033[0m");
price[n] = number[n] * e.fru[n].leaveprice;//直接更新
e.fru[n].fruitamount = e.fru[n].fruitamount + bfnumber - number[n];
customer_sumcost = customer_sumcost + price[n] - bfprice;
break;
}
else {
if(Double.parseDouble(s2) > e.fru[n].fruitamount) {
System.out.println("\033[31m" +"抱歉!" + "库存不足" + "\033[0m");
if(enter_5(f)) continue out;
else {
f.clear();
return;
}
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}
//f.clear();
if(enter_5(f)) {
shopping_cart(e, f);
}
System.out.println("购物成功正在退出....");
Robot r = new Robot();
r.delay(300);
f.clear();
}
public static boolean enter_5(Function_admin f) throws AWTException {
while(true) {
System.out.print("是否继续购买:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
public static boolean enter_6(Function_admin f) throws AWTException {
while(true) {
System.out.print("是否继续修改:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
public static boolean enter_7(Function_admin f) throws AWTException {
while(true) {
System.out.println("温馨提示:");
System.out.println("\033[33m" + "清除购物车后数据将无法恢复" + "\033[0m");
System.out.print("是否继续清除:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
public static boolean enter_8(Function_admin f) throws AWTException {
while(true) {
System.out.print("是否结账:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
}
主要存储顾客模式的一系列功能
五,Main类
package shop;
import java.awt.AWTException;
import java.awt.Robot;
import java.util.*;
public class Main {
public static void main(String[] args) throws AWTException {
Function_admin f = new Function_admin();
Function_customer fc = new Function_customer();
Expand e = new Expand();
f.memu1_print();
f.enter_1();
out: while(true) {
f.memu2_print();//模式选择界面
int N = f.enter_2();//输入后自动清屏
if(N == 0) {//离开
f.leave_print();
return;
}
else if(N == 2) {//选择管理员
if(!e.member) {
f.remind_print();
char choose = f.enter_3();
if(choose == 'n') continue out;
else {
f.check(e);//把member修改对才能出来
}
}//成功后自动清屏
if(e.member) {//进入管理员
in_1 : while(true) {
f.memu3_print();
switch(f.enter_4()) {
case 0: continue out;//退出至管理员界面
case 1: f.cipher_change(e); continue out;//退出已经自动清屏
case 2: f.add_fruit(e); continue in_1;
case 3: f.delete_fruit(e); continue in_1;
case 4: e.print(); f.enter_1(); continue in_1;//已自动清屏
case 5: f.revise_fruit_1(e); continue in_1;
case 6: f.view_profit(fc); f.enter_1(); continue in_1;
}
continue out;
}
}
}
else {//进入顾客模式
in_2: while(true) {
fc.memu4_print();
switch(f.enter_4()) {
case 0: continue out;//退出至管理员界面,已清屏
case 1: fc.shopping_cart(e, f); continue in_2;
case 3: fc.print_shop_cart(e, f); f.enter_1(); continue in_2;
case 2: fc.cart_change(f, e); continue in_2;
case 5: fc.cart_clear(f, e); continue in_2;
case 4: fc.checkout(f, e); continue in_2;
case 6: fc.view_bill(); f.enter_1(); continue in_2;
}
}
}
}
}
}
主函数
(3).直接在一个Main类运行(方便):
如果嫌上述运行方式麻烦的话可以将所有类装入一个Main类即可,运行很方便就是条理不清晰
源代码如下:
package Packge;
import java.awt.AWTException;
import java.awt.Robot;
import java.util.*;
import java.util.HashSet;
import java.util.Set;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args) throws AWTException {
Function_admin f = new Function_admin();
Function_customer fc = new Function_customer();
Expand e = new Expand();
f.memu1_print();
f.enter_1();
out: while(true) {
f.memu2_print();//模式选择界面
int N = f.enter_2();//输入后自动清屏
if(N == 0) {//离开
f.leave_print();
return;
}
else if(N == 2) {//选择管理员
if(!e.member) {
f.remind_print();
char choose = f.enter_3();
if(choose == 'n') continue out;
else {
f.check(e);//把member修改对才能出来
}
}//成功后自动清屏
if(e.member) {//进入管理员
in_1 : while(true) {
f.memu3_print();
switch(f.enter_4()) {
case 0: continue out;//退出至管理员界面
case 1: f.cipher_change(e); continue out;//退出已经自动清屏
case 2: f.add_fruit(e); continue in_1;
case 3: f.delete_fruit(e); continue in_1;
case 4: e.print(); f.enter_1(); continue in_1;//已自动清屏
case 5: f.revise_fruit_1(e); continue in_1;
case 6: f.view_profit(fc); f.enter_1(); continue in_1;
}
continue out;
}
}
}
else {//进入顾客模式
in_2: while(true) {
fc.memu4_print();
switch(f.enter_4()) {
case 0: continue out;//退出至管理员界面,已清屏
case 1: fc.shopping_cart(e, f); continue in_2;
case 3: fc.print_shop_cart(e, f); f.enter_1(); continue in_2;
case 2: fc.cart_change(f, e); continue in_2;
case 5: fc.cart_clear(f, e); continue in_2;
case 4: fc.checkout(f, e); continue in_2;
case 6: fc.view_bill(); f.enter_1(); continue in_2;
}
}
}
}
}
}
class Function_customer{
public static Set s = new HashSet<Integer>();
public static double price[];
public static double number[];
public static int fc_initail = 7;
public static int fc_size = 6;
public static double customer_sumcost = 0;
public static double total_cost = 0;
public static double foundation_cost = 0;
Function_customer(){
price = new double[fc_initail];
number = new double[fc_initail];
}
public static void update_message(int newfc_data) {
double new_price[] = new double[newfc_data];
double new_number[] = new double[newfc_data];
for(int i = 0; i < fc_size; i ++) {//挪过来
new_price[i] = price[i];
new_number[i] = number[i];
}
price = new_price;
number = new_number;
fc_initail = newfc_data;
}
public static void view_bill(){
System.out.println("\033[31m" + "总消费金额(yuan)" + "\033[0m");
System.out.println(total_cost);
}
public static void checkout(Function_admin f, Expand e) throws AWTException {
print_shop_cart(e, f);
Robot r = new Robot();
if(s.isEmpty()) {
System.out.println("购物车为空!");
f.enter_1();
return;
}
if(enter_8(f)) {
if(customer_sumcost < 0) customer_sumcost = 0;
total_cost = total_cost + customer_sumcost;
customer_sumcost = 0;
for(Object o : s) {
int n = (int)o;
foundation_cost = foundation_cost + e.fru[n].enterprice * number[n];
price[n] = 0;
number[n] = 0;
}
System.out.println("购买成功!感谢惠顾");
r.delay(300);
s.clear();
f.clear();
}
else
{
System.out.println("正在退出结账系统......");
r.delay(300);
f.clear();
}
}
public static void cart_clear(Function_admin f, Expand e) throws AWTException {
Robot r = new Robot();
print_shop_cart(e, f);
if(s.isEmpty()) {
System.out.println("购物车为空!");
f.enter_1();
return;
}
if(enter_7(f)) {
System.out.println("清除成功正在退出.....");
r.delay(200);
f.clear();
int n = 0;
for(Object o : s) {
n = (int)o;
e.fru[n].fruitamount = number[n] + e.fru[n].fruitamount;
number[n] = 0;
price[n] = 0;
}
}
else {
System.out.println("正在退出......");
r.delay(200);
f.clear();
}
}
public static void cart_change(Function_admin f, Expand e) throws AWTException {
print_shop_cart(e, f);
if(s.isEmpty()) {
System.out.println("购物车为空");
System.out.println();
f.enter_1();
return;
}
for(Object o : s) {
System.out.print("\033[33m" + o +"."+ e.fru[(int)o].name + " " + "\033[0m");
}
System.out.println();
System.out.println("\033[36m" + "请输出您想修改的水果其所对应的编号:"+ "\033[0m");
cart_change_fruit(input(f, e), f, e);
}
public static void cart_change_fruit(int n, Function_admin f, Expand e) throws AWTException {
Robot r = new Robot();
double sum1 = e.fru[n].fruitamount;
double sum2 = number[n];
System.out.println("\033[36m" + "请修改:" + "\033[0m");
System.out.println("\033[33m" + "请输入新的订单量:" + "\033[0m");
double new_number = 0;
while(true) {
new_number = input_new_number(f, e);
if(new_number < 0) {
System.out.println("\033[31m" + "请输入一个非负数!" + "请重新输入:" + "\033[0m");
continue;
}
if(sum1 + sum2 >= new_number) break;
System.out.println("\033[31m" + "抱歉库存不足,目前库存只有" + sum1 + "请重新输入:" + "\033[0m");
}
System.out.println("\033[31m" + "修改成功!" + "\033[0m");
if(new_number <= sum2) {
e.fru[n].fruitamount = sum2 - new_number + sum1;
}
else {
e.fru[n].fruitamount = sum1 - (new_number - sum2);
}
number[n] = new_number;
double bf_price = price[n];
price[n] = e.fru[n].leaveprice * number[n];
customer_sumcost = customer_sumcost - bf_price + price[n];
if(customer_sumcost < 0) customer_sumcost = 0;
if(new_number == 0) s.remove(n);
if(enter_6(f)) {
f.clear();
cart_change(f, e);
}
System.out.print("修改完成正在跳转......");
r.delay(300);
f.clear();
}
public static double input_new_number(Function_admin f, Expand e) throws AWTException {
Scanner sc = new Scanner(System.in);
while(true) {
String s = sc.nextLine();
if(!e.isDb_Number(s) && !(Double.parseDouble(s) >= 0)) {
System.out.println("\033[31m" + "请输入一个非负数" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s1 = sc.nextLine();
}
else {
return Double.parseDouble(s);
}
}
}
public static int input(Function_admin f, Expand e) throws AWTException {
Scanner sc = new Scanner(System.in);
while(true) {
String s = sc.nextLine();
if(!check(s, e)) {
System.out.println("\033[31m" + "No such order" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s1 = sc.nextLine();
}
else {
return Integer.parseInt(s);
}
}
}
public static boolean check(String number, Expand e) {//判断购物车是否右这个产品
if(!e.isIt_Number(number)) return false;
int int_number = 0;
for(Object o : s) {
int_number = (int) o;
if(int_number == number.charAt(0) - '0') return true;
}
return false;
}
public static void print_shop_cart(Expand e, Function_admin f) throws AWTException {
System.out.println("\033[36m" + " 购物车" + "\033[0m");
System.out.printf("%-20s%-20s%-20s\n","水果名称", "数量(kg)","花费(yuan)");
if(!s.isEmpty())
for(Object o : s) {
if(number[(int)o] != 0) {
System.out.print("\033[36m");
System.out.printf("%-20s%-20s%-20s\n",e.fru[(int) o].name, (float)number[(int)o], (float)price[(int)o]);
System.out.print("\033[0m");
}
}
System.out.println("\033[31m" + "总花费:" + (float)customer_sumcost + "\033[0m");
}
public static void memu4_print() {
System.out.println("\033[36m" + " 顾客模式" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 1.添加订单 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 2.修改订单 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 3.查看购物车 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 4.结账 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 5.清除购物车 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 6.查看总账单 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 0.退至模式选择 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("请输入您想要进行的操作选项编号(待更新):");
}
public static void shopping_cart(Expand e, Function_admin f) throws AWTException {
add_order(e, f);
}
public static void add_order(Expand e, Function_admin f) throws AWTException {
e.print_customer();
System.out.println("请选择您想要添加的水果序号:");
for(int i = 0; i < e.size; i ++) {
System.out.print("\033[33m" + i +"."+ e.fru[i].name + " " + "\033[0m");
}
System.out.println();
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
if(n.length() != 1 || !(n.charAt(0) >= '0' && n.charAt(0) - '0' < e.size)) {
System.out.println("\033[31m" + "please enter between 0 ~ " + (e.size - 1) + "\033[0m");
f.enter_1();
add_order(e, f);
}
else {
if(e.size >= fc_initail) {
update_message(2 * e.size);
}
s.add(Integer.parseInt(n));
add_remain(Integer.parseInt(n), e, f);
}
}
public static void add_remain(int n, Expand e,Function_admin f) throws AWTException {
Scanner sc = new Scanner(System.in);
String s2 = "";
out : while(true) {
System.out.println("\033[33m" + "请输入您想购买的数量(kg):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0 && Double.parseDouble(s2) <= e.fru[n].fruitamount) {
double bfnumber = number[n];
double bfprice = price[n];
double bfcost = customer_sumcost;
number[n] = number[n] + Double.parseDouble(s2);//更新
System.out.println("\033[31m" +"购买成功!" + "\033[0m");
price[n] = number[n] * e.fru[n].leaveprice;//直接更新
e.fru[n].fruitamount = e.fru[n].fruitamount + bfnumber - number[n];
customer_sumcost = customer_sumcost + price[n] - bfprice;
break;
}
else {
if(Double.parseDouble(s2) > e.fru[n].fruitamount) {
System.out.println("\033[31m" +"抱歉!" + "库存不足" + "\033[0m");
if(enter_5(f)) continue out;
else {
f.clear();
return;
}
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}
//f.clear();
if(enter_5(f)) {
shopping_cart(e, f);
}
System.out.println("购物成功正在退出....");
Robot r = new Robot();
r.delay(300);
f.clear();
}
public static boolean enter_5(Function_admin f) throws AWTException {
while(true) {
System.out.print("是否继续购买:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
public static boolean enter_6(Function_admin f) throws AWTException {
while(true) {
System.out.print("是否继续修改:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
public static boolean enter_7(Function_admin f) throws AWTException {
while(true) {
System.out.println("温馨提示:");
System.out.println("\033[33m" + "清除购物车后数据将无法恢复" + "\033[0m");
System.out.print("是否继续清除:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
public static boolean enter_8(Function_admin f) throws AWTException {
while(true) {
System.out.print("是否结账:(y/n):");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
f.clear();
}
else {
f.clear();
if(s.charAt(0) == 'y') return true;
return false;
}
}
}
}
class Function_admin{//存储菜单,实用功能
public static void clear() throws AWTException{//清除函数
Robot ro = new Robot();
ro.delay(100);//延时
ro.mousePress(InputEvent.BUTTON3_MASK);//单机鼠标右键
ro.mouseRelease(InputEvent.BUTTON3_MASK);//松开右键
ro.keyPress(KeyEvent.VK_CONTROL);//按ctrl键
ro.keyPress(KeyEvent.VK_R);//按R
ro.keyRelease(KeyEvent.VK_R);//松开R
ro.keyRelease(KeyEvent.VK_CONTROL);//松开ctrl
ro.delay(250);
}
public static void memu1_print() {
System.out.println("\033[36m" + "☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃");
System.out.println("\033[33m" + " ❀❀❀欢迎来到Narnat的水果店(库)❀❀❀");
System.out.println("\033[36m" + "☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃");
System.out.println();
System.out.println("\033[0m" + "这里的水果价格几乎都是批发价哦,绝对实惠!");
}
public static void memu2_print() {
System.out.println("\033[36m" + " narnat的水果店 " + "\033[0m");
System.out.println("\033[30m" + " 模式选择 " + "\033[0m");
System.out.println("*************" + "\033[35m"+" 1.顾客模式 "+"\033[0m"+"*************");
System.out.println("*************" + "\033[36m"+" 2.管理员模式 "+"\033[0m"+"*************");
System.out.println("*************" + "\033[37m"+" 0.退出系统 "+"\033[0m"+"*************");
System.out.println("请选择您想要的服务模式 (0 ~ 2): ");
}
public static void enter_1() throws AWTException {
System.out.println("\033[0m" + "请在控制台内单击 enter 以继续:");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
clear();
}
public static int enter_2() throws AWTException {
while(true) {
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
if(n.length() != 1 || !(n.charAt(0) >= '0' && n.charAt(0) <= '2')) {
System.out.println("\033[31m" + "please enter between 0 ~ 2" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s = sc.nextLine();
clear();
}
else {
clear();
return Integer.parseInt(n);
}
}
}
public static char enter_3() throws AWTException {
while(true) {
System.out.print("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) == 'y' || s.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
clear();
}
else {
clear();
return s.charAt(0);
}
}
}
public static int enter_4() throws AWTException {
Robot r = new Robot();
Scanner sc = new Scanner(System.in);
while(true) {
String s = sc.nextLine();
if(s.length() != 1 || !(s.charAt(0) >= '0' && s.charAt(0) <= '6')) {
System.out.println("\033[31m" + "please enter between 0 ~ 6" + "\033[0m");
System.out.println("\033[0m" + "请在控制台下按enter以继续:");
String s1 = sc.nextLine();
clear();
}
else {
clear();
System.out.print("\033[0m" + "输入成功正在跳转......");
r.delay(400);
clear();
return Integer.parseInt(s);
}
}
}
public static void view_profit(Function_customer fc){
System.out.println("\033[31m" + "净利润(yuan):" + "\033[0m");
System.out.println(fc.total_cost - fc.foundation_cost);
}
public static void leave_print() throws AWTException {
clear();
System.out.println("\033[34m" + "❀❀❀❀感谢您的光临,期待您的再次光临❀❀❀❀" + "\033[0m");
}
public static void check(Expand e) throws AWTException {
Robot r = new Robot();
Scanner sc = new Scanner(System.in);
int i = 3;
while(true) {
if(i <= 0) {
System.out.println("很抱歉验证次数已经用完");
System.out.println("您需要等待一段时间(15s)后才能再次输入");
System.out.println("若您忘记密码,您可以重启程序以恢复默认密码");
r.delay(10000); //10s
clear();
i = 2;
continue;
}
System.out.println("请输入" + e.cipher.length() + "位密码以进入管理员模式");
System.out.println("\033[32m" + "请在下方控制台内顶格输入:" + "\033[0m");
String s1 = "";
s1 = sc.nextLine();
if(s1.equals(e.cipher))
break;
else {
System.out.println("\033[31m" + "password error" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s2 = sc.nextLine();
clear();
}
i --;
}
e.member = true;
System.out.println("\033[31m" + "密码正确即将进入管理员模式!" + "\033[0m");
r.delay(1000);// 1s;
clear();
}
public static void memu3_print() {
System.out.println("\033[36m" + " 管理员模式" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 0.退至模式选择 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 1.密码修改 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 2.增添水果种类 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 3.删除水果种类 " + "\033[32m" + " ☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 4.打印库存 " + "\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 5.增添/删除水果信息 " + "\033[32m" + "☆☆☆☆☆☆☆☆ " + "\033[0m");
System.out.println("\033[32m" + "☆☆☆☆☆☆☆☆☆☆☆☆" + "\033[0m" + " 6.查看利润 " + "\033[32m" + "☆☆☆☆☆☆☆☆ " + "\033[0m");
System.out.println("请输入您想要进行的操作选项编号(待更新):");
}
public static void remind_print() {
System.out.println("温馨提示:");
System.out.println("1.管理员模式,只有超市管理员才能进入,进入需要输入密码。");
System.out.println("2.若之前编辑过新密码,且忘记密码,可以重启程序,恢复默认密码。");
System.out.println("3.若选择 'n' 可以直接退回到服务模式选择界面。");
System.out.println("\033[36m" + "请确认您是否要打开管理员模式(y/n):" + "\033[0m");
}
public static void cipher_change(Expand e) throws AWTException {
System.out.println(" 密码修改");
Robot r = new Robot();
Scanner sc = new Scanner(System.in);
int i = 3;
while(i > 0) {
System.out.println("请再次输入" + e.cipher.length() + "位管理员密码");
System.out.println("\033[32m" + "请在下方控制台内顶格输入:" + "\033[0m");
String s1 = "";
s1 = sc.nextLine();
if(s1.equals(e.cipher))
break;
else {
System.out.println("\033[31m" + "password error" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s2 = sc.nextLine();
clear();
}
i --;
if(i <= 0) {
System.out.println("很抱歉验证次数已经用完");
System.out.println("您需要等待一段时间(15s)后才能再次输入");
System.out.println("若您忘记密码,您可以重启程序以恢复默认密码");
r.delay(10000); //10s
clear();
i = 2;
}
}
clear();
String s2 = "";
String s3 = "";
while(true) {
System.out.println("请输入新密码:");
s2 = sc.nextLine();
if(s2.equals(e.cipher)) {
System.out.println("\033[31m" + "输入的密码与原密码相同!" + "\033[0m");
enter_1();
continue;
}
clear();
for(int j = 0; j < s2.length(); j ++) System.out.print("*");
System.out.println();
System.out.println("请再次输入密码:");
s3 = sc.nextLine();
if(s2.equals(s3)) {
e.cipher = s3;
e.member = false;
System.out.println("更改成功!即将退至模式选择界面");
r.delay(300);
clear();
return;
}
else {
System.out.println("\033[31m" + "error! two password not equals" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s4 = sc.nextLine();
clear();
}
}
}
public static void add_fruit(Expand e) throws AWTException {//添加
clear();
System.out.println(" 水果种类添加");
Scanner sc = new Scanner(System.in);
int i = 0;
String s = "";
String s2 = "";
String s3 = "";
if(e.size == e.icapacity) e.updatecapacity(2*e.size);//原容量翻倍
while(true) {
System.out.print("\033[33m" + "请输入新水果名称:" + "\033[0m");
s = sc.nextLine();
for(i = 0; i < e.size; i ++) {
if(s.equals(e.fru[i].name)) {
System.out.println("\033[31m" +"error!"+ s + "aready have! " + "\033[0m");
break;
}
}
if(i!= e.size) {
System.out.println("请在控制台下输入enter以继续");
String s1 = sc.nextLine();
}
else break;
}
e.fru[e.size].name = s;
System.out.println("\033[31m"+"水果名称添加成功!"+"\033[0m");
System.out.println("请将以下信息填写完整:");
System.out.println("\033[33m" + "请输入进货日期:" + "\033[0m");
e.fru[e.size].data = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
while(true) {
System.out.println("\033[33m" + "请输入保质期(天):" + "\033[0m");
s2 = sc.nextLine();
if(e.isIt_Number(s2) && Integer.parseInt(s2) > 0) {
e.fru[e.size].time = Integer.parseInt(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数: " + "\033[0m");
}
System.out.println("\033[33m" + "请输入产地:" + "\033[0m");
e.fru[e.size].place = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
while(true) {
System.out.println("\033[33m" + "请输入进价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Integer.parseInt(s2) > 0) {
e.fru[e.size].enterprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
while(true) {
System.out.println("\033[33m" + "请输入出口价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2)&& Double.parseDouble(s2) > 0) {
e.fru[e.size].leaveprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
while(true) {
System.out.println("\033[33m" + "请输入库存 (kg):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[e.size].fruitamount = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
e.size ++;
System.out.println("新的水果基本信息已经储存完毕,新的水果库存如下:");
Robot r = new Robot();
r.delay(250);
clear();
e.print();
while(true) {
System.out.println("是否继续添加? y/n");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
clear();
continue;
}
else
break;
}
r.delay(250);
System.out.println("正在跳转...");
clear();
if(s3.charAt(0) == 'y') {
System.out.println("已有水果种类及信息如下:");
e.print();
add_fruit(e);
}
else
return;
}
public static void delete_fruit(Expand e) throws AWTException {
Robot r = new Robot();
String s3 = "";
System.out.println("已有水果总类及信息如下:") ;
e.print();
Scanner sc = new Scanner(System.in);
int index = 0;
while(true) {
System.out.println("请选择您要删除的水果名称");
String s = sc.nextLine();
int i = 0;
for(i = 0; i < e.size; i ++)
if(e.fru[i].name.equals(s)) {
index = i;
break;
}
if(i == e.size) {
System.out.println("\033[31m" + "error!" + s + "这种水果,并未找到!" + "\033[0m");
while(true) {
System.out.println("是否继续进行删除操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s1 = sc.nextLine();
}
else
break;
}
if(s3.charAt(0) == 'y') continue;
else {
clear();
return;
}
}
else {
while(true) {
System.out.println("查找成功!" + "删除后数据将无法挽回,是否继续进行删除操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下输入enter以继续:");
String s1 = sc.nextLine();
}
else
break;
}
if(s3.charAt(0) == 'y') {
clear();
System.out.println("删除成功!更新后的状态如下:");
e.delete(i);
System.out.println("请按enter键以继续:");
String s4 = sc.nextLine();
clear();
return;
}
else {
System.out.println("正在退出删除模式...");
r.delay(250);
clear();
return;
}
}
}
}
public static void revise_fruit_1(Expand e) throws AWTException {
Robot r = new Robot();
String s3 = "";
System.out.println("已有水果总类及信息如下:") ;
e.print();
Scanner sc = new Scanner(System.in);
int index= 0;
while(true) {
System.out.println("请选择您要修改的水果名称");
String s = sc.nextLine();
int i = 0;
for(i = 0; i < e.size; i ++)
if(e.fru[i].name.equals(s)) {
index = i;
break;
}
if(i == e.size) {
System.out.println("\033[31m" + "error!" + s + "这种水果,并未找到!" + "\033[0m");
while(true) {
System.out.println("是否继续进行修改操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s1 = sc.nextLine();
}
else
break;
}
if(s3.charAt(0) == 'y') continue;
else {
clear();
return;
}
}
else {
out :while(true) {
System.out.println("查找成功!");
r.delay(300);
clear();
e.print_someone(index);
revise_fruit_2(e, index);
while(true) {
System.out.println("是否继续进行修改操作?y/n :");
s3 = sc.nextLine();
if(s3.length() != 1 || !(s3.charAt(0) == 'y' || s3.charAt(0) == 'n')) {
System.out.println("\033[31m" + "please enter 'y' or 'n'" + "\033[0m");
System.out.println("请在控制台下点击enter以继续:");
String s1 = sc.nextLine();
}
else {
clear();
break;
}
}
if(s3.charAt(0) == 'y') continue out ;
else {
clear();
return;
}
}
}
}
}
public static void revise_fruit_2(Expand e, int index) throws AWTException {
String revise[] = {"水果名称", "进货日期", "水果保质期", "产地", "进价", "出口价", "库存"};
Scanner sc = new Scanner(System.in);
System.out.println("请输出您想修改的信息所对应的编号:");
for(int i = 0; i < 7; i ++) {
System.out.print("\033[33m" + i +"."+ revise[i] + " " + "\033[0m");
}
System.out.println();
System.out.println("请输入您的选择:");
String n = sc.nextLine();
if(n.length() != 1 || !(n.charAt(0) >= '0' && n.charAt(0) <= '6')) {
System.out.println("\033[31m" + "please enter between 0 ~ 6" + "\033[0m");
enter_1();
revise_fruit_2(e, index);
}
else {
revise_fruit_3(e, Integer.parseInt(n), index);
}
}
public static void revise_fruit_3(Expand e, int n, int index) {
Scanner sc = new Scanner(System.in);
String s2 = "";
if(n == 0) {
System.out.println("\033[33m" + "请输入新水果名称:" + "\033[0m");
e.fru[index].name = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
}
else if(n == 1) {
System.out.println("\033[33m" + "请输入新进货日期:" + "\033[0m");
e.fru[index].data = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
}
else if(n == 2) {
while(true) {
System.out.println("\033[33m" + "请输入新保质期(天):" + "\033[0m");
s2 = sc.nextLine();
if(e.isIt_Number(s2) && Integer.parseInt(s2) > 0) {
e.fru[index].time = Integer.parseInt(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数: " + "\033[0m");
}
}
else if(n == 3) {
System.out.println("\033[33m" + "请输入新产地:" + "\033[0m");
e.fru[index].place = sc.nextLine();
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
}
else if(n == 4) {
while(true) {
System.out.println("\033[33m" + "请输入进价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[index].enterprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}else if(n == 5) {
while(true) {
System.out.println("\033[33m" + "请输入出口价(kg/元):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[index].leaveprice = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}
else if(n == 6) {
while(true) {
System.out.println("\033[33m" + "请输入库存 (kg):" + "\033[0m");
s2 = sc.nextLine();
if(e.isDb_Number(s2) && Double.parseDouble(s2) > 0) {
e.fru[index].fruitamount = Double.parseDouble(s2);
System.out.println("\033[31m" +"输入成功!" + "\033[0m");
break;
}
else
System.out.println("\033[31m" +"error!" + "请输入一个正整数或正浮点数: " + "\033[0m");
}
}
}
}
class Expand{
public String cipher = "123";
public boolean member = false;
public int icapacity = 7;//初始容量(当前容量)
public static int size = 6;//水果总类
public fruit fru[] = new fruit[icapacity];//初始容量
Expand(){//构造函数
initial(this.fru, icapacity);
//默认水果
fru[0].name = "富士苹果";
fru[0].data = "2023/3/4";
fru[0].enterprice = 23.45;
fru[0].leaveprice = 24.99;
fru[0].place = "许昌静9/215";
fru[0].time = 11;
fru[0].fruitamount = 100.45;
fru[1].name = "朴素香蕉";
fru[1].data = "2023/3/4";
fru[1].enterprice = 1.55;
fru[1].leaveprice = 1.99;
fru[1].place = "许昌静9/215";
fru[1].time = 11;
fru[1].fruitamount = 110.45;
fru[2].name = "沙糖桔";
fru[2].data = "2023/3/4";
fru[2].enterprice = 1.45;
fru[2].leaveprice = 1.99;
fru[2].place = "许昌静9/215";
fru[2].time = 11;
fru[2].fruitamount = 200.45;
fru[3].name = "朴素菠萝";
fru[3].data = "2023/4/26";
fru[3].enterprice = 16;
fru[3].leaveprice = 16.5;
fru[3].place = "许昌静9/215";
fru[3].time = 11;
fru[3].fruitamount = 200.45;
fru[4].name = "朴素西瓜";
fru[4].data = "2023/4/26";
fru[4].enterprice = 10;
fru[4].leaveprice = 11;
fru[4].place = "许昌静9/215";
fru[4].time = 11;
fru[4].fruitamount = 225.45;
fru[5].name = "芒果";
fru[5].data = "2023/4/26";
fru[5].enterprice = 13;
fru[5].leaveprice = 15;
fru[5].place = "许昌静9/215";
fru[5].time = 11;
fru[5].fruitamount = 110.45;
}
public void Exchage(fruit a, fruit b) {
a.name = b.name;
a.data = b.data;
a.enterprice = b.enterprice;
a.fruitamount = b.fruitamount;
a.leaveprice = b.leaveprice;
a.time = b.time;
a.place = b.place;
}
public void delete(int i) {
for(int j = i; j < size - 1; j ++)
Exchage(fru[j],fru[j + 1]);
size --;
}
public void initial(fruit fru[], int number) {//初始化
for(int i = 0; i < number; i ++) fru[i] = new fruit();
}
public void updatecapacity(int newcapacity) {//更新容量
fruit newdata[] = new fruit[newcapacity];
initial(newdata, newcapacity);
for(int i = 0; i < size; i ++) newdata[i] = fru[i];//挪过来
fru = newdata;//指向
this.icapacity = newcapacity;//最大容量更新
}
public void print() {
for(int i = 0; i < this.size; i ++) {
System.out.println("\033[35m" + "꧁༺๑๑༻꧂" + "\033[0m");
System.out.print("水果名称: " + "\033[36m" + fru[i].name + " ");
System.out.print("\033[0m" + "进货日期: " + "\033[36m" + fru[i].data + " ");
System.out.print("\033[0m" + "水果保质期(天): " + "\033[36m" + fru[i].time + " ");
System.out.println("\033[0m" + "产地:" + "\033[36m" + fru[i].place + " ");
System.out.print("\033[0m" + "\033[0m" + "进价(元/kg): " + "\033[36m" + fru[i].enterprice + " ");
System.out.print("\033[0m" + "出口价(元/kg): " + "\033[36m" + fru[i].leaveprice + " ");
System.out.print("\033[0m" + "库存 (kg): " + "\033[36m");
System.out.printf("%.2f\n", fru[i].fruitamount);
System.out.print("\033[0m");
}
System.out.println();
}
public boolean isDb_Number(String s) {
try {
Double.parseDouble(s);
return true;
}catch(Exception e) {
return false;
}
}
public boolean isIt_Number(String s) {
try {
Integer.parseInt(s);
return true;
}catch(Exception e) {
return false;
}
}
public void print_customer() {
System.out.printf("%-18s%-20s%-20s%-20s%-20s%-20s\n","水果名称 " , "进货日期 " , "水果保质期(天) " , "产地 " , "价格(元/kg) " , "库存 (kg) ");
for(int i = 0; i < this.size; i ++) {
System.out.println("\033[35m" + "꧁༺๑๑༻꧂" + "\033[0m");
System.out.print("\033[36m");
System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s\n", fru[i].name, fru[i].data , fru[i].time , fru[i].place , fru[i].leaveprice ,(float)fru[i].fruitamount);
System.out.print("\033[0m");
}
System.out.println();
}
public void print_someone(int i){
System.out.println("所选水果信息如下:");
System.out.println("\033[35m" + "꧁༺๑๑༻꧂" + "\033[0m");
System.out.println("水果名称: " + "\033[36m" + fru[i].name);
System.out.println("\033[0m" + "进货日期: " + "\033[36m" + fru[i].data);
System.out.println("\033[0m" + "水果保质期(天): " + "\033[36m" + fru[i].time);
System.out.println("\033[0m" + "产地:" + "\033[36m" + fru[i].place);
System.out.println("\033[0m" + "\033[0m" + "进价(元/kg): " + "\033[36m" + fru[i].enterprice);
System.out.println("\033[0m" + "出口价(元/kg): " + "\033[36m" + fru[i].leaveprice);
System.out.println("\033[0m" + "库存 (kg): " + "\033[36m" + (float)fru[i].fruitamount);
System.out.print("\033[0m");
}
}
class fruit {//每种水果的信息
String name;//水果名字
String data;//进货日期
int time;//保质期(天)
String place;//产地
double enterprice;//进价
double leaveprice;//出口价
double fruitamount;//库存 (kg)
}
5.综上:
由于第一次写这个代码,多多少少会有考虑不到的地方,如果不是涉及到要修改底层逻辑的bug,能修改我会尽量去修改,欢迎大家指出代码问题。
下一次的方向就是学习数据库,和一些网页模板将水果店做成网页的形式。