#include <stdlib.h>
#include <iostream>
#include <fstream>

#include "class_knap_sack.cpp"

class CKnapSackInput: public CKnapSack {
    private:
        ifstream file;
        int id;

    public:
        CKnapSackInput(const char *filename) {
            file.open(filename);
            // nepodarilo se otevrit soubor
            if(!file.is_open()) {
                cout << "Error opening file " << filename << ".\n";
                exit(1);
            }
        }

        ~CKnapSackInput() {
            if(file.is_open()) file.close();
        }

        int eof() {
            return file.eof();
        }

        int loadNext() {
            int items, lmaxweight;
            int lweight, lcost;
            clear();
            // prvni radek - id items maxweight
            file >> id >> items >> lmaxweight;
            setMaxWeight(lmaxweight);
            if(file.eof()) return 0;
            // cti itemy
            for(int i=0; i<items; i++) {
                file >> lweight >> lcost;
                addItem(lcost, lweight);
            }
        }

        int getId() {
            return id;
        }
};