Advertisement
earlution

(std) Task 2 - part 1.py

Jun 17th, 2022
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.55 KB | None | 0 0
  1. from datetime import datetime
  2.  
  3. components = ("Processor",
  4.               "RAM",
  5.               "Storage",
  6.               "Screen",
  7.               "Case",
  8.               "USB ports")
  9. choices = ("P3",
  10.            "P5",
  11.            "P7",
  12.            "16GB",
  13.            "32GB",
  14.            "1TB",
  15.            "2TB",
  16.            "19\"",
  17.            "23\"",
  18.            "Mini Tower",
  19.            "Midi Tower",
  20.            "2 ports",
  21.            "4 ports")
  22. prices = (100, 120, 200, 75, 150, 50, 100, 65, 120, 40, 70, 10, 20)
  23. stock = [3, 2, 1, 1, 5, 2, 0, 5, 6, 7, 8, 0, 8]
  24.  
  25.  
  26. def output_component_info(comp_type, start, stop):
  27.     """
  28.    Abstract implementation of procedure to display component info.
  29.    Uses zip() to associate component and price (from components, prices) using
  30.    list comprehension.  This is so component and proce can be outputted on
  31.    same line.
  32.  
  33.    :param str comp_type: Section header to specify component type.
  34.    :param int start: Value to be used as the list slice start index.
  35.    :param int stop: Value to be used as the list slice stop index.
  36.    """
  37.     print(comp_type)
  38.     print("Your choices (with prices) are: ")
  39.     for choice, price in zip(choices[start:stop], prices[start:stop]):
  40.         print(choice, str(price))
  41.  
  42.  
  43. def capture_component_choice(start, stop):
  44.     """
  45.    Abstract implementation of function to capture user's choice for each
  46.    component type.
  47.  
  48.    :param int start: Value to be used as the list slice start index.
  49.    :param int stop: Value to be used as the list slice stop index.
  50.    :return str component_choice: Section header to specify component type.
  51.    """
  52.     component_choice = input("Make your choice: ")
  53.     component_choice = validate_component_choice(component_choice, start, stop)
  54.     while not check_stock_level(component_choice):
  55.         print("That component is out of stock, please try again.")
  56.         component_choice = input("Make your choice: ")
  57.         component_choice = validate_component_choice(component_choice, start, stop)
  58.     print()
  59.     return component_choice
  60.  
  61.  
  62. def validate_component_choice(component_choice, start, stop):
  63.     """
  64.    Performs validation on users' input for component_choice.
  65.    Validation is case insensitive, however onece validated the inputted value,
  66.    even if of different case from original choice, will persist.
  67.  
  68.    :param str component_choice: The value to be validated.
  69.    :param int start: Value to be used as the list slice start index.
  70.    :param int stop: Value to be used as the list slice stop index.
  71.    :return str component_choice: Validated version of user inputted value.
  72.    """
  73.     while component_choice.lower() not in get_lowercase_components()[start:stop]:
  74.         print("That is not a valid choice.")
  75.         component_choice = input("Please choose again: ")
  76.     return component_choice
  77.  
  78.  
  79. def check_stock_level(component_choice):
  80.     """
  81.    Checks to see of the chosen component is in stock.
  82.  
  83.    :param str component_choice: The component to check:
  84.    :return boolean: In stock status of the component.
  85.    """
  86.     if stock[get_lowercase_components().index(component_choice.lower())] > 0:
  87.         return True
  88.     else:
  89.         return False
  90.  
  91.    
  92. def get_lowercase_components():
  93.     """
  94.    Returns a version of the global data structure choice, with all values
  95.    formatted to lowercase.
  96.    Can be used for validation, amoungst other things.
  97.    
  98.    :return list components: Reformatted version of choice.
  99.    """
  100.     components = []
  101.     for component in choices:
  102.         components.append(component.lower())
  103.     return components
  104.  
  105.  
  106. def get_componet_price(component_choice):
  107.     """
  108.    Returns the price associated with the passed component.
  109.    Uses the index of component_choice in choices, to find the associated
  110.    vaule from global data structure, prices.
  111.  
  112.    :param str component_choice: The value to find the associated value from prices.
  113.    :return int: The associated value from prices
  114.    """
  115.     return prices[get_lowercase_components().index(component_choice.lower())]
  116.  
  117.  
  118. def update_estimate(estimate, component_choice, component_price):
  119.     """
  120.    Updates the estimate with the most recent component details.
  121.    Appends parameters component_choice and component_price to the end of
  122.    param estimate. Adds parameter component_price to running total at
  123.    parameter estimate[0].
  124.  
  125.    :param list estimate: A list conatining the estimate so far.
  126.    :param str component_choice: The component choice to be added to estimate.
  127.    :param str component_price: The component price to be added to running total.
  128.    :return list estimate: The updated estimate.
  129.    """
  130.     estimate.append(component_choice)
  131.     estimate.append(component_price)
  132.     estimate[0] += component_price
  133.     return estimate
  134.  
  135.  
  136. def generate_estimate(estimate, estimate_num):
  137.     """
  138.    Generates an estimate for the user.
  139.    Prints component names and prices, by making temporary lists from param
  140.    estimate begining at indexes 1 and 2 (for component names, component prices
  141.    respectively), then every other index (step count of 2) thereafter.  Then
  142.    uses zip() to associate component name and proce using list comprehension.
  143.  
  144.    TODO: improve presentation using .format() to align output in columns.
  145.  
  146.    :param list estimate: The data needed to generate the estimate
  147.    """
  148.     date = datetime.today().strftime('%Y.%m.%d')
  149.     estimate_num = date + '.' + str(estimate_num)
  150.     print("ESTIMATE")
  151.     print("Estimate number: " + estimate_num)
  152.     print("Your choices (with prices) are: ")
  153.     component_names = estimate[1::2]
  154.     component_prices = estimate[2::2]
  155.     for choice, price in zip(component_names, component_prices):
  156.         print(choice, str(price))  
  157.     print("Total cost is: " + str(estimate[0] * 1.2))
  158.  
  159.  
  160. def main():
  161.     estimate_num = 0
  162.     while True:
  163.         estimate = [0]
  164.         estimate_num += 1
  165.        
  166.         output_component_info("Processor options", 0, 3)
  167.         processor_choice = capture_component_choice(0, 3)
  168.         processor_price = get_componet_price(processor_choice)
  169.         estimate = update_estimate(estimate, processor_choice, processor_price)  
  170.        
  171.         output_component_info("RAM options", 3, 5)
  172.         ram_choice = capture_component_choice(3, 5)
  173.         ram_price = get_componet_price(ram_choice)
  174.         estimate = update_estimate(estimate, ram_choice, ram_price)
  175.  
  176.         output_component_info("Storage options", 5, 7)
  177.         storage_choice = capture_component_choice(5, 7)
  178.         storage_price = get_componet_price(ram_choice)
  179.         estimate = update_estimate(estimate, storage_choice, storage_price)
  180.  
  181.         output_component_info("Screen options", 7, 9)
  182.         screen_choice = capture_component_choice(7, 9)
  183.         screen_price = get_componet_price(screen_choice)
  184.         estimate = update_estimate(estimate, screen_choice, screen_price)
  185.  
  186.         output_component_info("Case options", 9, 11)
  187.         case_choice = capture_component_choice(9, 11)
  188.         case_price = get_componet_price(case_choice)
  189.         estimate = update_estimate(estimate, case_choice, case_price)
  190.  
  191.         output_component_info("USP ports options", 11, 13)
  192.         usb_choice = capture_component_choice(11, 13)
  193.         usb_price = get_componet_price(usb_choice)
  194.         estimate = update_estimate(estimate, usb_choice, usb_price)
  195.        
  196.         generate_estimate(estimate, estimate_num)
  197.  
  198.         go_again = input("Do you want to generate another estimate (y/n): ")
  199.         if go_again.lower() == 'n':
  200.             break
  201.  
  202.         print()
  203.     #TODO
  204.  
  205. #main()
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement