← Computer Programming II

Week 1 Assignment

Variant 1: Library Member Tracker

A community library tracks members, their borrowed books, and a shared membership counter.

  1. Define a class LibraryMember with class variables: library_name = "City Library" and total_members = 0.
  2. Define __init__ with parameters name, member_id, borrowed=None:
    • Store self.name and self.member_id.
    • Use the None default pattern to initialize self.borrowed as a list.
    • Increment LibraryMember.total_members by 1.
  3. Create a method borrow_book with parameter title:
    • If title is a non-empty string, append it to self.borrowed and print "Borrowed: {title}".
  4. Create a method return_book with parameter title:
    • If title exists in self.borrowed, remove it and print "Returned: {title}".
    • Otherwise print "Book not found".
  5. Create a method display_member that prints "Member: {name} ({member_id}) at {library_name}".
  6. Create two members using the test data and perform the listed operations.
  7. Print the total members count as "Total members: {count}".

Input

Member 1: name="Aziza", member_id="L-101"
Member 2: name="Bekzod", member_id="L-102"
Operations:
- Display Aziza's info
- Aziza borrows "1984"
- Aziza borrows "Dune"
- Aziza returns "Dune"
- Display Bekzod's info
- Bekzod returns "Foundation"

Expected Output

Member: Aziza (L-101) at City Library
Borrowed: 1984
Borrowed: Dune
Returned: Dune
Member: Bekzod (L-102) at City Library
Book not found
Total members: 2

Variant 2: Fitness Class Enrollment

A gym manages class enrollments with capacity limits and shared scheduling data.

  1. Define a class FitnessClass with class variables: gym_name = "Power Gym", max_capacity = 3, and total_classes = 0.
  2. Define __init__ with parameters class_name, instructor:
    • Store self.class_name and self.instructor.
    • Initialize self.participants as an empty list.
    • Increment FitnessClass.total_classes by 1.
  3. Create a method add_participant with parameter name:
    • If the class is not full, append the name and print "Added {name} to {class_name}".
    • If full, print "Class is full".
  4. Create a method remove_participant with parameter name:
    • If name is in self.participants, remove it and print "Removed {name} from {class_name}".
    • Otherwise print "Participant not found".
  5. Create a method display_class that prints "Class: {class_name}, Instructor: {instructor}, Gym: {gym_name}".
  6. Create one class and perform the listed operations.
  7. Print total classes as "Total classes: {count}".

Input

Class: "Yoga", Instructor: "Malika"
Operations:
- Display class info
- Add "Ali"
- Add "Vali"
- Add "Gulnora"
- Add "Rustam" (should fail - full)
- Remove "Vali"
- Remove "Sarvar" (not enrolled)

Expected Output

Class: Yoga, Instructor: Malika, Gym: Power Gym
Added Ali to Yoga
Added Vali to Yoga
Added Gulnora to Yoga
Class is full
Removed Vali from Yoga
Participant not found
Total classes: 1

Variant 3: Digital Wallet

A digital wallet tracks balance, transaction history, and shared limits across all wallets.

  1. Define a class DigitalWallet with class variables: service_name = "PayWay", min_balance = 0, and total_wallets = 0.
  2. Define __init__ with parameters owner, balance=0, history=None:
    • Store self.owner and self.balance.
    • Initialize self.history using the None default pattern.
    • Increment DigitalWallet.total_wallets by 1.
  3. Create a method add_funds with parameter amount:
    • If amount > 0, add it to self.balance, record "+{amount}" in history, and print "Added {amount}. Balance: {balance}".
  4. Create a method spend with parameter amount:
    • If self.balance - amount >= DigitalWallet.min_balance, subtract, record "-{amount}" in history, and print "Spent {amount}. Balance: {balance}".
    • Otherwise print "Insufficient balance".
  5. Create a method display_wallet that prints "Owner: {owner}, Balance: {balance}, Service: {service_name}".
  6. Create a method show_history that prints each entry in self.history on a new line.
  7. Create one wallet and perform the listed operations.
  8. Print total wallets as "Total wallets: {count}".

Input

Wallet: owner="Umida", balance=40
Operations:
- Display wallet info
- Add 25
- Spend 50
- Spend 10
- Show history

Expected Output

Owner: Umida, Balance: 40, Service: PayWay
Added 25. Balance: 65
Spent 50. Balance: 15
Spent 10. Balance: 5
+25
-50
-10
Total wallets: 1

Variant 4: Pet Hotel Guest System

