Python代写:CSE101 Pharmacy

代写基础的Python作业,完成一个关于Pharmacy的小应用程序。由于是Lab作业,按照描述一步步往下写即可。

Assignment Objectives

This lab will give you some hands-on practice with object-oriented programming using Python to solve some simple problems.

Getting Started

For the labs in this course you will write functions that solve computational problems. To help you get started on each assignment, we will give you on Blackboard a “bare bones” file with a name like lab8.py. These files will contain function stubs, which are functions that have no bodies. You will fill in the bodies of these functions for the assignments. Do not, under any circumstance, change the names of the functions or their parameter lists. The automated grading system will be looking for exactly those functions provided in lab8.py. You will be able to test your work by uploading your file to CodeLoad. Note that the CodeLoad tests are not exhaustive - you will want to write your own tests too!

Directions

  • Solve the following problems to the best of your ability.
  • At the top of the lab8.py file, include the following information in comments, with each item on a separate line:
    • your first and last name as they appear in Blackboard
    • your Stony Brook ID # (e.g., 110999999)
    • your Net ID (e.g., jsmith)
    • the course number (CSE 101)
    • the assignment name and number (Lab #8)
  • Your functions must be named as indicated below. Submissions that have the wrong function names can’t be graded by the automated grading system.
  • Upload your .py file to Blackboard by the indicated due date and time. Late work will not be accepted for grading. Work is late if it is submitted after the due date and time.
  • Code that crashes will likely receive a grade of zero, so make sure you upload your work to CodeLoad and also thoroughly test your work with additional tests before submitting it.

Disclaimer

Although all drugs used in this assignment are real, they are randomly picked. All drug prices and individual names are made up and do correspond with any individual or group in any aspect.

Preliminaries

For this lab you will be working with the following classes:

1
2
3
4
5
6
7
8
9
10
class Prescription:
def __init__(self, patient, med_name, quantity):
self.patient = patient
self.med_name = med_name
self.quantity = quantity

class Pharmacy:
def __init__(self, inventory, unit_costs):
self.inventory = inventory
self.unit_costs = unit_costs

For this lab you will be working with the following example Pharmacy object called pharmacy created using the following inventory and unit costs dictionaries (different numbers will be assigned for the actual grading):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
inventory = {'Hydrocodone': 78, 'Zocor' : 25,
'Lisinopril': 150, 'Synthroid' : 32,
'Norvasc' : 100, 'Prilosec' : 44,
'Lisinopril': 150, 'Synthroid' : 32,
'Azithromycin' : 78, 'Amoxicillin' : 26,
'Lisinopril': 150, 'Synthroid' : 32,
'Glucophage' : 17, 'Hydrochlorothiazide' : 39,
'Lisinopril': 150, 'Synthroid' : 32,
'Crestor' : 28, 'Cymbalta' : 55,
'Lisinopril': 150, 'Synthroid' : 32,
'Adderall' : 0, 'Ibuprofen' : 44}
unit_costs = {'Hydrocodone': 4, 'Zocor' : 5,
'Lisinopril': 150, 'Synthroid' : 32,
'Lisinopril': 10, 'Synthroid' : 1,
'Lisinopril': 150, 'Synthroid' : 32,
'Norvasc' : 4, 'Prilosec' : 9,
'Lisinopril': 150, 'Synthroid' : 32,
'Azithromycin' : 4, 'Amoxicillin' : 8,
'Lisinopril': 150, 'Synthroid' : 32,
'Glucophage' : 1, 'Hydrochlorothiazide' : 13,
'Lisinopril': 150, 'Synthroid' : 32,
'Crestor' : 12, 'Cymbalta' : 9,
'Lisinopril': 150, 'Synthroid' : 32,
'Adderall': 5}

You will be asked to write three functions inside the Pharmacy class. To complete this lab you may write any helper functions inside Pharmacy you like. Just make sure to include them inside your lab8.py file.

Class Parameters

  • patient: The patient’s name (a string).
  • med name: The medicine’s name for the particular prescription (a string)
  • quantity: The quantity specified for the particular prescription (an integer)
  • inventory: A dictionary that represents the drug inventory in the particular pharmacy. The dictionary maps the drug name (string) to its stock amount (integer) in units. The unit is adaptive, meaning that you don’t need to worry about whether it counts in pills or milliliters or some other unit.
  • unit_costs: A dictionary that represents a look-up database that serves as a price book. The dictionary maps the drug name (string) to its price (integer).

Part I: Medication Dispensing

Write a function dispense() inside the Pharmacy class that takes one parameter, called prescription, which is a Prescription object (the Prescription class is provided for you in the lab8.py file).

Your function should read information from the passed in Prescription object, determine the drug name and quantity, then update the stocking amount in the pharmacy’s inventory dictionary. After updating the correct stocking amount of a drug in the pharmacy inventory dictionary, your function should return the updated stocking amount.

Note: Your program should be able to handle the invalid cases listed below. For these cases, your function should return None and make no changes to any value in the inventory dictionary. Invalid cases your function should handle are:

  • The stocked amount of the drug is not enough to fill the prescription. (For example, the prescription is for
    50 pills but only 30 are in stock.)
  • The quantity requested in the prescription is zero or negative.
  • The drug does not exist in the inventory. (That is, the name is invalid.)

Examples:

1
2
3
prescription1 = Prescription('Alice', 'Hydrocodone', 55)
prescription2 = Prescription('Jacob', 'Glucophage', 17)
prescription3 = Prescription('Sean', 'Zocor', 39)

Function Call Return Value Updated Dictionary Value
pharmacy.dispense(prescription1) 23 23
pharmacy.dispense(prescription2) 0 0
pharmacy.dispense(prescription3) None 25

Remember: CodeLoad has additional tests for you to try! Upload your code there and see how your code matches up against harder tests.

Part II: Restock Medication

Write a function restock_drug() that takes two parameters, in this order:

  1. name: The name of the drug that you want to restock
  2. amount: The amount of units you want to restock for that particular drug.

Your function should update (replenish/restock) the pharmacy inventory based on the passed in medication name and amount. After you have updated the inventory amount in the inventory dictionary, return the updated inventory stocking amount of that particular drug.

Special Cases

Your program should be a able to handle the case when the restock amount is an invalid integer (when it’s less than or equal to zero). In that case, return None.

Your function should also handle the case when the medication you are planning to restock doesn’t exist in the inventory. If the drug exists in the unit costs dictionary, add the medication to the inventory, set the inventory stocking amount to be amount, then return amount. If the drug does not exist in the unit costs dictionary, return None.

Examples:

Function Call Return Value Updated Dictionary Value
pharmacy.restock_drug(‘Hydrocodone’, 12) 90 90
pharmacy.restock_drug(‘Prilosec’, 33) 77 77
pharmacy.restock_drug(‘Adderall’, 50) 50 50

Remember: CodeLoad has additional tests for you to try! Upload your code there and see how your code matches up against harder tests.

Part III: Medication Batch Dispensing

Write a function batch_dispense() that takes one parameter called prescriptions. This variable is a list of Prescription objects, meaning that each element in the list is an individual Prescription object.

Your function should iterate through the list, inspect each individual Prescription object, then dispatch the order/prescription by updating the inventory dictionary as needed. The special invalid cases listed in Part I still apply. In the end, your program should return the total cost for all prescriptions in the list: the function will need to look-up the unit price for each drug in the unit costs dictionary located in the Pharmacy class.

Note: While you iterate though each Prescription object in prescriptions, it is possible that the errors arise due to invalid data (for example, there are not enough pills in stock to fill a prescription). In all invalid cases, simply skip that prescription and continue to inspect the next prescription. If all prescriptions are invalid, the function will return zero because they are all skipped.

Extra Invalid Case: In addition, your program should also handle the case when the passed-in list is an empty list. In that case, simply return zero.

Examples:

1
2
3
4
5
6
7
prescription1 = Prescription('Alice', 'Hydrocodone', 55)
prescription2 = Prescription('Mike', 'Chantix', 54)
prescription3 = Prescription('Sean', 'Zocor', 21)
prescription4 = Prescription('John', 'Prilosec', -38)
prescriptions1 = [prescription1]
prescriptions2 = [prescription2]
prescriptions3 = [prescription3, prescription4]

Function Call Return Value Updated Dictionary Value
pharmacy.batch_dispense(prescriptions1) 230 ‘Hydrocodone’: 23
pharmacy.batch_dispense(prescriptions2) 0 ‘Chantix’: Drug does not exist
pharmacy.batch_dispense(prescriptions3) 105 ‘Zocor’: 4, ‘Prilosec’: 44

Remember: CodeLoad has additional tests for you to try! Upload your code there and see how your code matches up against harder tests.

How to Submit Your Work for Grading

To submit your .py file for grading:

  1. Login and locate the course account for CSE 101.
  2. Go to the “Class Materials” folder for this week’s lab assignments.
  3. Click on the link for this lab assignment.
  4. Click the “Browse My Computer” button and locate the .py file you wish to submit. You should be submitting only one file!
  5. Click the “Submit” button to submit your work for grading.

Oops, I messed up and I need to resubmit a file!

No worries! Just follow the above directions again. We will grade only your last submission.