Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creating an array of existing objects.

 
Lloyd Hatch
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating code from scratch just to practice Java, so this might be a question of poor OO design instead of just me being stupid.  

I have a class called Commodity which represents items that my fictitious might sell.  Then I spun up and create 1000 of those objects in my main class.  Next I am creating a class called warehouse that would represent places where I am storing the objects before they are sold.



And that's where I'm getting stuck.  I want to populate that with a list of say 100 items that I stock in that warehouse.  So that isn't an array of new Commodity(100)  because those are existing objects, I just want to point to the existing Commodity objects.  

I would appreciate hints rather than explanations so I can go learn.  I'm just not finding much when searching using my title.  
 
Lloyd Hatch
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I had a thought  could I add a key to the commodity object?     Then have my array on the warehouse object be an array of int and populate that with the keys to the commodity objects?   I guess then in my main class I could have an array of all the commodity objects I have created.  That would let me use a binary search on that array to find the commodity that the array in Warehouse points to?   Seems there should be a better way though.
 
Piet Souris
Bartender
Posts: 4911
185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lloyd,

why using a fixed-length array? Having a sellList that is an ArrayList<Commodity> is much more flexible, and you could add a method 'addCommodity' to your Warehouse-class, and a 'removeCommodity'-method as well.

And you could also have a method 'findCommodity', with a parameter that enables you to find one or more Commodities, given some criteria.
 
Campbell Ritchie
Marshal
Posts: 75648
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lloyd Hatch wrote:. . . add a key to the commodity object?     Then have my array on the warehouse object be an array of int and populate that with the keys to the commodity objects? . . .

Maybe yes. But you seem to be adding things to your design left right and centre and maybe the design can't accommodate these additional features.

main class . . .

What's a main class? It is a term lots of people use. Does it mean a class which does more than any other class? If so, that is probably bad design.

binary search . . .

That sounds as though you were mixing implementation details with your large‑scale design, which is usually a bad idea. Get the design first and add implementation details later. Then you can change the implementation later on. Also remember that binary search only works on sorted arrays or collections.

Seems there should be a better way though.

I think I agree, even though you haven't told us much about your design. You might have to start from scratch though.
I suggest you start with a specification, which tells us what you want your app to do, not how you are going to do it.
 
Liutauras Vilda
Sheriff
Posts: 8269
592
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lloyd Hatch wrote:So that isn't an array of new Commodity(100)  because those are existing objects, I just want to point to the existing Commodity objects.


Maybe upon instantiation of Warehouse, you'd need to pass those Commodity objects (let's say an array of them) to a constructor and assign to the field (instance variable) of Warehouse class?
 
Liutauras Vilda
Sheriff
Posts: 8269
592
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lloyd Hatch wrote:So I had a thought  could I add a key to the commodity object?     Then have my array on the warehouse object be an array of int and populate that with the keys to the commodity objects?   I guess then in my main class I could have an array of all the commodity objects I have created.  That would let me use a binary search on that array to find the commodity that the array in Warehouse points to?   Seems there should be a better way though.


Think how you'd search those commodities. If you have an array, and the location (index) within the array doesn't help with search, because you need let's say some name of commodity, maybe a Map<String, Commodity> would work better for you? Where key would be something what easily distinguishes one commodity from another.

Some questions to answer for yourself before you jump into implementation.
 
Campbell Ritchie
Marshal
Posts: 75648
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are all sorts of ways of sorting or searching. Here's one which will be familiar to many reading this thread, but not everybody.I don't know whether OP is familiar with Maps.
 
Liutauras Vilda
Sheriff
Posts: 8269
592
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There are all sorts of ways of sorting or searching. Here's one which will be familiar to many reading this thread, but not everybody.


Absolutely. But then may become things more complicated when certain commodity gets sold out and let's say doesn't need to be in the array anymore.

It would be useful to know whether OP is bounded to use an array and whether that is the only data structure OP is familiar with.
 
Lloyd Hatch
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't know whether OP is familiar with Maps.



Nope not yet, but I'll check into it.  

What's a main class? It is a term lots of people use. Does it mean a class which does more than any other class? If so, that is probably bad design.


My main class is what I call the one with public static void main.   Currently all I have in it is methods to run tests on the other classes I'm creating.    Eventually I plan to add things like a method to load data from disk or db, another method to save data to disc or db upon exit.  I am coming from 20 years of mainframe Cobol and Oracle Pl/SQL so I'm very engrained in procedural rather than OO design.   Should things like loadData and SaveData be in another class?

I suggest you start with a specification, which tells us what you want your app to do



Here is the task I set myself  because it is something I'm familiar with.  I figured that would make it easier for me to learn a different language, since most of my 20 years have been working with inventory management systems, I know pretty much what tasks need to be handled.  Build an application that will track items across several warehouses, allow the users to receive quantity into the warehouse and ship quantity out.  The item list should be flexible to allow new items to be introduced and old items to be removed and have several types of items that would have different storing and shipping requirements.  Warehouses should be able to store a variety of items and be able to track quantity as it increases with receipts and decreases with shipments.  

Commodity and its subclasses  will define the items that the app tracks.   It contains data points like name, mass/unit, volume/unit, wholesale price, purchase price, list price, global qty and methods such  as a display (overrides toString cause I read that was important).    This is how far I've gotten.

Warehouse class which I'm starting was to contain data about the warehouse and what items/quantities are currently in the warehouse.  Should also contain methods such as receiveItems and shipItems that would be called to add and remove quantity.

That was as far as I got with my design.   I'm not really wanting to build a full fledged inventory management system just enough that I can learn the language.   Eventually I would add ability to interact with db  or a file to save data between runs as I said above.

Having a sellList that is an ArrayList<Commodity> is much more flexible, and you could add a method 'addCommodity' to your Warehouse-class, and a 'removeCommodity'-method as well.


Haven't gotten to lists yet either but that sounds like a much more efficient way to go.  I'll check into that too.
 
Campbell Ritchie
Marshal
Posts: 75648
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be on the right lines about testing classes, and that specification sounds good. I suggest you create a list of classes. Not a long list: “I have a little list, and they'll none of them be missed.” Use the nouns in the specification to give you the namwes of the classes and the verbs to say what they do. Example:-

Commodity: encapsulates description and price. Allows price to be changed.

That shouldn't take long and then you will be in a position to write some code.
By the way: I suggest you keep it simple. Do you need wholesale price, retail price and weight in the Commodity class? If you don't need them just now, leave them out; you can very easily add them later.
You can find out about Lists and Maps later on: suggested resource Java™ Tutorials. You can try your own looping and searching through an array, which is by no means the easiest way to do it, but it might teach you more basic programming. It will probably also teach you why nobody else codes like that
 
What are you doing in my house? Get 'em tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic