据说7k的Java面试题_银行业务调度系统

模拟实现银行业务调度系统逻辑,具体需求如下:
  • 银行内有6个业务窗口,1- 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
  • 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
  • 异步随机生成各种类型的客户,生成各类型用户的概率比例为:VIP客户 :普通客户 :快速客户 =  1 :6 :3。
  • 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,
    快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
  • 各类型客户在其对应窗口按顺序依次办理业务。
  • 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,
    而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
  • 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
银行业务调度系统需求源之于: http://blog.csdn.net/zhangxiaoxiang/archive/2011/04/01/6294132.aspx
前几天看到这上面那篇文章,就有点惊讶,那种题目做出来就7k,我的工资都还没有7k的一半呢?就用C#简单的实现看看,2/3小时就做好了。
以下是具体的代码实现:
View Code     
     
       
1 public abstract class CAbstractCustomer
2 {
3 public virtual ECustomerType CustomerType
4 {
5 get ;
6 private set ;
7 }
8
9 public string Name
10 {
11 get ;
12 set ;
13 }
14 }
抽象的客户类,包含两个属性:客户类型,名称。
客户类型枚举:
View Code
    
      
1 public enum ECustomerType
2 {
3 VIPCustomer,
4 GeneralCustomer,
5 FastCustomer
6 }
7
8 public class GetCustomerType
9 {
10 public static string GetName(ECustomerType type)
11 {
12 switch (type)
13 {
14 case ECustomerType.FastCustomer:
15 return " 快速客户 " ;
16 case ECustomerType.GeneralCustomer:
17 return " 普通客户 " ;
18 case ECustomerType.VIPCustomer:
19 return " VIP客户 " ;
20 }
21 throw new Exception( " 不存在该客户类型! " );
22 }
23 }
 VIP客户: 
View Code
    
      
1 public class CVIPCustomer : CAbstractCustomer
2 {
3 public CVIPCustomer( string name)
4 {
5 base .Name = name;
6 }
7
8 public override ECustomerType CustomerType
9 {
10 get
11 {
12 return ECustomerType.VIPCustomer;
13 }
14 }
15 }
 快速客户:
