框架部分
二、主功能区
1、一级界面
2、二级界面
2.1、用户登录注册
2.2、用户/访客查看商城
2.3、用户查看购物车
2.4、管理员登录
2.5、用户退出登录
2.6、退出系统
三、管理员相关功能
1、管理商城
2、管理用户
完整代码
Items类
package project;
public class Items {
private String ID = new String();
private String Name = new String();
private Double Price = 0.0;
private Integer Number = 0;
public Items(String ID,String Name,Double Price,Integer Number){
this.ID = ID;
this.Name = Name;
this.Number = Number;
this.Price = Price;
}
public Items(){
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Double getPrice() {
return Price;
}
public void setPrice(Double price) {
Price = price;
}
public Integer getNumber() {
return Number;
}
public void setNumber(Integer number) {
Number = number;
}
@Override
public String toString() {
return "Items{" +
"ID='" + ID + '\'' +
", Name='" + Name + '\'' +
", Price=" + Price +
", Number=" + Number +
'}';
}
}
Shop类
package project;
import java.util.ArrayList;
import java.util.Comparator;
public class Shop {
private ArrayList<Items> Bag = new ArrayList<>();
public ArrayList<Items> getBag() {
return Bag;
}
public void setBag(ArrayList<Items> bag) {
Bag = bag;
}
//打印背包内所有物品信息
public void Print(){
//System.out.println("=================商城===================");
System.out.println("商品编号\t\t\t名称\t\t\t\t价格\t\t\t\t数量");
for(Items i : Bag){
System.out.println(String.format("%-16s",i.getID())+String.format("%-16s",i.getName())+String.format("%-16s",i.getPrice())+String.format("%-16s",i.getNumber()));
}
}
//查找商品并输出
public Items Find(String s){
for(Items i : Bag){
if(i.getID().equals(s)||i.getName().equals(s)){
System.out.println("商品编号\t\t\t名称\t\t\t\t价格\t\t\t\t数量");
System.out.println(String.format("%-16s",i.getID())+String.format("%-16s",i.getName())+String.format("%-16s",i.getPrice())+String.format("%-16s",i.getNumber()));
return i;
}
}
Items i = new Items(null,null,null,null);
return i;
}
//查重
public boolean find(String s){
for(Items i : Bag){
if(i.getID().equals(s)||i.getName().equals(s))
return false;
}
return true;
}
//添加商品
public void addBag(Items It){//添加商品
Bag.add(It);
}
//排序
public void Sort(int n){
switch (n){
case 1://ID
Bag.sort(((o1, o2) -> {
return o1.getID().compareTo(o2.getID());
}));
break;
case 2://Price
Bag.sort(Comparator.comparingDouble(o->o.getPrice()));
break;
case 3://Number
Bag.sort(Comparator.comparingInt(o->o.getNumber()));
break;
default:break;
}
}
//删除商品
public void removeBag(String s,int n){//传入商品id和删除数量
for(Items i : Bag){
if(i.getID().equals(s)){
Integer x = i.getNumber() ;
x = x - n;
if(x == 0){
Bag.remove(i);
}
else{
i.setNumber(x);
}
return ;
}
}
System.out.println("Not Found!");
}
}
User类
package project;
public class User extends Shop{
private String UserName;
private String UserPassword;
public User(String userName,String userPassword){
UserName = userName;
UserPassword = userPassword;
}
public User(){
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPassword() {
return UserPassword;
}
public void setUserPassword(String userPassword) {
UserPassword = userPassword;
}
@Override
public void Print() {
int x = 1;
System.out.println(" =========================您的购物车======================");
System.out.println(" 商品编号\t\t\t名称\t\t\t\t价格\t\t\t 数量");
for(Items i : getBag()){
System.out.print("|| "+String.format("%-16s",i.getID())+String.format("%-16s",i.getName())+String.format("%-16s",i.getPrice())+String.format("%-6s",i.getNumber()));
for(int j = 0;j<getBag().size()-x;j++){
System.out.print(" ");
}
System.out.println("//");
x++;
}
System.out.println(" ======================================================");
System.out.println(" *** *** ");
System.out.println(" ***** ***** ");
System.out.println(" *** *** ");
}
}
Admin类
package project;
import java.util.ArrayList;
public class Admin {
private String AdminName;
private String AdminPassword;
public Admin(String adminName,String adminPassword){
AdminName = adminName;
AdminPassword = adminPassword;
}
public Admin(){
}
public String getAdminName() {
return AdminName;
}
public void setAdminName(String adminName) {
AdminName = adminName;
}
public String getAdminPassword() {
return AdminPassword;
}
public void setAdminPassword(String adminPassword) {
AdminPassword = adminPassword;
}
}
Massage类
package project;
import java.util.ArrayList;
import java.util.Scanner;
public class Message extends Shop{
Scanner scanner = new Scanner(System.in);
User NULL = new User(null,null);
private ArrayList<User> UserList = new ArrayList<>();
private ArrayList<Admin> AdminList = new ArrayList<>();
@Override
public void Print() {
System.out.println("========================商城==========================");
super.Print();
}
//用户注册
public User registerUser(){
User newUser = new User();
System.out.print("请输入用户名:");
newUser.setUserName(scanner.next());
if (newUser.getUserName().length()<3){
System.out.println("用户名需大于3位,请重新输入!");
return NULL;
}
if (!UserNameFind(newUser.getUserName())) {
System.out.print("用户名重复,请重新输入用户名:");
return NULL;
}
while (true) {
System.out.print("请输入密码:");
String s = scanner.next();
if(s.length()<6){
System.out.println("密码需大于6位,请重新输入!");
continue;
}
int ilen = 0, clen = 0;
for(int i=0;i<s.length();i++){
char ch = s.charAt(i);
if(ch >= '0'&&ch <= '9')
ilen++;
else
clen++;
}
if(ilen == 0||clen == 0){
System.out.println("密码需包含字符和数字,请重新输入!");
continue;
}
System.out.print("请再次输入密码:");
newUser.setUserPassword(scanner.next());
if (newUser.getUserPassword().equals(s)) {
System.out.println("恭喜用户:"+newUser.getUserName()+",注册并登录成功!");
addUser(newUser);
return newUser;
} else
System.out.println("两次密码不一致,请重新输入!");
}
}
//用户登录
public User loginUser(){
User testUser = new User(null,null);
System.out.print("请输入用户名:");
String s = scanner.next();
if (s.length()<3){
System.out.println("用户名需大于3位,请重新输入!");
return NULL;
}
testUser = loginFind(s);
if (testUser.getUserName() == null) {
System.out.println("该用户名不存在");
System.out.print("是否进行用户注册Y/n:");
String YN = scanner.next();
if(YN.equals("Y")||YN.equals("y")){
System.out.println("开始注册用户!");
User reUser = registerUser();
if(reUser != null) return reUser;
System.out.println("注册失败,请重新登录!");
return NULL;
}
System.out.println("请重新登录!");
return NULL;
}
if(true) {
System.out.print("请输入密码:");
s = scanner.next();
if (testUser.getUserPassword().equals(s)) {
System.out.println("恭喜用户:"+testUser.getUserName()+",登录成功!");
return testUser;
}
else
System.out.println("密码错误,请重新输入!");
}
return NULL;
}
//登录时搜索该用户信息
public User loginFind(String s){
for(User i : UserList){
if(i.getUserName().equals(s)){
return i;
}
}
User i = new User(null,null);
return i;
}
public Admin loginFindAdmin(String s){
for(Admin i : AdminList){
if(i.getAdminName().equals(s)){
return i;
}
}
Admin i = new Admin(null,null);
return i;
}
//查找用户名
public boolean UserNameFind(String s){
for(User i : UserList){
if(i.getUserName().equals(s)){
return false;
}
}
return true;
}
public boolean AdminNameFind(String s){
for(Admin i : AdminList){
if(i.getAdminName().equals(s)){
return false;
}
}
return true;
}
//添加用户
public void addUser(User user){
UserList.add(user);
}
//删除用户
public void deleteUser(User user){
UserList.remove(user);
}
//添加管理员
public void addAdmin(Admin admin){
AdminList.add(admin);
}
//所有用户信息
public void printUser(){
System.out.println("==========用户列表==========");
System.out.println("账号\t\t\t\t密码");
for(User i : UserList){
System.out.println(String.format("%-16s",i.getUserName())+String.format("%-16s",i.getUserPassword()));
}
}
//用户加购商品
public void userAddItems(User user,int number,Items items){
for(User i : UserList){
if(i.getUserName().equals(user.getUserName())){
Items it = new Items(items.getID(),items.getName(),items.getPrice(),number);
i.addBag(it);
return;
}
}
}
//管理员操作
//管理员登录
public Admin loginAdmin(){
Admin testAdmin = new Admin();
System.out.print("(输入‘q‘退出)请输入用户名:");
String s = scanner.next();
if(s.equals("q")){
testAdmin.setAdminName(s);
return testAdmin;
}
testAdmin = loginFindAdmin(s);
if (testAdmin.getAdminName() == null) {
System.out.println("用户名不存在,请重新输入!");
return testAdmin;
}
while(true) {
System.out.print("请输入密码:");
s = scanner.next();
if (testAdmin.getAdminPassword().equals(s)) {
System.out.println("恭喜管理员:"+testAdmin.getAdminName()+",登录成功!");
break;
}
System.out.println("密码错误,请重新输入!");
}
return testAdmin;
}
//修改用户信息
public boolean adminModUser(){
System.out.println("***************************");
System.out.print("(输入“q”退出)请输入需修改的用户名:");
String newName = scanner.next();
if(newName.equals("q")) return true;
User newUser = loginFind(newName);
if(newUser.getUserName() == null){
System.out.println("不存在该用户!");
return false;
}
while (true){
System.out.println("\t1.修改"+newUser.getUserName()+"的用户名");
System.out.println("\t2.修改"+newUser.getUserName()+"的密码");
System.out.println("\t3.返回上一步");
System.out.println("***************************");
System.out.print("请选择菜单:");
int q = scanner.nextInt();
if(q == 3) break;
while (q == 1){
System.out.print("请输入用户名:");
newName = scanner.next();
if (newName.length()<3){
System.out.println("用户名需大于3位,请重新输入!");
continue;
}
if (!UserNameFind(newName)) {
System.out.println("用户名重复,请重新输入用户名!");
continue;
}
newUser.setUserName(newName);
System.out.println("用户名修改成功!");
break;
}
while (q == 2){
System.out.print("请输入密码:");
String pass1 = scanner.next();
if(pass1.length()<6){
System.out.println("密码需大于6位,请重新输入!");
continue;
}
int ilen = 0, clen = 0;
for(int i=0;i<pass1.length();i++){
char ch = pass1.charAt(i);
if(ch >= '0'&&ch <= '9')
ilen++;
else
clen++;
}
if(ilen == 0||clen == 0){
System.out.println("密码需包含字符和数字,请重新输入!");
continue;
}
System.out.print("请再次输入密码:");
String pass2 = scanner.next();
if (pass1.equals(pass2)) {
newUser.setUserPassword(pass2);
System.out.println("用户:"+newUser.getUserName()+"修改密码成功!");
break;
} else
System.out.println("两次密码不一致,请重新输入!");
}
break;
}
return true;
}
//管理注册用户
public boolean adminRegisterUser(){
User newUser = new User();
System.out.print("请输入用户名:");
newUser.setUserName(scanner.next());
if (newUser.getUserName().length()<3){
System.out.println("用户名需大于3位,请重新输入!");
return false;
}
if (!UserNameFind(newUser.getUserName())) {
System.out.println("用户名重复,请重新输入用户名!");
return false;
}
while (true) {
System.out.print("请输入密码:");
String ss = scanner.next();
if(ss.length()<6){
System.out.println("密码需大于6位,请重新输入!");
continue;
}
int ilen = 0, clen = 0;
for(int i=0;i<ss.length();i++){
char ch = ss.charAt(i);
if(ch >= '0'&&ch <= '9')
ilen++;
else
clen++;
}
if(ilen == 0||clen == 0){
System.out.println("密码需包含字符和数字,请重新输入!");
continue;
}
System.out.print("请再次输入密码:");
newUser.setUserPassword(scanner.next());
if (newUser.getUserPassword().equals(ss)) {
System.out.println("用户:"+newUser.getUserName()+",注册成功!");
addUser(newUser);
break;
} else
System.out.println("两次密码不一致,请重新输入!");
}
return true;
}
//修改商品
public boolean modBagItems(){
System.out.println("***************************");
System.out.print("(输入“q”退出)请选择修改商品:");
String ss = scanner.next();
if(ss.equals("q")) return true;
Items it = Find(ss);
if (it.getName() == null) {
System.out.println(ss + "商品不存在");
return false;
}
System.out.println("\t1.商品编号");
System.out.println("\t2.商品名称");
System.out.println("\t3.商品价格");
System.out.println("\t4.商品数量");
System.out.println("\t5.退出修改");
System.out.println("***************************");
System.out.print("请选择修改项:");
int q = scanner.nextInt();
if(q == 5) return false;
while (q == 1){
System.out.print("请输入商品新编号:");
String newID = scanner.next();
if(!find(newID)){
System.out.println("该商品编号已存在,请重新输入!");
continue;
}
it.setID(newID);
break;
}
while (q == 2){
System.out.print("请输入商品新名称:");
String newName = scanner.next();
if(!find(newName)){
System.out.println("该商品名称已存在,请重新输入!");
continue;
}
it.setName(newName);
break;
}
if(q == 3){
System.out.print("请输入商品新单价:");
Double price = scanner.nextDouble();
it.setPrice(price);
}
if(q == 4){
System.out.print("请输入商品新数量:");
int number = scanner.nextInt();
it.setNumber(number);
}
System.out.println("修改成功!");
System.out.println("商品编号\t\t名称\t\t\t价格\t\t\t数量");
System.out.println(it.getID()+"\t\t"+it.getName()+"\t\t"+it.getPrice()+"\t\t"+it.getNumber());
return true;
}
//添加商品
public boolean addBagItems(){
Items newIt = new Items();
while (true) {
System.out.print("请输入新商品编号:");
newIt.setID(scanner.next());
if (!find(newIt.getID())) {
System.out.println("该商品编号已存在,请重新输入!");
continue;
}
break;
}
while (true) {
System.out.print("请输入新商品名称:");
newIt.setName(scanner.next());
if(!find(newIt.getName())){
System.out.println("该商品名称已存在,请重新输入!");
continue;
}
break;
}
System.out.print("请输入新商品单价:");
Double price = scanner.nextDouble();
newIt.setPrice(price);
System.out.print("请输入新商品数量:");
int number = scanner.nextInt();
newIt.setNumber(number);
addBag(newIt);
System.out.println("商品:"+newIt.getName()+"添加成功");
return true;
}
//删除商品
public boolean deleteBagItems(){
System.out.println("***************************");
System.out.print("(输入“q”退出)请选择删除商品:");
String ss = scanner.next();
if(ss.equals("q")) return true;
Items it = Find(ss);
if (it.getName() == null) {
System.out.println(ss + "商品不存在");
return false;
}
removeBag(it.getID(),it.getNumber());
System.out.println("删除成功!");
return true;
}
}
Main
package project;
import java.util.ArrayList;
import java.util.Scanner;
import project.Items;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//创建信息表
Message M = new Message();
//初始化设置
{
//添加管理员
Admin admin = new Admin("admin","admin");
M.addAdmin(admin);
//添加用户信息
User user = new User("ljk","ljk2023");
M.addUser(user);
user = new User("lxh","lxh2023");
M.addUser(user);
//添加商城商品
Items it = new Items("AD102","RTX4090",15999.0,12);
M.addBag(it);
it = new Items("AD104","RTX4070",6999.0,25);
M.addBag(it);
it = new Items("TU104","RTX2070",1499.0,17);
M.addBag(it);
it = new Items("IOS13","Iphone15",9999.0,10);
M.addBag(it);
it = new Items("CNMIOS","MI13Pro",3999.0,20);
M.addBag(it);
//添加用户已购商品
user = new User("ljk","ljk2023");
it = new Items("HUAWEI_1","mate60 Pro",8799.0,3);
M.userAddItems(user,3,it);
it = new Items("HUAWEI_2","matePad",3799.0,5);
M.userAddItems(user,5,it);
}
User nowUser = new User(null,null);
// 判断当前是否有用户登录
// if(nowUser.getUserName() == null)
// System.out.println(false);
int flag = 1;
while(flag == 1){
System.out.println(" _ _ ");
System.out.println(" | | / / ");
System.out.println("--|-/|-/-----__---/----__----__---_--_----__-");
System.out.println(" |/ |/ /___) / / ' / ) / / ) /___)");
System.out.println("__/__|____(___ _/___(___ _(___/_/_/__/_(___ _");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.查看商城");
System.out.println("\t4.查看我购买的商品");
System.out.println("\t5.管理员登录");
System.out.println("\t6.用户退出登录");
System.out.println("\t7.退出系统");
System.out.println("***************************");
System.out.print("请选择菜单:");
int choice = scanner.nextInt();
switch (choice){
//注册
case 1: {
nowUser = M.registerUser();
break;
}
//登录
case 2: {
while(nowUser.getUserName() == null) {
nowUser = M.loginUser();
}
break;
}
//查看商城
case 3: {
if(nowUser.getUserName() == null){
while (true) {
M.Print();
System.out.println("\t1.按商品编号进行排序");
System.out.println("\t2.按商品价格进行排序");
System.out.println("\t3.退出商城");
System.out.println("***************************");
System.out.print("请选择菜单:");
int x = scanner.nextInt();
if (x == 3) break;
if (x == 1){
M.Sort(1);
continue;
} else if (x == 2) {
M.Sort(2);
continue;
}
if (x != 2&&x != 3) {
System.out.println("无效输入!");
continue;
}
}
break;
}
while (true) {
M.Print();
System.out.println("\t1.选购商品");
System.out.println("\t2.按商品编号进行排序");
System.out.println("\t3.按商品价格进行排序");
System.out.println("\t4.退出商城");
System.out.println("***************************");
System.out.print("请选择菜单:");
int x = scanner.nextInt();
if(x == 4) break;
if(x != 1&&x != 2&&x!=3){
System.out.println("无效输入!");
continue;
}
if (x == 2){
M.Sort(1);
continue;
} else if (x == 3) {
M.Sort(2);
continue;
}
while (x == 1) {
System.out.println("***************************");
System.out.print("请选择商品:");
String s = scanner.next();
Items it = M.Find(s);
if (it.getName() == null) {
System.out.println(s + "商品不存在");
continue;
}
while (true){
System.out.print("请输入选购数量:");
int ans = scanner.nextInt();
if(ans > it.getNumber()){
System.out.println("库存不足,请重新输入数量!");
continue;
}
M.userAddItems(nowUser,ans,it);
it.setNumber(it.getNumber()-ans);
if(it.getNumber() == 0)
M.getBag().remove(it);
System.out.println(ans+"件"+it.getName()+"购买成功!");
break;
}
break;
}
}
break;
}
//查看我的购物车
case 4: {
if(nowUser.getUserName() == null){
System.out.println("请登录/注册后查看!");
break;
}
while (true) {
if(nowUser.getBag().size() == 0){
System.out.println("您暂未购买任何商品!");
break;
}
nowUser.Print();
System.out.println("\t1.删除已购的商品");
System.out.println("\t2.将已购的商品按编号排序");
System.out.println("\t3.将已购的商品按价格排序");
System.out.println("\t4.将已购的商品按数量排序");
System.out.println("\t5.退出我的购物车");
System.out.println("***************************");
System.out.print("请选择菜单:");
int x = scanner.nextInt();
if (x == 5) break;
else if (x == 2){
nowUser.Sort(1);
continue;
}
else if (x == 3){
nowUser.Sort(2);
continue;
}
else if (x == 4){
nowUser.Sort(3);
continue;
}
else if (x != 1) {
System.out.println("无效输入!");
continue;
}
while (x == 1){
System.out.println("***************************");
System.out.print("(输入“q”退出)请选择商品:");
String s = scanner.next();
if (s.equals("q")) break;
Items it = nowUser.Find(s);
if (it.getName() == null) {
System.out.println(s + "商品不存在");
continue;
}
while (true){
System.out.print("请输入删除数量:");
int ans = scanner.nextInt();
if(ans > it.getNumber()){
ans = it.getNumber();
}
nowUser.removeBag(it.getID(),ans);
if(it.getNumber() == 0)
M.getBag().remove(it);
System.out.println(ans+"件"+it.getName()+"删除成功!");
break;
}
break;
}
}
break;
}
//管理员登录
case 5: {
while(true) {
Admin admin = new Admin(null,null);
while (true){
admin = M.loginAdmin();
if(admin.getAdminName() != null){
break;
}
}
if(admin.getAdminName().equals("q")){
break;
}
while(true){
//一级菜单
System.out.println("\t1.管理商城");
System.out.println("\t2.查看用户信息");
System.out.println("\t3.退出登录");
System.out.println("***************************");
System.out.print("请选择菜单:");
int x = scanner.nextInt();
if(x == 3) break;
if (x != 1&&x != 2) {
System.out.println("无效输入!");
continue;
}
//查看用户信息
while (x == 2) {
//二级菜单
M.printUser();
System.out.println("\t1.修改用户信息");
System.out.println("\t2.添加用户信息");
System.out.println("\t3.删除用户信息");
System.out.println("\t4.返回上一步");
System.out.println("***************************");
System.out.print("请选择菜单:");
int n = scanner.nextInt();
if(n == 4) break;
if(n!=1&&n!=2&&n!=3)
System.out.println("无效输入!");
//管理员修改用户信息
while (n == 1) {
if(M.adminModUser()) break;
}
//管理员注册用户信息
while (n == 2) {
if(M.adminRegisterUser()) break;
}
//管理员删除用户信息
while (n == 3) {
System.out.println("***************************");
System.out.print("请输入需删除的用户名:");
String newName = scanner.next();
User newUser = M.loginFind(newName);
if(newUser.getUserName() == null){
System.out.println("不存在该用户!");
continue;
}
System.out.println("删除用户"+newUser.getUserName()+"成功");
M.deleteUser(newUser);
break;
}
}
//查看商城
while (x == 1) {
M.Print();
System.out.println("\t1.修改商品信息");
System.out.println("\t2.添加商品信息");
System.out.println("\t3.删除商品信息");
System.out.println("\t4.返回上一步");
System.out.println("***************************");
System.out.print("请选择菜单:");
int n = scanner.nextInt();
if(n == 4) break;
if(n!=1&&n!=2&&n!=3) {
System.out.println("无效输入!");
continue;
}
//管理员修改商品信息
boolean fl =false;
while (n == 1&&!fl) {
fl = M.modBagItems();
}
//管理员添加商品信息
while (n == 2&&!fl){
fl = M.addBagItems();
}
//管理员删除商品信息
while (n == 3&&!fl){
fl = M.deleteBagItems();
}
}
}
break;
}
break;
}
//用户退出登录
case 6: {
if(nowUser.getUserName() == null){
System.out.println("请先登录用户!");
break;
}
System.out.println("用户:"+nowUser.getUserName()+"退出登录成功!");
nowUser = new User(null,null);
break;
}
case 7: flag = 0;break;
default:break;
}
}
}
}