Week3Programs

.py
''' Created on Jan 19, 2021 @author: wuwei ''' import random class ExternalStorage: def __init__(self, size = 100 , name = "iPhnoe"): self.__size = size self.__brand = name def save(self,size): if (size <= self.__size): self.__size -= size print("data size is ", size, "MB is being saved in ", self.__brand," now .... Please wait") return 1 else: print("data size is ", size, "MB, but storage in ", self.__brand, " has only ", self.__size, "MB") return 0 class Computer: def __init__(self,mem): self.__maxprice = 900 self.__memory = mem self.__storage = 0 def sell(self): print("Selling Price: {}".format(self.__maxprice)) def setMaxPrice(self, price): if 0 < price < 1000: self.__maxprice = price def setExtStorage(self, storage_obj): self.__storage = storage_obj def getMaxPrice(self): return self.__maxprice def workingWithFile(self): print("Some operations are done with a file in ", self.__memory) size = random.randint(50,200) print("file data is being sent to...") if self.__storage.save(size) == 0: print("out of space for saving") else: print("data has been successfully saved")
def main(): c = Computer("RAM") c.sell() # change the price c.__maxprice = 1000 print("c.__maxprice is ",c.__maxprice, " but the private attribute value is ", c.getMaxPrice()) # using setter function c.setMaxPrice(200) c.sell() c.setMaxPrice(2000) c.sell() extobj = ExternalStorage(150,"USB") c.setExtStorage(extobj) c.workingWithFile() if __name__ == '__main__': main()
Uploaded by AdmiralSeahorsePerson838 on coursehero.com