Using Decomposition & Abstraction (Edexcel GCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Using Decomposition & Abstraction to Analyse,
Understand & Solve Problems

Examiner Tips and Tricks

Before attempting to use decomposition and abstraction it is important to understand the process by reading here

Task

  • Write a program that calculates the total cost of a pizza order. The program should include the following features:

    • Size: The user can choose from three sizes (small, medium, and large) with different base prices

    • Toppings: The user can choose any number of toppings from a list, with each topping having a set price

    • Discount: There is a 10% discount applied to orders over £20

Abstraction

  • Create a function that calculates and returns the base price of the pizza by taking the size as an input

Decomposition

  • Break down the task in to smaller, more manageable pieces

  • For example, using separate functions to:

    • Get the user input for pizza size and toppings

    • Calculate cost of toppings

    • Apply discount (if applicable)

    • Output final order total

Python code

def calculate_pizza_price(size):

# -----------------------------------------------------------------------
# This function takes the pizza size as input and returns the base price

# -----------------------------------------------------------------------

base_prices = {"small": 5, "medium": 10, "large": 20}

return base_prices[size]

def get_user_choice(options):

# -----------------------------------------------------------------------

# This function displays options and gets valid user input from the provided list

# -----------------------------------------------------------------------

while True:

choice = input("Choose from "+", ".join(options)+": ").lower()

if choice in options:

return choice

else:

print("Invalid choice. Please try again.")

def calculate_topping_cost(toppings, topping_prices):

# -----------------------------------------------------------------------

# This function calculates the total cost of selected toppings

# -----------------------------------------------------------------------

total_cost = 0

for topping in toppings:

total_cost = total_cost + topping_prices[topping]

return total_cost

def apply_discount(total_cost):

# -----------------------------------------------------------------------

# This function applies a 10% discount if the total cost exceeds £20

# -----------------------------------------------------------------------

if total_cost > 20:

discount = total_cost * 0.1

total_cost = total_cost - discount

return total_cost

def main():

# -----------------------------------------------------------------------

# This function coordinates the program flow and calls other functions

# -----------------------------------------------------------------------

pizza_size = get_user_choice(["small", "medium", "large"])

base_price = calculate_pizza_price(pizza_size)

toppings_available = ["pepperoni", "mushrooms", "onions"]

toppings = []

while True:

add_topping = input("Add a topping (y/n)?").lower()

if add_topping == 'n':

break

else:

topping = get_user_choice(toppings_available)

toppings.append(topping)

topping_cost = calculate_topping_cost(toppings, {"pepperoni": 1, "mushrooms": 0.5, "onions": 0.75})

total_cost = base_price + topping_cost

total_cost = apply_discount(total_cost)

print("Your total pizza cost is: £",total_cost)

# -----------------------------------------------------------------------

# Main program starts here

# -----------------------------------------------------------------------

main()

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.