IBM笔试 银行排队

§ 银行有 4 个服务窗口,其中有三个是普通窗口,一个是 VIP 窗口。 VIP 窗口在没有 VIP 用户时也可以当作普通服务窗口办理业务。银行的服务流程描述如下:
Ø 首先到达的客户需要在门口领取一张号码纸,号码纸上写明了前面排队的人数和你的号码。
Ø 客户等待自己被服务。如果号码纸的的号码过期,即当前的号码大于号码纸的号码,则号码纸就过期了
Ø 如果 VIP 用户到达后, VIP 用户进入 VIP 窗口办理,如果 VIP 窗口前面有其他的 VIP 用户,则排在其他 VIP 用户之后,如果有普通会员在办理,则服务完成后立刻服务 VIP 用户。
Ø 银行的业务员在服务完一个客户后,会根据号码纸依次通知下一个客户进行服务
 
源码:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class BankQueueThread {
 private static List<CustomerInfo> queue = Collections
   .synchronizedList(new ArrayList<CustomerInfo>());
 private static List<String> vipqueue = Collections
   .synchronizedList(new ArrayList<String>());
 private static int num = 0;
 private static int rnum = 0;
 private SimpleDateFormat datef = new SimpleDateFormat("HH:mm");

 public static void main(String args[]) {
  BankQueueThread bankQueueThread = new BankQueueThread();
  AddUser adduser = bankQueueThread.new AddUser();
  Thread th = new Thread(adduser);
  th.start();
  for (int i = 0; i < 3; i++) {
   OrderNormalQueue orn = bankQueueThread.new OrderNormalQueue();
   th = new Thread(orn);
   th.start();
  }
  OrderVIPQueue orv = bankQueueThread.new OrderVIPQueue();
  th = new Thread(orv);
  th.start();
 }

 // 增加普通用户排队
 public synchronized void addUser(int nums) {
  String s = "" + (++num);
  CustomerInfo customerInfo = new CustomerInfo();
  customerInfo.setType("普通");
  customerInfo.setId(s);
  customerInfo.setFtime(nums);
  customerInfo.setStime(datef.format(new Date()));
  queue.add(customerInfo);
 }

 // 增加VIP排队
 public synchronized void addVipUser(int nums) {
  String s = "" + (++num);
  CustomerInfo customerInfo = new CustomerInfo();
  customerInfo.setType("VIP");
  customerInfo.setId(s);
  customerInfo.setFtime(nums);
  customerInfo.setStime(datef.format(new Date()));
  queue.add(customerInfo);
  vipqueue.add(s);
 }

 // 普通窗口假设处理业务
 public synchronized void removieUser() {
  CustomerInfo cus = null;
  if (queue.size() > 0) {
   rnum++;
   cus = (CustomerInfo) queue.remove(0);
   if (vipqueue.size() > 0 && vipqueue.get(0).equals(cus.getId())) {
    vipqueue.remove(0);
    System.out.println(Thread.currentThread().getName() + ":>> "
      + rnum + " " + cus.getId() + " " + cus.getType() + " "
      + cus.getStime() + " " + cus.getFtime() + " <<办理业务完毕");
   } else {
    System.out.println(Thread.currentThread().getName() + ":>> "
      + rnum + " " + cus.getId() + " " + cus.getType() + " "
      + cus.getStime() + " " + cus.getFtime() + " <<办理业务完毕");
   }
  }
 }

 // VIP窗口假设处理业务
 public synchronized void removieVipUser() {
  String d = "";
  CustomerInfo cus = null;
  if (vipqueue.size() > 0) {
   rnum++;
   d = vipqueue.remove(0);
   for (int i = 0; i < queue.size(); i++) {
    cus = (CustomerInfo) queue.get(i);
    if (cus.getId().equals(d)) {
     queue.remove(i);
     System.out.println(Thread.currentThread().getName()
       + ":>> " + rnum + " " + cus.getId() + " "
       + cus.getType() + " " + cus.getStime() + " "
       + cus.getFtime() + " <<办理业务完毕");
     break;
    }
   }
  } else if (queue.size() > 0) {
   rnum++;
   cus = (CustomerInfo) queue.remove(0);
   System.out.println(Thread.currentThread().getName() + ":>> " + rnum
     + " " + cus.getId() + " " + cus.getType() + " "
     + cus.getStime() + " " + cus.getFtime() + " <<办理业务完毕");
  }
 }

 // 用户排队
 class AddUser implements Runnable {
  Random rd = new Random();
  int sd = 0;

  public void run() {
   // TODO Auto-generated method stub
   while (true) {
    sd = rd.nextInt(10);
    if (sd == 6) {
     addVipUser(sd);
    } else {
     addUser(sd);
    }
    if (queue.size() >= 100) {
     try {
      Thread.sleep(5000);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }
 }

 // 普通窗口业务受理
 class OrderNormalQueue implements Runnable {
  public void run() {
   // TODO Auto-generated method stub
   while (true) {
    removieUser();
    try {
     Thread.currentThread().sleep(1000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }

 // VIP窗口业务受理
 class OrderVIPQueue implements Runnable {
  public void run() {
   // TODO Auto-generated method stub
   while (true) {
    removieVipUser();
    try {
     Thread.currentThread().sleep(1000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }

 class CustomerInfo {
  private String stime;
  private String id;
  private String type;
  private int ftime;

  public String getStime() {
   return stime;
  }

  public void setStime(String stime) {
   this.stime = stime;
  }

  public String getId() {
   return id;
  }

  public void setId(String id) {
   this.id = id;
  }

  public String getType() {
   return type;
  }

  public void setType(String type) {
   this.type = type;
  }

  public int getFtime() {
   return ftime;
  }

  public void setFtime(int ftime) {
   this.ftime = ftime;
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值