play with objects

Discussion of Common Lisp
Post Reply
freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

play with objects

Post by freezy39 » Mon Apr 05, 2010 12:12 pm

Hello guys,
can I ask you for help regarding my problem?

I would like to create shop with cars. Then I will create two cars and I will put these cars to slot "cars" to my shop. I want to create method which will take cost of all cars from my shop and give back some number. could you advice pls ?

Code: Select all

(defclass autoshop () ((cars :initform nil :accessor cars)
                   (name :initarg :name :reader name) ))

Code: Select all

(defclass automobile ()
  ((car_name   :initform nil :accessor car_name)
   (cost :initform nil :accessor cost)))

Code: Select all

>(setf greencar (make-instance 'autoshop))   
>(setf (slot-value greencar 'name) 'Green_car_shop)
.....my shop will be called 'Green_car_shop'

first car:

Code: Select all

>(setf audi (make-instance 'automobile))
>(setf (slot-value audi 'car_name) 'AudiA3)
>(setf (slot-value audi 'cost) 10000)
result:

Code: Select all

#<AUTOMOBILE 2183BD67> is an AUTOMOBILE
CAR_NAME      AUDIA3
COST          10000

second car:

Code: Select all

>(setf bmw (make-instance 'automobile))
>(setf (slot-value bmw 'cost) 12000)
>(setf (slot-value bmw 'car_name) 'BmwM3)
result:

Code: Select all

#<AUTOMOBILE 21854D3B> is an AUTOMOBILE
CAR_NAME      BMWM3
COST          12000
Then I will put these cars to my shop:

Code: Select all

>(push bmw (slot-value greencar 'cars))
>(push audi (slot-value greencar 'cars))
results:

Code: Select all

#<AUTOSHOP 2180F753> is an AUTOSHOP
CARS      (#<AUTOMOBILE 2183BD67> #<AUTOMOBILE 21854D3B>)
NAME      GREEN_CAR_SHOP
How to continue pls? In case of these 2 cars, it should return 22000. when I will put there third car with cost 11000, cost will be 33000.

Code: Select all

(defmethod cost_of_all_cars ((autoshop autoshop))
 ????????????
  )
For example:

Code: Select all

(defmethod cost_of_all_cars ((autoshop autoshop))
  (list (cars autoshop)) )

Code: Select all

CL-USER 33 > (cost_of_all_cars greencar)
((#<AUTOMOBILE 2183BD67> #<AUTOMOBILE 21854D3B>))
but how to get in to the slots called cost and calculate costs of all cars?

any idea pls?

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: play with objects

Post by nuntius » Mon Apr 05, 2010 2:08 pm

Use LOOP or DOLIST to iterate over the cars in the shop. Then use your COST accessor or call (slot-value car 'cost) to get the cost of each car, summing that cost into a central accumulator variable.

freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

Re: play with objects

Post by freezy39 » Wed Apr 07, 2010 9:44 am

nuntius wrote:Use LOOP or DOLIST to iterate over the cars in the shop. Then use your COST accessor or call (slot-value car 'cost) to get the cost of each car, summing that cost into a central accumulator variable.

thanks nuntius, working.. :) . Loop was good choise. thx man . Im beginner and I didn't know this function/macro..

Post Reply