473,804 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inventory Program Part2

12 New Member
I have to assignment which includes the following: 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.

I have tried so far this is what I have;


Expand|Select|Wrap|Line Numbers
  1.  
  2. 1. import java.text.NumberFormat;
  3. 2. 
  4. 3. public class Produce {
  5. 4.
  6. 5.    //initialize and load array with values  
  7. 6.  
  8. 7.  private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018,   2019, 2020, 2021, 2022, 2023, 2024};
  9. 8. 
  10. 9. 
  11. 10.  private static String[] produceName = new String[] {"carrots","potatoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};
  12. 11. 
  13. 12.
  14. 13. private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};
  15. 14.  
  16. 15. 
  17. 16.
  18. 17. private static double[] producePrice = new double[] {.50,2.75,.60,1.00,1.50,2.50,3.00,1.89,2.50,1.48};
  19. 18.
  20. 19.
  21. 20. public static void DisplayOutput()
  22. 21.
  23. 22. {   //Displays inventory and totals
  24. 23.
  25. 24.   System.out.println("Item Number\tProduce Name\tStock\tPrice\tTotal Price\n");
  26. 25.
  27. 26.   NumberFormat numForm = NumberFormat.getCurrencyInstance();//Displays double values as currency
  28. 27.
  29. 28.        double totalValue = 0;
  30. 29.
  31. 30.   for (int i=0; i<10; i++){
  32. 31.
  33. 32.        //   String price = numform.format(producePrice[i]);
  34. 33.
  35. 34.           System.out.println(produceNumber[i]+"\t\t"+ produceName[i]+"\t\t"+produceStock[i]+"\t"+numForm.format(producePrice[i])+
  36. 35.
  37. 36.                   "\t"+(numForm.format(produceStock[i]*producePrice[i])));
  38. 37.
  39. 38.           totalValue=totalValue+(produceStock[i]*producePrice[i]);
  40. }
  41. 39.        System.out.println("\n\nTotal Stock Value: "+numForm.format(totalValue));
  42. 40.}
  43. 41.}
  44. 42.
  45. 43.// Inventory Part 2 July 17, 2008
  46. 44.
  47. 45.   function setObject (produceName, produceNumber, produceStock, producePrice){
  48. 46.      myObjectArray[objectArrayIndex++] = new ownObject (produceName, produceNumber, produceStock, producePrice);
  49. 47.
  50. 48.   }
  51. 49.
  52. 50.
  53. 51.
  54. 52.   var objectArrayIndex =0;
  55. 53.   var myObjectArray = new Array();
  56. 54.
  57. 55.   SetObject ("carrots","potatoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery");
  58. 56.   SetObject (2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024);
  59. 57.   SetObject (20, 50, 12, 15, 10, 30, 40, 6, 75, 8);
  60. 58.   SetObject (.50,2.75,.60,1.00,1.50,2.50,3.00,1.89,2.50,1.48);
  61. 59.
  62. 60.   function showArray (produceName, produceNumber, produceStock, producePrice) {
  63. 61.
  64. 62.     document.write (produceName + ':');
  65.  
Jul 16 '08
16 6009
lilsugaman
12 New Member
it did shut up but it didn't sort the data.
Jul 16 '08 #11
BigDaddyLH
1,216 Recognized Expert Top Contributor
it did shut up but it didn't sort the data.
http://java.sun.com/docs/books/tutor...x.html#sorting
Jul 16 '08 #12
lilsugaman
12 New Member
Thanks for the site but can you give me an example of how you would start the sort because the site you gave me basically has the same thing I have to sort but it's not sorting one product at a time with the name, number, units, and price.
Jul 17 '08 #13
JosAH
11,448 Recognized Expert MVP
Thanks for the site but can you give me an example of how you would start the sort because the site you gave me basically has the same thing I have to sort but it's not sorting one product at a time with the name, number, units, and price.
That's because you've split different attributes that belong in one single class,
not spread out over separate arrays; the is so Fortranesque and is cursing at
proper OO programming. The following little article can help you out but you do
have to read and understand it first; good luck with it.

kind regards,

Jos
Jul 17 '08 #14
lilsugaman
12 New Member
ok, I have changed my code about 15 million times, I am a beginner at this and when I say beginner like this is the first time I laid eyes on programming. No I don't understand everything but the more I work at it and perform practices programs I will conquer it. So here's my new code.

[code]

1. import java.util.*;
2. import java.text.Numbe rFormat;
3.
4. public class Produce {
5.
6. //initialize and load array with values
7.
8. private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024};
9.
10. private static String[] produceName = new String[] {"carrots","pot atoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};
11.
12. private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};
13.
14. private static double[] producePrice = new double[] {.50,2.75,.60,1 .00,1.50,2.50,3 .00,1.89,2.50,1 .48};
15.
16.
17. public static void DisplayOutput()
18.
19. { //Displays inventory and totals
20.
21. System.out.prin tln("Item Number\tProduce Name\tStock\tPr ice\tTotal Price\n");
22.
23. NumberFormat numForm = NumberFormat.ge tCurrencyInstan ce();//Displays double values as currency
24.
25. double totalValue = 0;
26.
27. for (int i=0; i<10; i++){
28.
29. // String price = numform.format( producePrice[i]);
30.
31. System.out.prin tln(produceNumb er[i]+"\t\t"+ produceName[i]+"\t\t"+produce Stock[i]+"\t"+numForm.f ormat(producePr ice[i])+
32.
33. "\t"+(numForm.f ormat(produceSt ock[i]*producePrice[i])));
34.
35. totalValue=tota lValue+(produce Stock[i]*producePrice[i]);
36. }
37. System.out.prin tln("\n\nTotal Stock Value: "+numForm.forma t(totalValue));
38. }
39. }
40.
41. // Inventory Part 2 July 17, 2008
42.
43.
44.
45. class Sort {
46.
47. public static void main(String[] args) {
48.
49.
50. List<String> list = Arrays.asList (args);
51. Collections.sor t (list);
52. System.out.prin tln (list);
53.
54.
55. }
56. }
Jul 17 '08 #15
JosAH
11,448 Recognized Expert MVP
You've still missed the point: a Product should be a single class, not just a couple
of separate arrays that take the individual attributes of a Product. First define that
little class.

