Home > Net >  If a user checks out 5 copies of book1 and returns 3 returns 2 and then returns 1 more it allows it
If a user checks out 5 copies of book1 and returns 3 returns 2 and then returns 1 more it allows it

Time:08-01

if you choose to return a book at the start of the program, without checking out any books,

then it won't allow the return (intended). So say the user wants to check out 5/10 copies of

book1. It checks them out and subtracts them from the copies on shelf. They choose to return

3 one day, and 2 another day. (For testing purposes) the day after that, they return another

copy of book1, which is more than they checked out. It appends it to the book1.borrow aka

num of copies. It should not allow the user to return more copies than the original numbr of copies. So this is what I have in my main module. I can add the class LibraryBook if

needed.

from LibraryBook import *

book1 = LibraryBook()
book1.title = "not set"
book1.year = "no se"
book1.author = "set not"
book1.borrow = 10

book2 = LibraryBook()
book2.title = "tes ton"
book2.year = "es on"
book2.author = "ton tes"
book2.borrow = 8

selection = 0
amount = 0
amount1 = 0

while selection != 5:
    print("Select an option")
    print('1 - Borrow a book')
    print('2 - Return a book')
    print('3 - See info on available books')
    print('4 - Exit')
    print()
    selection = int(input("What would you like to do? "))
    print()
    if selection == 1:
        print("Book 1 is: ", book1.title)
        print("Book 2 is: ", book2.title)
        print()
        bookNum = int(input("Enter the book number. "))
        print()
        if bookNum == 1:
            print()
            amount = int(input("Enter the number of copies you would like to check out. "))
            print()
            if (amount <= book1.borrow) and (amount > 0):
                book1.borrowed(amount)
            else:
                print("We only have", book1.borrow, "copies available.")
                print()
        elif bookNum == 2:
            print()
            amount = int(input("Enter the number of copies you would like to check out. "))
            print()
            if (amount <= book2.borrow) and (amount > 0):
                book2.borrowed(amount)
            else:
                print("We only have", book2.borrow, "copies available.")
                print()
        else: 
            print()
            print("That book is not here. Enter 1 or 2. ")
            print()


    if selection == 2:
        print("Book 1 is: ", book1.title)
        print("Book 2 is: ", book2.title)
        print()
        bookNum = int(input("Which book would you like to return? "))
        print()
        if bookNum == 1:
            print()
            amount1 = int(input("Enter the amount of copies you would like to return. "))
            print()
            if amount1 <= amount:
                book1.returned(amount1)
                print()
            elif book1.borrow == book1.borrow:
                print("Invalid number of copies. Count the books and try again ")
                print()
            else:
                print("You only checked out", amount)
                print()
        elif bookNum == 2:
            print()
            amount1 = int(input("Enter the amount of copies you would like to return. "))
            print()
            if amount1 <= amount:
                book2.returned(amount1)
                print()
            elif book2.borrow == book2.borrow:
                print("You did not check out this book.")
                print()
            else:
                print("You only checked out", amount)
                print()
        else: 
            print("Invalid selection. Choose book 1 or book 2.")
            print()
                      

    if selection == 3:
        print()
        print("Book 1:")
        print(book1.__str__())
        print()
        print("Book 2:")
        print(book2.__str__())
        print()

    if (selection <= 0) or (selection >= 5):
        print("Invalid selection. Enter a number 1-4. ")
        print()

    if selection == 4:
        break
    

CodePudding user response:

I'm not the best programmer but I think I made and example here Ofc is not the best I made it larger for you to understand, there are some bugs like when asked for book if u give bigger then 2 will break but I let you fix it ;D

import sys, os, time, re
class LibraryBook(object):
    def __init__(self, title, year, author, borrow):
        self.title = title
        self.year = year
        self.author = author
        self.borrow = borrow
    def borrow_add(self, amount: int):
        self.borrow  = amount
    def left(self):
        return self.borrow
    def get_title(self):
        return self.title
    def borrow_sell(self, amount: int):
        self.borrow -= amount
    def get_book_info(self):
        val = f'''
Book Title: {self.title}
Book Year: {self.year}
Book Author: {self.author}
Left in stock: {self.borrow}
        '''
        return val



book1 = LibraryBook('Prison Break', 2016, 'My House', 8)
book2 = LibraryBook('Hello', 1999, 'NO one', 10)

###########
#That is so you can do book3,4,5 etc.. without write in print all books on hand ;D
books = re.findall('book.', (str(globals().copy())).replace("'", ''))
books_titles = [eval(f'{o}.get_title()') for o in books]
##########
user_lib =[]

#You can use that in a function (def) and call it when you need it
while True:
    os.system('cls')
    option = int(input('''
Select an option
1 - Borrow a book
2 - Return a book
3 - See info on available books
4 - See my lib
5 - Exit
>>> '''))
    if option == 1:
        option_2 = input("\n".join(f"Book{o 1}:{i}" for o, i in enumerate(books_titles)) '\nWhat book(ex: 1):')
        option_3 = int(input('How much?: '))
        if int(eval(f'book{option_2}.left()'))-option_3 >= 0: 
            if x:=list(filter(lambda book: book['name'] == eval(f'book{option_2}.title'), user_lib)):
                user_lib[user_lib.index(x[0])]['amount']  =option_3
            else:
                user_lib.append({'name': eval(f'book{option_2}.get_title()'), 'amount': option_3})
            print(f'You borrowed {option_3} books')
            eval(f'book{option_2}.borrow_sell({option_3})') #Remove from store
        else:
            print(f'We have {eval(f"book{option_2}.left()")} books left...')
        time.sleep(1) # wait 1 second for user to read then reset

    elif option == 2:
        option_2 = input("\n".join(f"Book{o 1}:{i}" for o, i in enumerate(books_titles)) '\nWhat book(ex: 1):')
        if len(x:=list(filter(lambda book: book['name'] == eval(f'book{option_2}.get_title()'), user_lib))) > 0:
            option_3 = int(input('How much?: '))
            if user_lib[pos:=user_lib.index(x[0])]['amount'] -option_3 >= 0:
                user_lib[pos]['amount'] -= option_3
                if user_lib[pos]['amount'] == 0:
                    user_lib.pop(pos)
                print(f'You returned {option_3} books')
                eval(f'book{option_2}.borrow_add({option_3})') # Add them back to shop
            else:
                print(f"You don't have enogh books, you have only: {x[0]['amount']}")
            time.sleep(1) # wait 1 second for user to read then reset
        else:
            print("You don't any books of thta kind")
        time.sleep(1)
    elif option == 3:
        option_2 = input("\n".join(f"Book{o 1}:{i}" for o, i in enumerate(books_titles)) '\nWhat book(ex: 1):')
        print(eval(f'book{option_2}.get_book_info()'))
        input('Press enter to continue...')
    elif option == 4:
        print("\n".join(f"Book Name:{user_lib[o]['name']}\nAmount I have:{user_lib[o]['amount']}" for o, i in enumerate(user_lib)) if user_lib != [] else "You don't have any books")
    elif option == 5:
        sys.exit(1)
    else:
        print('Invalid number')
    time.sleep(1) # wait 1 second for user to read then reset
  • Related