Question

In: Computer Science

Assignment 4 You will be writing an inventory system that is backed by JSON data and...

Assignment 4 You will be writing an inventory system that is backed by JSON data and will be working with a starter file that contains a JSON string. The code you write will need to follow the following guidelines.

You’re at work one day and your boss asks you about that fancy programming language you’ve been learning, Python. She asks you if you can use it to read JSON data from a supplier and build an inventory. “Of course!” you say. She tells you that your parts supplier, Midwest Widget Co, has sent the company JSON data that needs to be loaded and then checked against orders. You’ll need to load the JSON data, and then take input from a user to order new parts. If the part is not in stock, or the number of parts is greater than the available inventory, you need to alert the user and show them that its either out of stock or that there are less available than requested. Once the user is done entering in their order, you’ll need to package up their order in JSON format, since that’s what Midwestern Widget Co uses after all!

The JSON data is comprised of a few nested dictionaries. In the top level, there is a key called “parts” that maps to a list that contains all the parts available for ordering. Each of those parts is also a key into the dictionary. That key maps to dictionary that has two values, quantity and price. The quantity maps to an integer with the number of parts available for order, the price maps to a float with the price of the part. A sample JSON object is provided below:

{

"parts":[

"widget",

"sprocket",

"thing-a-ma-bob"

],

"widget":{

"price":0.99,

"quantity":74

},

"sprocket":{

"price":3.99,

"quantity":123

},

"thing-a-ma-bob":{

"price":1.78,

"quantity":57

}

}

The strings in the list in parts are the keys for the rest of the data in the dictionary. Those keys map to separate dictionaries with the prices and quantities. The json string is provided in the code, it is called supplier_data.

The ordering system will work like this:

Prompt the user for input, and indicate they can enter in the word “quit” to quit. The user should enter in a part and then the quantity on two separate lines (so you’ll need two input statements). After both pieces of information have been entered in, check if the order is allowed or not, and display an error message with the appropriate information (part doesn’t exist, part exists but not enough quantity). If the order is valid, store it and continue. Once the user enters quit, print out an order summary showing the part, number ordered, the price per part and total per part with a grand total at the end. The order data MUST be stored in a dictionary. You must also allow the user to order a part more than once and validate that both orders are not exceeding the total amount available!

Sample Output:

Your program should produce output that is very similar if not identical to this. Major deviations from the formatting will result in points lost. The error handling behavior should be the same.

Welcome to the parts ordering system, please enter in a part name, followed by a quantity

Parts for order are:

sprocket

gizmo

widget

dodad

Please enter in a part name, or quit to exit: blargh

Error, part does not exist, try again

Please enter in a part name, or quit to exit: quit

Your order Total: $0

Thank you for using the parts ordering system!

And another run with an actual order

Welcome to the parts ordering system, please enter in a part name, followed by a quantity

Parts for order are:

sprocket

gizmo

widget

dodad

Please enter in a part name, or quit to exit: gizmo

Please enter in a quantity to order: 1000

Error, only 2 of gizmo are available!

Please enter in a part name, or quit to exit: gizmo

Please enter in a quantity to order: 1

Please enter in a part name, or quit to exit: sprocket

Please enter in a quantity to order: 15

Please enter in a part name, or quit to exit: widget

Please enter in a quantity to order: 1

Please enter in a part name, or quit to exit: quit

Your order

gizmo - 1 @ 7.98 = 7.98

sprocket - 15 @ 3.99 = 59.85

widget - 1 @ 14.32 = 14.32

Total: $82.15

Thank you for using the parts ordering system!

Another run with multiple orders for a single part

Welcome to the parts ordering system, please enter in a part name, followed by a quantity

Parts for order are:

sprocket

gizmo

widget

dodad

Please enter in a part name, or quit to exit: gizmo

Please enter in a quantity to order: 2

Please enter in a part name, or quit to exit: gizmo

Please enter in a quantity to order: 2

Error, only 0 of gizmo are available!

Please enter in a part name, or quit to exit: quit

Your order

gizmo - 2 @ 7.98 = 15.96

Total: $15.96

Thank you for using the parts ordering system!