kind regards,

Jos
Jul 17 '08 #16
chaarmann
785 Recognized Expert Contributor
first make a class called Product. It stores the name price, stock, name, and number (which are called attributes) of a single object. Then this class should have setter and getter methods for each attribute. Like
Expand|Select|Wrap|Line Numbers
  1. public String getName()
  2. {
  3.    return this.name;
  4. }
  5.  
  6. public void setName(String newName)
  7. {
  8.    this.name = newName;
  9. }
You don't need the setter methods if you make a constructor that sets them all, and if you are sure you don't need to change them anymore. But you need the getter methods to make up your Comparator later on.

Then you make an array of these products.
Expand|Select|Wrap|Line Numbers
  1. Product[10] productArray =
  2. {
  3.  new Product("apple", "10.20 Dollar", 3),
  4.  new Product("banana", "6.40 Dollar", 4),
  5.  ...
  6. }
That means, as what JosAH was saying, you don't have separate arrays of names, prices etc. Instead you have single products as objects which have all its data inside.

Then read about about Collections and Comparator to sort your array the way you want.
Sorting should be in a single line like:
Expand|Select|Wrap|Line Numbers
  1. Collections.sort(productArray, sortByPriceComparator);
Jul 18 '08 #17

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

Similar topics

109
25902
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the assignments... 4. CheckPoint: Inventory Program Part 1 • Resource: Java: How to Program • Due Date: Day 5 forum • Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD...
2
1256
by: begum | last post by:
hello everyone! my program is not working properly, it is not summing the values that i entered. It should take the double values and add them and also take strings and makes the * as a multiplacation . thanks !!! begum
0
7118
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
9
7614
by: xxplod | last post by:
I am suppose 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...
3
7013
by: cblank | last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory program that I have built so far with no problem. But I cannot figure out how to turn it into a GUI application. This is what I'm trying to get it to do. Modify the Inventory Program to use a GUI. The GUI should display the information one product...
2
2545
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item. Add a Save button to the GUI that saves the...
1
2665
by: jcato77 | last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created. Assignment: Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method...
3
4490
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI 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 GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. Here is my Inventory program from 1 to 3: package...
2
4449
by: blitz1989 | last post by:
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the reading has very little in it that helps can someone take a look at this code and point me in the right direction please. Thanks for any help that is offered. //Modify the Inventory Program to use a GUI. The GUI should display the //informationone...
0
9705
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
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
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
10311
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
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5516
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...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.