"""
SDD11 - File Handling: Reading
Live-coding demo for the instruction phase.

How to use:
  - Create tuckshop.csv in the same folder before the lesson (content
    below) so Example 2 onwards has a real file to read.
  - Build each TODO section live, following the lesson's worked examples
    in the same order.
  - The finished version is in SDD11_File_Handling_Read_answers.py.

Run with: python3 SDD11_File_Handling_Read_demo.py

--- tuckshop.csv contents (create this file first) ---
Chocolate,75,30
Crisps,63,45
Juice,90,20
Flapjack,110,15
Sweets,50,60
"""

from dataclasses import dataclass


@dataclass
class Item:
    name: str
    price: int
    stock: int


# ----- Example 1: reading a plain text file -----
# TODO: file = open("notice.txt", "r")
# TODO: noticeText = file.read()
# TODO: file.close() - then print noticeText.
# (Create a short notice.txt on screen first if it doesn't exist.)


# ----- Example 2: splitting a CSV file into lines and fields -----
# TODO: open tuckshop.csv, read().strip() it into fileText, close the
# file, then split fileText on "\n" into lines. Loop through lines,
# splitting each on "," into fields, and print fields.


# ----- Example 3: converting fields into an array of records -----
itemRecords = []

# TODO: for each line, split into fields, then build
# Item(fields[0], int(fields[1]), int(fields[2])) and append it to
# itemRecords. Remember: fields are always strings until converted.


# ----- Example 4: using the converted data -----
# TODO: total up itemRecords stock into totalStock and print it.
# TODO: total up itemRecords price into totalPrice, then
# averagePrice = totalPrice / len(itemRecords) - print it, and compare
# int(averagePrice) with round(averagePrice) as a reminder from SDD10.
