134****3077 3年前发布
悬赏:80.0 元

问题详情:分类: python-Data Type

It contains two classes: Package and Truck. You are to finish defining the Truck class.
The Package class has only one method, the constructor. Each package is initialized with a unique identifier id and has four attributes:
• id (a string with only numeric characters) for the package’s unique identifier.
• address (a string) for the address to which the package is supposed to be delivered.
• office (a string) for the address of a post service office: if a package is not collected, then the attribute refers to the office where the package is; otherwise, it refers to from which office the package is collected.
• ownerName (a string) for the name to which the package is supposed to be delivered, and
• delivered (a Boolean) for indicating if the package is delivered or not.
• collected (a Boolean) for indicating if the package is collected by a truck or not.
You are free to define accessors and mutators, or you may change the attributes directly. The Package class will not be graded.
The template contains its basic attributes and methods. You are free to add more.
In the template, a truck has three attributes:
• id (a string with only numeric characters): the unique identifier of a truck.
• size (a non-negative integer): the size of the truck, i.e., the maximum number of packages can be stored in a truck (assuming all packages are of the same volume).
• location (a string): the current location of a truck.
• packages: the packages that have been loaded in a truck.
You get to decide the type of the packages attribute. It can be a Python list or any of the data structures we have covered (or will cover) in this class.
The methods you need to define include the followings.
• __init__(self, id, n, loc):Eachtruckisinitializedwithauniqueidentifierid,asizenandalocation loc. No matter what type you choose for the collection of packages in a truck, it should be initalized to be empty.
• collectPackage(self, pk): collect a package pk (an instance of Package), i.e., add to pk to self.packages. The truck and pk must be at the same post service office for the truck to be able to collect the package.
• deliverOnePackage(self, pk): deliver one pckage pk.
The truck has to be at the address specified by pk.address to be able to deliver the package.
• deliverPackages(self): deliver all packages that are supposed to be delivered to truck’s current location.
• removePackage(self, pk): remove a package pk from the truck and put it back to a post service office without delivering it. The truck must be at a post-service office for this to work and the removed package will be at the post-service office afterwards.
• driveTo(self, loc): drive from current location to location loc.
• getPackagesIds(self): return a Python list of the ids of the packages in self.packages. This is for
testing. The order of the ids does not matter.
• Do not change the names of the attributes and methods in either class.
• Feel free to add more methods and attributes if needed.
• This part demonstrates the usage of at least one of the linear data structures we have learned in this class, including stack, queue, deque, linked list, and hash table.


虚高0人次 适中0人次 偏低0人次




客服QQ 1913284695