From d42ef7058da3be83c9e24c03b6bdc3183f000e13 Mon Sep 17 00:00:00 2001 From: Cassycq Date: Sun, 17 Aug 2025 19:42:29 +1000 Subject: [PATCH 1/5] workshop5-Cassy --- work_shop5/combine.py | 27 +++++++++++++++++++++++++++ work_shop5/puzzle1-out.txt | 10 ++++++++++ work_shop5/puzzle2-out.txt | 9 +++++++++ 3 files changed, 46 insertions(+) create mode 100644 work_shop5/combine.py create mode 100644 work_shop5/puzzle1-out.txt create mode 100644 work_shop5/puzzle2-out.txt diff --git a/work_shop5/combine.py b/work_shop5/combine.py new file mode 100644 index 0000000..7c70017 --- /dev/null +++ b/work_shop5/combine.py @@ -0,0 +1,27 @@ +file_name = input("Please enter the file name you want to combine: ") +try: + f1 = open("./data/" + file_name + "-A.txt","rt") + f2 = open("./data/" + file_name + "-B.txt","rt") + filename_out = open(file_name + "-out.txt","at") +except FileNotFoundError: + print("File not found") +line_num_odd = 1 +line_num_even = 1 +while True: + odd_line = f1.readline() + if not odd_line: + break + if line_num_odd %2 ==1: + filename_out.write(odd_line) + line_num_odd += 1 + even_line = f2.readline() + if not even_line: + break + if line_num_even %2 ==0: + filename_out.write(even_line) + line_num_even += 1 +f1.close() +f2.close() +filename_out.close() + + diff --git a/work_shop5/puzzle1-out.txt b/work_shop5/puzzle1-out.txt new file mode 100644 index 0000000..d089d56 --- /dev/null +++ b/work_shop5/puzzle1-out.txt @@ -0,0 +1,10 @@ + _ _.,----, + _ _.,----, +/__ '-. \ '-, ___(c-(6)=(6) + , `'. `._ '. _,' >\ " ) +;';:;;;;;'-._ /'._| Y/ _/' \ + '''"._ F | _/ _.'._ `\ + // 'L / \, ("--',=`)7 + | `._ : _, \ /'`-._L,_'-._ + ) `{ + snd \__) \ No newline at end of file diff --git a/work_shop5/puzzle2-out.txt b/work_shop5/puzzle2-out.txt new file mode 100644 index 0000000..6facecb --- /dev/null +++ b/work_shop5/puzzle2-out.txt @@ -0,0 +1,9 @@ + _ __ + \ / | |,'__ \ ,'\_ | \ | | | | ,' |_ /| + _ | | | |\/ \ \ | \ | |\_| _ | |_| | _ '-. .-',' |_ _ + `-. .-'| |/ / | | | | | | \ \// | | | | | || | | | | |_\ || |\_| + | | | || \_| | | | /_\ \ / | |` | | | || | | | | .---'| | + /_\ `------' \ | /-\ND _ `.\ | | `._,' /_\ + \| |HE `.\ + \__ | | / \ | \/ | |_) |__ | \ + _ _ _ __ _ _ __ ___ _ From 1e0f453c2c440ae220234c121e1138a5685d5fd1 Mon Sep 17 00:00:00 2001 From: Cassycq Date: Sun, 17 Aug 2025 21:51:01 +1000 Subject: [PATCH 2/5] modified workshop5 - Cassy --- work_shop5/combine.py | 26 +++++++++----------------- work_shop5/data/puzzle1-A.txt | 9 +++++++++ work_shop5/data/puzzle1-B.txt | 8 ++++++++ work_shop5/data/puzzle2-A.txt | 10 ++++++++++ work_shop5/data/puzzle2-B.txt | 9 +++++++++ work_shop5/puzzle1-out.txt | 9 ++++++++- work_shop5/puzzle2-out.txt | 10 ++++++++++ 7 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 work_shop5/data/puzzle1-A.txt create mode 100644 work_shop5/data/puzzle1-B.txt create mode 100644 work_shop5/data/puzzle2-A.txt create mode 100644 work_shop5/data/puzzle2-B.txt diff --git a/work_shop5/combine.py b/work_shop5/combine.py index 7c70017..f175160 100644 --- a/work_shop5/combine.py +++ b/work_shop5/combine.py @@ -1,27 +1,19 @@ file_name = input("Please enter the file name you want to combine: ") try: - f1 = open("./data/" + file_name + "-A.txt","rt") - f2 = open("./data/" + file_name + "-B.txt","rt") + fA = open("./data/" + file_name + "-A.txt","rt") + fB = open("./data/" + file_name + "-B.txt","rt") filename_out = open(file_name + "-out.txt","at") except FileNotFoundError: print("File not found") -line_num_odd = 1 -line_num_even = 1 while True: - odd_line = f1.readline() - if not odd_line: + odd_line = fA.readline() + even_line = fB.readline() + if not odd_line and not even_line: break - if line_num_odd %2 ==1: - filename_out.write(odd_line) - line_num_odd += 1 - even_line = f2.readline() - if not even_line: - break - if line_num_even %2 ==0: - filename_out.write(even_line) - line_num_even += 1 -f1.close() -f2.close() + filename_out.write(odd_line) + filename_out.write(even_line) +fA.close() +fB.close() filename_out.close() diff --git a/work_shop5/data/puzzle1-A.txt b/work_shop5/data/puzzle1-A.txt new file mode 100644 index 0000000..3436320 --- /dev/null +++ b/work_shop5/data/puzzle1-A.txt @@ -0,0 +1,9 @@ + _ _.,----, +| `-)_ '-. \ / < _ )/" } + , `'. `._ '. _,' >\ " ) +;:;;:;;, '..__ ,`-.`)'- '--' + '''"._ F | _/ _.'._ `\ + .-,-,_ | `. `'---, \_ _| + | `._ : _, \ /'`-._L,_'-._ + [ ( / + snd \__) \ No newline at end of file diff --git a/work_shop5/data/puzzle1-B.txt b/work_shop5/data/puzzle1-B.txt new file mode 100644 index 0000000..9cb322f --- /dev/null +++ b/work_shop5/data/puzzle1-B.txt @@ -0,0 +1,8 @@ + __ _.-._ / '-. - ,._ \) +/__ '-. \ '-, ___(c-(6)=(6) + :;;,,'-._ '---' ( ( "/`. -='/ +;';:;;;;;'-._ /'._| Y/ _/' \ + L \ \/ '._ \ + // 'L / \, ("--',=`)7 + '--' '-.\__/ _L .`' '.// + ) `{ diff --git a/work_shop5/data/puzzle2-A.txt b/work_shop5/data/puzzle2-A.txt new file mode 100644 index 0000000..ea0f2b5 --- /dev/null +++ b/work_shop5/data/puzzle2-A.txt @@ -0,0 +1,10 @@ + _ __ + ___ \ / ___ ,'\_ | .-. \ /| + _ | | | |\/ \ \ | \ | |\_| _ | |_| | _ '-. .-',' |_ _ +\\_| |_,' .-, _ | | | | |\ \ // .| |\_/ | / \ || | | | / |\ \| \ + | | | || \_| | | | /_\ \ / | |` | | | || | | | | .---'| | + /_\ | | //_____// .||` _ `._,' | | | | \ `-' /| | + \| |HE `.\ + / |__| /_\ |\ /| |_) |_ |_) + _ _ _ __ _ _ __ ___ _ + ._) |__ \_ | \ |__ | ._) \ No newline at end of file diff --git a/work_shop5/data/puzzle2-B.txt b/work_shop5/data/puzzle2-B.txt new file mode 100644 index 0000000..7d69a4c --- /dev/null +++ b/work_shop5/data/puzzle2-B.txt @@ -0,0 +1,9 @@ + ___ | ' \ + \ / | |,'__ \ ,'\_ | \ | | | | ,' |_ /| +// | | | |____| | | |\_|| |__ // | | ,'_`. | | '-. .-',' `. ,'\_ + `-. .-'| |/ / | | | | | | \ \// | | | | | || | | | | |_\ || |\_| + | | | |\___,_\ /_\ _ // | | | \_/ || | | | | | /\| | + /_\ `------' \ | /-\ND _ `.\ | | `._,' /_\ + __ _ _ __ _ + \__ | | / \ | \/ | |_) |__ | \ + (_)|- (_` |_ / |_) |_ | (_` diff --git a/work_shop5/puzzle1-out.txt b/work_shop5/puzzle1-out.txt index d089d56..49f857f 100644 --- a/work_shop5/puzzle1-out.txt +++ b/work_shop5/puzzle1-out.txt @@ -1,10 +1,17 @@ _ _.,----, - _ _.,----, + __ _.-._ / '-. - ,._ \) +| `-)_ '-. \ / < _ )/" } /__ '-. \ '-, ___(c-(6)=(6) , `'. `._ '. _,' >\ " ) + :;;,,'-._ '---' ( ( "/`. -='/ +;:;;:;;, '..__ ,`-.`)'- '--' ;';:;;;;;'-._ /'._| Y/ _/' \ '''"._ F | _/ _.'._ `\ + L \ \/ '._ \ + .-,-,_ | `. `'---, \_ _| // 'L / \, ("--',=`)7 | `._ : _, \ /'`-._L,_'-._ + '--' '-.\__/ _L .`' '.// + [ ( / ) `{ snd \__) \ No newline at end of file diff --git a/work_shop5/puzzle2-out.txt b/work_shop5/puzzle2-out.txt index 6facecb..a5e76b8 100644 --- a/work_shop5/puzzle2-out.txt +++ b/work_shop5/puzzle2-out.txt @@ -1,9 +1,19 @@ _ __ + ___ | ' \ + ___ \ / ___ ,'\_ | .-. \ /| \ / | |,'__ \ ,'\_ | \ | | | | ,' |_ /| _ | | | |\/ \ \ | \ | |\_| _ | |_| | _ '-. .-',' |_ _ +// | | | |____| | | |\_|| |__ // | | ,'_`. | | '-. .-',' `. ,'\_ +\\_| |_,' .-, _ | | | | |\ \ // .| |\_/ | / \ || | | | / |\ \| \ `-. .-'| |/ / | | | | | | \ \// | | | | | || | | | | |_\ || |\_| | | | || \_| | | | /_\ \ / | |` | | | || | | | | .---'| | + | | | |\___,_\ /_\ _ // | | | \_/ || | | | | | /\| | + /_\ | | //_____// .||` _ `._,' | | | | \ `-' /| | /_\ `------' \ | /-\ND _ `.\ | | `._,' /_\ \| |HE `.\ + __ _ _ __ _ + / |__| /_\ |\ /| |_) |_ |_) \__ | | / \ | \/ | |_) |__ | \ _ _ _ __ _ _ __ ___ _ + (_)|- (_` |_ / |_) |_ | (_` + ._) |__ \_ | \ |__ | ._) \ No newline at end of file From 2d96bdd9b1b131074df102b12c8b3a382f7ad5fe Mon Sep 17 00:00:00 2001 From: Cassycq Date: Tue, 19 Aug 2025 23:51:33 +1000 Subject: [PATCH 3/5] workshop6 practice Cassy --- work_shop6/book.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 work_shop6/book.py diff --git a/work_shop6/book.py b/work_shop6/book.py new file mode 100644 index 0000000..039e6ef --- /dev/null +++ b/work_shop6/book.py @@ -0,0 +1,94 @@ +# 1. Define a Book Class +# Attributes: title, author, isbn, price, stock +class Book: + def __init__(self,title,author,isbn,price,stock): + self.title = title + self.author = author + self.isbn = isbn + self.price = price + self.stock = stock +# Methods: display_info(), add_stock(), sell_book() + def display_info(self): + print(f"Title:{self.title}") + print(f"Author:{self.author}") + print(f"ISBN:{self.isbn}") + # float-2 decimal places.Currency standard. + print(f"Price:${self.price:.2f}") + print(f"Stock:{self.stock} copies") + + def add_stock(self,quantity): + if quantity > 0: + self.stock =+ quantity + print(f"Added: {quantity} copies. Updated stock: {self.stock}") + else: + print("Invalid input. Please enter a positive number.") + + def sell_book(self,quantity): + if quantity > 0: + if quantity > self.stock: + print(f"Not enough stock to sell. Only {self.stock} copies in stock.") + else: + self.stock -= quantity + sell_price = self.price * quantity + print(f"{quantity} copies sold. Total price is {sell_price}.") + else: + print("Invalid input.Please enter a positive number.") + +# 2. Define a Specialized Book Class +# Create a subclass of Book called EBook with an additional attribute for file format (file_format). +class Ebook(Book): + def __init__(self,title,author,isbn,price,stock,file_format): + super().__init__(title,author,isbn,price,stock) + self.file_format = file_format + def display_info(self): + super().display_info() + print(f"File Format:{self.file_format}") + + +# 3. Create an Inventory Class +# Attributes: books (a list to store Book objects) +class Inventory: + def __init__(self): + self.book_lists =[] +# Methods: add_book(), remove_book(), find_book(), display_inventory() + def add_book(self,book): + self.book_lists.append(book) + print(f"Book: {book.title} added to Inventory.") + def remove_book(self,isbn): + for book in self.book_lists: + if book.isbn == isbn: + self.book_lists.remove(book) + print(f"Book:{book.title} is removed from inventory.") + return + else: + print(f"Can not find the book.") + return + def find_book(self,isbn): + for book in self.book_lists: + if book.isbn == isbn: + return book + else: + print(f"Can not find the book.") + return + def display_inventory(self): + for book in self.book_lists: + book.display_info() + +# 4. Main Program +# Create an instance of Inventory +inventory = Inventory() +# Add a few books to the inventory +book1 = Book("One piece. Vol. 73, Operation Dressrosa","Oda, Eiichirō","9781421576831",27.88,3) +book2 = Book("Hot Desk","Dickerman, Laura","9780241729656",19.99,1) +book3 = Ebook("A little book about fear"," Memory, Jelani","9780241743423",6.99,1,"PDF") +inventory.add_book(book1) +inventory.add_book(book2) +inventory.add_book(book3) +# Display the inventory +inventory.display_inventory() +# Simulate selling a book and updating the stock +selling_book = inventory.find_book("9781421576831") +if selling_book: + selling_book.sell_book(2) +inventory.display_inventory() + From c06c27bedece6c298ed546b55814db9d3b3417de Mon Sep 17 00:00:00 2001 From: Cassycq Date: Wed, 20 Aug 2025 00:10:29 +1000 Subject: [PATCH 4/5] modified --- work_shop6/book.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/work_shop6/book.py b/work_shop6/book.py index 039e6ef..0161ad9 100644 --- a/work_shop6/book.py +++ b/work_shop6/book.py @@ -89,6 +89,6 @@ def display_inventory(self): # Simulate selling a book and updating the stock selling_book = inventory.find_book("9781421576831") if selling_book: - selling_book.sell_book(2) + selling_book.sell_book(5) inventory.display_inventory() From 252b41208b123c9469c21fa7002ffdf4fd959025 Mon Sep 17 00:00:00 2001 From: Cassycq Date: Mon, 25 Aug 2025 00:19:20 +1000 Subject: [PATCH 5/5] fixed book inventory --- work_shop6/book.py | 81 +++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/work_shop6/book.py b/work_shop6/book.py index 0161ad9..6b86670 100644 --- a/work_shop6/book.py +++ b/work_shop6/book.py @@ -2,34 +2,45 @@ # Attributes: title, author, isbn, price, stock class Book: def __init__(self,title,author,isbn,price,stock): - self.title = title - self.author = author - self.isbn = isbn - self.price = price - self.stock = stock -# Methods: display_info(), add_stock(), sell_book() + self._title = title + self._author = author + self._isbn = isbn + self._price = price + self._stock = stock + # Safely access without breaking encapsulation + @property + def title(self): + return self._title + @property + def author(self): + return self._author + @property + def isbn(self): + return self._isbn + @property + def price(self): + return self._price + @property + def stock(self): + return self._stock + # Methods:display_info() def display_info(self): - print(f"Title:{self.title}") - print(f"Author:{self.author}") - print(f"ISBN:{self.isbn}") - # float-2 decimal places.Currency standard. - print(f"Price:${self.price:.2f}") - print(f"Stock:{self.stock} copies") - + print(f"Title:{self._title},Author:{self._author},ISBN:{self._isbn},Price:${self._price},Stock:{self._stock} copies") + # add_stock() def add_stock(self,quantity): if quantity > 0: - self.stock =+ quantity - print(f"Added: {quantity} copies. Updated stock: {self.stock}") + self._stock =+ quantity + print(f"Added: {quantity} copies. Updated stock: {self._stock}") else: print("Invalid input. Please enter a positive number.") - + # sell_book() def sell_book(self,quantity): if quantity > 0: - if quantity > self.stock: - print(f"Not enough stock to sell. Only {self.stock} copies in stock.") + if quantity > self._stock: + print(f"Not enough stock to sell. Only {self._stock} copies in stock.") else: - self.stock -= quantity - sell_price = self.price * quantity + self._stock -= quantity + sell_price = self._price * quantity print(f"{quantity} copies sold. Total price is {sell_price}.") else: print("Invalid input.Please enter a positive number.") @@ -49,29 +60,37 @@ def display_info(self): # Attributes: books (a list to store Book objects) class Inventory: def __init__(self): - self.book_lists =[] -# Methods: add_book(), remove_book(), find_book(), display_inventory() + self.books =[] + # Methods: add_book() def add_book(self,book): - self.book_lists.append(book) - print(f"Book: {book.title} added to Inventory.") + # do a isbn duplicate check + for existing_book in self.books: + if existing_book.isbn == book.isbn: + print(f"The book with ISBN{book.isbn} is already in the inventory.") + return + self.books.append(book) + print(f"Book: {book._title} added to Inventory.") + # remove_book() def remove_book(self,isbn): - for book in self.book_lists: + for book in self.books: if book.isbn == isbn: - self.book_lists.remove(book) + self.books.remove(book) print(f"Book:{book.title} is removed from inventory.") return else: print(f"Can not find the book.") return + # find_book() def find_book(self,isbn): - for book in self.book_lists: + for book in self.books: if book.isbn == isbn: return book else: print(f"Can not find the book.") return + # display_inventory() def display_inventory(self): - for book in self.book_lists: + for book in self.books: book.display_info() # 4. Main Program @@ -81,14 +100,16 @@ def display_inventory(self): book1 = Book("One piece. Vol. 73, Operation Dressrosa","Oda, Eiichirō","9781421576831",27.88,3) book2 = Book("Hot Desk","Dickerman, Laura","9780241729656",19.99,1) book3 = Ebook("A little book about fear"," Memory, Jelani","9780241743423",6.99,1,"PDF") +book4 = Ebook("Test"," Memory, Jelani","9780241743423",6.99,1,"PDF") inventory.add_book(book1) inventory.add_book(book2) inventory.add_book(book3) +# testing for a isbn duplicate check +inventory.add_book(book4) # Display the inventory inventory.display_inventory() # Simulate selling a book and updating the stock selling_book = inventory.find_book("9781421576831") if selling_book: - selling_book.sell_book(5) + selling_book.sell_book(2) inventory.display_inventory() -