CSCI 240 Spring 2025

Assignment 10
Classes and C-style strings
(100 points)


Thursday, May 1 on the autograder and Blackboard by 11:59 PM

No Late Assignments Will Be Accepted

Overview

For this assignment, implement a class called Product that will be used to represent a product that might be found in a novelty shop.

A partial int main() has already been written for this assignment. It is available for download from the autograder, Blackboard or by using the following link:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign10.cpp

The cpp file that is submitted for grading must be named assign10.cpp.

class Product

The Product class definition should be placed at the top of the source code file while the method implementations should be placed after the closing curly brace for main().

Data Members

The Product class contains four private data members.

Constructors

The first constructor for the Product class is the default constructor. It takes no arguments.

The product identification code should be set to "00000000000". The name data member should be set to the "empty" string (this can be done by copying an empty string literal ("") into a character array using strcpy or by setting the first element of the array to a null character ('\0')). The price and quantity in stock data members should be set to 0.

The second constructor for the Product class takes four arguments: an array of constant character (const char []) that represents an initial product identification code, an array of constant character that represents an initial product name, a float that represents an initial price for the product, and an integer that represents the initial quantity in stock for the product. DO NOT GIVE THESE ARGUMENTS THE SAME NAMES AS THE DATA MEMBERS.

Call the various "set" methods described below to set the values for all 4 data members.

Methods

print

This public method displays the contents of an object. It takes no arguments and returns nothing.

It should display the product identification code, name, price, and quantity members on a single line. Use setw() to line the displayed values up in columns. Use a width of 16 for the product identification code, 48 for the name, 6 for the price, and 6 for the quantity. The product identification code and name should be left-justified while the price and quantity should be right-justified. The price should be displayed with exactly two digits after the decimal point, including zeros.

set_id

This public accessor method sets the product identification code for an object.

It takes one argument: an array of constant character that holds a new product identification code. It returns nothing.

The method should verify the length of the new product identification code. If the length is less than 15, use the strcpy function to copy the new product identification code into the product identification code data member. Otherwise, copy "00000000000" into the product identification code data member.

set_name

This public accessor method sets the product name for an object.

It takes one argument: an array of constant character that holds a new product name. It returns nothing.

The method should use the strcpy function to copy the new product name into the product name data member. No error checking is required.

set_price

This public accessor method sets the price for an object.

It takes one argument: a float that holds a new price. It returns nothing.

The method should check if the new price is greater than 0. If it is, it should be used to set the price data member to the new price. Otherwise, it should set the price data member to 0.

set_quantity

This public accessor method will set the quantity in stock for an object.

It takes one argument: an integer that holds a new quantity in stock. It returns nothing.

The method should check if the new quantity in stock is greater than or equal to 0. If it is, it should be used to set the quantity in stock data member to the new quantity in stock. Otherwise, it should set the quantity in stock data member to 0.

get_price

This public accessor method returns the price data member. It takes no arguments and returns a float.

get_quantity

This public accessor method returns the quantity in stock data member. It takes no arguments and returns an integer.

process_order

This public method processes an order.

It takes one argument: an integer that represents the quantity of the product that has been ordered. It returns an integer: the quantity of the product that the store can ship at this time.

The logic for this method should be as follows:

main()

In main(), implement the following steps:

Step 1
Create 4 Product objects. They can have any name but should contain the values:

The rest of main() will include using the various methods on the 4 Product objects. The assign10.cpp that has been provided contains some cout statements to display headers for the various steps in an attempt to help with spacing issues on the autograder.

Step 2
For each of the products, call the print method to display the product information. Before each call to print, display the titles "Product 1", "Product 2", "Product 3", and "Product 4".

Step 3
For product 1, call the set methods to set the product identification code to "11111111111", product name to "Flowbee Pet Groomer", price to $449.99, and the quantity in stock to 11. Display the title "Product 1" and then call the print method to display the updated product 1 information.

