Java实现一个排序系统
package com.sortsystem; /** * @ClassName User * @Description 排队的人 * @Author Administrator * @Date 2019/5/30 21:10 * @Version 1.0 **/ public class User { private int Id; String name; int seq; // 排队的序号 public User(int id, String name) { Id = id; this.name = name; } public int getId() { return Id; } public void setId(int id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSeq() { return seq; } public void setSeq(int seq) { this.seq = seq; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", seq=" + seq + '}'; } }
排序系统
package com.sortsystem; import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; /** * @ClassName SortSystem * @Description TODO * @Author Administrator * @Date 2019/5/30 20:52 * @Version 1.0 **/ public class SortSystem { Queue<User> queue = new LinkedList<>(); /** * 入队 * @param user */ public void enQueue(User user) { queue.offer(user); } /** * 出队 */ public void deQueue() { if(queue.size() > 0) { queue.poll(); } return; } /** * 某个人出队列 * @param user */ public void deQueue(User user) { queue.remove(user); updateSeq(); } /** * 更新排号 */ public void updateSeq() { int i = 1; Iterator iterator = queue.iterator(); while(iterator.hasNext()) { User u = (User)iterator.next(); u.setSeq(i); i++; } } public void printList() { Iterator iterator = queue.iterator(); while(iterator.hasNext()) { User u = (User)iterator.next(); System.out.println(u); } } }
测试
package com.sortsystem; /** * @ClassName Main * @Description 排序系统的主类 * @Author Administrator * @Date 2019/5/30 21:07 * @Version 1.0 **/ public class Main { public static void main(String[] args) { SortSystem sortSystem = new SortSystem(); User u01 = new User(1, "person01"); User u02 = new User(2, "person02"); User u03 = new User(3, "person03"); User u04 = new User(4, "person04"); User u05 = new User(5, "person05"); User u06 = new User(6, "person06"); sortSystem.enQueue(u01); sortSystem.enQueue(u02); sortSystem.enQueue(u03); sortSystem.enQueue(u04); sortSystem.enQueue(u05); sortSystem.enQueue(u06); /** * 更新排序 */ sortSystem.updateSeq(); sortSystem.printList(); /** * 队列中某一个人离开 */ sortSystem.deQueue(u02); sortSystem.printList(); } }
积沙成塔