IN PYTHON
You are building a program for a Point of Sale (POS) machine for a fruit store. The inventory and price list are stored in a file (i.e. items.txt) with an example as follows:
banana 3 1.25
apple 6 1.75
orange 32 0.5
pear 55 2.5
For illustration, the first line implies that "There are 3 bananas in the inventory and the price of a banana is $1.25"
You have to write four functions:
1. The first function readstockprice takes as argument the filename (i.e. items.txt) as input and creates the following dictionaries:
a. A stock dictionary for which the key is the fruit name and the value is the numbers (int) of that fruit in the inventory
b. A price dictionary for which the key is the fruit name and the value is the price (float) of that fruits.
Now let us see what happens when a sale is made. A customer's order consists of a list of fruits and the numbers (int) they wish to purchase. This is in the form of a dictionary similar in structure to the stock dictionary.
2. The function readshoppinglist takes as argument the filename (i.e. shoppinglist.txt) and read the content of the file into shopping dictionary. For example, shoppinglist.txt contains following:
banana 2
apple 12
orange 6
3. The function process_order takes as argument the customer's shopping list dictionary shopping and stock & price dictionaries. This function does three tasks:
a. Print number of items sold for each item where stock quantity is more than or equal to sell quantity.
b. Update the store inventory for each item sold to the customer and only include in the total price actual items sold to the customer if the item is available in the store. That is, if the item’s value in stock is 0, the customer is no longer sold that item and is not charged for it.
c. Print the total cost of the sale.
Example output:
You bought 2 banana
You bought 6 orange
Cost of items sold is: 5.5
4. Function updatestockprice takes as argument the filename (i.e. items.txt), stock & price dictionaries as inputs and writes them back to file (i.e. items.txt) in the similar format. For example after the function is called item.txt should have:
banana 1 1.25
apple 6 1.75
orange 26 0.5
pear 55 2.5
All the explanation is in the code comments. Hope this helps!
Please take care of indentation in python by referring to code screenshots.
Code:
import os
# function takes as argument the filename (i.e. items.txt) as
input
# and creates the following dictionaries -
# A stock dictionary, key -> fruit name and value -> numbers
(int) of that fruit
# A price dictionary, key -> fruit name and value -> price
(float) of that fruit
def readstockprice(filename):
# open file
file = open(filename)
stock = {}
price = {}
# read line by line from file
for line in file:
# split the line to get item
name, stock and price
val = line.split()
item = val[0]
# convert to required
datatype
numbers = int(val[1])
cost = float(val[2])
# add it to the dictionary
stock[item] = numbers
price[item] = cost
# close the file
file.close()
# return stock and price dictionary
return stock, price
# function takes as argument the filename (i.e.
shoppinglist.txt)
# and read the content of the file into shopping dictionary
def readshoppinglist(filename):
# open file
file = open(filename)
shopping = {}
# read line-by-line from shopping list
for line in file:
# get the item name and its
quantity
val = line.split()
item = val[0]
numbers = int(val[1])
# add to the shoppinglist
dictionary
shopping[item] = numbers
# close file
file.close()
# return the shopping dictionary
return shopping
# function takes as argument the customer's shopping list
dictionary shopping and stock & price dictionaries.
# This function does three tasks -
# 1. Print number of items sold for each item where stock quantity
is more than or equal to sell quantity.
# 2. Update the store inventory for each item sold to the
customer
# and only include in the total price actual items sold to the
customer if the item is available in the store.
# That is, if the item’s value in stock is 0, the customer is no
longer sold that item and is not charged for it.
# 3. Print the total cost of the sale.
def process_order(shopping, stock, price):
total_cost = 0
for item in shopping:
# 1 -> print number of items
sold
if(stock[item] >=
shopping[item]):
item_qty_sold
= shopping[item]
print('You
bought', item_qty_sold, item)
# 2 ->
update the stock inventory
stock[item] =
stock[item] - item_qty_sold
# add to the
total price
total_cost =
total_cost + (item_qty_sold * price[item])
# 3-> print total cost
print('Cost of items sold is:', total_cost)
# Function takes as argument the filename (i.e. items.txt),
stock & price dictionaries as inputs
# and writes them back to file (i.e. items.txt) in the similar
format.
def updatestockprice(filename, stock, price):
# write to temporary file
temp_file = open('temp.txt', 'w')
# loop through each item and write in the same
format
for item in stock:
temp_file.write(item + ' ' +
str(stock[item]) + ' ' + str(price[item]) + '\n')
# close the file
temp_file.close()
# remove the old items.txt file
os.remove(filename)
# rename the temporary file to items.txt
os.rename('temp.txt', filename)
# function calls
stock, price = readstockprice('items.txt')
shopping = readshoppinglist('shoppinglist.txt')
process_order(shopping, stock, price)
updatestockprice('items.txt', stock, price)
Sample output:


Code screenshots:



IN PYTHON You are building a program for a Point of Sale (POS) machine for a...
In Python 3 only please. A simple function that will be used on
a file.
commonpair(str) – Takes a single string
argument, representing the first word. This function should return
the word that most frequently followed the given argument word (or
one of, in case of ties). If the argument word does not appear in
the text at all, or is never followed by another word (i.e., is the
last word in the file), this function should return None.
I...
C++ language
Shopping Cart You are to write a program that reads the name of the item, the quantity of each item, and the cost per item from a file. The program will then print out the item and how much is owed for each item. After everything is read in, it will print out the total cost of the cart. Your program should have the following functions main - This function asks the user to enter a filename and...
In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...
In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...
Write a C++ program to manage a Point of Sale System for a Supermarket. The main user is an employee at the Supermarket. Build Specifications (36 points) 1. The system should load a catalog of all items that the store sells. 2. A user can search the inventory: The user of the system can search the inventory by using the name of the item or by category. 3. A user can sell items once they are found. Or can sell...
please use Java!!
We are storing the inventory for our fruit stand in a hash table. The attached file shows all the possible key/fruit combinations that can be inserted into your hash table. These are the real price lookup values for the corresponding fruit. Problem Description In this programming assignment, you will implement a hash table class fruitHash. Your hash table must include the following methods: 1) hashFunction(key) This method takes a key as an argument and returns the address...
Help needed with Python 3: Dictionaries and Sets.
The problem is one that asks the user to create a completed
program that prompts the user for the name of a data file, and
reads the contents of that data file, then creates variables in a
form suitable for computing the answer to some questions.
The format should be in the same manner as the attached .py
file. All subsequent assignment details (.py and .csv files) can be
found at this...
Please have the function written in python, thank you!
Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class Pharmacy: det init (selt, inventory, unit_prices) self.inventory -inventory self.unit_pricesunit_prices class Prescription: definit_(self, patient, drug_name, quantity): self.patient patient self.drug_name - drug_name self.quantity -quantity You will be asked to write several functions that work with the Prescription and Pharmacy classes. To complete this assignment you may write any helper functions you like inside...
Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...
Vle/content/731186/fullscreen/3876261/View n3x You are the owner of three small grocery stores in your city. You want to know how much money you made last week selling pre-packaged gift boxes of fruit (a box contains just apples, oranges, pears, or plums). You purchase the fruit boxes wholesale from your distributor (apples S3.20/box, oranges $3.65/box, pears $4.00/box, plums $3.50 box). Your selling price includes a 30% markup and is rounded to the nearest dime. Note: Markup % s(selling price-cost) /cost x100 For...