Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 101 Principles of Programming

Similar presentations


Presentation on theme: "COMPSCI 101 Principles of Programming"— Presentation transcript:

1 COMPSCI 101 Principles of Programming
Lecture 18 – Problem Solving and Modular Design

2 Learning outcomes At the end of this lecture, students should be able to: understand complicated examples on file input output process the content of text files inside a large program design a larger program by breaking it up into manageable parts COMPSCI Principles of Programming

3 Recap Complete the update_quantity function below, which updates the quantity value of an item by an integer (either positive or negative). Use the split function to break up the item string into a list of attributes, where item_list[3] represents the quantity value. After calculating the new quantity value, we need to regenerate the item string and update it back into the stock list. def update_quantity(stock_list, index, value): item = stock_list[index] # take the item out item_list = item.split(",") item_list[3] = str(int(item_list[3])+value) item = item_list[0] for i in range(1, len(item_list)): item = item + "," + item_list[i] stock_list[index] = item # put the item back COMPSCI Principles of Programming

4 Online Shopping System
Break down a large program into smaller parts (functions). COMPSCI Principles of Programming

5 Display Items The display_stock function prints out all the available items on sale in the stock. Uses a for loop with the list name to scan through the items list, For each item, use the split function to break down the item into a list of attributes, where item_list[3] stores the quantity value. Only prints out the items with quantity value greater than 0. def display_stock(stock_list): display_separator() print("List of the items on sale") for item in stock_list: item_list = item.split(",") if int(item_list[3]) > 0: print(item) COMPSCI Principles of Programming

6 Add an Item to Cart The add_to_cart function adds an item into the shopping cart with an user given item code. If the item exists and available, put item in the cart list and update its quantity. def add_to_cart(stock_list, cart): index = input_code(stock_list) item = stock_list[index] item_list = item.split(",") if int(item_list[3]) > 0: cart = cart + [item] print("Item - " + item_list[1] + " is added to cart.") update_quantity(stock_list, index, -1) else: print("Sorry, item "+ item_list[0] + " is out of stock.") print("Please select a different item.") display_separator() return cart COMPSCI Principles of Programming

7 Remove an Item from Cart
The remove_from_cart function removes an unwanted item from the shopping cart with an user given item code. If the item exists in the cart, remove it in the cart list and update its quantity. def remove_from_cart(stock_list, cart): index = input_code(stock_list) item = stock_list[index] item_list = item.split(",") cart_index = find_item(cart, item_list[0]) if cart_index != -1: cart.remove(cart[cart_index]) print("Item - " + item_list[1] + " is removed from cart.") update_quantity(stock_list, index, 1) else: print("This item is not in the shopping cart.") display_separator() return cart COMPSCI Principles of Programming

8 Check Out The check_out function prints out the bill with the items bought and sums up the total amount due. Use a for loop with list to scan through the items in the shopping cart, display the description together with price, and sum up the total costs. def check_out(cart): display_separator() print("Your Shopping Bill") sum = 0 for item in cart: item_list = item.split(",") print(item_list[1] + "/$" + item_list[2]) sum = sum + float(item_list[2]) print("Amount due: $" + str(format(sum, ".2f"))) COMPSCI Principles of Programming

9 Discard Shopping Cart The discard_cart function put the items in the shopping cart back to shelf, by simply updating the quantity value of the corresponding items in the stock list. The save_stock function stores the items list back to a file. def discard_cart(stock_list, cart): for item in cart: item_list = item.split(",") index = find_item(stock_list, item_list[0]) update_quantity(stock_list, index, 1) display_separator() def save_stock(stock_list, filename): out_file = open(filename, 'w') for item in stock_list: out_file.write(item + "\n") out_file.close() COMPSCI Principles of Programming

10 Putting it all together
Modularity emphasizes on separating the functionality of a program into independent and interchangeable modules. def main_menu(stock_list): cart = [] option = get_user_input(1,5) while (option != 4 and option !=5): if option == 1: display_stock(stock_list) elif option == 2: cart = add_to_cart(stock_list, cart) else: cart = remove_from_cart(stock_list, cart) if option == 4 and len(cart)>0: check_out(cart) print("Thank you for shopping with us.") discard_cart(stock_list, cart) print("Nothing bought. Thank you.") def main(): stock_list = load_stock("stock.txt") display_separator() display_intro() display_menu() main_menu(stock_list) save_stock(stock_list, "stock2.txt") main() COMPSCI Principles of Programming

11 Summary A complex task can be accomplished by dividing it into a number of smaller sub tasks to achieve individually. This technique of ‘divide and conquer’ strategy is very effective and popularly used in problem solving. A program represents a solution to a problem, which consists of a set of instructions written in a specific programming language that is executable on a computer. Therefore, it is natural and particularly useful to design a larger program by breaking it up into smaller and manageable parts. In Python, we can achieve this with functions and assemble them together to provide a modularized solution. COMPSCI Principles of Programming

12 Python features used in this lecture
Opening a file f = open( filename[, mode] ) Closing a file f.close() Reading a file f.read() Writing a file f.write( string ) The split function item = "BC009,Broccoli,1.47,11" item_list = item.split(",") # item_list is ['BC009', 'Broccoli', '1.47', '11'] COMPSCI Principles of Programming


Download ppt "COMPSCI 101 Principles of Programming"

Similar presentations


Ads by Google