phuzums / BookDatabase

Project for CIS 22B
GNU General Public License v3.0
1 stars 1 forks source link

pseudocode #7

Open hoangmatthew1999 opened 5 years ago

hoangmatthew1999 commented 5 years ago

include

include

include "BookDB.h"

///////////////////////////////////////////////////////////////////////// /* this is the constructor to the BookDB it accepts arguments, a string filename that and a string sFIlename set the filename string parameter to the filename of the book databse set the sFilename string parameter to the filename of the book databse where the sales information is stored

sets totalWholesaleValue to 0 sets totalRetailValue to 0 sets currentNetProfit to 0 sets currentSalesAmount to 0

dyamically allocates memory for the Book array runs the readBooks function that reads the file and puts all info about books in listFilename to the books array runs the readSalesData function passing into it salesFilename that puts the currentNetProfit and currentSalesAmount into the salesFile */ BookDB::BookDB(std::string filename, std::string sFilename) : BookList() { listFilename = filename; salesFilename = sFilename;

totalWholesaleValue = 0;
totalRetailValue    = 0;
currentNetProfit    = 0;
currentSalesAmount  = 0;

books = new Book[MAX_BOOKS];
readBooks();
readSalesData(salesFilename);

}

///////////////////////////////////////////////////////////////////////// //Pre: BookDB Exists //Post: stuff gets deleted and whatnot /* this is the destructor to the BookDB debugging statement to check what file the data is writing to if the writeBook function opened properly and returned true cout success else cout error writing to file cout statement to tell the user that the sales data is writing to the file if writeSalesData is true cout success else cout error writing to file

*/ BookDB::~BookDB() { std::cout << "\nWritting Database to file " << listFilename << "... "; if (writeBooks()) std::cout << "Success!\n"; else std::cout << "Error writting to file...\n";

std::cout << "\nWritting Sales data to file " << salesFilename << "... ";
if (writeSalesData(salesFilename))
    std::cout << "Success!\n";
else
    std::cout << "Error writting to file...\n";

}

// ============Getters==============

///////////////////////////////////////////////////////////////////////// //Pre: books has been initialized //Post: returns the wholesale value of all the books combined double BookDB::getWholesaleValue() { return totalWholesaleValue; }

///////////////////////////////////////////////////////////////////////// //Pre: books has been initialized //Post: returns the retail value of all the books double BookDB::getRetailValue() { return totalRetailValue; }

// ============Mutators============== ///////////////////////////////////////////////////////////////////////// //Pre: Filename is valid and bk is initialized to hold at least // MAX_BOOKS //Post: bk is filled with books from the file . // WholesaleValue and retailValue are updated as the books // are moved into the books array /* sets the number of books to 0 sets both totalRetailValue and totalWholesaleValue to 0 opens the filename listFilename if the listFile did not open properly return false sets a temp to 0 do while loop that iterates while it is not the end of the file inializes a string variable tstr reads the first word in the line and puts it in the book array at the index of numBooks inializes a tc char varibale reads the space character from listFile stores the ISBN into the temp variable if the temp is 0 (the ISBN is 0) iniailizes an int variable tempint inializes a double variable tempdbl reads the characters in the listFile and stores it in tstr until the } character reads the characters in the listFile and stores it in tstr until the } character reads the characters in the listFile and stores it in tstr until the } character reads the next word in the listFile and stores it in tempint variable reads the next word in the listFile and stores it in tempint variable reads the next word in the listFile and stores it in tempint variable reads the next word in the listFile and stores it in tempint variable increments the value of books[temp].quantity by tempint reads the next word of listFile and puts it into tempdbl increments totalRetailValue by the product of tempdbl and tempint reads the next word and puts it into the tempdbl variable increments totalWholesaleValue by the product of tempdbl and tempint reads the line in listFIle until the } and stores the string in tstr stores the tstr variable in title element of the books object

     reads the line in listFIle until the } and stores the string in tstr
    stores the tstr variable in author element of the books object

    reads the line in listFIle until the } and stores the string in tstr
    stores the tstr variable in publisher element of the books object

    reads the word in the listFile file and stores it in the addedOn.month attribute of the book object
    reads the word in the listFile file and stores it in the addedOn.day attribute of the book object
    reads the word in the listFile file and stores it in the addedOn.year attribute of the book object
    reads the word in the listFile file and stores it in the quantity attribute of the book object
    reads the word in the listFile file and stores it in the .retailCost attribute of the book object
    reads the word in the listFile file and stores it in the .wholesaleCost attribute of the book object

