# CSC 15 OOP Lab ############################################################################# ## Part I: ## I'm a gas station owner and I've hired you to program for me. Each # gas station sells only one type of gas: regular unleaded. # Each station will have an initial supply of 1000 gallons of gas. # Each station also has a manager, who will be fired if sales aren't good. # I want a computer to keep track of the sales and supply situation at each # gas station. I want to be able to run the following code AS IS. YOU # ARE NOT ALLOWED TO CHANGE IT. Get to work nerd! # Create station objects with manager's name and initial price per gallon: st1 = gasstation("john makeabuck",2.79) st2 = gasstation("jane gouger",2.59) st1.sellgas(10.5) # station1 just sold 10.5 gallons of gas st2.sellgas(20) # station2 just sold 20 gallons of gas st1.sellgas(3000) # should print not enough gas, no sale. st1.sellgas(11) print(st1.totalsales()) # prints the dollar amount of total sales. (2.59*21.5) print(st1.currentsupply()) # prints gallons of gas left in supply. st1.resupply() # reset gas supply to 1000 gallons st2.sale() # decrease price by 10 cents (do the math carefully nerd!) st2.gouge() # double the price right before natural disasters st1.newmanager("mike manageron") # fire john, hire mike if st1.moresales(st2): print "station1 has better sales than station2" ## Now write the class to make this work. Think first: what variables # will each object contain? ##### #Part Ib: keeping track of prices: # After a change in gas price, I would also like to be able to restore the # price of gas to its previous value. For example, if the current price # for st2 is 3.00, then I want to be able to to run the following code: st2.sale() # price is now 2.39 st2.gouge() # price is now 4.78 st2.gouge() # price is now 9.56 st2.rollback() # price should now be 4.78 st2.rollback() # price should now be 2.39 st2.rollback() # price should now be 2.49 # ... st2.rollback() # if there's no previous price, then price stays the same # That is, each call to rollback should cancel the previous sale or # gouging. To do this, you should use a list and .append to keep track # of prices. You should treat the list as a "stack". Think of the right # end of the list as the "top of stack" (tos). The following built-in call # *removes* the last element from the right-end of the stack, and returns it: # A.pop() # you can also use A.pop(i) to remove the ith indexed element # so for example, A = [] A.append(2) A.append(4) x = A.pop() # x is 4, and A is now [2] y = A.pop() # y is 2 and A should now be [] again. #Part Ic: (do this part after I lectured about JSON in class) # add methods to you class that 'serializes' the object into a # a hash table, which can then be represented by a json string. # Also add methods to decode a serialized object into a real one. ############################## Part II ##################################### # For this part you need to implement objects similar to the time # object representing minutes and seconds. You want a class of # objects called 'date'. Each date object will have a year, a month, # and a day. Here's the code you'd like to write: date1 = date(11,14,2019) # create date object for november 14th, 2019 date2 = date(12,31,2013) # december 31st, 2013 print(date1) # define __str__ so this will print "11/14/2019" print(date1.european()) # should print "14/11/2019" in the European style print(date2.leapyear()) # prints False, since 2013 % 4 != 0 date1.nextday() # advance date (destructively) to 11/15/2019 date2.nextday() # advance date destructively to 1/1/2014 date3 = date2.daybefore() # RETURNS a date object representing 12/31/2013. # This method should be NON-DESTRUCTIVE. if date2.cmp(date1)<0: print("date2 comes before date1") #### see below ## Hint: the difficult method is nextday. For this, you need to know ## the number of days in each month. The following array may help you: Days = [31,28,31,30,31,30,31,31,30,31,30,31] # However, you also need to adjust the value of Days[1] (Feburary) if # the current year is a leap year. You should define this array as a class variable (and referr to it as date.Days): #class date: # Days = [31,28,31,30,31,30,31,31,30,31,30,31] # class-wide variable #... # Define the cmp function as follows: def cmp(self,other): # This function should return 0 if self and other are considered "equal", # it should return a negative integer if self is considered "less than" other, # and a positive integer if self is considered "greater" than other. # Optionally, once you've defined cmp, you can overload operators like <, >= # as follows def __lt__(self,other): # less than, overrides <, (use __gt__ for >) return self.cmp(other)<0 def __ge__(self,other): # overrides >= (use __le__ for <=) return self.cmp(other)>=0 def __eq__(self,other): # overrides == (use __ne__ for !=) return self.cmp(other) == 0 # How to you define cmp? To check if one date comes before # another, first compare the year. If the years are the same, compare # the month. If the months are also the same, compare the day. print(date1 >= date2) # should print True (assumed you've redefined >=) ############################ OPTIONAL EXTRAS #################### ## (CHALLENGE) The formula for determining leap years is not as simple # as dividing by 4. Research the real formula and implement it. ## (MEGACHALLENGE): implement a method to compute the number of days until # the given date. For example: christmas = date(12,25,2019) print(date1.daysuntil(christmas), "days until Christmas") # Hint: convert the date to one number: christmas is day 359 of 365. But # always take into account leap years first. If the argument comes before # the date object, the function should return a negative number: for example, # date(12,26,2019).daysuntil(christmas) should return -1. ## (GIGACHALLENGE) Write a method to return the day of the week # of a data object. It should work as follows: date1 = date(11,18,2013) print(date1.dayofweek()) # should print "Monday" # Knowing that 11/18/2013 is a Monday should be enough for you to figure # out the day-of-week of all the other dates. What day was 1/1/1? # (don't worry about when the Gregorian calendar was adopted.) ## (TERACHALLENGE) Friday the 13th is an unlucky day (and a very bad movie). # Write a function to find the NEXT friday the 13th after a given date: date1.nextfri13() # should return a date object representing the next # friday the 13th after date1.