Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation NEED HELP FAST! Javascript Objects & Methods

    Heres An Assesment I'm Doing, I really Need help On The Item & stockTake Objects, If Anyone Could Help that Would be Wonderful, Cheers; SATRAH

    The ITEM Object

    Overview of the ‘item’ object
    ? Create the ‘item’ object using only stubs and output statements.
    Use meaningful variables and use plenty of internal comments.

    ‘Item’ objects represent/hold information about different things sold by a shop. Each object must contain attributes and methods as detailed:
    Attributes:
    ? The name of the item; a string (eg: MILK)
    ? The quantity currently in stock; an integer (eg: 4)
    ? The current price of the item; a double (eg: 8.90)

    NOTE: When a new ‘item’ object is created it accepts three parameters to initially set the above attributes.

    Methods:
    ? A Reader method ("toString") which returns a string containing the details of the current state of an item. These details should include the name, quantity, price & value.
    ? A Writer method for the name
    ? A Writer method for the price. This should only change the price attribute if the new price is a positive (can't have a negative number).
    ? A Writer method for the quantity. This should only change the quantity attribute if the new quantity is a positive (can't have a negative items).

    NOTE: Only these attributes and methods are required, although others such as size etc could be added.


    Save the finished file as StockTake.js (do not include test data!)








    The STOCKTAKE Object

    Overview of the ‘item’ object
    ? Create the ‘item’ object using only stubs and output statements.
    Use meaningful variables and use plenty of internal comments.

    The ‘stockTake’ object must contain attributes and methods detailed:

    Attributes:
    ? An array to hold ‘item’ objects – called ‘groceries’
    ? An integer to keep track of how many ‘item’ objects are actually stored in the array at any time called ‘numStored’.

    NOTE: When a new ‘stockTake’ object is created it accepts as a parameter a number to indicate how big the groceries array will be. If no parameter is received it will create a groceries array with 50 elements.

    Methods:
    ? A method called ‘addItem’ which accepts as a parameter a reference to an ‘item’ object. This method will store the reference to this ‘item’ object into the next available cell in the array. The attribute, which keeps track of the number of stored ‘items’, should then be incremented.
    ? A method called ‘findItem’ which accepts as a parameter a string representing the name of an ‘item’. This method will search through the array of ‘items’ and if found it will return the items details via the ‘toString’ method. If no matching item is found the Boolean value false is returned.
    ? A method called ‘stockValue’ which returns the current value of the entire collection of ‘items’. This value is the sum of all the values of the individual ‘items’ in the array.
    ? A method called ‘getNeededItems’ which traverses the array of ‘items’ and collects the names (ONLY) of any ‘item’, which has a quantity that is less than 10. These names (strings) are copied into another array which is returned to the calling code.

    NOTE: The collection does not have to be sorted in any way and deleting items is not required.

    The USER INTERFACE
    The user interface is web based and should create a single ‘stockTake’ object when the page is loaded.
    It is suggested that you also code the creation of three items and add them to the newly created ‘stockTake’ object each time the page is loaded. This will allow for easier testing as your collection will already have 3 ‘items’ every time you load the page.

    The user interface must provide a simple button menu to the user and use a text area for displaying the results of each operation.

    to help with scripting the buttons will be called:
    ? View All Stock
    ? Add An Item
    ? Find An Item
    ? Needed Items
    ? Value Of All items
    ? RESET

    --------------


    if anyone could help out with this it would be great! CHEERS!

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    ok, I guess that you need first to understand what your teacher asked you (which merely I confess I don't :D )

    I guess that your teacher wants to see how you suppose to build a collection.

    Ok now. There are 3 main types of collections: arrays, objects (created with newObject() method or with JSON) and DOM collections of pages elements with the same name or tagName or the same parent (the childNodes).

    The first 2 may be used in your problem. Besides the syntax, the main difference between an Array and an Object, is the sorting. Arrays are sorted collections with a length (the elements have an index), while Objects are unsorted, without a length. Let's see how we can create an Object (I will use JSON, as it is simplier)

    var myitem={
    name:'MILK',
    quantity:4,
    price:8.90
    }

    How to return (the Reader method)

    We may circle throught the object's properties like:

    for(arg in myitem){
    alert('the item\'s '+arg+' is '+myitem[arg])
    }

    Or we may return a certain property's value, on will:
    alert('the item\'s name is '+myitem.name)
    alert('the item\'s quantity is '+myitem.quantity)

    Ok. But how to "write" dynamically the object's prperties and values? You may use a prompt method to input the values:

    var name = prompt('Please Enter Product','');
    var quantity = prompt('Please Enter Quantity','');
    var price = prompt('Please Enter Price','');

    Now you may either use a preset object
    var myitem={
    name:name,
    quantity:quantity,
    price:price
    }
    for(arg in myitem){
    alert('the item\'s '+arg+' is '+myitem[arg])
    }

    Or you may inserted it one by one:

    var myitem={}
    myitem['name']=name;
    myitem['quantity']=quantity;
    myitem['price']=price;
    for(arg in myitem){
    alert('the item\'s '+arg+' is '+myitem[arg])
    }

    Is it clear till here?
    Last edited by Kor; 05-24-2006 at 02:21 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    How to display all these on a web page? Yout teacher must have told you about document.write(). Not a bright ideea, it is an old and non-dynamic method. DOM methods or even innerHTML are better by far.

    The Whole code could be now something like this:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    var 
    name prompt('Please Enter Product',''); 
    var 
    quantity prompt('Please Enter Quantity','');
    var 
    price prompt('Please Enter Price','');
    var 
    myitem={
    name:name,
    quantity:quantity,
    price:price
    }
    var 
    txt='';
    for(
    arg in myitem){
    txt+=(arg+' : '+myitem[arg]+'<br>')
    }
    onload=function(){
    document.getElementById('mydiv').innerHTML=txt;
    }
    </script>
    </head>
    <body>
    <div id="mydiv"></div>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes, you may use a new Function() as well, and return the this properties and their values. Or you might have used prototype. But, as you have seen, Objects are simplier and easier to be handled, at least I think so.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •