unc comp 110 fall 07.

Program 6: Java store

Due: Tue Dec 4, 11:59 pm EST

Motivation

Program 6 simulates a customer purchasing a variety of products in a store. The concept of products sold in a store is a similar theme to Program 4: Online store. The implementations of the programs differ and demonstrate different designs (besides the difference in programming language). In Program 4 information on products (item name and price) are stored in separate arrays. In Program 6, information on each product is stored in a Java class.

Design

Program 6 is an extension to the Java class/inheritance recitation and involves the generic product, produce, and member products. Your extension is to add a new product named Bulk. You will also modify the Store class to demonstrate how the Product classes work.

Implementing program 6 requires the recitation to be complete.  The Java project involves 5 files. Your submission, however, is only two files: Bulk.java that you create and Store.java that you modify.

Bulk product (Bulk.java)

The bulk product is a kind of generic product (it has a name and unit price). The cost differs from the generic product. The bulk product is discounted according the amount of product purchased. The Bulk product inherits from the Product class and has a data member named units that indicates the number of units purchased (a whole number). The discount is as follows:
The units and discounts (magic numbers) should be constants in the Bulk class.

Store class (Store.java)

The Store class is the main program to demonstrate how the Product classes are used. In the program, create an array of products a customer is purchasing. Then the array is displayed and tallied as would be the case when a customer is checking out at a store register.

The main program is divided into three steps and you will create a method for each.

  1. customer1() and customer2() return a lists of products to be purchased. The lists are specified below 
  2. showProducts() displays the array of products. It takes the array of products as an parameter.
  3. totalCost() return total cost of products to be purchased
The main method is as follows. Either the customer1() or customer2() method is called to create the array of items purchased by the customer. These are two test cases. The returned array is stored in the shoppingCart variable. The products in the array are then displayed and totaled.

Summary of the Store class methods. The main method is complete and you complete the other methods:

public class Store {


// Total the customer's items at the check-out
public static double totalCost(Product[] cart){
}

// Display the customer's shopping cart items
public static void showCart(Product[] cart) {
}

public static Product[] customer1() {
}


public static Product[] customer2() {
}

public static void main(String[] args) {


// Start of program

Product shoppingCart[]; // list of customer's products

shoppingCart = customer1(); // Replace with customer2() for other test case
showCart(shoppingCart);
double cost = totalCost(shoppingCart);
System.out.println("Total cost is " + cost);

}
}


Notes

Shopping lists

These are the products placed in the array (in the given order) by the customer1() and customer2() methods:

customer1()
Type of Prodcuct
Name
Price
Extra Attribute
Product
Candy bar
0.99

Bulk
Sugar
0.5
20
Bulk Flour
0.6
50
Bulk Ketchup
1.25
100

customer2()
Type of Prodcuct
Name
Price
Extra Attribute
Product
Candy bar
0.99

Produce
Apple
1.67
1.3
Member
Radio
10
true
Member
Toy
10
false
Bulk
Sugar
0.5
20
Bulk Flour
0.6
50
Bulk Ketchup
1.25
100

Arrays and methods

Arrays of data can be used in methods as parameters and return types (covered after Thanksgiving in lecture). Parameters or the return types that are arrays are declared with a square brackets [] to indicate an array.

Here is an example program (ArrayExample.java). The createArray method creates an array and returns it. displayArray() takes the array as a parameter and displays the contents. 

Tester

For Java programs, the tester runs on your computer in Eclipse. Run the programmer in the tester (StoreTester). The program will write results to the console like when your program runs.

Tester: StoreTester.java


Grading

We will grade your Java program based on the following:

Program Submission

You will submit two files: Bulk.java and Store.java. Store the Java files on your local disk until it's finished, then save it into your Onyen public_html directory. Save WaldoData.java in a directory using a secret 8-character name. Do not link your page to other pages in that directory.

Example
http://www.unc.edu/~onyern/c0d3wwwx2/Bulk.java
http://www.unc.edu/~onyern/c0d3wwwx2/Store.java

When your assignment is ready, you will submit your assignment through Blackboard.

Include the following block comment at the top of each submitted files. Change the appropriate information. Such a header is good practice to summarize the purpose of the file. Usually we have used // for comments. In this case, /*   */ is used for a multi-line comment so it is not necessary to place // in front of every line.

/* COMP 110 F07
* Summary:
* Date:
* Author:
*/