home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vismalta.zip / BOOKEX / AUCTION.APP
Text File  |  1994-02-24  |  34KB  |  1,283 lines

  1.  
  2.  Application create: #AuctionDataAccess with:
  3.          (#( Kernel)
  4.              collect: [:each | Smalltalk at: each ifAbsent: [
  5.                  self error: 'Not all of the prerequisites are loaded']])!
  6.  
  7.  AuctionDataAccess becomeDefault!
  8.  
  9.  Object subclass: #Address
  10.      instanceVariableNames: 'street postalLocation '
  11.      classVariableNames: ''
  12.      poolDictionaries: ''
  13.  !
  14.  
  15.  Address comment: 'Keeps track of an address; mainly street and PostalLocation. PostalLocation is a separate
  16.  class since it is more efficient to manage city and state with one instance of PostalLocation than
  17.  require every address to include city and state as well as post code. However, it is a design
  18.  choice to have Address still return the city and state since this then isolates other classes
  19.  from having to look to post code for this information.'!
  20.  
  21.  Object subclass: #Person
  22.      instanceVariableNames: 'name address '
  23.      classVariableNames: 'SocialSecurityNumbers '
  24.      poolDictionaries: ''
  25.  !
  26.  
  27.  Person comment: 'Keeps track of general information about people. This is mainly name and address. A
  28.  person can be an auctioneer, a bidder, a seller, or any combination of these. To avoid
  29.  creating duplicate instances for the same person, every person must have a social security
  30.  number specified.
  31.  Keeps track of all instances of Person, indexed by social security number.
  32.  '!
  33.  
  34.  Object subclass: #PostalLocation
  35.      instanceVariableNames: 'city state postCode '
  36.      classVariableNames: 'PostalLocations '
  37.      poolDictionaries: ''
  38.  !
  39.  
  40.  PostalLocation comment: 'Manages the post code, state, and city combination. This class returns the state and
  41.  city for a particular post code. A fast indexing between post code and postal location
  42.  information is provided.
  43.  
  44.  '!
  45.  
  46.  Application subclass: #AuctionDataAccess
  47.      instanceVariableNames: ''
  48.      classVariableNames: ''
  49.      poolDictionaries: ''
  50.  !
  51.  
  52.  
  53.  !AuctionDataAccess class publicMethods !
  54.  
  55.  loaded
  56.          "Set up the PostalLocations."
  57.  
  58.      PostalLocation
  59.          addPostCode: '27511' forCity: 'Cary' forState: 'NC';
  60.          addPostCode: '78746' forCity: 'Austin' forState: 'TX';
  61.          addPostCode: '49255' forCity: 'Montgomery' forState: 'MI'.!
  62.  
  63.  removing
  64.          "Remove the PostalLocation instances."
  65.  
  66.      PostalLocation flushInstances.
  67.  ! !
  68.  
  69.  
  70.  !Address class publicMethods !
  71.  
  72.  street: aString postCode: aPostCodeString
  73.          "Returned an initialized instance of the receiver."
  74.  
  75.      ^super new
  76.          street: aString;
  77.          postalLocation: (PostalLocation postalLocationFor: aPostCodeString);
  78.          yourself! !
  79.  
  80.  
  81.  !Address publicMethods !
  82.  
  83.  city
  84.          "Return the city for the receiver."
  85.  
  86.      ^self postalLocation city!
  87.  
  88.  postalLocation
  89.          "Return the postalLocation for the receiver."
  90.  
  91.      ^postalLocation!
  92.  
  93.  postalLocation: aPostalLocation
  94.          "Set the postalLocation for the receiver."
  95.  
  96.      postalLocation := aPostalLocation!
  97.  
  98.  postCode
  99.          "Return the post code for the receiver."
  100.  
  101.      ^self postalLocation postCode!
  102.  
  103.  postCode: aString
  104.          "Set the post code for the receiver."
  105.  
  106.      self postalLocation postCode: aString!
  107.  
  108.  state
  109.          "Return the state for the receiver."
  110.  
  111.      ^self postalLocation state!
  112.  
  113.  street
  114.          "Return the street for the receiver."
  115.  
  116.      ^street!
  117.  
  118.  street: aStreet
  119.          "Set the street for the receiver."
  120.  
  121.      street := aStreet! !
  122.  
  123.  
  124.  !Person class publicMethods !
  125.  
  126.  addPerson: aPerson withSocialSecurityNumber: socialSecurityNumber
  127.          "Add a new social security number for the specified person.
  128.           If the socialSecurityNumber already exists, do nothing.
  129.           Parameter expected:
  130.              socialSecurityNumber : a number."
  131.  
  132.      self socialSecurityNumbers at: socialSecurityNumber ifAbsentPut: [aPerson]
  133.  !
  134.  
  135.  personFor: socialSecurityNumber
  136.          "Return the person with this socialSecurityNumber.
  137.           Return the Person if socialSecurityNumber is found.
  138.           Return nil if socialSecurityNumber is not found.
  139.           Parameter expected:
  140.              socialSecurityNumber : a number."
  141.  
  142.      ^self socialSecurityNumbers at: socialSecurityNumber ifAbsent: [nil]
  143.  !
  144.  
  145.  removePersonWith: socialSecurityNumber
  146.          "Remove the person for this social security number.
  147.           Return the removed person.
  148.           Return nil if this socialSecurityNumber is not found.
  149.           Parameter expected:
  150.              socialSecurityNumber : a number."
  151.  
  152.      ^self socialSecurityNumbers removeKey: socialSecurityNumber ifAbsent: [nil]
  153.  !
  154.  
  155.  socialSecurityNumberFor: aPerson
  156.          "Return the social security number for this person.
  157.           Return nil the person does not have a social security number."
  158.  
  159.      ^self socialSecurityNumbers keyAtValue: aPerson ifAbsent: [nil]
  160.  !
  161.  
  162.  socialSecurityNumbers
  163.          "Return the social security numbers.
  164.           If SocialSecurityNumbers is nil, set it to a new dictionary."
  165.  
  166.      SocialSecurityNumbers isNil ifTrue: [SocialSecurityNumbers := Dictionary new].
  167.      ^SocialSecurityNumbers! !
  168.  
  169.  
  170.  !Person publicMethods !
  171.  
  172.  address
  173.          "Return the address for the receiver."
  174.  
  175.      ^address!
  176.  
  177.  address: anAddress
  178.          "Set the address for the receiver."
  179.  
  180.      address := anAddress!
  181.  
  182.  name
  183.          "Return the name for the receiver."
  184.  
  185.      ^name!
  186.  
  187.  name: aString
  188.          "Set the name for the receiver."
  189.  
  190.      name := aString!
  191.  
  192.  socialSecurityNumber
  193.          "Return the social security number for the receiver. This information
  194.           is found by querying the SocialSecurityNumbers dictionary.
  195.           The value is an instance of Person and the key is the person's
  196.           social security number."
  197.  
  198.      ^self class socialSecurityNumbers keyAtValue: self! !
  199.  
  200.  
  201.  !PostalLocation class publicMethods !
  202.  
  203.  addPostCode: postCode forCity: city forState: state
  204.          "Create a new PostalLocation and add it to the list.
  205.           Parameters expected:
  206.                  postCode : a String
  207.                  city : a String
  208.                  state : a String.
  209.           Return the new PostalLocation."
  210.  
  211.      ^self postalLocations add:
  212.          (PostalLocation new
  213.                  city: city;
  214.                  state: state;
  215.                  postCode: postCode;
  216.                  yourself)!
  217.  
  218.  postalLocationFor: postCode
  219.          "Return a PostalLocation for the specified postCode.
  220.           If postCode is invalid, return a new PostalLocation.
  221.           Parameters expected:
  222.                  postCode : a String."
  223.  
  224.      ^self postalLocations at: postCode ifAbsent: [PostalLocation new]!
  225.  
  226.  postalLocations
  227.          "Return the postal locations.
  228.           If nil, initialize to a new dictionary."
  229.  
  230.      PostalLocations isNil ifTrue: [PostalLocations := Dictionary new].
  231.      ^PostalLocations! !
  232.  
  233.  !PostalLocation class privateMethods !
  234.  
  235.  flushInstances
  236.          "Private - Initialize PostalLocations so that all instances
  237.           of PostalLocation are removed."
  238.  
  239.      PostalLocations := Dictionary new! !
  240.  
  241.  
  242.  !PostalLocation publicMethods !
  243.  
  244.  = aPostalLocation
  245.      "Return true if the receiver is the same kind of object
  246.      and has the same key as aPostalLocation."
  247.  
  248.      ^self species == aPostalLocation species and:
  249.          [self key = aPostalLocation key]!
  250.  
  251.  city
  252.          "Return the city for the receiver."
  253.  
  254.      ^city!
  255.  
  256.  hash
  257.      "Return a hash value that is based upon the same
  258.      information used to test equality."
  259.  
  260.      ^self key hash!
  261.  
  262.  key
  263.          "Return the information that uniquely identifies
  264.          the receiver."
  265.  
  266.      ^postCode!
  267.  
  268.  postCode
  269.          "Return the post code for the receiver."
  270.  
  271.      ^postCode!
  272.  
  273.  state
  274.          "Return the state for the receiver."
  275.  
  276.      ^state!
  277.  
  278.  value
  279.          "Return the entire receiver."! !
  280.  
  281.  !PostalLocation privateMethods !
  282.  
  283.  city: aCity
  284.          "Set the city for the receiver."
  285.  
  286.      city := aCity!
  287.  
  288.  postCode: aString
  289.          "Set the post code for the receiver."
  290.  
  291.      postCode := aString!
  292.  
  293.  state: aState
  294.          "Set the state for the receiver."
  295.  
  296.      state := aState! !
  297.  
  298.  AuctionDataAccess loaded.!
  299.  
  300.  Application create: #AuctionDomain with:
  301.          (#( AuctionDataAccess)
  302.              collect: [:each | Smalltalk at: each ifAbsent: [
  303.                  self error: 'Not all of the prerequisites are loaded']])!
  304.  
  305.  AuctionDomain becomeDefault!
  306.  
  307.  Object subclass: #Auction
  308.      instanceVariableNames: 'contracts date address salesTaxRate auctioneer bidders bidderID auctionItemID '
  309.      classVariableNames: 'Auctions '
  310.      poolDictionaries: ''
  311.  !
  312.  
  313.  Auction comment: 'Keeps track of the information of an auction. This includes such information as the
  314.  auctioneer, location, date, sales tax rate, bidders, and a list of contracts.
  315.  
  316.  Also responsible for assigning a unique ID to each item and each bidder
  317.  for each auction.
  318.  '!
  319.  
  320.  Object subclass: #AuctionItem
  321.      instanceVariableNames: 'description id buyer sellingPrice advancedBids reservedBid '
  322.      classVariableNames: ''
  323.      poolDictionaries: ''
  324.  !
  325.  
  326.  AuctionItem comment: 'Keeps track of information about items to be sold in an auction. This includes its
  327.  description, unique ID, selling price, and bidder that bought the item.
  328.  '!
  329.  
  330.  Object subclass: #Bidder
  331.      instanceVariableNames: 'person number itemsBought '
  332.      classVariableNames: ''
  333.      poolDictionaries: ''
  334.  !
  335.  
  336.  Bidder comment: 'Keeps track of name and address (kept by an instance of Person), the bidder''s card number, the list of items bought,
  337.  and the calculation of total costs associated with the bought items.
  338.  A Person can be a bidder and a seller and an auctioneer.
  339.  '!
  340.  
  341.  Object subclass: #Contract
  342.      instanceVariableNames: 'seller items commission '
  343.      classVariableNames: ''
  344.      poolDictionaries: ''
  345.  !
  346.  
  347.  Contract comment: 'Manages the agreement between an auctioneer and a seller. There is an instance per
  348.  auction per seller. It keeps track of the seller, the commission, and a list of items to be
  349.  sold.
  350.  '!
  351.  
  352.  Application subclass: #AuctionDomain
  353.      instanceVariableNames: ''
  354.      classVariableNames: ''
  355.      poolDictionaries: ''
  356.  !
  357.  
  358.  
  359.  !Auction class publicMethods !
  360.  
  361.  addAuction
  362.          "Create a new auction and add it to the list of auctions.
  363.           Return the new auction."
  364.  
  365.      ^self auctions addLast: self new
  366.  !
  367.  
  368.  auctions
  369.          "Return the list of auctions. Initialize the list if it is nil."
  370.  
  371.      Auctions isNil ifTrue: [Auctions := OrderedCollection new].
  372.      ^Auctions!
  373.  
  374.  auctionsFor: aPerson
  375.          "Return a collection of auctions that are for a particular auctioneer."
  376.  
  377.      ^self auctions select: [:anAuction | anAuction auctioneer == aPerson]
  378.  !
  379.  
  380.  new
  381.          "Returned an initialized instance of the receiver."
  382.  
  383.      ^super new initialize!
  384.  
  385.  removeAuction: anAuction
  386.          "Remove an auction from the list of auctions.
  387.           Return the auction that was removed, or
  388.           return nil if the auction is not in the list"
  389.  
  390.      ^self auctions remove: anAuction ifAbsent: [nil]! !
  391.  
  392.  
  393.  !Auction publicMethods !
  394.  
  395.  addBidder: aPerson
  396.          "Add a bidder to the list. A bidder cannot exist without belonging to
  397.           an auction. This method creates a new instance of bidder for aPerson.
  398.  
  399.           Return the new bidder."
  400.  
  401.      ^self bidders addLast:
  402.          (Bidder new
  403.              person: aPerson;
  404.              number: self assignBidderID;
  405.              yourself)
  406.          !
  407.  
  408.  addContract: aContract
  409.          "Add a contract to the list."
  410.  
  411.      self contracts addLast: aContract!
  412.  
  413.  address
  414.          "Return the address for the receiver."
  415.  
  416.      ^address!
  417.  
  418.  address: anAddress
  419.          "Set the address for the receiver."
  420.  
  421.      address := anAddress!
  422.  
  423.  allItems
  424.          "Return the list of all items to be sold in the receiver,
  425.           regardless of the seller. "
  426.  
  427.      | theList |
  428.      theList := OrderedCollection new.
  429.      self contracts do: [:aContract | theList addAll: aContract items].
  430.      ^theList!
  431.  
  432.  allItemsNotSold
  433.          "Return a collection of all items that were not
  434.           sold in the receiver for all of the sellers."
  435.  
  436.      ^self contracts collect: [:aContract |
  437.          aContract items reject: [:item | item isSold]
  438.       ]!
  439.  
  440.  allItemsSold
  441.          "Return a collection of all items that were not
  442.           sold in the receiver for all of the sellers."
  443.  
  444.      ^self contracts collect: [:aContract |
  445.          aContract items select: [:item | item isSold]
  446.       ]
  447.  !
  448.  
  449.  assignAuctionItemID
  450.          "Increment the auctionItemID by one and return the new value."
  451.  
  452.      self auctionItemID: self auctionItemID + 1.
  453.      ^self auctionItemID
  454.          !
  455.  
  456.  assignBidderID
  457.          "Increment the bidderID by one and return the new value."
  458.  
  459.      self bidderID: self bidderID + 1.
  460.      ^self bidderID
  461.          !
  462.  
  463.  auctioneer
  464.          "Return the auctioneer (an instance of Person) for the receiver."
  465.  
  466.      ^auctioneer!
  467.  
  468.  auctioneer: aPerson
  469.          "Set the auctioneer for the receiver."
  470.  
  471.      auctioneer := aPerson!
  472.  
  473.  auctionItemID
  474.          "Return the current auctionItemID for the receiver.
  475.           Each auction item must have a unique ID in the receiver."
  476.  
  477.      ^auctionItemID!
  478.  
  479.  bidderID
  480.          "Return the current bidderID for the receiver.
  481.           Each bidder must have a unique ID in the receiver."
  482.  
  483.      ^bidderID!
  484.  
  485.  bidders
  486.          "Return the list of bidders for the receiver.
  487.           Initialize if bidders is nil."
  488.  
  489.      bidders isNil ifTrue: [bidders := OrderedCollection new].
  490.      ^bidders!
  491.  
  492.  contracts
  493.          "Return the list of contracts for the receiver.
  494.           Initialize if contracts is nil."
  495.  
  496.      contracts isNil ifTrue: [contracts := OrderedCollection new].
  497.      ^contracts!
  498.  
  499.  date
  500.          "Return the date for the receiver."
  501.  
  502.      ^date!
  503.  
  504.  date: aDate
  505.          "Set the date for the receiver."
  506.  
  507.      date := aDate!
  508.  
  509.  initialize
  510.          "Initialize the data for the receiver."
  511.  
  512.      self date: Date today.
  513.      self address: Address new.
  514.      self salesTaxRate: 0.0.
  515.      self auctioneer: Person new.
  516.      self bidderID: 0.
  517.      self auctionItemID: 0.  !
  518.  
  519.  removeBidder: aBidder
  520.          "Remove a bidder from the list of bidders.
  521.           Return nil if the bidder is not in the list.
  522.           Otherwise, return the bidder that was removed."
  523.  
  524.      ^self bidders remove: aBidder ifAbsent: [nil]!
  525.  
  526.  removeContract: aContract
  527.          "Remove a contract from the list of contracts.
  528.           Return nil if the contract is not in the list.
  529.           Otherwise, return the contract that was removed."
  530.  
  531.      ^self contracts remove: aContract ifAbsent: [nil]!
  532.  
  533.  salesTaxForBidder: aBidder
  534.          "Compute and return the sales tax for aBidder."
  535.  
  536.      ^aBidder costOfItems * self salesTaxRate!
  537.  
  538.  salesTaxRate
  539.          "Return the sales tax rate for the receiver."
  540.  
  541.      ^salesTaxRate!
  542.  
  543.  salesTaxRate: aRate
  544.          "Set the sales tax rate for the receiver.
  545.           Parameter expected:
  546.              aRate : a number between 0 and 1."
  547.  
  548.      salesTaxRate := aRate!
  549.  
  550.  totalCostForBidder: aBidder
  551.          "Return the total cost for aBidder including sales tax.
  552.           The sales tax rate is specified between 0 and 1, not as a percentage.
  553.  
  554.           The total is computed by adding the cost of the items to the
  555.           amount of sales tax on the cost of items. This equation is factored
  556.           to avoid having to retrieve the cost of items twice."
  557.  
  558.      ^aBidder costOfItems * (1 + self salesTaxRate)! !
  559.  
  560.  !Auction privateMethods !
  561.  
  562.  auctionItemID: aNumber
  563.          "Private - Set the current auctionItemID for the receiver."
  564.  
  565.      auctionItemID := aNumber!
  566.  
  567.  bidderID: aNumber
  568.          "Private - Set the current bidderID for the receiver."
  569.  
  570.      bidderID := aNumber! !
  571.  
  572.  
  573.  !AuctionItem class publicMethods !
  574.  
  575.  new
  576.          "Returned an initialized instance of the receiver."
  577.  
  578.      ^super new initialize! !
  579.  
  580.  
  581.  !AuctionItem publicMethods !
  582.  
  583.  addAdvancedBid: anAmount for: aBidder
  584.          "Add an advanced bid to the Dictionary with aBidder being the key
  585.           and anAmount being the value. The bidder is the key since keys have
  586.           to be unique and a bidder can have only one advanced bid for an item.
  587.  
  588.           Parameter expected:
  589.              anAmount : a number."
  590.  
  591.      self advancedBids at: aBidder put: anAmount!
  592.  
  593.  advancedBidForBidder: aBidder
  594.          "Return the amount of the advanced bid for aBidder.
  595.           If aBidder does not have an advanced bid for the receiver, return nil."
  596.  
  597.      ^self advancedBids at: aBidder ifAbsent: [nil].!
  598.  
  599.  advancedBidGreaterThan: anAmount
  600.          "Return the association for the highest advanced bid if
  601.           the amount is greater than anAmount. Otherwise return nil.
  602.           Parameter expected:
  603.              anAmount : a number."
  604.  
  605.      | advancedBid |
  606.      advancedBid := self highestAdvancedBid.
  607.      ^advancedBid value > anAmount
  608.          ifTrue: [advancedBid]
  609.          ifFalse: [nil].!
  610.  
  611.  advancedBids
  612.          "Return the advanced bids for the receiver. This allows multiple advanced
  613.           bids even though the highest advanced bid is the only valid advanced bid."
  614.  
  615.      advancedBids isNil ifTrue: [advancedBids := Dictionary new].
  616.      ^advancedBids!
  617.  
  618.  buyer
  619.          "Return the buyer (an instance of Bidder) for the receiver."
  620.  
  621.      ^buyer!
  622.  
  623.  description
  624.          "Return the description for the receiver."
  625.  
  626.      ^description!
  627.  
  628.  description: aString
  629.          "Set the description for the receiver."
  630.  
  631.      description := aString!
  632.  
  633.  highestAdvancedBid
  634.          "Return the highest advanced bid in the list. In the association returned,
  635.           the key is the bidder and the value is the amount.
  636.           If the key is nil or the value is zero, then no advanced bids were bid."
  637.  
  638.      | highestBid |
  639.      highestBid := Association key: nil value: 0.
  640.      self advancedBids associationDo: [:anAssociation |
  641.          anAssociation value > highestBid value
  642.              ifTrue: [highestBid := anAssociation]
  643.      ].
  644.      ^highestBid!
  645.  
  646.  id
  647.          "Return the id for the receiver."
  648.  
  649.      ^id!
  650.  
  651.  id: anID
  652.          "Set the id for the receiver.
  653.           Parameter expected:
  654.              anID : a number."
  655.  
  656.      id := anID!
  657.  
  658.  initialize
  659.          "Initialize the data for the receiver."
  660.  
  661.      self description: String new.!
  662.  
  663.  isAdvancedBidGreaterThan: anAmount
  664.          "Return true if the advanced bid is greater than the amount.
  665.           Otherwise return false. Defaults to false if there is no
  666.           advanced bid.
  667.           Parameter expected:
  668.              anAmount : a number."
  669.  
  670.      ^self highestAdvancedBid value > anAmount
  671.  !
  672.  
  673.  isReserveBidHigherThan: anAmount
  674.          "Return true if the reserve bid is greater than a specified amount.
  675.           Otherwise return false.
  676.           Parameter expected:
  677.              anAmount : a number."
  678.  
  679.      ^self reservedBid > anAmount
  680.  !
  681.  
  682.  isSold
  683.          "Return true if the receiver has been sold.
  684.           Otherwise return false."
  685.  
  686.      ^self sellingPrice notNil!
  687.  
  688.  removeAdvancedBidForBidder: aBidder
  689.          "Remove an advanced bid from the list for aBidder."
  690.  
  691.      ^self advancedBids remove: aBidder ifAbsent: [nil]!
  692.  
  693.  reservedBid
  694.          "Return the reserved bid for the receiver."
  695.  
  696.      ^reservedBid!
  697.  
  698.  reservedBid: anAmount
  699.          "Set the reserved bid for the receiver.
  700.           Parameter expected:
  701.              anAmount : a number."
  702.  
  703.      reservedBid := anAmount!
  704.  
  705.  sellingPrice
  706.          "Return the selling price for the receiver. If this value is nil,
  707.           the receiver has not been sold."
  708.  
  709.      ^sellingPrice!
  710.  
  711.  sellingPrice: anAmount buyer: aBidder
  712.          "Set the selling price and the buyer for the receiver.
  713.           These two items should always be set together.
  714.           Parameter expected:
  715.              anAmount : a number."
  716.  
  717.      self
  718.          sellingPrice: anAmount;
  719.          buyer: aBidder! !
  720.  
  721.  !AuctionItem privateMethods !
  722.  
  723.  buyer: aBidder
  724.          "Private - Set the buyer for the receiver."
  725.  
  726.      buyer := aBidder!
  727.  
  728.  sellingPrice: anAmount
  729.          "Private - Set the selling price for the receiver.
  730.           Parameter expected:
  731.              anAmount : a number."
  732.  
  733.      sellingPrice := anAmount! !
  734.  
  735.  
  736.  !Bidder class publicMethods !
  737.  
  738.  new
  739.          "Returned an initialized instance of the receiver."
  740.  
  741.      ^super new initialize! !
  742.  
  743.  
  744.  !Bidder publicMethods !
  745.  
  746.  addBoughtItem: anAuctionItem
  747.          "Add anAuctionItem that was bought by the receiver to the list."
  748.  
  749.      self itemsBought add: anAuctionItem!
  750.  
  751.  address
  752.          "Return the address for the receiver."
  753.  
  754.      ^self person address!
  755.  
  756.  costOfItems
  757.          "Return the total cost of the bought items without any sales
  758.           tax added. This method assumes that if an item is added to this list,
  759.           it has a valid selling price."
  760.  
  761.      | totalCost |
  762.      totalCost := 0.
  763.      self itemsBought do: [ :anItem |
  764.          totalCost := totalCost + anItem sellingPrice].
  765.      ^totalCost!
  766.  
  767.  initialize
  768.          "Initialize the data for the receiver."
  769.  
  770.      self person: Person new.
  771.      self number: 0!
  772.  
  773.  itemsBought
  774.          "Return the list of items bought.
  775.           If the list is nil, initialize it to an OrderedCollection."
  776.  
  777.      itemsBought isNil
  778.          ifTrue: [itemsBought := OrderedCollection new].
  779.      ^itemsBought!
  780.  
  781.  name
  782.          "Return the name for the receiver."
  783.  
  784.      ^self person name!
  785.  
  786.  number
  787.          "Return the number for the receiver."
  788.  
  789.      ^number!
  790.  
  791.  number: aNumber
  792.          "Set the number for the receiver. It is up to the sender of this message
  793.           to make sure the number is unique for the auction."
  794.  
  795.      number := aNumber!
  796.  
  797.  person
  798.          "Return the person for the receiver."
  799.  
  800.      ^person!
  801.  
  802.  person: aPerson
  803.          "Set the person for the receiver."
  804.  
  805.      person := aPerson!
  806.  
  807.  removeBoughtItem: anAuctionItem
  808.          "Remove anAuctionItem from the list. If the item is not in the list,
  809.           return nil. Otherwise, return anAuctionItem."
  810.  
  811.      ^self itemsBought remove: anAuctionItem ifAbsent: [nil].! !
  812.  
  813.  
  814.  !Contract publicMethods !
  815.  
  816.  addItem: anAuctionItem
  817.          "Add anAuctionItem to the list of items for the receiver.
  818.           If anAuctionItem is already in the list, do nothing."
  819.  
  820.      (self items includes: anAuctionItem)
  821.          ifFalse: [self items addLast: anAuctionItem]!
  822.  
  823.  commission
  824.          "Return the commission for the receiver."
  825.  
  826.      ^commission!
  827.  
  828.  commission: aCommission
  829.          "Set the commission for the receiver.
  830.           Parameter expected:
  831.              aCommission : a number between 0 and 1."
  832.  
  833.      commission := aCommission!
  834.  
  835.  items
  836.          "Return the list of items for the receiver."
  837.  
  838.      items isNil ifTrue: [items := OrderedCollection new].
  839.      ^items!
  840.  
  841.  items: aCollection
  842.          "Set the list of items for the receiver to a collection."
  843.  
  844.      items := aCollection!
  845.  
  846.  removeItem: anAuctionItem
  847.          "Remove an item from the list of items. Return nil if the item is not
  848.           in the list. Otherwise return anAuctionItem."
  849.  
  850.      ^self items remove: anAuctionItem ifAbsent: [nil]!
  851.  
  852.  seller
  853.          "Return the seller for the receiver."
  854.  
  855.      ^seller!
  856.  
  857.  seller: aSeller
  858.          "Set the seller for the receiver."
  859.  
  860.      seller := aSeller! !
  861.  
  862.  
  863.  Application create: #AuctionTest with:
  864.          (#( AuctionDomain)
  865.              collect: [:each | Smalltalk at: each ifAbsent: [
  866.                  self error: 'Not all of the prerequisites are loaded']])!
  867.  
  868.  AuctionTest becomeDefault!
  869.  
  870.  Application subclass: #AuctionTest
  871.      instanceVariableNames: ''
  872.      classVariableNames: ''
  873.      poolDictionaries: ''
  874.  !
  875.  
  876.  AuctionTest comment: 'Test methods for the Auction class and application.'!
  877.  
  878.  
  879.  !AuctionTest class publicMethods !
  880.  
  881.  runAuction1
  882.          "Set up Auction #1"
  883.          "AuctionTest runAuction1"
  884.  
  885.      | auction |
  886.      "Create the auction"
  887.      auction := Auction new
  888.          address: Address address1;
  889.          auctioneer:
  890.              (Person new
  891.                  name: 'Robin Smith';
  892.                  address: Address address2);
  893.          salesTaxRate: 7.0;
  894.          yourself.
  895.  
  896.      "Add the bidders"
  897.      auction
  898.          addBidder: Person person1;
  899.          addBidder: Person person2;
  900.          addBidder: Person person3;
  901.          addBidder: Person person4.
  902.  
  903.      "Add the items for sale"
  904.  
  905.      ^auction!
  906.  
  907.  runAuction2
  908.          "Set up Auction #2"
  909.          "AuctionTest runAuction2"
  910.  
  911.      | auction contract |
  912.      "Create the auction"
  913.      auction := Auction new
  914.          address: Address address2;
  915.          auctioneer:
  916.              (Person new
  917.                  name: 'T. Jones';
  918.                  address: Address address3);
  919.          salesTaxRate: 8.0;
  920.          yourself.
  921.  
  922.      "Add the bidders"
  923.      auction
  924.          addBidder: Person person1;
  925.          addBidder: Person person2;
  926.          addBidder: Person person3;
  927.          addBidder: Person person4.
  928.  
  929.      "Add the contract with the items for sale"
  930.      auction addContract: (contract := Contract new).
  931.      contract
  932.          commission: 3.0;
  933.          seller: 'H. Ball';
  934.          addItem: (AuctionItem item1 id: auction assignAuctionItemID);
  935.          addItem: (AuctionItem item2 id: auction assignAuctionItemID);
  936.          addItem: (AuctionItem item3 id: auction assignAuctionItemID);
  937.          addItem: (AuctionItem item4 id: auction assignAuctionItemID).
  938.  
  939.      ^auction! !
  940.  
  941.  
  942.  !Address class publicMethods !
  943.  
  944.  address1
  945.          "Return an instance of the receiver."
  946.          "Address address1"
  947.  
  948.      ^Address
  949.          street: 'Fisher Ave.'
  950.          postCode: '49255'!
  951.  
  952.  address2
  953.          "Return an instance of the receiver."
  954.          "Address address2"
  955.  
  956.      ^Address
  957.          street: 'Baseline Ave.'
  958.          postCode: '49255'!
  959.  
  960.  address3
  961.          "Return an instance of the receiver."
  962.          "Address address3"
  963.  
  964.      ^Address
  965.          street: 'Knoxdale Ave.'
  966.          postCode: '78746'  !
  967.  
  968.  address4
  969.          "Return an instance of the receiver."
  970.          "Address address4"
  971.  
  972.      ^Address
  973.          street: 'Exeter Blvd.'
  974.          postCode: '78746'!
  975.  
  976.  address5
  977.          "Return an instance of the receiver."
  978.          "Address address5"
  979.  
  980.      ^Address
  981.          street: 'Merivale Ave.'
  982.          postCode: '78746'!
  983.  
  984.  address6
  985.          "Return an instance of the receiver."
  986.          "Address address6"
  987.  
  988.      ^Address
  989.          street: 'Clyde Ave.'
  990.          postCode: '49255'! !
  991.  
  992.  
  993.  !AuctionItem class publicMethods !
  994.  
  995.  item1
  996.          "Return an instance of the receiver."
  997.          "AuctionItem item1"
  998.  
  999.      ^AuctionItem new
  1000.          description: 'Good old fashion rocking chair'!
  1001.  
  1002.  item2
  1003.          "Return an instance of the receiver."
  1004.          "AuctionItem item2"
  1005.  
  1006.      ^AuctionItem new
  1007.          description: 'Solid oak pantry'!
  1008.  
  1009.  item3
  1010.          "Return an instance of the receiver."
  1011.          "AuctionItem item3"
  1012.  
  1013.      ^AuctionItem new
  1014.          description: '18th century lantern'!
  1015.  
  1016.  item4
  1017.          "Return an instance of the receiver."
  1018.          "AuctionItem item4"
  1019.  
  1020.      ^AuctionItem new
  1021.          description: '1920 radio'!
  1022.  
  1023.  item5
  1024.          "Return an instance of the receiver."
  1025.          "AuctionItem item5"
  1026.  
  1027.      ^AuctionItem new
  1028.          description: 'Lime green french provincial couch'!
  1029.  
  1030.  item6
  1031.          "Return an instance of the receiver."
  1032.          "AuctionItem item6"
  1033.  
  1034.      ^AuctionItem new
  1035.          description: '10 piece mahogany dining room set'! !
  1036.  
  1037.  
  1038.  !Person class publicMethods !
  1039.  
  1040.  person1
  1041.          "Return an instance of the receiver."
  1042.          "Person person1"
  1043.  
  1044.      ^Person new
  1045.          name: 'Robin Smith';
  1046.          address: (Address
  1047.                      street: 'Holmwood Ave'
  1048.                      postCode: '27511');
  1049.          yourself
  1050.  !
  1051.  
  1052.  person2
  1053.          "Return an instance of the receiver."
  1054.          "Person person2"
  1055.  
  1056.      ^Person new
  1057.          name: 'Carmen LeDuke';
  1058.          address: (Address
  1059.                      street: 'Bank St.'
  1060.                      postCode: '27511');
  1061.          yourself
  1062.  !
  1063.  
  1064.  person3
  1065.          "Return an instance of the receiver."
  1066.          "Person person3"
  1067.  
  1068.      ^Person new
  1069.          name: 'I. StandTall';
  1070.          address: (Address
  1071.                      street: 'Bronson Ave.'
  1072.                      postCode: '27511');
  1073.          yourself
  1074.  !
  1075.  
  1076.  person4
  1077.          "Return an instance of the receiver."
  1078.          "Person person4"
  1079.  
  1080.      ^Person new
  1081.          name: 'Joey Jones';
  1082.          address: Address address5;
  1083.          yourself! !
  1084.  
  1085.  
  1086.  !PostalLocation class publicMethods !
  1087.  
  1088.  addInvalidPostalLocation
  1089.          "Add an invalid post code."
  1090.          "PostalLocation addInvalidPostalLocation"
  1091.  
  1092.      ^PostalLocation addPostCode: '78ui7ji' forCity: 'xx' forState: 'anycity' !
  1093.  
  1094.  
  1095.  addPostCode27511
  1096.          "Add a post code for 27511."
  1097.  
  1098.      ^PostalLocation addPostCode: '27511' forCity: 'Cary' forState: 'NC' !
  1099.  
  1100.  addPostCode49255
  1101.          "Add a post code for 49255."
  1102.  
  1103.      ^PostalLocation addPostCode: '49255' forCity: 'Montogomery' forState: 'MI' !
  1104.  
  1105.  addPostCode78746
  1106.          "Add a post code for 78746."
  1107.  
  1108.      ^PostalLocation addPostCode: '78746' forCity: 'Austin' forState: 'TX' !
  1109.  
  1110.  addPostCodes
  1111.          "Add and return all of the postal locations."
  1112.          "PostalLocation addPostCodes"
  1113.  
  1114.      ^self
  1115.          addPostCode27511;
  1116.          addPostCode49255;
  1117.          addPostCode78746;
  1118.          postCodes
  1119.                  !
  1120.  
  1121.  invalidPostalLocationFor11111
  1122.          "Get the PostalLocation for post code 11111 which is
  1123.           known to be invalid."
  1124.          "PostalLocation invalidPostalLocationFor11111"
  1125.  
  1126.      ^PostalLocation postalLocationFor: '11111'!
  1127.  
  1128.  listOfLocations
  1129.      "Return a Collection of printable representations of all the known post codes."
  1130.  
  1131.      ^self postalLocations asArray collect: [:postalLocation |
  1132.          postalLocation printString]!
  1133.  
  1134.  postCodes
  1135.          "Return all of the current post codes."
  1136.  
  1137.      ^PostalLocation postalLocations asArray collect: [:postalLocation |
  1138.          postalLocation postCode]!
  1139.  
  1140.  postLocationFor27511
  1141.          "Get the PostalLocation for 27511."
  1142.  
  1143.      ^PostalLocation postalLocationFor: '27511'!
  1144.  
  1145.  postLocationFor49255
  1146.          "Get the PostalLocation for 49255."
  1147.  
  1148.      ^PostalLocation postalLocationFor: '49255'!
  1149.  
  1150.  postLocationFor78746
  1151.          "Get the PostalLocation for 78746."
  1152.  
  1153.      ^PostalLocation postalLocationFor: '78746'!
  1154.  
  1155.  randomPostalLocation
  1156.      "Return a random post code."
  1157.  
  1158.      ^PostalLocations values asArray
  1159.          at: (Random new next * PostalLocations size) truncated + 1! !
  1160.  
  1161.  
  1162.  Application create: #AuctionUI with:
  1163.          (#( AuctionDomain)
  1164.              collect: [:each | Smalltalk at: each ifAbsent: [
  1165.                  self error: 'Not all of the prerequisites are loaded']])!
  1166.  
  1167.  AuctionUI becomeDefault!
  1168.  
  1169.  Application subclass: #AuctionUI
  1170.      instanceVariableNames: ''
  1171.      classVariableNames: ''
  1172.      poolDictionaries: ''
  1173.  !
  1174.  
  1175.  
  1176.  !Address publicMethods !
  1177.  
  1178.  printOn: aStream
  1179.          "Print the receiver onto a Stream."
  1180.  
  1181.      aStream
  1182.          space;
  1183.          nextPutAll: self street;
  1184.          cr.
  1185.  
  1186.      self postalLocation printOn: aStream ! !
  1187.  
  1188.  
  1189.  !AuctionItem publicMethods !
  1190.  
  1191.  printOn: aStream
  1192.          "Print the receiver onto a Stream."
  1193.  
  1194.      aStream
  1195.          nextPutAll: 'ID: ';
  1196.          nextPutAll: self id printString;
  1197.          cr;
  1198.          nextPutAll: self description;
  1199.          cr.
  1200.      self buyer notNil
  1201.          ifTrue: [
  1202.              aStream
  1203.                  nextPutAll: 'Buyer: ';
  1204.                  cr.
  1205.              self buyer printOn: aStream.
  1206.              aStream
  1207.                  nextPutAll: 'Selling price: $.';
  1208.                  nextPutAll: self sellingPriced printString
  1209.          ].
  1210.  
  1211.      ! !
  1212.  
  1213.  
  1214.  !Bidder publicMethods !
  1215.  
  1216.  printOn: aStream
  1217.          "Print the receiver onto a Stream."
  1218.  
  1219.      aStream
  1220.          nextPutAll: 'Bidder(';
  1221.          cr;
  1222.          nextPutAll: self person printString;
  1223.          nextPutAll: 'ID: ';
  1224.          nextPutAll: self number printString;
  1225.          cr;
  1226.          nextPutAll: 'Items bought:';
  1227.          cr.
  1228.  
  1229.      self itemsBought asArray printOn: aStream.
  1230.  
  1231.      aStream
  1232.          nextPut: $);
  1233.          cr! !
  1234.  
  1235.  
  1236.  !Contract publicMethods !
  1237.  
  1238.  printOn: aStream
  1239.          "Print the receiver onto a Stream."
  1240.  
  1241.      aStream
  1242.          nextPutAll: 'Seller: ';
  1243.          cr;
  1244.          nextPutAll: self seller;
  1245.          nextPutAll: ' with commission: ';
  1246.          nextPutAll: self commission printString;
  1247.          cr;
  1248.          nextPutAll: 'Items:';
  1249.          cr.
  1250.  
  1251.      self items asArray printOn: aStream.! !
  1252.  
  1253.  
  1254.  !Person publicMethods !
  1255.  
  1256.  printOn: aStream
  1257.          "Print the receiver onto a Stream."
  1258.  
  1259.      aStream
  1260.          nextPut: $(;
  1261.          nextPutAll: self name;
  1262.          cr.
  1263.  
  1264.      self address printOn: aStream.
  1265.  
  1266.      aStream
  1267.          nextPut: $);
  1268.          cr! !
  1269.  
  1270.  
  1271.  !PostalLocation publicMethods !
  1272.  
  1273.  printOn: aStream
  1274.          "Print the receiver onto a Stream."
  1275.  
  1276.      aStream
  1277.          space;
  1278.          nextPutAll: self city;
  1279.          space;
  1280.          nextPutAll: self state;
  1281.          space;
  1282.          nextPutAll: self postCode           ! !
  1283.