473,763 Members | 8,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a method to sort objects

12 New Member
Hi I have created an Inventory program that displays the product name, item number, how many units, and it's price and at the end displays the totals, I have to modify the program so that it will 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.
In order to do this I have to create a method to calculate the value of the entire inventory and create another method to sort the array items by the name of the product.

Here is my code:

import java.text.Numbe rFormat;

public class Produce {

//initialize and load array with values

private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024};

private static String[] produceName = new String[] {"carrots","pot atoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};

private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};

private static double[] producePrice = new double[] {.50,2.75,.60,1 .00,1.50,2.50,3 .00,1.89,2.50,1 .48};


public static void DisplayOutput()

{ //Displays inventory and totals

System.out.prin tln("Item Number\tProduce Name\tStock\tPr ice\tTotal Price\n");

NumberFormat numForm = NumberFormat.ge tCurrencyInstan ce();//Displays double values as currency

double totalValue = 0;

for (int i=0; i<10; i++){

// String price = numform.format( producePrice[i]);

System.out.prin tln(produceNumb er[i]+"\t\t"+ produceName[i]+"\t\t"+produce Stock[i]+"\t"+numForm.f ormat(producePr ice[i])+

"\t"+(numForm.f ormat(produceSt ock[i]*producePrice[i])));

totalValue=tota lValue+(produce Stock[i]*producePrice[i]);
}
System.out.prin tln("\n\nTotal Stock Value: "+numForm.forma t(totalValue));
}
}
Jul 15 '08 #1
1 1633
JosAH
11,448 Recognized Expert MVP
Yep, another person who uses separate arrays where they should've used a
proper little class to hold the information of an item in the inventory; Fortan
seems to live on forever ...

Have a look at this Generic Heap Sort algorithm (there's also a second part)
that can clear up the array mess.

Remember: object oriented programming is not about arrays and never rip object
data apart again just to be able to store them in separate arrays; never,

kind regards,

Jos
Jul 15 '08 #2

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

Similar topics

23
40646
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a
5
1731
by: Dan Williams | last post by:
Hi people I understand that Python is strongly, but also dynamically typed. However, I need to create a variable which initially isn't actually set to anything, but still needs to be of a certain type. I've looked around a bit, but I'm not entirely sure how to do this. Specifically, I have a bit of C++ code which says: struct in_addr ipAddress;
5
2789
by: Max Ischenko | last post by:
Hi, I wrote simple implementation of the "synchronized" methods (a-la Java), could you please check if it is OK: def synchronized(method): """ Guards method execution, similar to Java's synchronized keyword.
4
1616
by: Gonzalo Aguirre | last post by:
i have a classes diagram like this ------------ * ---------- | Vehicles | ------------------------- | Parking| ------------ vehicles ---------- ^ | +--+--------+ | | -------- ---------
3
1871
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help clear up. 1. What is the difference between initializing a control in the constructor, vs the OnInit(), vs the CreateChildControls() methods? 2. The control that I created contains an Items collection attribute...
5
2290
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a collection object -- say, a dictionary. Populating that collection object with custom objects, say, Person. What I really want to see is how to populate the properties of those Person objects from a datasource: instantiate one Person, fill...
9
2838
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
2
1352
by: Ben Sizer | last post by:
I have Python embedded in a C++ application (yes, yes, I know, I'd prefer it the other way around too) and essentially need to expose some read-only values and functions to Python so it can be used to script the host application. When scripting a similar app in TCL, it's possible to associate each command with some client data, so that the command can be written in the script as a free function but it actually executes in some sort of...
6
3884
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the examples I've seen are creating the information that will be displayed from scratch, while I have to use my previously created classes and add a GUI to it. I'm trying to do this GUI using JLabels but it won't let me refer to my CD class methods that...
0
9387
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
10148
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
9823
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...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
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
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
2794
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.