/*******************************************************************************************
08.cpp
    read data into an array of objects, then display them and some calculations
*******************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class item
    {
    public:
        int part;
        string desc;
        float price;
    };

//prototypes
void readFrom(string file, item list[], const int size, int& count);
float subTotal(item list[], const int size);
void showList(item [], const int size);

void main()
    {
    const int MaxSize = 50;
    item goods[MaxSize];
    int count = 0;

    string filename;
    cout << "Enter the filename to read: ";
    cin >> filename;
    readFrom(filename, goods, MaxSize, count);

    float subtotal, total;
    subtotal = subTotal(goods, count);

    float taxRate, tax;
    cout << "Enter the tax rate (as a percent): ";
    cin >> taxRate;
    tax = subtotal * taxRate/100.0;
    total = subtotal + tax;

    showList(goods, count);
    cout << "----------------------------------" << endl
         << "Subtotal: $" << subtotal << endl
         << "Tax (" << taxRate << "%): $" << tax << endl
         << "Total: $" << total << endl;

    return;
    }

float subTotal(item list[], const int size)
    {
    float sum = 0.;

    for (int i = 0; i < size; i++)
        sum += list[i].price;

    return sum;
    }

void showList(item list[], const int count)
    {
    for (int i = 0; i < count; i++)
        {
        cout << list[i].desc << " (" << list[i].part << ") $" << list[i].price << endl;
        }

    cout << endl;
    return;
    }

void readFrom(string file, item list[], const int size, int& count)
    {
    ifstream fin(file.c_str());

    for (int i = 0; i < size; i++)
        {
        if(fin >> list[i].part >> list[i].desc >> list[i].price)
            count++;
        else
            break;
        }

    return;
    }