A pet hotel tracks animals staying at the facility, managing check-ins and check-outs with a shared guest counter.

  1. Define a class PetGuest with class variables: hotel_name = "Paws & Relax Hotel" and total_guests = 0.
  2. Define __init__ with parameters pet_name, owner_id, toys=None:
    • Store self.pet_name and self.owner_id.
    • Use the None default pattern to initialize self.toys as a list.
    • Increment PetGuest.total_guests by 1.
  3. Create a method add_toy with parameter toy_name:
    • If toy_name is a non-empty string, append it to self.toys and print "Added toy: {toy_name}".
  4. Create a method remove_toy with parameter toy_name:
    • If toy_name exists in self.toys, remove it and print "Removed toy: {toy_name}".
    • Otherwise print "Toy not found".
  5. Create a method display_guest that prints "Guest: {pet_name} (Owner: {owner_id}) at {hotel_name}".
  6. Create two guests using the test data and perform the listed operations.
  7. Print the total guests count as "Total guests: {count}".

Input

Guest 1: pet_name="Mittens", owner_id="P-201"
Guest 2: pet_name="Rex", owner_id="P-202"
Operations:
- Display Mittens' info
- Mittens adds "Ball"
- Mittens adds "Mouse"
- Mittens removes "Mouse"
- Display Rex's info
- Rex removes "Bone"

Expected Output

Guest: Mittens (Owner: P-201) at Paws & Relax Hotel
Added toy: Ball
Added toy: Mouse
Removed toy: Mouse
Guest: Rex (Owner: P-202) at Paws & Relax Hotel
Toy not found
Total guests: 2

Variant 5: Restaurant Table Manager

A restaurant manages table seating with capacity limits and tracks total tables in the establishment.

  1. Define a class RestaurantTable with class variables: restaurant_name = "Taste of Tashkent", max_seats = 4, and total_tables = 0.
  2. Define __init__ with parameters table_number, section:
    • Store self.table_number and self.section.
    • Initialize self.diners as an empty list.
    • Increment RestaurantTable.total_tables by 1.
  3. Create a method seat_diner with parameter name:
    • If the table is not full, append the name and print "Seated {name} at Table {table_number}".
    • If full, print "Table is full".
  4. Create a method unseat_diner with parameter name:
    • If name is in self.diners, remove it and print "Removed {name} from Table {table_number}".
    • Otherwise print "Diner not found".
  5. Create a method display_table that prints "Table {table_number} in {section} at {restaurant_name}".
  6. Create one table and perform the listed operations.
  7. Print total tables as "Total tables: {count}".

Input

Table: 5, Section: "Patio"
Operations:
- Display table info
- Seat "Nodira"
- Seat "Kamola"
- Seat "Farhod"
- Seat "Shavkat"
- Seat "Dilshod" (should fail - full)
- Unseat "Kamola"
- Unseat "Zafar" (not seated)

Expected Output

Table 5 in Patio at Taste of Tashkent
Seated Nodira at Table 5
Seated Kamola at Table 5
Seated Farhod at Table 5
Seated Shavkat at Table 5
Table is full
Removed Kamola from Table 5
Diner not found
Total tables: 1

Variant 6: Bus Pass Tracker

A transit system tracks passenger rides using a stored-value pass system with fare history and shared minimum balance rules.

  1. Define a class BusPass with class variables: transit_name = "Tashkent Metro", min_fare = 3, and total_passes = 0.
  2. Define __init__ with parameters holder, balance=0, rides=None:
    • Store self.holder and self.balance.
    • Initialize self.rides using the None default pattern.
    • Increment BusPass.total_passes by 1.
  3. Create a method load_pass with parameter amount:
    • If amount > 0, add it to self.balance, record "+{amount}" in rides, and print "Loaded {amount}. Balance: {balance}".
  4. Create a method take_ride with parameter fare:
    • If self.balance - fare >= BusPass.min_fare, subtract fare, record "-{fare}" in rides, and print "Charged {fare}. Balance: {balance}".
    • Otherwise print "Insufficient balance for ride".
  5. Create a method display_pass that prints "Holder: {holder}, Balance: {balance}, Transit: {transit_name}".
  6. Create a method show_rides that prints each entry in self.rides on a new line.
  7. Create one pass and perform the listed operations.
  8. Print total passes as "Total passes: {count}".

Input

Pass: holder="Jamshid", balance=10
Operations:
- Display pass info
- Load 20
- Take ride costing 5
- Take ride costing 8
- Show rides

Expected Output

Holder: Jamshid, Balance: 10, Transit: Tashkent Metro
Loaded 20. Balance: 30
Charged 5. Balance: 25
Charged 8. Balance: 17
+20
-5
-8
Total passes: 1

Variant 7: School Locker Management