Step 4
For product 4, call the set_id method to set the product identification code to "42424242424". Display the title "Product 4" and then call the print method to display the updated product 4 information.

Step 5
For products 2 and 3, call the get_price and get_quantity methods and display the prices and quantities in stock as shown in the output.

Step 6
For product 1, call the print method to display the initial product information. Call the process_order method and attempt to order -7 product 1s. Display the number of product 1s that were shipped and the sales total as shown below. Call the print method again to display the updated product information.

Use the following format when displaying the number of products shipped and sales total:

Shipped: [number shipped]     Sales Total: $[sales total]

where [number shipped] is replaced by the number of products that were shipped and [sales total] is replaced by the sales total. The display should be preceded by a single newline character and followed by two newline characters.

Step 7
For product 1, call the print method to display the initial product information. Call the process_order method and attempt to order 15 product 1s. Display the number of product 1s that were shipped and the sales total using the above format. Call the print method again to display the updated product information.

Step 8
For product 2, call the print method to display the initial product information. Call the process_order method and attempt to order 11 product 2s. Display the number of product 2s that were shipped and the sales total using the above format. Call the print method again to display the updated product information.

Step 9
For product 3, call the print method to display the initial product information. Call the process_order method and attempt to order 1 product 3. Display the number of product 3s that were shipped and the sales total using the above format. Call the print method again to display the updated product information.

Step 10
For product 4, call the print method to display the initial product information. Call the process_order method and attempt to order 7 product 4s. Display the number of product 4s that were shipped and the sales total using the above format. Call the print method again to display the updated product information.

Step 11
For product 4, call the print method to display the initial product information. Call the process_order method and attempt to order 50 more product 4s. Display the number of product 4s that were shipped and the sales total using the above format. Call the print method again to display the updated product information.

Programming Notes

  1. Each method must have a documentation box like a function.

  2. Hand in a copy of the source code on the autograder and Blackboard.

Output

*** The 4 Products ***

Product 1
00000000000                                                       0.00     0

Product 2
22222222222     Virtual Education Pack                            0.99    31

Product 3
33333333333     Dehydrated Water Bed                              0.00     0

Product 4
4444444444444   3ft Pet Rock Garden Gnome                        72.04    42


*** Product 1 after using the set methods ***

Product 1
11111111111     Flowbee Pet Groomer                             449.99    11


*** Product 4 after using the set_id method ***

Product 4
42424242424     3ft Pet Rock Garden Gnome                        72.04    42


*** Product 2 and 3s price and quantity ***

Product 2  Price: $0.99     Quantity in Stock: 31

Product 3  Price: $0.00     Quantity in Stock: 0


*** Order a negative number of Product 1s ***

11111111111     Flowbee Pet Groomer                             449.99    11

Shipped: 0     Sale Total: $0.00

11111111111     Flowbee Pet Groomer                             449.99    11


*** Order 15 Product 1s ***

11111111111     Flowbee Pet Groomer                             449.99    11

Shipped: 11     Sale Total: $4949.89

11111111111     Flowbee Pet Groomer                             449.99     0


*** Order 11 Product 2s ***

22222222222     Virtual Education Pack                            0.99    31

Shipped: 11     Sale Total: $10.89

22222222222     Virtual Education Pack                            0.99    20


*** Order 1 Product 3 ***

33333333333     Dehydrated Water Bed                              0.00     0

Shipped: 0     Sale Total: $0.00

33333333333     Dehydrated Water Bed                              0.00     0


*** Order 7 Product 4s ***

42424242424     3ft Pet Rock Garden Gnome                        72.04    42

Shipped: 7     Sale Total: $504.28

42424242424     3ft Pet Rock Garden Gnome                        72.04    35


*** Order 50 Product 4s ***

42424242424     3ft Pet Rock Garden Gnome                        72.04    35

Shipped: 35     Sale Total: $2521.40

42424242424     3ft Pet Rock Garden Gnome                        72.04     0