商品信息类:
public class Cat {
private int Id;//序号
private String Name;//商品名
private double money;
private String Description;//商品描述
private String origin ;//产地
private double number;
public Cat() {
}
public Cat(int id, String name, double money, String description, String origin, double number) {
Id = id;
Name = name;
this.money = money;
Description = description;
this.origin = origin;
this.number = number;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
}
用户信息类:
import java.util.ArrayList;
import java.util.Scanner;
public class Text {
private int id;
private String Name;
private String address;
private String possword;
private int age;
private double salary;
private ArrayList<String> list;
public Text() {
}
public Text(int id, String name, String address, String possword, int age, double salary, ArrayList<String> list) {
this.id = id;
Name = name;
this.address = address;
this.possword = possword;
this.age = age;
this.salary = salary;
this.list = list;
}
public ArrayList<String> getList() {
return list;
}
public void setList(ArrayList<String> list) {
this.list = list;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getPossword() {
return possword;
}
public void setPossword(String possword) {
this.possword = possword;
}
}
订单信息:
public class form {
private String name;
private String time;
private String tai;
public form() {
}
public form(String name, String time, String tai) {
this.name = name;
this.time = time;
this.tai = tai;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTai() {
return tai;
}
public void setTai(String tai) {
this.tai = tai;
}
}
mian方法:
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Cat> s = new ArrayList<Cat>();//商品动态数组
ArrayList<Text> s2 = new ArrayList<Text>();//用户动态数组
ArrayList<Cat> cats = new ArrayList<Cat>();//购物车
ArrayList<Double> cat = new ArrayList<>();
ArrayList<form> forms = new ArrayList<>();
ArrayList<Cat> cats1 = new ArrayList<>();//订单
ArrayList<Double> s4 = new ArrayList<>();//付款金额
ArrayList<String> list = new ArrayList<>();
fruit(s);//存储
user(s2);
fruit(cats1);
Scanner scanner = new Scanner(System.in);
System.out.println(" 我们在行动/帮助农民共度难关");
System.out.println(" 一起携手共进/爱心助农/你我一起");
System.out.println("绿色天然 实物拍摄 产地直发 坏果必赔 快速售后");
System.out.println();
while (true) {
A(s, s2, cats, cat, forms, cats1, s4, list);
}
}
public static void A(ArrayList<Cat> s, ArrayList<Text> s2, ArrayList<Cat> s3, ArrayList<Double> s4, ArrayList<form> s6, ArrayList<Cat> s7, ArrayList<Double> s8, ArrayList<String> s9) {
System.out.println("=========请按照菜单序号输入对应的数字进入对应的菜单内容=========");
System.out.println("1:登入 2:注册 3:退出 ");
System.out.println("===============================================================");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
switch (a) {
case "1":
A2(s, s2, s3, s4, s6, s7, s8, s9);
break;
case "2":
A1(s2);
break;
case "3":
System.out.println("退出");
return;
default:
System.out.println("没有选项");
}
}
public static void A1(ArrayList<Text> s2) {
System.out.println("1.新用户注册 2.手机号注册 3.退出");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
switch (a) {
case "1":
C(s2);
break;
case "2":
C2(s2);
break;
case "3":
System.out.println("返回");
return;
default:
System.out.println("没有选项");
}
}
public static void A2(ArrayList<Cat> s2, ArrayList<Text> s, ArrayList<Cat> s3, ArrayList<Double> s4, ArrayList<form> s6, ArrayList<Cat> s7, ArrayList<Double> s8, ArrayList<String> s9) {
System.out.println("1.管理员登入 2.用户登入 3.退出");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
switch (a) {
case "1":
B1(s, s2);
break;
case "2":
B(s, s2, s3, s4, s6, s7, s8, s9);
break;
case "3":
System.out.println("退出");
return;
default:
System.out.println("没有");
}
}
//商品信息
public static void fruit(ArrayList<Cat> s) {
Cat cat1 = new Cat(1, "牛肉干", 10.0, "个大皮薄,脆爽口", "烟台", 0);
Cat cat2 = new Cat(2, "芒果干", 20.0, "果核薄少,果肉多", "海南", 0);
Cat cat3 = new Cat(3, "粑粑柑", 30.0, "颗颗果粒,甜味足", "四川", 0);
s.add(cat1);
s.add(cat2);
s.add(cat3);
}
//用户信息
public static void user(ArrayList<Text> s2) {
ArrayList<String> list = new ArrayList<>();
list.add("广州省xx市xx县");
ArrayList<String> list2 = new ArrayList<>();
list2.add("北京省xx市xx县");
Text text = new Text(1, "user1", "广州", "userA123", 30, 800.89, list);
Text text1 = new Text(2, "user2", "北京", "userB123", 25, 700.84, list2);
s2.add(text);
s2.add(text1);
}
//管理员登入
public static void B1(ArrayList<Text> s2, ArrayList<Cat> s) {
Scanner scanner = new Scanner(System.in);
System.out.println("登录功能");
System.out.println("请输入用户名");
String name = scanner.nextLine();
System.out.println("请输入密码");
String password = scanner.nextLine();
if (name.equals("admin") && password.equals("admin123")) {
for (int i = 0; i < 100; i++) {
System.out.println("1:查看用户、2:添加商品、3:查看商品、4:退出");
String count = scanner.next();
switch (count) {
case "1":
viewUsers(s2);//查看用户
break;
case "2":
AddaProduct(s);
break;
case "3":
BrowseProducts(s);
break;
case "4":
System.out.println("退出");
return;
}
}
} else {
System.out.println("信息错误");
}
}
//用户登入
public static void B(ArrayList<Text> s2, ArrayList<Cat> s, ArrayList<Cat> s3, ArrayList<Double> s4, ArrayList<form> s5, ArrayList<Cat> s6, ArrayList<Double> s7, ArrayList<String> s8) {
Scanner scanner = new Scanner(System.in);
System.out.println("登录功能");
form form = new form();
System.out.println("请输入用户名或者手机号");
String name = scanner.next();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
Date date = new Date();
String format = simpleDateFormat.format(date);
form.setTime(format);
form.setName(name);
s8.add(name);
s5.add(form);
System.out.println("请输入密码");
String password = scanner.next();
for (Text text : s2) {
if (name.equals(text.getName()) && password.equals(text.getPossword())) {
for (int j = 0; j < 100; j++) {
System.out.println("1:浏览商品、2:添加到购物车、3:购买商品、4:修改用户信息、5:查看购物车、6:查看订单、7:增加地址、8:删除地址、9:查看收获地址、10:退出");
System.out.println("===============================================================");
String count = scanner.next();
switch (count) {
case "1":
//浏览商品
BrowseProducts(s);
break;
case "2":
//添加到购物车
shoppingCart(s, s3);
break;
case "3":
//购买商品
PurchaseGoods(s, s4, s6, s5, s7);
break;
case "4":
//修改用户信息
UserInformation(s2, s8);
break;
case "5":
//查看购物车
see(s3);
break;
case "6":
seeing(s5, s6);
break;
case "7":
jia(s2,s8);
break;
case "8":
san(s2, s8);
break;
case "9":
see(s2,s8);
case "10":
//退出
System.out.println("用户退出");
return;
default:
System.out.println("对不起,没有找到选项");
break;
}
}
}
}
}
//用户注册方法
public static void C(ArrayList<Text> s2) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
Text text = new Text();
System.out.println("注册功能");
System.out.println("注册功能");
System.out.println("尊敬的用户,请输您的注册信息");
System.out.println("请输入用户名");
String User = scanner.next();
text.setName(User);
System.out.println("请输入密码");
String password = scanner.next();
text.setPossword(password);
System.out.println("在输入密码");
String passwords = scanner.next();
if (password.equals(passwords)) {
System.out.println("请输入地址");
String address = scanner.next();
text.setAddress(address);
System.out.println("以下的信息仅作为用户分析,不会用做其他用途,请放心填写");
System.out.println("请输入您的年龄");
int age = scanner.nextInt();
text.setAge(age);
System.out.println("请输入您的月薪");
double MoPay = scanner.nextDouble();
text.setSalary(MoPay);
System.out.println("请输入您的收获地址");
String a=scanner.next();
list.add(a);
text.setList(list);
s2.add(text);
System.out.println("注册成功");
} else {
System.out.println("和上一个密码不同");
}
}
//手机号注册方法
public static void C2(ArrayList<Text> s2) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
Text text = new Text();
System.out.println("请输入手机号");
String a = scanner.next();
text.setName(a);
System.out.println("请输入验证码,此处不调用短信发送api,用123作为验证码");
String b = scanner.next();
System.out.println("请输入密码");
String c = scanner.next();
text.setPossword(c);
System.out.println("请输入地址");
String d = scanner.next();
text.setAddress(d);
System.out.println("一下信息仅作为用户分析,不会用作其他用途,请放心填写");
System.out.println("请输入您的年龄");
int e = scanner.nextInt();
text.setAge(e);
System.out.println("请输入您的月薪");
double f = scanner.nextDouble();
text.setSalary(f);
System.out.println("请输入您的收获地址");
String q=scanner.next();
list.add(q);
text.setList(list);
System.out.println("注册成功");
s2.add(text);
}
//选择1
// public static void D(ArrayList<Cat> s, ArrayList<Text> s2, ArrayList<Cat> s3,ArrayList<Double> s4) {
// Scanner scanner = new Scanner(System.in);
// String count = scanner.next();
// switch (count) {
// case "1":
// //管理员登入
// B1(s2,s,s3);
// break;
// case "2":
// //用户登入
// B(s2,s,s3,s4);
// break;
// case "3":
// C(s2);
// break;
// case "4":
// //用户手机号注册
// C2(s2);
// break;
// case "5":
// return;
// default:
// System.out.println("对不起,没有找到选项");
// break;
// }
// }
//浏览商品
public static void BrowseProducts(ArrayList<Cat> s) {
System.out.println("序号\t商品名\t价格\t\t详细描述\t\t\t产地");
for (int i = 0; i < s.size(); i++) {
Cat cat = s.get(i);
System.out.println(cat.getId() + "\t" + cat.getName() + "\t" + cat.getMoney() + "\t" + cat.getDescription() + "\t" + cat.getOrigin());
}
System.out.println("--------------------------------");
}
//查看用户
public static void viewUsers(ArrayList<Text> s2) {
System.out.println("序号\t用户名\t地址\t年龄\t月薪\t收货地址");
for (int i = 0; i < s2.size(); i++) {
Text text = s2.get(i);
System.out.print(i + 1 + "\t" + text.getName() + "\t" + text.getAddress() + "\t" + text.getAge() + "\t" + text.getSalary() + "\t");
for (String s : text.getList()) {
System.out.print(s + " ");
}
System.out.println();
}
}
//修改用户信息
public static void UserInformation(ArrayList<Text> s2, ArrayList<String> s8) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < s8.size(); i++) {
String s = s8.get(i);
for (int j = 0; j < s2.size(); j++) {
Text text = s2.get(j);
if (s.equals(text.getName())) {
System.out.println("修改信息");
System.out.println("请输入新的密码");
String a = scanner.next();
text.setPossword(a);
System.out.println("请输入新的地址");
String a2 = scanner.next();
text.setAddress(a2);
System.out.println("请输入新的年龄");
int a3 = scanner.nextInt();
text.setAge(a3);
System.out.println("请输入新的月薪");
double a4 = scanner.nextDouble();
text.setSalary(a4);
}
}
s8.clear();
break;
}
}
//添加商品
public static void AddaProduct(ArrayList<Cat> s) {
Cat cat = new Cat();
System.out.println("请输入商品信息");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品序号");
int a = scanner.nextInt();
cat.setId(a);
System.out.println("请输入商品名称");
String a2 = scanner.next();
cat.setName(a2);
System.out.println("请输入价格");
double a5 = scanner.nextDouble();
cat.setMoney(a5);
System.out.println("请输入商品描述");
String a3 = scanner.next();
cat.setDescription(a3);
System.out.println("请输入商品产地");
String a4 = scanner.next();
cat.setOrigin(a4);
s.add(cat);
}
//购买商品
public static void PurchaseGoods(ArrayList<Cat> s, ArrayList<Double> s2, ArrayList<Cat> s6, ArrayList<form> s4, ArrayList<Double> s5) {
Scanner scanner = new Scanner(System.in);
String c = "y";
int a = 0;
double b = 0;
double sum = 0;
while ("y".equals(c)) {
System.out.println("购买商品");
System.out.println("请输入你要购买的商品序号");
a = scanner.nextInt();
System.out.println("请输入商品数量");
b = scanner.nextDouble();
if (a - 1 >= 0 && a - 1 < s.size()) {
Cat cat = s6.get(a - 1);
cat.setNumber(b);
} else {
System.out.println("不存在商品,待添加");
}
Cat cat = s6.get(a - 1);
sum = cat.getMoney() * b;
s2.add(sum);
System.out.println("是否继续购买,继续输入y,不继续购买输入n");
c = scanner.next();
}
System.out.println("退出购买,即将支付");
System.out.println("是否支付,支付输入yes,不支付输入no");
String d = scanner.next();
double NewSum = 0;
if (d.equals("yes")) {
for (int i = 0; i < s2.size(); i++) {
Double v = s2.get(i);
NewSum += v;
}
s5.add(NewSum);
for (int i = 0; i < s5.size(); i++) {
Double v = s5.get(i);
System.out.println(v);
}
s2.clear();
s5.clear();
String q = "支付成功";
for (int i = 0; i < s4.size(); i++) {
form form = s4.get(i);
form.setTai(q);
s4.add(form);
break;
}
} else {
String q = "支付失败";
for (int i = 0; i < s4.size(); i++) {
form form = s4.get(i);
form.setTai(q);
s4.add(form);
break;
}
}
}
//添加购物车
public static void shoppingCart(ArrayList<Cat> s, ArrayList<Cat> s2) {
Scanner scanner = new Scanner(System.in);
System.out.println("添加到购物车");
String c = "T";
while ("T".equals(c)) {
System.out.println("请输入你要购买的商品序号");
int a = scanner.nextInt();
System.out.println("请输入商品的数量");
int b = scanner.nextInt();
if (a - 1 >= 0 && a - 1 < s.size()) {
Cat cat1 = s.get(a - 1);
cat1.setNumber(b);
s2.add(cat1);
} else {
System.out.println("不存在商品,待添加");
}
System.out.println("是否继续,继续输入T,不继续输入F");
c = scanner.next();
}
}
//用户查看购物车
public static void see(ArrayList<Cat> s2) {
System.out.println("序号\t商品名\t金钱\t\t商品详解\t\t\t产地\t数量");
for (int i = 0; i < s2.size(); i++) {
Cat cat = s2.get(i);
System.out.println(cat.getId() + "\t" + cat.getName() + "\t" + cat.getMoney() + "\t" + cat.getDescription() + "\t" + cat.getOrigin() + "\t" + cat.getNumber());
}
}
//查看订单
public static void seeing(ArrayList<form> s, ArrayList<Cat> s6) {
for (int j = 0; j < s.size(); j++) {
System.out.println("该用户的订单信息如下");
form form = s.get(j);
int a = j + 1;
System.out.println("用户名=" + form.getName() + "," + "订单号=" + a + ", 订单日期=" + form.getTime() + ", 订单状态=" + form.getTai());
System.out.println("序号\t商品名\t金钱\t\t商品详解\t\t\t产地\t数量");
for (int i = 0; i < s6.size(); i++) {
Cat cat = s6.get(i);
int b = i + 1;
if (cat.getNumber() > 0) {
System.out.println(b + "\t" + cat.getName() + "\t" + cat.getMoney() + "\t" + cat.getDescription() + "\t" + cat.getOrigin() + "\t" + cat.getNumber());
}
}
break;
}
}
//增加地址
public static void jia(ArrayList<Text> list,ArrayList<String>s8) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < s8.size(); i++) {
String s = s8.get(i);
for (int j = 0; j < list.size(); j++) {
Text text = list.get(j);
if (s.equals(text.getName())) {
System.out.println("请输入要添加的地址");
String a = scanner.next();
text.getList().add(a);
}
}
s8.clear();
break;
}
}
public static void san(ArrayList<Text> list, ArrayList<String> s8) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < s8.size(); i++) {
String s = s8.get(i);
for (int j = 0; j < list.size(); j++) {
Text text = list.get(j);
if (s.equals(text.getName())) {
System.out.println("请输入要删除的地址的序号");
int a = scanner.nextInt();
text.getList().remove(a - 1);
}
}
s8.clear();
break;
}
}
public static void see(ArrayList<Text> list, ArrayList<String> s8) {
for (int i = 0; i < s8.size(); i++) {
String s = s8.get(i);
for (int j = 0; j < list.size(); j++) {
Text text = list.get(j);
if (s.equals(text.getName())) {
for (String string : text.getList()) {
System.out.print(string+" ");
}
System.out.println();
}
}
}
}
}