play with objects
Posted: 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 ?
.....my shop will be called 'Green_car_shop'
first car:
result:
second car:
result:
Then I will put these cars to my shop:
results:
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.
For example:
but how to get in to the slots called cost and calculate costs of all cars?
any idea pls?
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)
first car:
Code: Select all
>(setf audi (make-instance 'automobile))
>(setf (slot-value audi 'car_name) 'AudiA3)
>(setf (slot-value audi 'cost) 10000)
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)
Code: Select all
#<AUTOMOBILE 21854D3B> is an AUTOMOBILE
CAR_NAME BMWM3
COST 12000
Code: Select all
>(push bmw (slot-value greencar 'cars))
>(push audi (slot-value greencar 'cars))
Code: Select all
#<AUTOSHOP 2180F753> is an AUTOSHOP
CARS (#<AUTOMOBILE 2183BD67> #<AUTOMOBILE 21854D3B>)
NAME GREEN_CAR_SHOP
Code: Select all
(defmethod cost_of_all_cars ((autoshop autoshop))
????????????
)
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>))
any idea pls?