Classes and Arrays


#1

I am struggling with this assignment. I would appreciate some help on where to begin. The way my professor is explaining this assignment is overwhelming to me. Is there someone that could “dumb” this down for me…
I understand Ill need to use Arrays, Hashes, and Classes. But I have no idea where to begin.


ASSIGNMENT:
In this assignment, you will create an application that keeps track of a store’s inventory of
products as well as allow product purchases. This application will be written using the
object-oriented coding paradigm, so we will build a class as well as a program, each of which
will reside in its own file. Therefore, your solution this week will consist of two files, both of
which you’ll need to submit together in a single zip file. So let’s get started.
The Product Class
Create a file named Product.rb that contains a Product class. The class should have the
following attributes:
ProductNumber A string used to hold a product number. Each product in the inventory
should have a unique product number.
Description A string that holds a brief description of the item.
Cost A decimal value that holds the amount the retail store paid for the item.
Retail A decimal value that holds the retail price for the item.
QuantityOnHand An integer value that holds the number of units in stock for the product.
This value cannot be less than zero.
Your Product class also needs a method to display all of the aforementioned attributes. Create
a method named display() that prints out the values in an attractive way. One approach
you could take is to display the data in two columns — the first to display the name of the
attribute, and the second to display the corresponding value.
Make sure you have a comment block at the top of the Product.rb file that describes the class
and provides a description of each attribute and method.
The Program
Create a file named Ch9Asg.rb and put it in the same folder. Put a comment block at the top
that lists the appropriate information. Since we will be creating Product objects in the
program, you need to make this file aware of the Product.rb file. To do that, place the
following line of code directly underneath the comment block:
require_relative ‘Product’
Summer 2017 CIS 116 Computer Programming I
Your program should consist of a central loop that displays a menu, gets the user’s selection,
carries out the selection, and returns back to the menu. The menu should list the following
options:

  1. Create a new product
  2. Look up a product
  3. Process a product purchase
  4. Exit
    Your program needs an array to hold the Product objects the user will create with menu
    option 1 in order to access those objects in menu options 2 and 3. Here’s an overview of how
    each menu item should work:
    Create a New Product
    The user should be prompted for all values that make up a product. All appropriate values
    should be validated as they are entered. Make sure the product number the user supplies
    does not already exist in the list of existing products. Once all of the data are supplied and are
    valid, create a new Product object, passing the data to it when created. Then, add the
    Product object the array that holds them.
    Look Up a Product
    The user should be prompted for a product number. The program will then attempt to locate
    the product in the array. If it finds a Product with a matching number, the product will be
    described. Call the display() method of the appropriate Product object to do so. If a
    product with the number the user entered cannot be found, inform the user and return back
    to the menu.
    Process a Product Purchase
    Display the product number, description, retail price, and number of units on hand for all
    products whose QuantityOnHand is greater than zero. The user should then be prompted for
    a product number from that list. If the user enters a valid product number, the user should be
    asked how many to purchase. The quantity to be purchased cannot exceed the number of
    units on hand. If the quantity is valid, the application should display the subtotal, sales tax
    amount (6%), and the grand total for the sale. Don’t forget to update the product’s
    QuantityOnHand by subtracting the number purchased.

#2

So far I have this…


#3

I am also guessing I let the user enter information and then use that to create an array. I dont “pre make” an array for the program…?


#4

The documentation is old. But for the most part still relevant:

http://ruby-doc.com/docs/ProgrammingRuby/

the pickaxe is a great investment for a ruby programmer

I would change your first line product = {“Galaxy” => “Note 4” , “Google” => “Pixel”, etc …}

imagine an array with automatic ordering, and you got an hashes

Array are very useful to display a list of thing with a linefeed.


#5

Hello @sdem9,

Would be really helpful if you provide the source directly here.


#6

2nd for the Pickaxe book – it is truly a great read.

build your class for Product, which you have already started.

then build the main program one menu item at a time.

From reading the description… no, there is no “pre-entered” data.

The Array is used to hold the instances of the Product class that you create with Menu Item 1.
Apparently you need to check all the constraints on the entered data (this could also be done in the Product class). e.g. quantity_on_hand cannot be less than zero.

products = Array.new

loop do
  # get user input
  case selection
  when 1
     # ask for all the product attributes, check them against the parameters
     # create a Product class instance
     # add it to the products array
  when 4
     break
  end
end

#7

One more hint, to add a new product, you’re going to do something like “products << Product.new(…)”