0

I am working on an assignment that I am needing assistance on. I need to verify I did my array first (to make sure they are all pulling correctly) and then pass that array to another function that will loop through the array and look for a certain custID that will be entered into the console by the user. Here is my current code.

This is my singleton file

#include "MyDataStore.h"
#include <fstream>

MyDataStore * MyDataStore::iInstance;
//Declare private customer array..
Customer customers[5];
void getCustomerList(Customer[], int);

MyDataStore::MyDataStore()
{
    getCustomerList(customers, 5);
}

MyDataStore * MyDataStore::GetInstance()
{
    if (iInstance == NULL())
    {
        iInstance = new MyDataStore();
    }
    return iInstance;
}

void MyDataStore::getBooks(Book books[], int size)
{
    ifstream input;
    input.open("Books.txt");
    string ISBN, title, author;
    double price = 0.0;
    int i = 0;
    while (input >> title >> author >> ISBN >> price)
    {
        Book b = Book(ISBN, title, author, price);
        if (i < size)
        {
            books[i] = b;
            i++;
        }
    }
}

Customer MyDataStore::getCustomer(int custID) // need help here 
{
    //for (int i = -0; i < 5; i++) {
    for (Customer c: customers)
    {
        if (custID = c.getCustID())
        {
            //custID = c.getCustID();
            return c;
        }
    }
    //}
    //for range
    //for each customer c in customers 
    //if custID = c.getCustID()
    //return c
    //
}

void getCustomerList(Customer customers[], int size)
{
    //need help here
    ifstream custfile;
    custfile.open("Customer.txt");
    int custID;
    string firstName, lastName, city, street, state, zip;
    Address address;
    int i = 0;
    while (custfile >> custID >> firstName >> lastName >> city >> street >> state >> zip)
    {
        Customer c = Customer(custID, firstName, lastName, address);
        if (i < size)
        {
            customers[i] = c;
            i++;
        }
        //Need to make the address part work 
    }
    //load customer array
}

This is the source file it is being called to:

#include <iostream>
#include <string>
#include "Address.h"
#include "Book.h"
#include "Customer.h"
#include "MyDataStore.h"
#include <fstream>
using namespace std;

//Main function
void main()
{
     MyDataStore * obj1 = MyDataStore::GetInstance();
     MyDataStore * obj2 = MyDataStore::GetInstance();
     //declare a book array --> this works great!
     Book books[6];
     //send array to get books
     MyDataStore::getBooks(books, 6);
     //loop through the filled in book array and just show the author
     for (Book b: books)
     {
         cout << b.getTitle() << endl;
         cout << b.getAuthor() << endl;
         cout << b.getPrice() << endl;
     }
     //This will only print out the first line. 
     Customer c = MyDataStore::getCustomer(2345);
     cout << c.print();
     system("pause");
}

I hope this all makes sense. So I need to check to see if in the singleton if my

void getCustomerList(Customer customers[], int size)

is actually doing the array correctly (there are 5 items, all coming from the text file. I also need to then put then in the:

Customer MyDataStore::getCustomer(int custID)

But, I need to ask the console what the custID is and validate it, if it is what is listed in the text, then print it, if not then default it to 0 or say invalid.

I hope this all makes sense, thank you for any help!

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • 1
    Can you please ask a more specific question? At what point in this assignment are you stuck? – Neha Karanjkar Nov 27 '18 at 05:14
  • @NehaKaranjkar I need to be able to ask the console for the custID that I want to look up. It should then be passed to the Customer MyDataStore::getCustomer(int custID) where I need to loop through the array and find the custID that matches what the user had entered – Anna Collins Nov 27 '18 at 05:16
  • btw, why do use a singelton? from what you have shown it looks like that a pretty straightforward local instance of MyDataStore in main should do the trick. – AndersK Nov 27 '18 at 05:18
  • @Anders sadly this is how my teacher has it laid out. All the functions and methods were declared by her so I have to use them. – Anna Collins Nov 27 '18 at 05:20
  • Ok fair enough, although that is not a good way to write a singleton, it is prone to race conditions if used by several threads. So if MyDataStore is to keep track of books and customers then why do you keep the customer array outside of MyDataStore? You declared it global. You should also consider having a more dynamic way of holding books and customers, like with linked lists. – AndersK Nov 27 '18 at 05:31
  • @Anders I wondered that as well because she gave us a video and explain how it is not a global, which made no sense to me. But I think I figured it out! – Anna Collins Nov 27 '18 at 05:32
  • Not much use to you right now, but [one of the better ways to write a singleton](https://stackoverflow.com/a/1008289/4581301). – user4581301 Nov 27 '18 at 06:18

1 Answers1

0

Figured out, the rest of the code was correct, this is what I had to add to get it to work properly.

Inside the Singleton:

Customer MyDataStore::getCustomer(int custID)
{

for (Customer c : customers) {
    if (custID == c.getCustID()) {
        return c;
    }   
    }

}

Inside of the source code:

int x = 0;
cout << "Please enter the customer ID!" << endl;
cin >> x;
Customer c = MyDataStore::getCustomer(x);
cout << c.print();