|
| 1 | +https://powcoder.com |
| 2 | +代写代考加微信 powcoder |
| 3 | +Assignment Project Exam Help |
| 4 | +Add WeChat powcoder |
| 5 | +# You should write your Python program in this file. Currently, it contains |
| 6 | +# a skeleton of the methods you will need to write. |
| 7 | + |
| 8 | +import csv |
| 9 | +import os |
| 10 | +import sqlite3 |
| 11 | + |
| 12 | + |
| 13 | +# in Python, we specify functions using "def" -- this would be equiv to Java |
| 14 | +# `public void load_data()`. Note that Python doesn't specify return types. |
| 15 | +def load_data(): |
| 16 | + # This function should: |
| 17 | + # 1) create a new database file called "library.db" |
| 18 | + # 2) create appropiate tables |
| 19 | + # 3) read the data.csv file and insert data into your database |
| 20 | + |
| 21 | + # first, we will check to see if library.db already exists. |
| 22 | + # if it does, we will delete it. |
| 23 | + if os.path.exists("library.db"): |
| 24 | + os.remove("library.db") |
| 25 | + |
| 26 | + # next, we will create a new SQLite database. |
| 27 | + |
| 28 | + # create a database connection. |
| 29 | + conn = sqlite3.connect("library.db") |
| 30 | + |
| 31 | + # create a cursor (this is like a single session) |
| 32 | + curr = conn.cursor() |
| 33 | + |
| 34 | + # send a pragma command to tell SQLite to check foreign key |
| 35 | + # constraints (it does not by default :( ) |
| 36 | + curr.execute("PRAGMA foreign_keys = ON;") |
| 37 | + |
| 38 | + # here's an example for how to create a table and insert 2 rows into it |
| 39 | + # (note the `?` syntax to put values into the query) |
| 40 | + curr.execute("CREATE TABLE t ( val1 INTEGER, val2 TEXT )") |
| 41 | + curr.execute("INSERT INTO t VALUES(?, ?)", |
| 42 | + (8, "this is a test")) |
| 43 | + curr.execute("INSERT INTO t VALUES(?, ?)", |
| 44 | + (64, "this is another test")) |
| 45 | + |
| 46 | + |
| 47 | + # commit is like save -- if you don't do it, nothing is written. |
| 48 | + conn.commit() |
| 49 | + |
| 50 | + # here's an example of how to read data from the database |
| 51 | + curr.execute("SELECT * FROM t") |
| 52 | + for row in curr: |
| 53 | + # each time through the loop, row[0] will be the first column |
| 54 | + # of the result, and row[1] will be the second. |
| 55 | + print(row[0], row[1]) |
| 56 | + |
| 57 | + conn.close() # close the DB connection when we are done |
| 58 | + |
| 59 | + # here's how to read a CSV file. |
| 60 | + with open("data.csv") as f: |
| 61 | + reader = csv.reader(f) |
| 62 | + next(reader) # throw out the header row |
| 63 | + |
| 64 | + for row in reader: |
| 65 | + # row[0] is the first column of the first row |
| 66 | + # row[1] is the second column of the first row |
| 67 | + print(row) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +def overdue_books(date_str): |
| 72 | + # This function should take in a string like YYYY-MM-DD and print out |
| 73 | + # a report of all books that are overdue as of that date, and the |
| 74 | + # patrons who still have them. |
| 75 | + |
| 76 | + pass # delete this when you write your code |
| 77 | + |
| 78 | +def most_popular_books(): |
| 79 | + # This function should print out a report of which books are the |
| 80 | + # most popular (checked out most frequently). The library cares about |
| 81 | + # the books themselves, not who published them. |
| 82 | + |
| 83 | + pass # delete this when you write your code |
| 84 | + |
| 85 | +def note_return(patron_card, book_barcode): |
| 86 | + # This function should update the database to indicate that the patron |
| 87 | + # with the passed card number has returned the book with the given |
| 88 | + # barcode. This function should print out an error if that patron didn't |
| 89 | + # have the book currently checked out. |
| 90 | + |
| 91 | + pass # delete this when you write your code |
| 92 | + |
| 93 | +def note_checkout(patron_card, book_barcode, checkout_date): |
| 94 | + # This function should update the database to indicate that a patron |
| 95 | + # has checked out a book on the passed date. The due date of the book |
| 96 | + # should be 7 days after the checkout date. This function should print |
| 97 | + # out an error if the book is currently checked out. |
| 98 | + |
| 99 | + pass # delete this when you write your code |
| 100 | + |
| 101 | +def replacement_report(book_barcode): |
| 102 | + # This function will be used by the library when a book has been lost |
| 103 | + # by a patron. It should print out: the publisher and publisher's contact |
| 104 | + # information, the patron who had checked out the book, and that patron's |
| 105 | + # phone number. |
| 106 | + |
| 107 | + pass # delete this when you write your code |
| 108 | + |
| 109 | +def inventory(): |
| 110 | + # This function should report the library's inventory, the books currently |
| 111 | + # available (not checked out). |
| 112 | + |
| 113 | + pass # delete this when you write your code |
| 114 | + |
| 115 | +# this is the entry point to a Python program, like `public static void main` |
| 116 | +# in Java. |
| 117 | +if __name__ == "__main__": |
| 118 | + while True: |
| 119 | + print("Hello! Welcome to the library system. What can I help you with today?") |
| 120 | + print("\t1) Load data") |
| 121 | + print("\t2) Overdue books") |
| 122 | + print("\t3) Popular books") |
| 123 | + print("\t4) Book return") |
| 124 | + print("\t5) Book checkout") |
| 125 | + print("\t6) Book replacement") |
| 126 | + print("\t7) Inventory") |
| 127 | + print("\t8) Quit") |
| 128 | + |
| 129 | + user_response = int(input("Select an option: ")) |
| 130 | + |
| 131 | + if user_response == 1: |
| 132 | + load_data() |
| 133 | + elif user_response == 2: |
| 134 | + date = input("Date (YYYY-MM-DD): ") |
| 135 | + overdue_books(date) |
| 136 | + elif user_response == 3: |
| 137 | + most_popular_books() |
| 138 | + elif user_response == 4: |
| 139 | + patron = input("Patron card: ") |
| 140 | + book = input("Book barcode: ") |
| 141 | + note_return(patron, book) |
| 142 | + elif user_response == 5: |
| 143 | + patron = input("Patron card: ") |
| 144 | + book = input("Book barcode: ") |
| 145 | + chd = input("Checkout date (YYYY-MM-DD): ") |
| 146 | + note_checkout(patron, book, chd) |
| 147 | + elif user_response == 6: |
| 148 | + book = input("Book barcode: ") |
| 149 | + replacement_report(book) |
| 150 | + elif user_response == 7: |
| 151 | + inventory() |
| 152 | + elif user_response == 8: |
| 153 | + break |
| 154 | + else: |
| 155 | + print("Unrecognized option. Please try again.") |
| 156 | + |
| 157 | + |
0 commit comments