天天看点

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

利用ArrayList实现电商系统模块功能

功能介绍

完成一个电商系统的商品模块功能

管理员类包含以下属性:账号、密码(初始账号为admin、初始密码为admin)

商品类包含以下属性:商品ID,商品名,类别名,单价,库存量,产地,计量单位等信息

要求实现商品管理功能以及管理员登录功能,具体如下:

  1. 管理员登录(账号密码固定admin/admin)
  2. 修改管理员密码
  3. 商品添加
  4. 查看商品列表
  5. 查询指定id的商品
  6. 根据商品id删除商品
  7. 根据id修改指定商品的价格
  8. 根据id修改指定商品的库存
  9. 根据商品类别查询所有商品
  10. 查询指定价格区间的商品信息

分析

要实现这些功能,我创建了5个类:

  • Manager类(管理员类):定义管理员属性,存储管理员信息
  • ManagerMode类(管理员管理类):声明管理员功能:管理员登录、修改管理员密码
  • Product类(商品类):定义商品属性,存储商品信息
  • ProductMode类(商品管理类):声明商品的功能方法:商品添加、查询商品列表、查询指定id的商品、根据商品id删除商   品、根据id修改指定商品的价格、根据id修改指定商品的库存、根据商品类别查询所有商品、查询指定价格区间的商品信息
  • ManagerClient类(管理员客户端类):程序的主入口,展示管理员输入控制台界面,通过控制台输入实现各模块功能

在ProductMode类中主要用到了ArrayList来存放商品信息,是因为动态数组可以快速的添加、删除数组元素

代码如下

Manager类

public class Manager {
	
	private String id;
	private String password;
	
	//初始化管理员账号、密码
	public Manager() {
		id = "admin";
		password = "admin";
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

}
           

ManagerMode类

public class ManagerMode {
	
	Manager m = new Manager();
	
	/**管理员登录*/
	public boolean sign(String id,String password) {
		if(m.getId().equals(id) && m.getPassword().equals(password)) {
			return true;
		}
		return false;
	}
	
	/**修改管理员密码*/
	public boolean modify(String password) {
		if(m.getPassword()!= null) {
			m.setPassword(password);
			return true;
		}
		return false;
		
	}

}
           

Product类

public class Product {
	
	private int id;
	private String name;
	private String type;
	private double price;
	private int count;
	private String place;
	private String unit;
	
	public Product() {
	}
	
	public Product(int id, String name, String type, double price, int count, String place, String unit) {
		super();
		this.id = id;
		this.name = name;
		this.type = type;
		this.price = price;
		this.count = count;
		this.place = place;
		this.unit = unit;
	}


	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}

	@Override
	public String toString() {
		return  id + "\t" + name + "\t" + type + "\t" + price + "\t" + count
				+ "\t" + place + "\t" + unit ;
	}
	
}
           

ProductMode类

import java.util.ArrayList;


public class ProductMode {
	
	static ArrayList<Product> list = new ArrayList<>();
	
	/**商品添加*/
	public void add(Product p) {
		list.add(p);
	}
	
	/**查询所有商品*/
	public ArrayList<Product> findAll(){
		return list;
	}
	
	/**查询指定id的商品*/
	public Product findById(int id) {
		Product pro = null;
		for(Product p : list) {
			if(p.getId() == id) {
				pro = p;
				break;
			}
		}
		return pro;
	}
	
	/**根据商品id删除商品*/
	public boolean delete(int id){
		Product pro = findById(id);
		if(pro != null){
			return list.remove(pro);
		}
		return false;
	}
	
	/**根据id修改指定商品的价格*/
	public boolean modifyPrice(int id,double price) {
		Product pro = findById(id);
		if(pro != null) {
			pro.setPrice(price);
			return true;
		}
		return false;
	}
	
	/**根据id修改指定商品的库存*/
	public boolean modifyCount(int id,int count) {
		Product pro = findById(id);
		if(pro != null) {
			pro.setCount(count);
			return true;
		}
		return false;
	}
	
	/**根据商品类别查询所有商品*/
	public ArrayList<Product> findByType(String type){
		ArrayList<Product> pros = new ArrayList<>();
		for(Product p:list) {
			if(type.equals(p.getType())) {
				pros.add(p);
			}
		}
		return pros;
	}
	
	/**查询指定价格区间的商品信息*/
	public ArrayList<Product> findByPrices(double price1,double price2){
		ArrayList<Product> pros = new ArrayList<>();
		for(Product p: list) {
			if(p.getPrice()>=price1 && p.getPrice()<=price2) {
				pros.add(p);
			}
		}
		return pros;
	}
}
           

ManagerClient类

import java.util.ArrayList;
import java.util.Scanner;

import com.softeem.hrms.Emp;
/**
 * 
 * @author max
 *
 */
public class ManageClient {
	
	private ManagerMode mm = new ManagerMode();
	private ProductMode pm = new ProductMode();
	private Scanner sc;
	
	/**管理员登录*/
	public void signin() {
		sc = new Scanner(System.in);
                msg("欢迎使用max的电商系统");
		msg("请输入账户名:");
		String id =  sc.next();
		msg("请输入密码:");
		String password = sc.next();
		if(mm.sign(id, password)) {
			menu();
		}else {
			msg("账户不存在或密码错误");
			msg("请重新输入密码");
			signin();
		}
	}
	
	public void menu() {
		msg("=========================");
		msg("========max电商系统========");
		msg("====【1】修改密码===========");
		msg("====【2】添加商品===========");
		msg("====【3】商品列表===========");
		msg("====【4】查询商品===========");
		msg("====【5】删除商品===========");
		msg("====【6】修改商品价格========");		
		msg("====【7】修改商品库存========");		
		msg("====【8】根据类别查询商品=====");		
		msg("====【9】查询价格区间的商品====");		
		msg("====【0】退出系统===========");		
		msg("=========================");
		msg("请输入操作指令:");
		start();
	}
	