View Code
    
      
1 public class CFastCustomer : CAbstractCustomer
2 {
3 public CFastCustomer( string name)
4 {
5 this .Name = name;
6 }
7
8 public override ECustomerType CustomerType
9 {
10 get
11 {
12 return ECustomerType.FastCustomer;
13 }
14 }
15 }
普通客户:
View Code
    
      
1 public class CGeneralCustomer : CAbstractCustomer
2 {
3 public CGeneralCustomer( string name)
4 {
5 this .Name = name;
6 }
7
8 public override ECustomerType CustomerType
9 {
10 get
11 {
12 return ECustomerType.GeneralCustomer;
13 }
14 }
15 }
客户生成类:用于动态产生客户
View Code
    
      
1 public class CCustomerBuilder:IDisposable
2 {
3 private CCustomerBuilder()
4 {
5 }
6 private static readonly object obj = new object ();
7 private static CCustomerBuilder instance = null ;
8 public static CCustomerBuilder GetInstance
9 {
10 get
11 {
12 if (instance == null )
13 {
14 lock (obj)
15 {
16 if (instance == null )
17 {
18 instance = new CCustomerBuilder();
19 }
20 }
21 }
22 return instance;
23 }
24 }
25 private const double BuildTime = 2000 ;
26 // VIP客户:普通客户:快速客户 = 1 :6 :3。转化为普通客户每2秒生成一个,VIP客户12秒生成一个,快速客户4秒生成一个
27 private static Timer VIPTimer = new Timer(BuildTime * 6.0 );
28 private static Timer GeneralTimer = new Timer(BuildTime);
29 private static Timer FastTimer = new Timer(BuildTime * 2.0 );
30 private static int generalCount = 0 ;
31 private static int fastCount = 0 ;
32 private static int vipCount = 0 ;
33 static CCustomerBuilder()
34 {
35 FastTimer.Elapsed += new ElapsedEventHandler(FastTimer_Elapsed);
36 VIPTimer.Elapsed += new ElapsedEventHandler(VIPTimer_Elapsed);
37 GeneralTimer.Elapsed += new ElapsedEventHandler(GeneralTimer_Elapsed);
38 }
39 public static string GetGeneralNextNum()
40 {
41 generalCount = ++ generalCount;
42 return generalCount.ToString();
43 }
44 public static string GetFastNextNum()
45 {
46 fastCount = ++ fastCount;
47 return fastCount.ToString();
48 }
49 public static string GetVipNextNum()
50 {
51 vipCount = ++ vipCount;
52 return vipCount.ToString();
53 }
54 static void GeneralTimer_Elapsed( object sender, ElapsedEventArgs e)
55 {
56 CGeneralCustomerDispatcher.GetInstance.GeneralCustomerEnqueue( new CGeneralCustomer(GetCustomerType.GetName(ECustomerType.GeneralCustomer) + GetGeneralNextNum()));
57 }
58
59 static void VIPTimer_Elapsed( object sender, ElapsedEventArgs e)
60 {
61 CVIPCustomerDispatcher.GetInstance.VIPCustomerEnqueue( new CVIPCustomer(GetCustomerType.GetName(ECustomerType.VIPCustomer) + GetVipNextNum()));
62 }
63
64 static void FastTimer_Elapsed( object sender, ElapsedEventArgs e)
65 {
66 CFastCustomerDispatcher.GetInstance.FastCustomerEnqueue( new CFastCustomer(GetCustomerType.GetName(ECustomerType.FastCustomer) + GetFastNextNum()));
67 }
68
69 public void TimerStart()
70 {
71 FastTimer.Start();
72 VIPTimer.Start();
73 GeneralTimer.Start();
74 }
75 public void TimerStop()
76 {
77 FastTimer.Stop();
78 VIPTimer.Stop();
79 GeneralTimer.Stop();
80 }
81
82 #region IDisposable 成员
83
84 public void Dispose()
85 {
86 this .TimerStop();
87 if (FastTimer != null )
88 {
89 FastTimer.Dispose();
90 }
91 if (VIPTimer != null )
92 {
93 VIPTimer.Dispose();
94 }
95 if (GeneralTimer != null )
96 {
97 GeneralTimer.Dispose();
98 }
99 }
100
101 #endregion
102 }
不同类型的客户队列调度类:
View Code
    
      
1 /// <summary>
2 /// 快速客户队列
3 /// </summary>
4 public class CFastCustomerDispatcher : IDispatcher,IDisposable
5 {
6 private CFastCustomerDispatcher()
7 { }
8 ~ CFastCustomerDispatcher()
9 {
10 this .Dispose();
11 }
12 private static readonly object lockObj = new object ();
13 private static CFastCustomerDispatcher dispatcher = null ;
14 public static CFastCustomerDispatcher GetInstance
15 {
16 get
17 {
18 if (dispatcher == null )
19 {
20 lock (lockObj)
21 {
22 if (dispatcher == null )
23 {
24 dispatcher = new CFastCustomerDispatcher();
25 }
26 }
27 }
28 return dispatcher;
29 }
30 }
31 private static Queue < CAbstractCustomer > fastList = null ;
32 static CFastCustomerDispatcher()
33 {
34 fastList = new Queue < CAbstractCustomer > ();
35 }
36 [DebuggerStepThrough]
37 public void FastCustomerEnqueue(CAbstractCustomer customer)
38 {
39 lock (lockObj)
40 {
41 fastList.Enqueue(customer);
42 }
43 }
44 [DebuggerStepThrough]
45 public CAbstractCustomer FastCustomerDequeue()
46 {
47 lock (lockObj)
48 {
49 if (fastList.Count > 0 )
50 {
51 return fastList.Dequeue();
52 }
53 }
54 return null ;
55 }
56
57 public int Count
58 {
59 get
60 {
61 return fastList.Count;
62 }
63 }
64
65 #region IDisposable 成员
66
67 public void Dispose()
68 {
69 dispatcher = null ;
70 if (fastList != null )
71 {
72 fastList.Clear();
73 fastList = null ;
74 }
75 }
76
77 #endregion
78 }
View Code
    
      
1 /// <summary>
2 /// vip客户队列
3 /// </summary>
4 public class CVIPCustomerDispatcher : IDispatcher,IDisposable
5 {
6 private CVIPCustomerDispatcher()
7 { }
8 ~ CVIPCustomerDispatcher()
9 {
10 this .Dispose();
11 }
12 private static readonly object lockObj = new object ();
13 private static CVIPCustomerDispatcher dispatcher = null ;
14
15 public static CVIPCustomerDispatcher GetInstance
16 {
17 get
18 {
19 if (dispatcher == null )
20 {
21 lock (lockObj)
22 {
23 if (dispatcher == null )
24 {
25 dispatcher = new CVIPCustomerDispatcher();
26 }
27 }
28 }
29 return dispatcher;
30 }
31 }
32
33 private static Queue < CAbstractCustomer > vipList = null ;
34 static CVIPCustomerDispatcher()
35 {
36 vipList = new Queue < CAbstractCustomer > ();
37 }
38 [DebuggerStepThrough]
39 public void VIPCustomerEnqueue(CAbstractCustomer customer)
40 {
41 lock (lockObj)
42 {
43 vipList.Enqueue(customer);
44 }
45 }
46 [DebuggerStepThrough]
47 public CAbstractCustomer VIPCustomerDequeue()
48 {
49 lock (lockObj)
50 {
51 if (vipList.Count > 0 )
52 {
53 return vipList.Dequeue();
54 }
55 }
56 return null ;
57 }
58
59 public int Count
60 {
61 get
62 {
63 return vipList.Count;
64 }
65 }
66
67 #region IDisposable 成员
68
69 public void Dispose()
70 {
71 dispatcher = null ;
72 if (vipList != null )
73 {
74 vipList.Clear();
75 vipList = null ;
76 }
77 }
78
79 #endregion
80 }
View Code
    
      
1 /// <summary>
2 /// 普通客户队列
3 /// </summary>
4 public class CGeneralCustomerDispatcher : IDispatcher,IDisposable
5 {
6 private CGeneralCustomerDispatcher()
7 { }
8 ~ CGeneralCustomerDispatcher()
9 {
10 this .Dispose();
11 }
12 private static readonly object lockObj = new object ();
13 private static CGeneralCustomerDispatcher dispatcher = null ;
14 public static CGeneralCustomerDispatcher GetInstance
15 {
16 get
17 {
18 if (dispatcher == null )
19 {
20 lock (lockObj)
21 {
22 if (dispatcher == null )
23 {
24 dispatcher = new CGeneralCustomerDispatcher();
25 }
26 }
27 }
28 return dispatcher;
29 }
30 }
31
32 private static Queue < CAbstractCustomer > generalList = null ;
33 static CGeneralCustomerDispatcher()
34 {
35 generalList = new Queue < CAbstractCustomer > ();
36 }
37 [DebuggerStepThrough]
38 public void GeneralCustomerEnqueue(CAbstractCustomer customer)
39 {
40 lock (lockObj)
41 {
42 generalList.Enqueue(customer);
43 }
44 }
45 [DebuggerStepThrough]
46 public CAbstractCustomer GeneralCustomerDequeue()
47 {
48 lock (lockObj)
49 {
50 if (generalList.Count > 0 )
51 {
52 return generalList.Dequeue();
53 }
54 }
55 return null ;
56 }
57
58 public int Count
59 {
60 get
61 {
62 return generalList.Count;
63 }
64 }
65
66 #region IDisposable 成员
67
68 public void Dispose()
69 {
70 dispatcher = null ;
71 if (generalList != null )
72 {
73 generalList.Clear();
74 generalList = null ;
75 }
76 }
77
78 #endregion
79 }
服务窗口类:
View Code
    
      
1 public class CServiceWindow
2 {
3 private const int MIN_SERVICE_TIME = 10000 ;
4 private const int MAX_SERVICE_TIME = 60000 ;
5 public string ID
6 {
7 get ;
8 set ;
9 }
10
11 public EWindowType WindowType
12 {
13 get ;
14 set ;
15 }
16
17 public void StartDoWork()
18 {
19 DateTime now = DateTime.Now;
20 do
21 {
22
23 switch (WindowType)
24 {
25 case EWindowType.GeneralWindow:
26 ProcessGeneralCustomer(EWindowType.GeneralWindow);
27 break ;
28
29 case EWindowType.VIPWindow:
30 ProcessVIPCustomer();
31 break ;
32
33 case EWindowType.FastWindow:
34 ProcessFastCustomer();
35 break ;
36 }
37
38 if (DateTime.Now.Subtract(now).Minutes > 5 ) // 执行5分钟后退出业务,银行关门
39 {
40 CCustomerBuilder.GetInstance.Dispose();
41 Console.WriteLine( " 银行关门时间到了, " + this .ID + GetWindowType.GetName(WindowType) + " 停止服务! " );
42 break ;
43 }
44 }
45 while ( true );
46 }
47
48 /// <summary>
49 /// 快速客户办理业务所需时间为最小值 VIP和普通客户在最大值和最小值之间随机取值
50 /// </summary>
51 private void ProcessFastCustomer()
52 {
53 if (CFastCustomerDispatcher.GetInstance.Count == 0 ) // 快速客户为空可以处理普通客户
54 {
55 this .ProcessGeneralCustomer(EWindowType.FastWindow);
56 }
57 else
58 {
59 Random r = new Random();
60 CFastCustomer customer = CFastCustomerDispatcher.GetInstance.FastCustomerDequeue() as CFastCustomer;
61 if (customer != null )
62 {
63 string winName = GetWindowType.GetName(EWindowType.FastWindow);
64 Console.BackgroundColor = ConsoleColor.Blue;
65 Console.WriteLine( this .ID + " " + winName + " 呼叫客户: " + customer.Name);
66 Console.BackgroundColor = ConsoleColor.Blue;
67 Console.WriteLine( this .ID + " " + winName + " 正在办理,请稍候......! " );
68 int time = r.Next(MIN_SERVICE_TIME);
69 System.Threading.Thread.Sleep(time);
70 Console.BackgroundColor = ConsoleColor.Blue;
71 Console.WriteLine( this .ID + " " + winName + " " + customer.Name + " 办理完成!\n " );
72 }
73 else
74 {
75 System.Threading.Thread.Sleep( 2000 ); // 当前队列为空,休息2秒在叫号
76 }
77 }
78 }
79
80 private void ProcessVIPCustomer()
81 {
82 if (CVIPCustomerDispatcher.GetInstance.Count == 0 ) // vip客户为空可以处理普通客户
83 {
84 this .ProcessGeneralCustomer(EWindowType.VIPWindow);
85 }
86 else
87 {
88 Random r = new Random();
89 CVIPCustomer customer = CVIPCustomerDispatcher.GetInstance.VIPCustomerDequeue() as CVIPCustomer;
90 if (customer != null )
91 {
92 string winName = GetWindowType.GetName(EWindowType.VIPWindow);
93 Console.BackgroundColor = ConsoleColor.White;
94 Console.WriteLine( this .ID + " " + winName + " 呼叫客户: " + customer.Name);
95 Console.BackgroundColor = ConsoleColor.White;
96 Console.WriteLine( this .ID + " " + winName + " 正在办理,请稍候......! " );
97 int time = r.Next(MIN_SERVICE_TIME, MAX_SERVICE_TIME);
98 System.Threading.Thread.Sleep(time);
99 Console.BackgroundColor = ConsoleColor.White;
100 Console.WriteLine( this .ID + " " + winName + " " + customer.Name + " 办理完成!\n " );
101 }
102 else
103 {
104 System.Threading.Thread.Sleep( 2000 ); // 当前队列为空,休息2秒在叫号
105 }
106 }
107 }
108
109 private void ProcessGeneralCustomer(EWindowType type)
110 {
111 Random r = new Random();
112 CGeneralCustomer customer = CGeneralCustomerDispatcher.GetInstance.GeneralCustomerDequeue() as CGeneralCustomer;
113 if (customer != null )
114 {
115 string winName = GetWindowType.GetName(type);
116 Console.BackgroundColor = ConsoleColor.DarkRed;
117 Console.WriteLine( this .ID + " " + winName + " 呼叫客户: " + customer.Name);
118 Console.BackgroundColor = ConsoleColor.DarkRed;
119 Console.WriteLine( this .ID + " " + winName + " 正在办理!请稍候..! " );
120 int time = r.Next(MIN_SERVICE_TIME, MAX_SERVICE_TIME);
121 System.Threading.Thread.Sleep(time);
122 Console.BackgroundColor = ConsoleColor.DarkRed;
123 Console.WriteLine( this .ID + " " + winName + " " + customer.Name + " 办理完成!\n " );
124 }
125 else
126 {
127 System.Threading.Thread.Sleep( 2000 ); // 当前队列为空,休息2秒在叫号
128 }
129 }
130 }
窗口类别类:
View Code
    
      
1 public enum ECustomerType
2 {
3 VIPCustomer,
4 GeneralCustomer,
5 FastCustomer
6 }
7
8 public class GetCustomerType
9 {
10 public static string GetName(ECustomerType type)
11 {
12 switch (type)
13 {
14 case ECustomerType.FastCustomer:
15 return " 快速客户 " ;
16 case ECustomerType.GeneralCustomer:
17 return " 普通客户 " ;
18 case ECustomerType.VIPCustomer:
19 return " VIP客户 " ;
20 }
21 throw new Exception( " 不存在该客户类型! " );
22 }
23 }
主函数:
View Code
    
      
1 static void Main( string [] args)
2 {
3 CCustomerBuilder.GetInstance.TimerStart();
4 Thread t1 = new Thread(() =>
5 {
6 CServiceWindow window = new CServiceWindow();
7 window.WindowType = EWindowType.GeneralWindow;
8 window.ID = " 1 " ;
9 window.StartDoWork();
10 });
11 t1.Start();
12
13 Thread t2 = new Thread(() =>
14 {
15 CServiceWindow window = new CServiceWindow();
16 window.WindowType = EWindowType.GeneralWindow;
17 window.ID = " 2 " ;
18 window.StartDoWork();
19 });
20 t2.Start();
21
22 Thread t3 = new Thread(() =>
23 {
24 CServiceWindow window = new CServiceWindow();
25 window.WindowType = EWindowType.GeneralWindow;
26 window.ID = " 3 " ;
27 window.StartDoWork();
28 });
29 t3.Start();
30
31 Thread t4 = new Thread(() =>
32 {
33 CServiceWindow window = new CServiceWindow();
34 window.WindowType = EWindowType.GeneralWindow;
35 window.ID = " 4 " ;
36 window.StartDoWork();
37 });
38 t4.Start();
39
40 Thread vipThread = new Thread(() =>
41 {
42 CServiceWindow vipWindow = new CServiceWindow();
43 vipWindow.WindowType = EWindowType.VIPWindow;
44 vipWindow.ID = " 5 " ;
45 vipWindow.StartDoWork();
46 });
47 vipThread.Start();
48
49 Thread fastThread = new Thread(() =>
50 {
51 CServiceWindow fastWindow = new CServiceWindow();
52 fastWindow.WindowType = EWindowType.FastWindow;
53 fastWindow.ID = " 6 " ;
54 fastWindow.StartDoWork();
55 });
56 fastThread.Start();
57
58 Console.ReadLine();
59 }

你可能感兴趣的:(Java面试题)