java basic commodity management system

Keywords: Java Attribute Programming Database

java basic commodity management system

This commodity management system is similar to the e-commerce system and employee management system that I wrote before. The updated functions include modifying the commodity name and price, deleting multiple specified id commodities, and exiting the system after deleting multiple commodities.

system requirements

requirement analysis

According to the above demand chart, analyze the commodity management system

Commodity attribute

  • Product number (pid)
  • Trade name (pname)
  • Unit price
  • Flag (flag)
  • Category? ID

function

  • Add merchandise
  • Modify commodity name and price
  • Delete item with specified id
  • Delete all items (multiple delete)
  • Query commodity with specified id
  • Query all items
  • Exit system

Programming analysis

According to the above requirements, create three classes

  • Product class: define product properties and constructors
  • ProductManage class: declare the function method of Product
  • ProductClient class: draw function menu, realize console input, realize different functions

The code is as follows

Class Product

/**
 * 
 * @author max
 *
 */
public class Product {

	private int pid;
	private String pname;
	private int price;
	private int flag;
	private String category_id;

	public Product() {
		// TODO Auto-generated constructor stub
	}

	public Product(int pid, String pname, int price, int flag, String category_id) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.price = price;
		this.flag = flag;
		this.category_id = category_id;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getCategory_id() {
		return category_id;
	}

	public void setCategory_id(String category_id) {
		this.category_id = category_id;
	}

}

ProductManage class

import java.util.ArrayList;
/**
 * 
 * @author max
 *
 */
public class ProductManage {

	static ArrayList<Product> list = new ArrayList<>();

	/** Commodity addition */
	public void add(Product p) {
		list.add(p);
	}

	/** Modify the price of specified goods according to id */
	public boolean modifyPrice(int id, int price) {
		Product pro = findById(id);
		if (pro != null) {
			pro.setPrice(price);
			return true;
		}
		return false;
	}

	/** Modify the commodity name of the specified commodity according to the id */
	public boolean modifyPname(int id, String pname) {
		Product pro = findById(id);
		if (pro != null) {
			pro.setPname(pname);
			return true;
		}
		return false;
	}

	/** Delete item by item id */
	public boolean delete(int id) {
		Product pro = findById(id);
		if (pro != null) {
			return list.remove(pro);
		}
		return false;
	}

	/** Query commodity with specified id */
	public Product findById(int pid) {
		Product pro = null;
		for (Product p : list) {
			if (p.getPid() == pid) {
				pro = p;
				break;
			}
		}
		return pro;
	}

	/** Query all items */
	public ArrayList<Product> findAll() {
		return list;
	}

}

ProductClient class

import java.util.ArrayList;
import java.util.Scanner;
/**
 * 
 * @author max
 *
 */
public class ProductClient {

	ProductManage pm = new ProductManage();
	private Scanner sc;
	private int index = 0;

	public void menu() {
		msg("=========================");
		msg("========Commodity management system=========");
		msg("====Enter the following command to operate=======");
		msg("====[C]Create commodity=============");
		msg("====[U]Modify trade name and price======");
		msg("====[D]Delete specified id commodity=======");
		msg("====[DA]Delete all items========");
		msg("====[I]adopt id query===========");
		msg("====[FA]Query all items=========");
		msg("====[Q]Exit system=============");
		msg("=========================");
		msg("Please enter the operation instruction:");
		start();
	}

	public void start() {
		sc = new Scanner(System.in);
		String i = sc.next().toUpperCase();
		switch (i) {
		case "C":
			add();
			break;
		case "U":
			updatePrice();
			break;
		case "D":
			delete();
			break;
		case "DA":
			deleteAll();
			break;
		case "I":
			findById();
			break;
		case "FA":
			findAll();
			break;
		case "Q":
			exit();
			break;
		default:
			msg("Please input correct operation instruction!!!");
			break;
		}
		menu();
	}

