Exercise 103 Work with an array of product data In this exer

Exercise 10-3   Work with an array of product data

In this exercise, you’ll work with a two-dimensional array that stores the data for three products.

Review the application

1.      Open the project named ch10_ex3_ProductArray.

2.      Open the classes and review the code. Note that the ProductDB class defines a two-dimensional array that stores the data for three products. Also, note that this class contains three methods that aren’t implemented.

3.      Run the application. At this point, it should print some messages to the console, but these messages won’t contain any product data.

Add the code that works with the array of product data

4.      In the ProductDB class, add code that implements the getProductByIndex method. This should return a Product object for the product data at the specified index.

5.      In the Main class, add code that uses the getProductByIndex method and displays the data that’s returned by it.

6.      Run the application and make sure it works correctly. At this point, it should print data for the PRODUCT BY INDEX heading.

7.      In the ProductDB class, add code that implements the getProductByCode method. This should return a Product object for the product data with the specified code.

8.      In the Main class, add code that uses the getProductByCode method and displays the data that’s returned by it.

9.      Run the application and make sure it works correctly. At this point, it should print data for the PRODUCT BY CODE heading.

10. In the ProductDB class, add code that implements the getAllProducts method. This should return an array of Product objects for all of the product data.

11. In the Main class, add code that uses the getAllProducts method and displays the data that’s returned by it. To do that, you’ll need to loop through the array of Product objects that’s returned.

12. Run the application and make sure it works correctly. At this point, it should print data for the LIST OF ALL PRODUCTS heading.

I need help with steps 10 and 11.

Solution

10) This method can be impleneted using SQL statements

Assuming that you already know SQL

Implementing the getAllProducts method. You\'ll implement the getAllProducts method to return array of Product objects from the product database.

You\'ll start the method by declaring and initializing an array that will hold all the products.

Here you\'ll use NSMutableArray because you want to be able to add product objects to the arrayone by one as you retrieve rows from the database.

Beginning of the method

(NSMutableArray*) getAllProducts

{

//The array of products that let\'s us create

NSMutableArray *products = [(NSMutableArray alloc) init];

To implement getAllProducts declare a char*variable and populate it with the SQL statements

//The SQL statements that we plan on executing against the database const char *sql= \"SELECT product.ID, product.Name\"\\,\\ Manufacturer.name,product.details,product.price,\\product.quantityonhand,country.country,\\ product.image FROM Product,Manufacturer,\\ where country manufacturer.manufacturerid=product.manufacturerid \\ and product.countryoforiginid=country.countryid\";

slightly in more readable form:

SELECT product.ID,

product.name,

Manufacturer.name,

product.details,

product.price,

product.quantityonhand,

country.country,

product.image

FROM Product,Manufacturer,Country

WHERE manufacturer.manufacturerid=product.manufactureid

AND product.countryoforiginid=country.countryid

Exercise 10-3 Work with an array of product data In this exercise, you’ll work with a two-dimensional array that stores the data for three products. Review the
Exercise 10-3 Work with an array of product data In this exercise, you’ll work with a two-dimensional array that stores the data for three products. Review the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site