	public void start() {
		sc = new Scanner(System.in);
		int i = sc.nextInt();
		switch(i){
		case 1:
			modifyPwd();
			break;
		case 2:
			add();
			break;
		case 3:
			list();
			break;
		case 4:
			queryById();
			break;
		case 5:
			delete();
			break;
		case 6:
			updatePrice();
			break;
		case 7:
			updateCount();
			break;
		case 8:
			queryByType();
			break;
		case 9:
			queryByPrice();
			break;
		case 0:
			exit();
			break;
			default:
				msg("请输入正确的操作指令!!!");
				break;
		}
		menu();
	}
	
	/**修改管理员密码*/
	public void modifyPwd() {
		msg("请输入新密码:");
		String password = sc.next();
		if(mm.modify(password)) {
			msg("修改成功");
		}else {
			msg("修改失败");
		}
	}
	
	/**添加商品*/
	public void add() {
		msg("请输入商品信息(按以下格式:商品id/商品名/类别/单价/库存/产地/计量单位)");
		sc = new Scanner(System.in);
		String s = sc.nextLine();
		//根据“/”截取商品信息
		String[] info = s.split("/");
		if(pm.findById(Integer.parseInt(info[0])) != null){
			msg("该编号的商品已存在,请重新输入");
			add();
			return;
		}else{
			Product p = new Product(Integer.parseInt(info[0]), 
					info[1], 
					info[2], 
					Double.parseDouble(info[3]), 
					Integer.parseInt(info[4]), 
					info[5],
					info[6]	);
			pm.add(p);
			msg("添加成功");
		}
	}
	
	/**商品列表*/
	public void list() {
		msg("商品id\t商品名\t类别\t单价\t库存量\t产地\t计量单位");
		for (Product p: pm.findAll()) {
			msg(p);
		}
	}
	
	/**根据id查询商品的客户端实现*/
	public void queryById() {
		sc = new Scanner(System.in);
		msg("请输入需要查询的商品id:");
		int id = sc.nextInt();
		Product p = pm.findById(id);
		if(p == null) {
			msg("您输入的商品id不存在");
			queryById();
			return;
		}
		msg("商品id\t商品名\t类别\t单价\t库存量\t产地\t计量单位");
		msg(p);
	}
	
	/**根据商品id删除商品的客户端实现*/
	public void delete() {
		sc = new Scanner(System.in);
		msg("请输入商品的id:");
		int id = sc.nextInt();
		if(pm.delete(id)){
			msg("删除成功!");
		}else{
			msg("删除失败!");
		}
	}
	
	/**根据商品id修改价格的客户端实现*/
	public void updatePrice() {
		sc = new Scanner(System.in);
		msg("请输入修改商品的id:");
		int id = sc.nextInt();
		msg("请输入修改商品的价格:");
		double price = sc.nextDouble();
		if(pm.modifyPrice(id, price)) {
			msg("修改成功");
		}else {
			msg("修改失败");
		}
	}
	/**根据商品id修改库存的客户端实现*/
	public void updateCount() {
		sc = new Scanner(System.in);
		msg("请输入修改商品的id:");
		int id = sc.nextInt();
		msg("请输入修改商品的库存:");
		int count = sc.nextInt();
		if(pm.modifyCount(id, count)) {
			msg("修改成功");
		}else {
			msg("修改失败");
		}
	}
	
	/**根据商品类别查询所有商品的客户端实现*/
	public void queryByType() {
		sc = new Scanner(System.in);
		msg("请输入需要查询的类别名称:");
		String type = sc.next();
		ArrayList<Product> list = pm.findByType(type);
		msg("商品id\t商品名\t类别\t单价\t库存量\t产地\t计量单位");
		if(list.size()>0){
			for (Product p  : list) {
				msg(p);
			}
		}
	}
	/**查询指定价格区间的商品的客户端实现*/
	public void queryByPrice() {
		sc = new Scanner(System.in);
		msg("请输入需要查询的价格下界:");
		double price1 = sc.nextDouble();
		msg("请输入需要查询的价格上界:");
		double price2 = sc.nextDouble();
		ArrayList<Product> list = pm.findByPrices(price1,price2);
		msg("商品id\t商品名\t类别\t单价\t库存量\t产地\t计量单位");
		if(list.size()>0){
			for (Product p  : list) {
				msg(p);
			}
		}
	}
	/**系统退出*/
	public void exit() {
		sc = new Scanner(System.in);
		msg("是否确定退出?(Y/N)");
		String op = sc.next();
		if(op.equalsIgnoreCase("y")){
			msg("谢谢使用,再见!");
			System.exit(1);
		}
	}
	
	
	public void msg(Object obj) {
		System.out.println(obj);
	}
	
	public static void main(String[] args) {
		
		new ManageClient().signin();
		
	}

}
           

以上就是此简易电商系统模块功能源码

接下来看运行结果

输入正确的账户名与密码才可登录系统

输错则重新输入

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能
使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

接下来按照控制台菜单依步骤操作

1.修改密码

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

2.添加商品

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能
使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能
使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

3.商品列表

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

4.查询商品

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

5.删除商品

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能
使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

6.修改商品价格

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能
使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

7.修改商品库存

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能
使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

8.根据类别查询商品

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

9.查询价格区间的商品

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

0.退出系统

使用Java的ArrayList实现电商系统模块功能利用ArrayList实现电商系统模块功能

以上就是此简易系统的实现,希望可以帮到对java感兴趣的朋友,也希望各位评论探讨更多java知识