	/** Add merchandise */
	public void add() {
		msg("Please enter product information(In the following format:Commodity number/Trade name/Unit Price/sign/Category number)");
		sc = new Scanner(System.in);
		String s = sc.nextLine();
		String[] info = s.split("/");
		if (pm.findById(Integer.parseInt(info[0])) != null) {
			msg("The item No. already exists, please re-enter");
			add();
			return;
		} else {
			Product p = new Product(Integer.parseInt(info[0]), info[1], Integer.parseInt(info[2]),
					Integer.parseInt(info[3]), info[4]);
			pm.add(p);
			msg("Add success");
		}
	}

	/** Query all */
	public void findAll() {
		msg("Commodity number\t Trade name\t Unit Price\t sign\t Category number");
		for (Product p : pm.findAll()) {
			msg("Product" + "[pid=" + p.getPid() + " pname=" + p.getPname() + " price" + p.getPrice() + " flag="
					+ p.getFlag() + " category_id=" + p.getCategory_id() + "]");
		}
	}

	/** The client implementation of querying goods according to id */
	public void findById() {
		sc = new Scanner(System.in);
		msg("Please enter the commodity number to query:");
		int id = sc.nextInt();
		Product p = pm.findById(id);
		if (p == null) {
			msg("The product number you entered does not exist");
			findById();
			return;
		}
		msg("Product" + "[pid=" + p.getPid() + " pname=" + p.getPname() + " price" + p.getPrice() + " flag="
				+ p.getFlag() + " category_id=" + p.getCategory_id() + "]");
	}

	/** Client implementation of deleting goods according to commodity id */
	public void delete() {
		sc = new Scanner(System.in);
		msg("Please enter the number of the deleted item:");
		int id = sc.nextInt();
		if (pm.delete(id)) {
			msg("Delete successfully!");
		} else {
			msg("Delete failed!");
		}
	}

	/** Delete all items */
	public void deleteAll() {
		sc = new Scanner(System.in);
		msg("Please enter the deletion item number:");
		int id = sc.nextInt();
		Product p = pm.findById(id);
		if (id == -1) {
			msg("Are you sure you want to delete the marked"+index+"A commodity??Please input y:");
			if(sc.next().toLowerCase().equals("y")) {
				msg("Delete successful");
				System.exit(1);
			}
			
		}
		if (pm.delete(id)) {
			msg("Items marked for deletion"+"Product" + "[pid=" + p.getPid() + " pname=" + p.getPname() + " price" + p.getPrice() + " flag="
					+ p.getFlag() + " category_id=" + p.getCategory_id() + "]");
		} else {
			msg("Delete failed!");
		}
		index++;
		// recursion
		deleteAll();
	}

	/** Client implementation of price modification based on commodity id */
	public void updatePrice() {
		sc = new Scanner(System.in);
		msg("Please enter the number of the edit item:");
		int id = sc.nextInt();
		Product pro = pm.findById(id);
		if (pro == null) {
			msg("Modified item no longer exists");
			menu();
		} else {
			msg("Product" + "[pid=" + pro.getPid() + " pname=" + pro.getPname() + " price" + pro.getPrice() + " flag="
					+ pro.getFlag() + " category_id=" + pro.getCategory_id() + "]");
		}
		msg("Please enter the modified trade name:");
		String pname = sc.next();
		msg("Please enter the price of the edit item:");
		int price = sc.nextInt();
		if (pm.modifyPrice(id, price) && pm.modifyPname(id, pname)) {
			msg("Modified success");
		} else {
			msg("Modification failed");
		}
	}

	/** System exit */
	public void exit() {
		sc = new Scanner(System.in);
		msg("Are you sure you want to exit?(Y/N)");
		String op = sc.next();
		if (op.equalsIgnoreCase("y")) {
			msg("Thank you for using.,Bye!");
			System.exit(1);
		}
	}

	public void msg(Object obj) {
		System.out.println(obj);
	}

	public static void main(String[] args) {

		new ProductClient().menu();

	}

}

The function menu is simple and beautified, but the connection to database needs to be further realized.

Posted by iblackedout on Wed, 16 Oct 2019 12:06:28 -0700