A school tracks locker usage, allowing students to store and retrieve items with a shared locker counter.

  1. Define a class SchoolLocker with class variables: school_name = "Al-Khwarizmi Academy" and total_lockers = 0.
  2. Define __init__ with parameters student_name, locker_id, items=None:
    • Store self.student_name and self.locker_id.
    • Use the None default pattern to initialize self.items as a list.
    • Increment SchoolLocker.total_lockers by 1.
  3. Create a method store_item with parameter item_name:
    • If item_name is a non-empty string, append it to self.items and print "Stored: {item_name}".
  4. Create a method retrieve_item with parameter item_name:
    • If item_name exists in self.items, remove it and print "Retrieved: {item_name}".
    • Otherwise print "Item not found".
  5. Create a method display_locker that prints "Locker {locker_id} assigned to {student_name} at {school_name}".
  6. Create two lockers using the test data and perform the listed operations.
  7. Print the total lockers count as "Total lockers: {count}".

Input

Locker 1: student_name="Gulnora", locker_id="A-12"
Locker 2: student_name="Ravshan", locker_id="A-15"
Operations:
- Display Gulnora's locker info
- Gulnora stores "Calculator"
- Gulnora stores "Notebook"
- Gulnora retrieves "Calculator"
- Display Ravshan's locker info
- Ravshan retrieves "Backpack"

Expected Output

Locker A-12 assigned to Gulnora at Al-Khwarizmi Academy
Stored: Calculator
Stored: Notebook
Retrieved: Calculator
Locker A-15 assigned to Ravshan at Al-Khwarizmi Academy
Item not found
Total lockers: 2

Variant 8: Parking Lot Spot Manager

A parking facility manages vehicle spots with capacity limits and tracks total available spots.

  1. Define a class ParkingSpot with class variables: lot_name = "City Center Parking", max_vehicles = 3, and total_spots = 0.
  2. Define __init__ with parameters spot_number, level:
    • Store self.spot_number and self.level.
    • Initialize self.vehicles as an empty list.
    • Increment ParkingSpot.total_spots by 1.
  3. Create a method park_vehicle with parameter plate:
    • If the spot is not full, append the plate and print "Parked vehicle {plate} at Spot {spot_number}".
    • If full, print "Spot is full".
  4. Create a method remove_vehicle with parameter plate:
    • If plate is in self.vehicles, remove it and print "Removed vehicle {plate} from Spot {spot_number}".
    • Otherwise print "Vehicle not found".
  5. Create a method display_spot that prints "Spot {spot_number} on Level {level} at {lot_name}".
  6. Create one spot and perform the listed operations.
  7. Print total spots as "Total spots: {count}".

Input

Spot: 42, Level: "B2"
Operations:
- Display spot info
- Park "AA 123"
- Park "BB 456"
- Park "CC 789"
- Park "DD 000" (should fail - full)
- Remove "BB 456"
- Remove "ZZ 999" (not parked)

Expected Output

Spot 42 on Level B2 at City Center Parking
Parked vehicle AA 123 at Spot 42
Parked vehicle BB 456 at Spot 42
Parked vehicle CC 789 at Spot 42
Spot is full
Removed vehicle BB 456 from Spot 42
Vehicle not found
Total spots: 1

Variant 9: Cafeteria Meal Card

A cafeteria tracks student meal balances with transaction history and a minimum required balance.

  1. Define a class MealCard with class variables: cafeteria_name = "Campus Cafe", min_balance = 5, and total_cards = 0.
  2. Define __init__ with parameters student, balance=0, transactions=None:
    • Store self.student and self.balance.
    • Initialize self.transactions using the None default pattern.
    • Increment MealCard.total_cards by 1.
  3. Create a method deposit with parameter amount:
    • If amount > 0, add it to self.balance, record "+{amount}" in transactions, and print "Deposited {amount}. Balance: {balance}".
  4. Create a method purchase with parameter amount:
    • If self.balance - amount >= MealCard.min_balance, subtract, record "-{amount}" in transactions, and print "Purchased meal for {amount}. Balance: {balance}".
    • Otherwise print "Insufficient balance for purchase".
  5. Create a method display_card that prints "Student: {student}, Balance: {balance}, Cafeteria: {cafeteria_name}".
  6. Create a method show_transactions that prints each entry in self.transactions on a new line.
  7. Create one card and perform the listed operations.
  8. Print total cards as "Total cards: {count}".

Input

Card: student="Malika", balance=15
Operations:
- Display card info
- Deposit 30
- Purchase meal for 12
- Purchase meal for 10
- Show transactions

Expected Output

Student: Malika, Balance: 15, Cafeteria: Campus Cafe
Deposited 30. Balance: 45
Purchased meal for 12. Balance: 33
Purchased meal for 10. Balance: 23
+30
-12
-10
Total cards: 1

Variant 10: Music Studio Booking