*/ bool BookDB::readBooks() { numBooks = 0; totalRetailValue = totalWholesaleValue = 0;

listFile.open(listFilename);

if (!listFile)
    return false;

int temp = 0;
do
{
    std::string tstr;

    listFile >> books[numBooks].ISBN;
    char tc = ' ';
    listFile >> tc; //Dummy char to read the seperator between isbn & books (used in case a book title starts with a number)

    // Search for an existing entry
    temp = findBook(books[numBooks].ISBN);

    // increase quantity if a match was found
    if (temp > 0)
    {
        int tempint;
        double tempdbl;
        std::getline(listFile, tstr, '}');
        std::getline(listFile, tstr, '}');
        std::getline(listFile, tstr, '}');
        listFile >> tempint; //day
        listFile >> tempint; //month
        listFile >> tempint; //year
        listFile >> tempint;
        books[temp].quantity += tempint;
        listFile >> tempdbl;
        totalRetailValue += tempdbl * (double)tempint;
        listFile >> tempdbl;
        totalWholesaleValue += tempdbl * (double)tempint;

        continue;
    }

    std::getline(listFile, tstr, '}');
    books[numBooks].title = tstr;
    std::getline(listFile, tstr, '}');
    books[numBooks].author = tstr;
    std::getline(listFile, tstr, '}');
    books[numBooks].publisher = tstr;
    listFile >> books[numBooks].addedOn.month;
    listFile >> books[numBooks].addedOn.day;
    listFile >> books[numBooks].addedOn.year;
    listFile >> books[numBooks].quantity;
    listFile >> books[numBooks].retailCost;
    listFile >> books[numBooks].wholesaleCost;

    totalRetailValue += books[numBooks].retailCost * (double)books[numBooks].quantity;
    totalWholesaleValue += books[numBooks].wholesaleCost * (double)books[numBooks].quantity;

    numBooks++;

} while (!listFile.eof());

listFile.close();
return true;

}

///////////////////////////////////////////////////////////////////////// //Pre: Filename is valid and not currently being r/w'd to. We're assuming the // file will never shrink (as only quantities are set to zero, not removed) //Post: Book array is formatted and outputted to a file. The filename should // always be listFile? as you want to 'edit' the database you've opened, // not copy it. / if the file fails to open return false open the file if the file fails to opne return false loop for the number of books in the database writes the books isbn to listFle writes the books title to listFle writes the books authir to listFle writes the books publisher to listFle writes the books month that it was added to listFle writes the books day it was addedto listFle writes the books year that it was added to listFle writes the books quanitity to listFle setPrcision to make it look nice writes the books retailCost to listFle setprecision to make it look good writes the books wholesaleCost to listFile if the index is less than the numBooks in the array add a newline character /

bool BookDB::writeBooks() { if (listFile.is_open()) return false;

listFile.open(listFilename);

if (!listFile.is_open())
    return false;

for (int idx = 0; idx < numBooks; idx++)
{
    listFile << books[idx].ISBN << '}';
    listFile << books[idx].title << '}';
    listFile << books[idx].author << '}';
    listFile << books[idx].publisher << '}';
    listFile << books[idx].addedOn.month << ' ';
    listFile << books[idx].addedOn.day << ' ';
    listFile << books[idx].addedOn.year << ' ';
    listFile << books[idx].quantity << ' ';
    listFile << std::fixed << std::setprecision(2);
    listFile << books[idx].retailCost << ' ';
    listFile << std::fixed << std::setprecision(2);
    listFile << books[idx].wholesaleCost;
    listFile.unsetf(std::ios_base::fixed);
    if (idx < numBooks - 1)
        listFile << '\n';
}

listFile.close();
return true;

}

///////////////////////////////////////////////////////////////////////// //Pre:
//Post: / open the filename parameter for output and appending if the file failed to open return false close the file open the file if it is the end of the file set currentNetProfit to 0 close the file return true output the currentNetProfit and currentSalesAmount to the salesFile close the file return true / bool BookDB::readSalesData(std::string filename) {

// Attempt to create or open the file
salesFile.open(filename,std::fstream::out | std::fstream::app);
if (!salesFile.is_open())
    return false;

// Reopen for input after creating
salesFile.close();
salesFile.open(filename, std::fstream::in);
if (salesFile.eof())
{
    currentNetProfit = 0.0;
    salesFile.close();
    return true;
}

salesFile >> currentNetProfit >> currentSalesAmount;
salesFile.close();
return true;

}