Tips and Hints 1. Start on something small and get it working before moving on. Don’t try and tackle it all at once! 2. You’ll need to validate the input between inputs 3. You can use print() with no arguments to print out extra blank lines 4. A while loop is ideal for the main program loop 5. You’ll need to check if a part has already been ordered before checking if sufficient quantity is available. If it has been ordered, you’ll need to account for that when checking the inventory levels 6. supplier_data in the provided code is a JSON string. You need to convert it to a dict with json.loads

**CODE TEMPLATE**

# Program header goes here
#
#
#


supplier_data = '{"parts": ["sprocket", "gizmo", "widget", "dodad"], "sprocket": {"price": 3.99, "quantity": 32}, "gizmo": {"price": 7.98, "quantity": 2}, "widget": {"price": 14.32, "quantity": 4}, "dodad": {"price": 0.5, "quantity": 0}}'


# Your code goes here

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

import json

data = '{"parts": ["sprocket", "gizmo", "widget", "dodad"], "sprocket": {"price": 3.99, "quantity": 32}, "gizmo": {"price": 7.98, "quantity": 2}, "widget": {"price": 14.32, "quantity": 4}, "dodad": {"price": 0.5, "quantity": 0}}'

json_data = json.loads(data)

orders = {}
print("Welcome to the parts ordering system, please enter in a part name, followed by a quantity")

parts = json_data["parts"]
print("Parts for order are:")
for part in parts:
    print(part)

while True:
    part_name = input("Please enter in a part name, or quit to exit: ")

    if part_name == "quit":
        break
    elif part_name not in parts:
        print("Error, part does not exist, try again")

        continue

    quant = int(input("Please enter in a quantity to order: "))

    data_quant = json_data[part_name]["quantity"]

    ord_quant = orders.get(part_name, 0)

    actual_quant = data_quant-ord_quant

    if quant > actual_quant:
        print("Error, only {} of {} are available!".format(
            actual_quant, part_name))

        continue

    orders[part_name] = ord_quant + quant

print("Your order")
total = 0
for order in orders:
    price = json_data[order]["price"]
    order_price = price * orders[order]
    print("{} - {} @ {:.2f} = {:.2f}".format(order,
                                             orders[order], price, order_price))
    total += order_price

print("Total: ${:.2f}".format(total))
print("Thank you for using the parts ordering system!")


Related Solutions

Python Assignment You will be writing an inventory system that is backed by JSON data and...
Python Assignment You will be writing an inventory system that is backed by JSON data and will be working with a starter file that contains a JSON string. The code you write will need to follow the following guidelines. The what You’re at work one day and your boss asks you about that fancy programming language you’ve been learning, Python. She asks you if you can use it to read JSON data from a supplier and build an inventory. “Of...
Assignment Instructions Module 4 Writing Assignment (M1-4) You will soon find yourself fulfilling roles in organizations...
Assignment Instructions Module 4 Writing Assignment (M1-4) You will soon find yourself fulfilling roles in organizations where the ability to think ethically about issues will make you much more valuable to employers. In this activity you will refer to the materials in your text and lectures to respond to the questions. Part A The questions in Part A refer to the material discussed in Module 1 of this course. Respond to the following. Discuss reasons why ethics are an important...
In this assignment you are to make an inventory management system for a car dealership. The...
In this assignment you are to make an inventory management system for a car dealership. The dealerships current method of tracking inventory is using a list which has the color and name of each car they have in inventory. There current inventory list will look like the example below. RED TOYOTAWHITE HONDA...BLACK BMW The types of cars that they order are as follows: TOYOTA, HONDA, BMW, NISSAN, TESLA, DODGE The colors they purchase are as follows: RED, WHITE, BLUE, BLACK,...
In this assignment, you will create a well-formed JSON object which encapsulates the information specified. Alignment...
In this assignment, you will create a well-formed JSON object which encapsulates the information specified. Alignment This assignment aligns with the following objectives: Identify the differences between these three modes of interacting with data, i.e. web forms, programs, and APIs Recognize well-formed JSON objects Parse JSON objects Details Imagine that using the API provided by a service like Yelp (let's call it YelpYou) you have extracted information about Sichuan Dragon, which has three branches (restaurants) in New York City. The...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains data on past students in this course. Under Midterm is information on whether past studentsgot an A grade (A−, A, A+) an F or D grade (D is a passing grade but most students need aC− for it to count towards their program) or Other (any grade in between). Under FinalExam is information on whether students got a D or F grade or anything...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT