Write a Java program to simulate online shopping. When the user selects the item and submits the order, each order will automatically generate a unique order number.

View this chapter

View job directory


Statement of needs:

Simulating online shopping, when the user selects the item and submits an order, each order will automatically generate a unique order number. Some e-commerce websites may need to process nearly a thousand orders in one millisecond during peak data periods

Now simply simulate the scenario where 5 orders are submitted at the same time to generate a unique order number. The order number generation rule is the current time in milliseconds + the order number

For example, if the number of milliseconds in the current time is 150000, the number of the first order is 1500001, and the number of the ninth order is 1500009

Realization ideas:

Create order class Order in the project (order number, order amount, order user)

Create the order processing class ProcessOrder to implement the Runnable interface, define a member variable of the Order type and an Integer static count as the order number (initial 1), and add a method to generate the order number

In the method of generating the order number, use sleep to delay 500 milliseconds, and at the same time add a synchronization code block, specify the static variable count as the synchronization monitor, and use the system time milliseconds and the order number in the synchronization code block to generate the order number operation

Outside the synchronization code block, print out the order user, order amount and order number

Create the test startup class TestOrder. In the main() method of this class, use the parameterized constructor to instantiate 5 Order objects. Use the ProcessOrder (Order order) construction method to instantiate 5 objects of the ProcessOrder class, and pass the 5 Order objects to the parameter order of the construction method in turn. Create 5 Thread objects respectively and pass in 5 ProcessOrders to start the test

Implementation code:

Order


public class Order {
	//订单编号
	private String orderNumber;
	//订单金额
	private double orderAmount;
	//用户名称
	private String username;
	//无参构造
	public Order() {
		super();
	}
	//有参构造
	public Order(String orderNumber, double orderAmount, String username) {
		super();
		this.orderNumber = orderNumber;
		this.orderAmount = orderAmount;
		this.username = username;
	}
	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	public double getOrderAmount() {
		return orderAmount;
	}
	public void setOrderAmount(double orderAmount) {
		this.orderAmount = orderAmount;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

}

Order processing class (ProcessOrder)

 
public class ProcessOrder implements Runnable{
	private Order order;
	//订单计数器
	private static Integer count = 1;
 
	public ProcessOrder(Order order) {
		this.order = order;
	}
	
	@Override
	public void run() {
		generateOrderNumber();
	}
	
	public void generateOrderNumber() {
		try {
			//加入线程延迟
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//同步监听器是count变量
		synchronized (count) {
			//生成订单编号
			StringBuffer orderNumber = new StringBuffer("").append(System.currentTimeMillis()).append(count++);
			//将订单编号保存到订单对象中
			order.setOrderNumber(orderNumber.toString());
		}
		System.out.printf("%s的订单金额是%.2f元,订单编号是:%s\n",order.getUsername(),order.getOrderAmount(),order.getOrderNumber());
	}
	
}

Start and run the class (TestOrder)


public class TestOrder {
	public static void main(String[] args) {
		//实例化5个订单对象
		Order order1 = new Order(null,300,"张三");
		Order order2 = new Order(null,400,"李四");
		Order order3 = new Order(null,500,"王五");
		Order order4 = new Order(null,300,"赵六");
		Order order5 = new Order(null,200,"秦七");
		//实例化5个Runnable对象
		ProcessOrder processOrder1 = new ProcessOrder(order1);
		ProcessOrder processOrder2 = new ProcessOrder(order2);
		ProcessOrder processOrder3 = new ProcessOrder(order3);
		ProcessOrder processOrder4 = new ProcessOrder(order4);
		ProcessOrder processOrder5 = new ProcessOrder(order5);
		//实例化5条线程
		Thread thread1 = new Thread(processOrder1);
		Thread thread2 = new Thread(processOrder2);
		Thread thread3 = new Thread(processOrder3);
		Thread thread4 = new Thread(processOrder4);
		Thread thread5 = new Thread(processOrder5);
		//依次启动线程
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
		thread5.start();
	}
}

 

Guess you like

Origin blog.csdn.net/weixin_44893902/article/details/108815780