Last active
February 24, 2021 21:26
-
-
Save hbersey/01f054f1f012ca652dfac1bf1a8f0f42 to your computer and use it in GitHub Desktop.
Pre-Release Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TYPE Book | |
DECLARE Id: INTEGER 100...999 | |
DECLARE Title: STRING | |
DECLARE Author: STRING | |
DECLARE YearPublished: INTEGER | |
ENDTYPE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book: | |
def __init__(self, book_id, title, author, year_published): | |
self.book_id = book_id # INTEGER | |
self.title = title # STRING | |
self.author = author # STRING | |
self.year_published = year_published # INTEGER | |
N = 1 # INTEGER | |
MIN_ID = 100 # INTEGER | |
MAX_ID = 999 # INTEGER | |
if __name__ == "__main__": | |
books = [] # Array [1:10] OF BOOK | |
# Input The Data | |
for i in range(N): | |
print("Book #" + str(i + 1)) | |
valid_id = False | |
while not valid_id: | |
try: | |
book_id = int(input("Enter Book ID: ")) | |
if not book_id in range(MIN_ID, MAX_ID + 1): | |
raise ValueError() | |
valid_id = True | |
except ValueError: | |
print("Please enter a valid ID between", MIN_ID, "and", MAX_ID, "(inclusive).") | |
title = input("Enter Book Title: ") | |
author = input("Enter Book Author: ") | |
valid_year = False | |
while not valid_year: | |
try: | |
year_published = int(input("Enter Book's Year of Publication: ")) | |
valid_year = True | |
except ValueError: | |
print("Please enter a valid year.") | |
book = Book(book_id, title, author, year_published) | |
books.append(book) | |
# Output The Data | |
for book in books: | |
print(book.title + " (ID: " + str(book.book_id) + "), by " + book.author + " (" + str(book.year_published) + ")") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONSTANT DELIMETER = '|' | |
PROCEDURE SaveBooks(Books: Array[1:10] OF Book, Path: STRING) | |
DECLARE OutFile: FILE | |
DECLARE Index: INTEGER | |
DECLARE CurrentBook: Book | |
DECLARE Line: STRING | |
OPENFILE Path, OutFile FOR WRITE | |
FOR Index <-- 1 TO 10 | |
CurrentBook <-- Books[Index] | |
Line <-- CurrentBook.Id + DELIMETER + CurrentBook.Title + DELIMETER + CurrentBook.Author + DELIMETER + CurrentBook.Published | |
WRITEFILE OutFile, Line | |
ENDFOR | |
CLOSEFILE OutFile | |
ENDPROCEDURE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMETER = '|' # CHAR | |
N = 10 # INTEGER | |
def save_books(books, path): # books: Array[1:10] OF Book, path: STRING | |
file = open(path, "w") # FILE | |
for i in range(10): | |
current_book = books[i] | |
line = str(book.book_id) + DELIMETER + book.title + DELIMETER + book.author + DELIMETER + str(book.year_published) + '\n' | |
file.write(line) | |
file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONSTANT DELIMETER = '|' | |
PROCEDURE PrintBookFile(Path: STRING) | |
ENDPROCEDURE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment