You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Customer.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. */
  13. package telecom;
  14. import java.util.Vector;
  15. /**
  16. * Customers have a unique id (name in this case for didactic purposes
  17. * but it could be telephone number) and area code.
  18. * They also have protocol for managing calls: call, pickup, etc.
  19. */
  20. public class Customer {
  21. private String name;
  22. private int areacode;
  23. private Vector calls = new Vector();
  24. /**
  25. * unregister a call
  26. */
  27. protected void removeCall(Call c){
  28. calls.removeElement(c);
  29. }
  30. /**
  31. * register a call
  32. */
  33. protected void addCall(Call c){
  34. calls.addElement(c);
  35. }
  36. /**
  37. * Make a new customer with given name
  38. */
  39. public Customer(String name, int areacode) {
  40. this.name = name;
  41. this.areacode = areacode;
  42. }
  43. /**
  44. * String rendition of customer
  45. */
  46. public String toString() {
  47. return name + "(" + areacode + ")";
  48. }
  49. /**
  50. * what area is the customer in?
  51. */
  52. public int getAreacode(){
  53. return areacode;
  54. }
  55. /**
  56. * Is the other customer in the same area?
  57. */
  58. public boolean localTo(Customer other){
  59. return areacode == other.areacode;
  60. }
  61. /**
  62. * Make a new call to receiver
  63. */
  64. public Call call(Customer receiver) {
  65. Call call = new Call(this, receiver);
  66. addCall(call);
  67. return call;
  68. }
  69. /**
  70. * pick up a call
  71. */
  72. public void pickup(Call call) {
  73. call.pickup();
  74. addCall(call);
  75. }
  76. /**
  77. * hang up a call
  78. */
  79. public void hangup(Call call) {
  80. call.hangup(this);
  81. removeCall(call);
  82. }
  83. /**
  84. * Merge a pair of calls -- conference them
  85. * PRE: call1.includes(this)
  86. * call2.includes(this)
  87. * call1.connected()
  88. * call2.connected()
  89. * POST: call1 includes all customers connected by call1@pre and call2@pre
  90. */
  91. public void merge(Call call1, Call call2){
  92. call1.merge(call2);
  93. removeCall(call2);
  94. }
  95. }