///////////////////////////////////////////////////////////////////////// //Pre:
//Post: / open the filename parameter for output if the file fails to open return false cout the currentNetProfit writing the currentNetProfit to the file that is specified in the filename parameter write currentSalesAmount to the salesFile close the file return true / bool BookDB::writeSalesData(std::string filename) { salesFile.open(filename, std::fstream::out); if (!salesFile.is_open()) return false; std::cout << "currentnetprofit = " << currentNetProfit; salesFile << std::setprecision(2) << std::fixed << currentNetProfit; salesFile << std::endl << currentSalesAmount;

salesFile.close();
return true;

}

///////////////////////////////////////////////////////////////////////// //Pre: iunno? //Post: A book of ISBN is sold from the database IF the ISBN is found AND the // quantity is > 0. Retail and wholesale value of the databse is adjusted // for the book. Returns the price of the book sold and 0 if there was an error (assuming nothing is free). /* if the index passed is less than 0 return 0.0 if the quantity attribute is less than or equal to 0.0 return 0.0 decrement quantityItems decrement the quantity attribute of the book decrement the retailValue by the retail cost of the boook decrement the totalWholesaleValue by the wholesaleValue cost of the boook add the currentNetProfit by the difference of the book's retailCost and the book's wholesaleCost cout the currentNetProfit increment the currentSalesAmount by the retailCost return the retailCost of the book

*/ double BookDB::sellBook(unsigned long ISBN) { int idx = findBook(ISBN);

// Error checking
if (idx < 0)
    return 0.0;
if (books[idx].quantity <= 0)
    return 0.0;

quantityItems--;
books[idx].quantity--;
totalRetailValue    -= books[idx].retailCost;
totalWholesaleValue -= books[idx].wholesaleCost;

currentNetProfit += (books[idx].retailCost - books[idx].wholesaleCost);
std::cout << "Book sold. Profit: " << currentNetProfit << std::endl;
currentSalesAmount += books[idx].retailCost;

return books[idx].retailCost;

}

///////////////////////////////////////////////////////////////////////// //Pre:
//Post: double BookDB::sellBook(int idx) /* if the index passed is less than 0 return 0.0 if the quantity attribute is less than or equal to 0.0 return 0.0 decrement quantityItems decrement the quantity attribute of the book decrement the retailValue by the retail cost of the boook decrement the totalWholesaleValue by the wholesaleValue cost of the boook add the currentNetProfit by the difference of the book's retailCost and the book's wholesaleCost cout the currentNetProfit return the retailCost of the book

*/ { // Error checking if (idx < 0) return 0.0; if (books[idx].quantity <= 0) return 0.0;

quantityItems--;
books[idx].quantity--;
totalRetailValue -= books[idx].retailCost;
totalWholesaleValue -= books[idx].wholesaleCost;

currentNetProfit += (books[idx].retailCost - books[idx].wholesaleCost);
std::cout << "currentnetprof = " << currentNetProfit;
currentSalesAmount += books[idx].retailCost;

return books[idx].retailCost;

}

///////////////////////////////////////////////////////////////////////// //Pre: sm is a valid sort method. books array should hold > 1 book //Post: books array is sorted through using the appropriate SORT_METHOD / set a books pointer to the value of the getBooks function sets the numBooks variable to the getNumsBooks function if the number of books is less than 1 return false if the sm parameter is quantity inialize i j and min_idx do a loop for the number of books() sets the min_indx to the i value loops through and checks if the j quantity is less than the min idx if so it resets the min_indx to the value of j swaps books[min_idx] with books[i] if the sm parameter is cost sets the min_indx to the i value loops through and checks if the j quantity is less than the min idx if so it resets the min_indx to the value of j swaps books[min_idx] with books[i] if the sm parameter is age sets the min_indx to the i value loops through and checks if the j quantity is less than the min idx if so it resets the min_indx to the value of j swaps books[min_idx] with books[i] / bool BookDB::sortBooks(SORT_METHOD sm) { //is the array > 1? //if no, return 0 Book *books = getBooks(); int numBooks = getNumBooks(); if (numBooks <= 1) { return false; }

