• Language

    Python

  • Description

    At the beginning of the program read the library data from a file called "books.txt". The file is organized as book name, status (borrowed or not borrowed), customer name, as follows:
    C++_How_to_program A None
    DATA_STRUCTURE B Bob
    . . .
    The letter A means available, the letter B means borrowed, and None means no customer borrowed the book.
    The choices are as follows:
    1) Choice 1: Add a book: the user is asked to enter the name of a book.
    4) Choice 4: Search a book: the program should display all the books in the library with their status and customer name.
    5) Choice 5: Save the data into the file and terminate the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import sys
from subprocess import call
from platform import system

path = 'books.txt'
os = system()
CLEAR_SCREEN = 'clear' if os == 'Linux' else 'cls'
main_option = '0'

def getch ():
    if os == 'Linux':
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw (sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr (fd, termios.TCSADRAIN, old_settings)
        return ch
    else:
        import msvcrt, sys
        sys.stdout.flush()
        return msvcrt.getwch()

def pause_screen (message):
    print (message)
    sys.stdout.write ('Press any key to continue . . . ')
    getch()

class Book:

    def __lt__(self, other):
        return self.name < other.name

    def __eq__(self, other):
        return self.name == other.name

    def __str__(self):
        text = ''
        text += 'name         : ' + self.name + '\n'
        text += 'status       : ' + repr (self.status) + '\n'
        text += 'customer name: ' + self.customer_name + '\n'
        return text

    def print_on_file(self, file_stream):
        file_stream.write (self.name + '\t')
        file_stream.write (repr (self.status) + '\t')
        file_stream.write (self.customer_name + '\n')

book = Book()
array_var = []
try:
    file_stream = open (path, 'r')
    for string_line in file_stream:
        fields = string_line.strip().split('\t')
        datum = Book()
        datum.name = name
        datum.status = status[0]
        datum.customer_name = customer_name
        array_var.append (datum)
    file_stream.close()
except IOError:
    file_stream = None
while main_option!='7':
    call (CLEAR_SCREEN, shell = True)
    print('MENU')
    print ('1.- Insertions\n2.- Queries\n3.- Updates')
    print ('4.- Deletions\n5.- Sort records\n6.- List records')
    sys.stdout.writeln ('7.- Exit');
    sys.stdout.write ('Select an option: ')
    main_option = '0'
    while main_option<'1' or main_option>'7':
        main_option = getch ()
    print (main_option + '\n\n')
    if len(array_var)==0 and main_option!='1' and main_option!='7':
        pause_screen ('There are not records.\n')
        continue
    if main_option<'5':
        book.name = input ('Enter the name of the book: ')
        datum = array_var[array_var.index (book)] if book in array_var else None
        if datum is not None:
            print ()
            print (datum)
    if main_option=='1' and datum is not None:
        print ('The record already exists.')
    elif main_option>='2' and main_option<='4' and datum is None:
        print ('\nRecord not found.')
    elif main_option=='1':
        datum = Book()
        datum.name = book.name
        datum.status = input ('Enter the status: ')[0]
        datum.customer_name = input ('Enter the customer name: ')
        array_var.append (datum)
        print ('\nRecord added correctly.')
    elif main_option=='3':
        print ('Menu of fields updating')
        print ('1.- status')
        print ('2.- customer name')
        suboption = 0
        while suboption<1 or suboption>2:
            suboption = int (input ('Select a field number to be updated: '))
            if suboption<1 or suboption>2:
                print ('Incorrect option.')
        if suboption==1:
            datum.status = input ('Enter the status: ')[0]
        elif suboption==2:
            datum.customer_name = input ('Enter the customer name: ')
        print ('\nRecord updated correctly.')
    elif main_option=='4':
        array_var.remove (datum)
        print ('Record removed correctly.')
    elif main_option=='5':
        array_var.sort()
        print ('Records sorted correctly.')
    elif main_option=='6':
        for e in array_var:
            print (e)
        print ('Total of records: ' + repr (len (array_var)))
    if main_option!='7':
        pause_screen ('')
try:
    file_stream = open (path, 'w')
    for e in array_var:
        e.print_on_file (file_stream)
    file_stream.close()
except IOError:
    file_stream = None