package com.softeem.basic.work;
/**
-
用户类 */
-
public class User {
-
private String userName;//用户名
-
private String passWord;//用户密码
-
private String otherName;//用户别名
-
private int age;//用户年龄
public User() { }
public User(String userName, String passWord, String otherName, int age) { this.userName = userName; this.passWord = passWord; this.otherName = otherName; this.age = age; }
@Override
-
public String toString() { return "用户名:" + userName + "\n" + "昵称:" + otherName + "\n" + "年龄:" + age ; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getPassWord() { return passWord; }
public void setPassWord(String passWord) { this.passWord = passWord; }
public String getOtherName() { return otherName; }
public void setOtherName(String otherName) { this.otherName = otherName; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; } }
package com.softeem.basic.work;
/**
-
商品类 */
-
public class Commodity {
-
private int CommodityId;//商品编号
-
private String name;//商品名称
-
private double price;//商品价格
-
private int number;//商品库存
public Commodity() { }
public Commodity(int commodityId, String name, double price, int number) { CommodityId = commodityId; this.name = name; this.price = price; this.number = number; }
@Override
-
public String toString() { return CommodityId + "\t" + "\t" + name + "\t" + "\t" + price + "\t" + "\t" + number; }
public int getCommodityId() { return CommodityId; }
public void setCommodityId(int commodityId) { CommodityId = commodityId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public int getNumber() { return number; }
public void setNumber(int number) { this.number = number; } }
package com.softeem.basic.work;
public class Gwc extends Commodity {
private String name;
public Gwc() {}
public Gwc(int commodityId, String name, double price, int number, String name1) {
super(commodityId, name, price, number);
this.name = name1;
}
@Override
public String toString() {
return super.getCommodityId() + "\t" + "\t" +
super.getName() + "\t" + "\t" +
super.getPrice() + "\t" + "\t" +
super.getNumber();
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
package com.softeem.basic.work;
import javax.jws.soap.SOAPBinding;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.SortedMap;
/**
-
电商系统类 */
-
public class DAO {
-
Scanner sc = new Scanner(System.in);
-
Random r = new Random();
-
ArrayList<User> us = new ArrayList<User>();
-
ArrayList<Commodity> cd = new ArrayList<Commodity>();
-
ArrayList<Gwc> cd1 = new ArrayList<Gwc>();
//电商登录
-
public void add01 (){
-
int n = 0; System.out.println("请输入账号:"); String a1 = sc.next(); System.out.println("请输入密码:"); String a2 = sc.next(); if(a1.equals("admin") && a2.equals("123456")){ System.out.println("登录成功"); menu3(); }else{ System.out.println("账号或密码错误"); n++; if((3-n)<=0){ System.out.println("您的账号已锁死"); menu1(); }else{ System.out.println("您还有" + (3-n) + "次机会"); add01(); } } }
-
//商品信息导入
- public void add02 (){
- System.out.println("输入商品编号"); int CommodityId = sc.nextInt(); System.out.println("输入商品名称"); String name = sc.next(); System.out.println("输入商品价格"); double price = sc.nextInt(); System.out.println("输入商品库存"); int number = sc.nextInt(); Commodity c = new Commodity(CommodityId,name,price,number); cd.add(c); System.out.println("现有商品信息为"); System.out.println("商品编号" + "\t" + "商品名称" + "\t" + "商品价格 " + "\t" + "商品库存"); for(int i=0 ; i<cd.size() ; i++){ System.out.println(cd.get(i).toString()); } System.out.println("是否继续导入商品信息Y/N"); String a1 = sc.next(); if(a1.equals("y")){ add02(); }else{ menu3(); } }
//商品信息修改
- public void add03 (){
- System.out.println("输入您要修改的商品编号"); int a1 = sc.nextInt(); for(int i=0 ; i<cd.size() ; i++){ if(cd.get(i).getCommodityId()==a1){ System.out.println("输入新的商品名称"); String a2 = sc.next(); System.out.println("输入新的商品价格"); double a3 = sc.nextDouble(); System.out.println("输入新的商品库存"); int a4 = sc.nextInt(); cd.get(i).setName(a2); cd.get(i).setPrice(a3); cd.get(i).setNumber(a4); System.out.println("修改后的商品信息为"); System.out.println("商品编号" + "\t" + "商品名称" + "\t" + "商品价格 " + "\t" + "商品库存"); System.out.println(cd.get(i).toString()); } } System.out.println("是否继续修改其他商品信息Y/N"); String s = sc.next(); if(s.equals("y")){ add03(); }else{ menu3(); } }
//用户注册
- public void add11 (){
- System.out.println("输入用户名"); String userName = sc.next(); String passWord = ""; for(;;){ System.out.println("输入密码"); String passWord1 = sc.next(); System.out.println("再次输入密码"); String passWord2 = sc.next(); if(passWord1.equalsIgnoreCase(passWord2)){ if(passWord1.length()>=6){ passWord = passWord1; System.out.println("输入昵称"); String otherName = sc.next(); System.out.println("输入年龄"); int age = sc.nextInt(); User u = new User(userName,passWord,otherName,age); us.add(u); System.out.println("注册成功"); break; }else{ System.out.println("密码长度不能小于6"); } }else{ System.out.println("两次输入密码不一致"); } } menu2(1); }
//用户登录
- public void add12 (){
- int n = 0; System.out.println("输入用户名"); String userName = sc.next(); System.out.println("输入密码"); String passWord = sc.next(); for(int i=0 ; i<us.size() ; i++){ if(us.get(i).getUserName().equalsIgnoreCase(userName) && us.get(i).getPassWord().equalsIgnoreCase(passWord)){ System.out.println("登录成功"); menu4(us.get(i)); }else{ System.out.println("账号或密码错误"); n++; if((3-n)<=0){ System.out.println("您的账号已锁死"); menu1(); }else{ System.out.println("您还有" + (3-n) + "次机会"); add12(); } } } }
//商品购买
- public void add13 (User u){
- System.out.println("现有商品信息"); System.out.println("商品编号" + "\t" + "商品名称" + "\t" + "商品价格 " + "\t" + "商品库存"); for(int i=0 ; i<cd.size() ; i++){ System.out.println(cd.get(i).toString()); } for(;;){ System.out.println("请输入您要购买的商品编号"); int a1 = sc.nextInt(); boolean flag = false; for(int i=0 ; i<cd.size() ; i++){ if(cd.get(i).getCommodityId()==a1){ flag = true; for(;;){ System.out.println("请输入您要购买的数量"); int a2 = sc.nextInt(); if(a2<=cd.get(i).getNumber()){ System.out.println("已成功添加到购物车"); Gwc c = new Gwc(cd.get(i).getCommodityId(),cd.get(i).getName(),cd.get(i).getPrice(),a2,u.getOtherName()); cd1.add(c); cd.get(i).setNumber(cd.get(i).getNumber()-a2); System.out.println("是否继续购买其他商品Y/N"); String s = sc.next(); if(s.equals("y")){ add13(u); }else{ break; } }else{ System.out.println("请输入正确的购买数量:("+0+"," + cd.get(i).getNumber()+"]"); } } break; } } if(flag){ }else{ System.out.println("您输入的商品编号不存在,是否继续购买:Y/N"); String a = sc.next(); if(a.equals("y")){ add13(u); }else{ menu4(u); } } }}
//我的购物车
- public void add14 (User u){
- double sum = 0; System.out.println("商品编号" + "\t" + "商品名称" + "\t" + "商品价格 " + "\t" + "购买数量"); for(int i=0 ; i<cd1.size() ; i++){ System.out.println(cd1.get(i).toString()); sum = sum + (cd1.get(i).getNumber()*cd1.get(i).getPrice()); } System.out.println("总金额为:" + sum + "元"); System.out.println("是否付款:Y/N"); String a = sc.next(); if(a.equals("y")){ for(;;){ System.out.println("请输入付款金额"); double a1 = sc.nextInt(); if(a1<sum){ System.out.println("金额不足"); }else{ double d1 = a1 - sum; DecimalFormat df = new DecimalFormat("#.##"); String d2 = df.format(d1); double d = Double.valueOf(d2); System.out.println("找零:" + d + "元"); break; } } for(int i=0 ; i<cd1.size() ; i++){ cd1.remove(i); } } menu4(u); }
//个人信息
- public void add15 (User u){
- System.out.println("用户名:" + u.getUserName()); System.out.println("年龄:" + u.getAge()); System.out.println("昵称:" + u.getOtherName()); menu4(u); }
//初始系统页面
- public void menu1 (){
- System.out.println("******电器商城******"); System.out.println("1、用户系统"); System.out.println("2、商家系统"); System.out.println("0、退出系统"); System.out.println("******电器商城******"); int a = sc.nextInt(); int num = 0; switch (a){ case 1: num = 1; menu2(num); break; case 2: menu2(num); break; case 0: System.out.println("系统已退出"); System.out.println("期待您的下次使用"); System.exit(0); } }
//登录、注册页面
- public void menu2 (int num){
- if(num==0){ System.out.println("******电器商城******"); System.out.println("1、商家登录"); System.out.println("2、返回上一页面"); System.out.println("0、退出系统"); System.out.println("******电器商城******"); int a = sc.nextInt(); switch (a){ case 1: add01(); break; case 2: menu1(); break; case 0: System.out.println("系统已退出"); System.out.println("期待您的下次使用"); System.exit(0); } }else{ System.out.println("******电器商城******"); System.out.println("1、用户登录"); System.out.println("2、用户注册"); System.out.println("3、返回上一页面"); System.out.println("0、退出系统"); System.out.println("******电器商城******"); int a = sc.nextInt(); switch (a){ case 1: add12(); break; case 2: add11(); break; case 3: menu1(); break; case 0: System.out.println("系统已退出"); System.out.println("期待您的下次使用"); System.exit(0); } } } //电商 public void menu3 (){ System.out.println("******电器商城******"); System.out.println("1、商品导入"); System.out.println("2、商品信息修改"); System.out.println("3、返回初始页面"); System.out.println("0、退出系统"); System.out.println("******电器商城******"); int a = sc.nextInt(); switch (a){ case 1: add02(); break; case 2: add03(); break; case 3: menu1(); break; case 0: System.out.println("系统已退出"); System.out.println("期待您的下次使用"); System.exit(0); }}
//用户
- public void menu4 (User u){
- System.out.println("尊敬的" + u.getOtherName() + "用户,您好!"); System.out.println("*********************电器商城*********************"); System.out.println("1、商品购买"); System.out.println("2、我的购物车"); System.out.println("3、个人信息"); System.out.println("4、返回上一页面"); System.out.println("0、退出系统"); System.out.println("*********************电器商城*********************"); int a = sc.nextInt(); switch (a){ case 1: add13(u); break; case 2: add14(u); break; case 3: add15(u); break; case 4: menu2(1); break; case 0: System.out.println("系统已退出"); System.out.println("期待您的下次使用"); System.exit(0); } }
//用户
public void menu4 (User u){
System.out.println("尊敬的" + u.getOtherName() + "用户,您好!");
System.out.println("*********************电器商城*********************");
System.out.println("1、商品购买");
System.out.println("2、我的购物车");
System.out.println("3、个人信息");
System.out.println("4、返回上一页面");
System.out.println("0、退出系统");
System.out.println("*********************电器商城*********************");
int a = sc.nextInt();
switch (a){
case 1:
add13(u);
break;
case 2:
add14(u);
break;
case 3:
add15(u);
break;
case 4:
menu2(1);
break;
case 0:
System.out.println("系统已退出");
System.out.println("期待您的下次使用");
System.exit(0);
}
}
}
package com.softeem.basic.work;
public class Test {
public static void main(String[] args) {
DAO d =new DAO();
d.menu1(); }
}