Kbaor_2023_9_28_Java第一次实战项目_ELM_V1_商户的实体类与实现类
商户实体类代码展示
package elm_V1;
/**
* [商户实体类]
*
* @author 秦帅
* @date 2023-9-25
*/
public class Business {
private int businessId;
private String password;
private String businessName;
private String businessAddress;
private String businessExplain;
private double starPrice;
private double deliveryPrice;
Business() {
}
public int getBusinessId() {
return businessId;
}
public void setBusinessId(int businessId) {
this.businessId = businessId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getBusinessAddress() {
return businessAddress;
}
public void setBusinessAddress(String businessAddress) {
this.businessAddress = businessAddress;
}
public String getBusinessExplain() {
return businessExplain;
}
public void setBusinessExplain(String businessExplain) {
this.businessExplain = businessExplain;
}
public double getStarPrice() {
return starPrice;
}
public void setStarPrice(double starPrice) {
this.starPrice = starPrice;
}
public double getDeliveryPrice() {
return deliveryPrice;
}
public void setDeliveryPrice(double deliveryPrice) {
this.deliveryPrice = deliveryPrice;
}
@Override
public String toString() {
return "Business{" +
"businessId=" + businessId +
", password='" + password + '\'' +
", businessName='" + businessName + '\'' +
", businessAddress='" + businessAddress + '\'' +
", businessExplain='" + businessExplain + '\'' +
", starPrice=" + starPrice +
", deliveryPrice=" + deliveryPrice +
'}';
}
}
商户实现类代码展示
package elm_V1;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import static elm_V1.BusinessGJ.fileToList;
import static elm_V1.BusinessGJ.listToFile;
import static elm_V1.MenuServiceImp.menu01;
import static elm_V1.MenuServiceImp.menu02;
/**
* [商户业务类]
*
* @author 秦帅
* @date 2023-9-27
*/
public class BusinessServiceImp {
public static int value = 0; //登录之后的id
// 商户注册
public static void businessReg() throws IOException {
// System.out.println("通过逐步提示 让商户利用键盘输入 注册信息");
// System.out.println("注册成功后回到menu01(1级菜单)");
// System.out.println("按回车键...");
// Scanner sc = new Scanner(System.in);
// // 暂停程序
// String str = sc.nextLine();
// Menu.menu01();
// Scanner sc = new Scanner(System.in);
// System.out.println("欢迎注册本系统");
// System.out.println("请输入商户的登录密码:");
// String pwd = sc.nextLine();
// System.out.println("登录密码:" + pwd);
// 获取文件中的记录数量 得到新序号
int num = BusinessGJ.getFileRow() + 1;
Scanner sc = new Scanner(System.in);
System.out.println("请按如下格式输入商家信息 空格间隔");
System.out.println("商家名称 商家地址 商家介绍 起送费 配送费");
String str = sc.nextLine();
String[] str_arr = str.split(" ");
while (BusinessGJ.isRepeat(str_arr[0])) {
System.out.println("商家名称重复 请重新输入商家名称:");
str_arr[0] = sc.next();
}
while (BusinessGJ.isOver(str_arr[0], 40)) { // 40
System.out.println("请重新输入商家名称 长度<=40:"); // 40
str_arr[0] = sc.next();
}
while (BusinessGJ.isOver(str_arr[1], 50)) { // 50
System.out.println("请重新输入商家地址 长度<=50:"); // 50
str_arr[1] = sc.next();
}
while (BusinessGJ.isOver(str_arr[2], 40)) { // 40
System.out.println("请重新输入商家介绍 长度<=40:"); // 40
str_arr[2] = sc.next();
}
while (!BusinessGJ.isDouble(str_arr[3])) {
System.out.println("请重新输入起送费:");
str_arr[3] = sc.next();
}
while (!BusinessGJ.isDouble(str_arr[4])) {
System.out.println("请重新输入配送费:");
str_arr[4] = sc.next();
}
System.out.println("请输入密码");
String pwd = sc.next();
System.out.println("请再次输入密码");
String pwd2 = sc.next();
boolean bz = BusinessGJ.isInclude(pwd, BusinessGJ.c1) && BusinessGJ.isInclude(pwd, BusinessGJ.c2)
&& BusinessGJ.isInclude(pwd, BusinessGJ.c3) && BusinessGJ.isInclude(pwd, BusinessGJ.c4) && pwd.length() >= 8;
while (!bz) {
System.out.println("密码强度不够或位数不够,请重新输入");
pwd = sc.next();
bz = BusinessGJ.isInclude(pwd, BusinessGJ.c1) && BusinessGJ.isInclude(pwd, BusinessGJ.c2)
&& BusinessGJ.isInclude(pwd, BusinessGJ.c3) && BusinessGJ.isInclude(pwd, BusinessGJ.c4)
&& pwd.length() >= 8;
}
while (!pwd.equals(pwd2)) {
System.out.println("两次输入的密码不相同 请重新输入密码:");
pwd = sc.next();
System.out.println("请再次输入密码");
pwd2 = sc.next();
}
Business bs = new Business();
bs.setBusinessName(str_arr[0]);
bs.setBusinessAddress(str_arr[1]);
bs.setBusinessExplain(str_arr[2]);
bs.setStarPrice(Double.parseDouble(str_arr[3]));
bs.setDeliveryPrice(Double.parseDouble(str_arr[4]));
bs.setPassword(pwd);
bs.setBusinessId(num);
List<Business> list = fileToList();
list.add(bs);
listToFile(list);
System.out.println("商户注册成功 商户ID为: " + num + " 密码为: " + pwd + " 可以用它登录系统");
// 暂停程序
String str2 = sc.nextLine();
menu01();
}
// 商户登录
public static void businessLogin() throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请按如下格式输入商家信息 空格间隔");
System.out.println("商家ID 商家密码");
String str = sc.nextLine();
String[] str_arr = str.split(" ");
while(str_arr.length!=2){
System.out.println("请重新输入商家信息 空格间隔");
System.out.println("商家ID 商家密码");
String str1 = sc.nextLine();
str = str1;
str_arr = str.split(" ");
}
String id = str_arr[0];
String psd = str_arr[1];
while (!(BusinessGJ.isRightpsd(id, psd))) {
System.out.println("您输入的ID或密码有误,商家ID 商家密码(空格间隔)请重新输入...");
String str1 = sc.nextLine();
String[] str_arr1 = str1.split(" ");
id = str_arr1[0];
psd = str_arr1[1];
}
//传参
value = Integer.parseInt(id);
System.out.println("登录成功后正在进入商户管理菜单...");
// System.out.println("通过逐步提示 让商户利用键盘输入 登录信息");
// System.out.println("登录成功后进入menu02(商户管理 2级菜单)");
// System.out.println("登录失败后返回menu01(1级菜单)");
System.out.println("按回车键...");
// 暂停程序
String str3 = sc.nextLine();
menu02();
}
public int chuancan() {
return value;
}
// Business(int businessId, String password) {
// this.businessId = businessId;
// this.password = password;
// }
// 查询商户信息
void queryBusiness() throws IOException {
List<Business> list = fileToList();
// System.out.println("请输入您的商户ID...");
Scanner sc = new Scanner(System.in);
// String str = sc.nextLine();
//ID转整形
// int id = Integer.parseInt(str);
Business bus = (Business) list.get(value - 1);
System.out.println("您的商户名称为:" + bus.getBusinessName());
System.out.println("您的商户ID为:" + bus.getBusinessId());
System.out.println("您的商户登录密码为:" + bus.getPassword());
System.out.println("您的商户地址为:" + bus.getBusinessAddress());
System.out.println("您的商户描述为:" + bus.getBusinessExplain());
System.out.println("您的商户起送费为:" + bus.getStarPrice());
System.out.println("您的商户配送费为:" + bus.getDeliveryPrice());
System.out.println("按回车键后回到商户管理菜单.....");
// 暂停程序
String str1 = sc.nextLine();
menu02();
}
// 修改商户信息
void modifyBusiness() throws IOException {
List<Business> list = fileToList();
// System.out.println("请输入您的商户ID...");
Scanner sc = new Scanner(System.in);
// String str = sc.nextLine();
System.out.println("请选择您要修改的商户信息...");
System.out.println("请输入相应的数字,在0-5之间选择...");
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("0.返回上一级菜单");
boolean flag;
do {
Scanner sc1 = new Scanner(System.in);
String str1 = sc1.nextLine();
Business bus = (Business) list.get(value - 1);
int ca = Integer.parseInt(str1);
flag = false ;
switch (ca) {
case 1:
System.out.println("请输入您修改后的商户登录密码...");
String pwd = sc.next();
System.out.println("请再次输入您修改后的商户登录密码...");
String pwd2 = sc.next();
boolean bz = BusinessGJ.isInclude(pwd, BusinessGJ.c1) && BusinessGJ.isInclude(pwd, BusinessGJ.c2)
&& BusinessGJ.isInclude(pwd, BusinessGJ.c3) && BusinessGJ.isInclude(pwd, BusinessGJ.c4) && pwd.length() >= 8;
while (!bz) {
System.out.println("密码强度不够或位数不够,请重新输入");
pwd = sc.next();
bz = BusinessGJ.isInclude(pwd, BusinessGJ.c1) && BusinessGJ.isInclude(pwd, BusinessGJ.c2)
&& BusinessGJ.isInclude(pwd, BusinessGJ.c3) && BusinessGJ.isInclude(pwd, BusinessGJ.c4)
&& pwd.length() >= 8;
}
while (!pwd.equals(pwd2)) {
System.out.println("两次输入的密码不相同 请重新输入密码:");
pwd = sc.next();
System.out.println("请再次输入密码");
pwd2 = sc.next();
}
bus.setPassword(pwd2);
listToFile(list);
System.out.println("密码修改成功!");
break;
case 2:
System.out.println("请输入您修改后的商户地址...");
String dz = sc.next();
while (BusinessGJ.isOver(dz, 50)) { // 50
System.out.println("请重新输入商家地址 长度<=50:"); // 50
dz = sc.next();
}
bus.setBusinessAddress(dz);
listToFile(list);
System.out.println("商家地址修改成功!");
break;
case 3:
System.out.println("请输入您修改后的商户描述...");
String ms = sc.next();
while (BusinessGJ.isOver(ms, 40)) { // 50
System.out.println("请重新输入商家介绍 长度<=40:"); // 50
ms = sc.next();
}
bus.setBusinessExplain(ms);
listToFile(list);
System.out.println("商家介绍修改成功!");
break;
case 4:
System.out.println("请输入您修改后的商户起送费...");
String sp = sc.next();
while (!BusinessGJ.isDouble(sp)) {
System.out.println("请重新输入起送费:");
sp = sc.next();
}
bus.setStarPrice(Double.parseDouble(sp));
listToFile(list);
System.out.println("商家起送费修改成功!");
break;
case 5:
System.out.println("请输入您修改后的商户配送费...");
String ps = sc.next();
while (!BusinessGJ.isDouble(ps)) {
System.out.println("请重新输入配送费:");
ps = sc.next();
}
bus.setDeliveryPrice(Double.parseDouble(ps));
listToFile(list);
System.out.println("商家配送费修改成功!");
break;
case 6:
System.out.println("请输入您修改后的商户名称.....");
String mc = sc.next();
while (BusinessGJ.isOver(mc, 40)) { // 50
System.out.println("请重新输入商家介绍 长度<=40:"); // 50
mc = sc.next();
}
bus.setBusinessExplain(mc);
listToFile(list);
System.out.println("商家名称修改成功!");
break;
case 0:
System.out.println("按回车键后回到商户管理菜单.....");
menu02();
break;
default:
System.out.println("请选择相应的功能! 在0-5之间选择。");
flag = true;
}
} while (flag);
// 暂停程序
String str3 = sc.nextLine();
menu02();
}
// 删除商户
void deleteBusiness() throws IOException {
List<Business> list = fileToList();
System.out.println("您确定要注销该商户吗?(Y/N)");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if (str.equals("Y") || str.equals("y")) {
// System.out.println("请输入您要注销的商户ID...");
// String str6 = sc.nextLine();
// int id = Integer.parseInt(str6);
Business bus = (Business) list.get(value - 1);
list.remove(bus.getBusinessId() - 1);
listToFile(list);
System.out.println("您的商户注销成功!");
System.out.println("即将返回主菜单!");
String str5 = sc.nextLine();
menu01();
} else {
System.out.println("按回车键后回到商户管理菜单...");
// 暂停程序
String str5 = sc.nextLine();
menu02();
}
}
// 商户食品管理
void businessFood() throws IOException {
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
System.out.println("饿了么系统>> 商户食品管理菜单");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");// \n
System.out.println("1.显示本商户的所有食品菜单");// \n
System.out.println("2.添加菜品");// \n
System.out.println("3.修改菜品");// \n
System.out.println("4.删除菜品");// \n
System.out.println("0.返回上一级菜单");// \n
Scanner sc = new Scanner(System.in);
System.out.println("请选择功能(0-4):");
int fun = sc.nextInt();
FoodServiceImp food = new FoodServiceImp();
switch (fun) {
case 1:
food.showAllFood();
break;
case 2:
food.saveFood();
break;
case 3:
food.updateFood();
break;
case 4:
food.removeFood();
break;
case 0:
menu02();
break;
default:
System.out.println("请选择相应的功能! 在0-4之间选择。");
// 暂停程序
String str = sc.nextLine();
menu02();
}
}
}
商户工具类代码展示
package elm_V1;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* [集合与文件处理--商户工具类]
*
* @author 秦帅
* @date 2023-9-21
*/
public class BusinessGJ {
static char[] c1 = {'!', '@', '#', '$', '&', '*', '+'};
static char[] c2 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
static char[] c3 = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
static char[] c4 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z'};
public static void main(String[] args) {
try {
// List<Student> list = new ArrayList<Student>();
// Student a1 = new Student(1, "aaa");
// Student a2 = new Student(2, "bbb");
// Student a3 = new Student(3, "ccc");
// list.add(a1);
// list.add(a2);
// list.add(a3);// 添加对象入集合
// listToFile(list);// 集合写入文件
// List<Student> list2 = fileToList();// 文件读入集合
// Student a4 = new Student(4, "张三疯子");
// 修改
// list2.set(3, a4);
// 删除
// list2.remove(a4.getSno() - 1);
// 添加
// list2.add(a4);
// listToFile(list2);// 集合写入文件
int n = getFileRow();// 文件读入集合
// System.out.println("n=" + n);
System.out.println(isDouble("13.5"));// a3.5
// System.out.println(isRepeat("bbb"));// sss
// System.out.println(isOver("bbbb", 5));// sss
// System.out.println(isInclude("1234", c2));
String ss1 = "aaa1eee!M";// aaA1!
System.out.println(isInclude(ss1, c1) && isInclude(ss1, c2) && isInclude(ss1, c3) && isInclude(ss1, c4)
&& ss1.length() >= 8);
} catch (IOException e) {
e.printStackTrace();
}
}
// 字符流读文件数据到集合中
public static List<Business> fileToList() throws IOException {
List<Business> list = new ArrayList<Business>();
File file = new File("G:\\Idea_hn_ws\\DB\\Business\\Business.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while (true) {
String str = br.readLine();// 一次读取一行文本
if (str != null) {
// System.out.println(str);
String[] arr = str.split(" ");
Business bus = new Business();
bus.setBusinessId(Integer.parseInt(arr[0]));
bus.setPassword(arr[1]);
bus.setBusinessName(arr[2]);
bus.setBusinessAddress(arr[3]);
bus.setBusinessExplain(arr[4]);
bus.setStarPrice(Double.parseDouble(arr[5]));
bus.setDeliveryPrice(Double.parseDouble(arr[6]));
list.add(bus);
} else {
break;
}
}
return list;
}
// 字符流把集合数据写入文件
public static void listToFile(List list) throws IOException {
// 确认文件是utf-8编码
File file = new File("G:\\Idea_hn_ws\\DB\\Business\\Business.txt");
FileWriter fileWriter = null;
// 第一个参数是为是否append
BufferedWriter bw = null;
try {
// 第二个参数是表示是否向文件中追加内容 true==追加 false==覆盖
fileWriter = new FileWriter(file, false);
bw = new BufferedWriter(fileWriter);
// 传统的遍历方式
for (int i = 0; i < list.size(); i++) {
Business bus = (Business) list.get(i);
StringBuilder sbr = new StringBuilder();
sbr.append(bus.getBusinessId() + " ");
sbr.append(bus.getPassword() + " ");
sbr.append(bus.getBusinessName() + " ");
sbr.append(bus.getBusinessAddress() + " ");
sbr.append(bus.getBusinessExplain() + " ");
sbr.append(bus.getStarPrice() + " ");
sbr.append(bus.getDeliveryPrice());
bw.write(sbr.toString());
bw.newLine();// 换行
}
} catch (IOException e) {
e.printStackTrace();
} finally { // 记着关闭流
// 如果调用了关闭流的方法,就不用手动调bw.flush()
bw.close();
fileWriter.close();
}
}
// 获取文件里数据的行数
// public static int getFileRow() throws IOException {
// int sum = 0;
// File file = new File("G:\\Idea_hn_ws\\DB\\Business\\Business.txt");
// FileReader fr = new FileReader(file);
// BufferedReader br = new BufferedReader(fr);
// while (true) {
// String str = br.readLine();// 一次读取一行文本
// if (str != null) {
// sum++;
// } else {
// break;
// }
// }
// return sum;
// }
public static int getFileRow() throws IOException {
int sum = 0,bid = 0;
List<Business> list = fileToList();
if(list.size()==0)
return sum ;
// 传统的遍历方式
for (int i = 0; i < list.size(); i++) {
Business bus = (Business) list.get(i);
bid = bus.getBusinessId();
}
return bid ;
}
// 判断参数是否为双精度浮点数的字符串
public static boolean isDouble(String str) {
boolean b = true;
try {
double d1 = Double.parseDouble(str);
} catch (NumberFormatException e) {
b = false;
}
return b;
}
// 判断文件中是否有与参数重名商家
public static boolean isRepeat(String str) throws IOException {
boolean b = false;
List<Business> list = fileToList();
// 传统的遍历方式
for (int i = 0; i < list.size(); i++) {
Business bus = (Business) list.get(i);
String bname = bus.getBusinessName();
if (bname.equals(str)) {
b = true;
break;
}
}
return b;
}
// 判断字符串是否超出指定长度
public static boolean isOver(String str, int length) {
boolean b = false;
if (str.length() > length)
b = true;
return b;
}
// 判断字符串中是否包含指定字符数组中的任意字符
public static boolean isInclude(String str, char[] c) throws IOException {
boolean b = false;
for (int i = 0; i < c.length; i++) {
if (str.indexOf(c[i]) >= 0) {
b = true;
break;
}
}
return b;
}
// 密码是否与ID匹配
public static boolean isRightpsd(String id, String str) throws IOException {
boolean b = false;
List<Business> list = fileToList();
// 传统的遍历方式
for (int i = 0; i < list.size(); i++) {
Business bus = (Business) list.get(i);
int j = 0;
Integer j1 = new Integer(bus.getBusinessId());//生成Integer类
String bid = j1.toString(); // id 转 str
String bpsd = bus.getPassword();
if (bid.equals(id) && str.equals(bpsd)) {
b = true;
break;
}
}
return b;
}
// 验证密码是否符合要求
public static void isRightPwd(String pwd) throws IOException {
Scanner sc = new Scanner(System.in);
boolean bz = BusinessGJ.isInclude(pwd, BusinessGJ.c1) && BusinessGJ.isInclude(pwd, BusinessGJ.c2)
&& BusinessGJ.isInclude(pwd, BusinessGJ.c3) && BusinessGJ.isInclude(pwd, BusinessGJ.c4) && pwd.length() >= 8;
while (!bz) {
System.out.println("密码强度不够或位数不够,请重新输入");
pwd = sc.next();
bz = BusinessGJ.isInclude(pwd, BusinessGJ.c1) && BusinessGJ.isInclude(pwd, BusinessGJ.c2)
&& BusinessGJ.isInclude(pwd, BusinessGJ.c3) && BusinessGJ.isInclude(pwd, BusinessGJ.c4)
&& pwd.length() >= 8;
}
String pwd2 = null;
while (!pwd.equals(pwd2)) {
System.out.println("两次输入的密码不相同 请重新输入密码:");
pwd = sc.next();
System.out.println("请再次输入密码");
pwd2 = sc.next();
}
}
}