//loop through books using books array and numBooks
//compare values determined by the sm (case switch?)
//move and sort the books
if (sm == QUANTITY) {
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < numBooks - 1; i++)
    {
        min_idx = i;
        for (j = i + 1; j < numBooks; j++)
            if (books[j].quantity < books[min_idx].quantity)
                min_idx = j;
        // Swap
        Book temp = books[min_idx];
        books[min_idx] = books[i];
        books[i] = temp;
    }
    return true;
}
else if (sm == COST) {
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < numBooks - 1; i++)
    {
        min_idx = i;
        for (j = i + 1; j < numBooks; j++)
            if (books[j].retailCost < books[min_idx].retailCost)
                min_idx = j;
        // Swap
        std::swap(books[i], books[min_idx]);
        /*Book temp = books[min_idx];
        books[min_idx] = books[j];
        books[j] = temp;*/
    }
    return true;
}
else if (sm == AGE) {
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < numBooks - 1; i++)
    {
        min_idx = i;
        for (j = i + 1; j < numBooks; j++)
            if (books[j].addedOn.year < books[min_idx].addedOn.year && books[j].addedOn.year != books[min_idx].addedOn.year)
                min_idx = j;
            else if (books[j].addedOn.month < books[min_idx].addedOn.month && books[j].addedOn.month != books[min_idx].addedOn.month)
                min_idx = j;
            else if (books[j].addedOn.day < books[min_idx].addedOn.day && books[j].addedOn.day != books[min_idx].addedOn.day)
                min_idx = j;

        // Swap
        if (min_idx == i) {
            Book temp = books[min_idx];
            books[min_idx] = books[i];
            books[i] = temp;
        }
    }
    return true;
}
return false; // end-of-function return val needed incase it gets reached somehow

}

///////////////////////////////////////////////////////////////////////// //Pre: stuff //Post: Pretty much the same as sellBook, except it can remove multiple books at // one and returns true if successful and false if there was an error /* inializes a variable idx and it is the return value of the findBook function if idx < 0 return false if the quantity of the book is less than 1 more than the quantity parameter return false decrement the quantityItems by quantity decrement the quantity of the books objects by the quantity parameter passed decrement the totalRetailValue by the product of the retailCost of the book and the quantity parameter decrement the totalWholesaleValue by the product of the wholeSaleCost of the book and the quantity

*/ bool BookDB::removeBook(unsigned long ISBN, int quantity) { int idx = findBook(ISBN); if (idx < 0) return false; if (books[idx].quantity < 1 + quantity) return false;

quantityItems -= quantity;
books[idx].quantity -= quantity;
totalRetailValue -= books[idx].retailCost * quantity;
totalWholesaleValue -= books[idx].wholesaleCost * quantity;
return 1;

}

///////////////////////////////////////////////////////////////////////// //Pre:
//Post: void BookDB::printByMethod(int verbosity = 0, SORT_METHOD sm = SORT_METHOD::QUANTITY) / loop for the number of books in the array cout the string book and the index the loop is on if the sm parameter passed is age cout the title of the book followed by the string age followed by the day the book was added followed by a / followed by the month it was added and the year it was added if the sm parameter passed is cost cout the title of the book followed by the stringcost : $ followed by the retailCost if the sm parameter passed is quantity cout the title of the book followed by the string quantity followed by the quantity if the verbosity parameter is greater than 1 cout the string author followed by the author of the book followed by the publisher string followed by the publisher of the book if the verbosity parameter is greater than 0 cout the string ISBN followed by the ISBN of the book
/ { for (int idx = 0; idx < numBooks; idx++) { std::cout << "\nBook " << idx << " : "; if (sm == SORT_METHOD::AGE) { std::cout << std::setw(50) << books[idx].title << "Age : " << books[idx].addedOn.day << '/' << books[idx].addedOn.month << '/' << books[idx].addedOn.year; } if (sm == SORT_METHOD::COST) { std::cout << std::setw(50) << books[idx].title << "Cost : $" << std::fixed << std::setprecision(2) << books[idx].retailCost; } if (sm == SORT_METHOD::QUANTITY) { std::cout << std::setw(50) << books[idx].title << "Quantity : " << books[idx].quantity; } if (verbosity > 1) std::cout << std::endl << "Author : " << books[idx].author << "... Publisher : " << books[idx].publisher; if (verbosity > 0) std::cout << std::endl << "ISBN : " << books[idx].ISBN << std::endl; } } void BookDB::addBook(const Book &bk){ / if numBooks is larger than the number of books the books array has allocated return false else store the book object into the next element of the book object / if (numBooks >= MAX_BOOKS) return false; books[numBooks++] = bk; return 1; }

hoangmatthew1999 commented 5 years ago

here the one for bookDB

phuzums commented 5 years ago

Thanks! Added!