A music studio tracks artists and their booked equipment with a shared booking counter.

  1. Define a class StudioBooking with class variables: studio_name = "Harmony Studios" and total_bookings = 0.
  2. Define __init__ with parameters artist_name, session_id, equipment=None:
    • Store self.artist_name and self.session_id.
    • Use the None default pattern to initialize self.equipment as a list.
    • Increment StudioBooking.total_bookings by 1.
  3. Create a method book_equipment with parameter item_name:
    • If item_name is a non-empty string, append it to self.equipment and print "Booked equipment: {item_name}".
  4. Create a method return_equipment with parameter item_name:
    • If item_name exists in self.equipment, remove it and print "Returned equipment: {item_name}".
    • Otherwise print "Equipment not booked".
  5. Create a method display_booking that prints "Session {session_id} for {artist_name} at {studio_name}".
  6. Create two bookings using the test data and perform the listed operations.
  7. Print the total bookings count as "Total bookings: {count}".

Input

Booking 1: artist_name="Daler", session_id="M-501"
Booking 2: artist_name="Nargiza", session_id="M-502"
Operations:
- Display Daler's booking info
- Daler books "Guitar"
- Daler books "Microphone"
- Daler returns "Microphone"
- Display Nargiza's booking info
- Nargiza returns "Drums"

Expected Output

Session M-501 for Daler at Harmony Studios
Booked equipment: Guitar
Booked equipment: Microphone
Returned equipment: Microphone
Session M-502 for Nargiza at Harmony Studios
Equipment not booked
Total bookings: 2

Variant 11: Cinema Hall Seating

A cinema manages theater seating with capacity limits and tracks total halls.

  1. Define a class CinemaHall with class variables: cinema_name = "Silver Screen", max_viewers = 5, and total_halls = 0.
  2. Define __init__ with parameters hall_number, movie_title:
    • Store self.hall_number and self.movie_title.
    • Initialize self.viewers as an empty list.
    • Increment CinemaHall.total_halls by 1.
  3. Create a method seat_viewer with parameter name:
    • If the hall is not full, append the name and print "Seated {name} in Hall {hall_number}".
    • If full, print "Hall is full".
  4. Create a method remove_viewer with parameter name:
    • If name is in self.viewers, remove it and print "Removed {name} from Hall {hall_number}".
    • Otherwise print "Viewer not found".
  5. Create a method display_hall that prints "Hall {hall_number} showing {movie_title} at {cinema_name}".
  6. Create one hall and perform the listed operations.
  7. Print total halls as "Total halls: {count}".

Input

Hall: 3, Movie: "Inception"
Operations:
- Display hall info
- Seat "Aziz"
- Seat "Bobur"
- Seat "Charos"
- Seat "Dilshod"
- Seat "Eldor"
- Seat "Feruza" (should fail - full)
- Remove "Bobur"
- Remove "Gulnora" (not seated)

Expected Output

Hall 3 showing Inception at Silver Screen
Seated Aziz in Hall 3
Seated Bobur in Hall 3
Seated Charos in Hall 3
Seated Dilshod in Hall 3
Seated Eldor in Hall 3
Hall is full
Removed Bobur from Hall 3
Viewer not found
Total halls: 1

Variant 12: Tool Rental System

A hardware store manages tool rentals with balance tracking and rental history.

  1. Define a class ToolRental with class variables: shop_name = "BuildIt Hardware", min_deposit = 10, and total_rentals = 0.
  2. Define __init__ with parameters renter, deposit=0, history=None:
    • Store self.renter and self.deposit.
    • Initialize self.history using the None default pattern.
    • Increment ToolRental.total_rentals by 1.
  3. Create a method add_deposit with parameter amount:
    • If amount > 0, add it to self.deposit, record "+{amount}" in history, and print "Added deposit {amount}. Total: {deposit}".
  4. Create a method rent_tool with parameter fee:
    • If self.deposit - fee >= ToolRental.min_deposit, subtract fee, record "-{fee}" in history, and print "Rented tool for {fee}. Remaining: {deposit}".
    • Otherwise print "Insufficient deposit for rental".
  5. Create a method display_rental that prints "Renter: {renter}, Deposit: {deposit}, Shop: {shop_name}".
  6. Create a method show_history that prints each entry in self.history on a new line.
  7. Create one rental and perform the listed operations.
  8. Print total rentals as "Total rentals: {count}".

Input

Rental: renter="Shokir", deposit=20
Operations:
- Display rental info
- Add deposit 40
- Rent tool costing 15
- Rent tool costing 25
- Show history

Expected Output

Renter: Shokir, Deposit: 20, Shop: BuildIt Hardware
Added deposit 40. Total: 60
Rented tool for 15. Remaining: 45
Rented tool for 25. Remaining: 20
+40
-15
-25
Total rentals: 1