473,698 Members | 2,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working with arrays question

6 New Member
I am pretty new to Java and have a question about where to begin with my program.

The assignment is to Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
• Create a method to calculate the value of the entire inventory.
• Create another method to sort the array items by the name of the product.

Here is my code
Expand|Select|Wrap|Line Numbers
  1.  
  2. /** 
  3.   *@author Greg Hamilton
  4.   *Program was changed 31 Aug 07
  5.   *The purpose of this program is to create a product class that holds an item number, name of the DVD movie, number of units in stock
  6.   *and the price of each unit
  7. */
  8. public class Dvd 
  9.  
  10. {
  11.  
  12. private String dvdTittle;
  13. private double dvdStock;
  14. private double dvdPrice;
  15. private double dvdItem;
  16. public Dvd( String tittle, double stock, double price, double item )
  17.  
  18. {
  19. //implicit call to Object constructor occurs here
  20. dvdTittle = tittle;
  21. dvdStock = stock;
  22. dvdPrice = price;
  23. dvdItem = item;
  24. } //end four-argument constructor
  25.  
  26.  
  27.  
  28. //set DVD name
  29. public void setdvdTittle( String tittle )
  30. {
  31. dvdTittle = tittle;
  32. } //end method setDvdTittle
  33.  
  34. //return dvd Tittle
  35. public String getDvdTittle()
  36. {
  37. return dvdTittle;
  38. } //end method getDvdTittle
  39.  
  40. //set Dvd stock
  41. public void setDvdStock( double stock)
  42. {
  43. dvdStock = ( stock < 0.0 ) ? 0.0 : stock;
  44. } //end method setDvdStock
  45.  
  46. //return dvd stock
  47. public double getDvdStock()
  48. {
  49. return dvdStock;
  50. } //end method getDvdStock
  51.  
  52. public void setDvdPrice( double price )
  53. {
  54. dvdPrice = ( price <0.0 ) ? 0.0 : price;
  55. } //end method SetDvdPrice
  56.  
  57. //return dvd price
  58. public double getDvdPrice()
  59. {
  60. return dvdPrice;
  61. } //end method get Dvd Price
  62.  
  63. public void setDvdItem( double item )
  64. {
  65. dvdItem = ( item <0.0) ? 0.0 : item;
  66. } //end method set dvd Item
  67.  
  68. //return dvd item
  69.  
  70. public double getDvdItem()
  71. {
  72. return dvdItem;
  73. } //end method getDvdItem
  74.  
  75.  
  76. // calculate inventory value
  77. public double value()
  78. {
  79. return dvdPrice * dvdStock;
  80.  
  81. } //end method value
  82.  
  83.  
  84.  
  85.  
  86.  
  87. public static void main( String args[] )
  88.  
  89. {
  90. //instantiate Collection object
  91. Dvd aDvd= new Dvd ("Dejavu", 10, 12.50, 101); 
  92.  
  93.  
  94. System.out.println("Product Tittle is "+ aDvd.getDvdTittle() ); //display tittle
  95. System.out.println("The number of units in stock is "+ 
  96. aDvd.getDvdStock() ); //display units in stock
  97. System.out.println("The price of each DVD is $"+ aDvd.getDvdPrice() ); //display price
  98. System.out.println( "The item number is "+ aDvd.getDvdItem()); //display unit number
  99. System.out.println( "The value of the inventory is $"+ aDvd.value() ); //display total value
  100.  
  101. } //end main
  102.  
  103. } //end class Dvd
  104.  
I am really struggling where to start.

On the array side, would I use the array only to store the item name, or would the array need to include the other features?

I am confused since an array can only hold the same data type and don't think that the assignment is asking me to use multiple arrays.

Thanks for any pointers.
Sep 6 '07 #1
2 2061
Nepomuk
3,112 Recognized Expert Specialist
...
I am really struggling where to start.

On the array side, would I use the array only to store the item name, or would the array need to include the other features?

I am confused since an array can only hold the same data type and don't think that the assignment is asking me to use multiple arrays.

Thanks for any pointers.
You're right, an array can only hold data of the same type. However, who says that type has to be String?

First of all, think about how you would do it in the real world. There are two possibilities:

Either you have an Index of all Items (e.g. Name and position) and a Collection of the items
or you just have a Collection of the items themselves, including everything at once.

Each Item (it was a DVD, right?) has certain Values: a name, a language, a length... what ever is important for your tast. Then think about how you can put that information into one big lump and have an array of those "lumps".
That should be enough to bring you on the right track, I guess.

Greetings,
Nepomuk
Sep 7 '07 #2
hamiltongreg
6 New Member
Thanks for the pointer, you got me back on track!


You're right, an array can only hold data of the same type. However, who says that type has to be String?

First of all, think about how you would do it in the real world. There are two possibilities:

Either you have an Index of all Items (e.g. Name and position) and a Collection of the items
or you just have a Collection of the items themselves, including everything at once.

Each Item (it was a DVD, right?) has certain Values: a name, a language, a length... what ever is important for your tast. Then think about how you can put that information into one big lump and have an array of those "lumps".
That should be enough to bring you on the right track, I guess.

Greetings,
Nepomuk
Sep 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1560
by: kf4pfw | last post by:
Hi: I have a form with 4 arrays in it. Each array can contain up to 11 elements. I am using the following code (this is for proof of concept): <? if($submit) {
7
11773
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
2844
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
12
1985
by: Samee Zahur | last post by:
Back in the days of old C, only numeric literals could be used as dimensions for statically allocated arrays - the size had to be resolved to a constant at/before compile time. Now I'm beginning to suspect(!) that this is no longer the case either with C or with C++ :( Can anyone upgrade me with the present state/version of rules? (for C++) Exactly, by how many years am I back-dated ???
9
6668
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
15
6994
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with automatic allocation of memory. In C, arrays are groups of variables of the same type in adjacent memory. Allocation for dynamic arrays is handled by the programmer. This is an 8 mark question in an old exam paper, so I am assuming there are
5
2418
by: Frederic Soustra | last post by:
Hi, I am trying to speed up some work I have to do with a 3D Matrix. If I am not mistaken, C is row major, hence the matrix A of size 2 x 2 x 2 would be stored like this in memory: A,A,A,A,A,A,A,A ? Am I correct, I need to work on Big 3D matrices, and I know that the way
41
4956
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
16
2541
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over at just comp.lang.c++. If one of these groups is too inappropriate, just take it off from where you send your replies.) Hi.
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.