473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Creating a method to sort objects

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.NumberFormat;

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","potatoe", "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.println("Item Number\tProduce Name\tStock\tPrice\tTotal Price\n");

NumberFormat numForm = NumberFormat.getCurrencyInstance();//Displays double values as currency

double totalValue = 0;

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

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

System.out.println(produceNumber[i]+"\t\t"+ produceName[i]+"\t\t"+produceStock[i]+"\t"+numForm.format(producePrice[i])+

"\t"+(numForm.format(produceStock[i]*producePrice[i])));

totalValue=totalValue+(produceStock[i]*producePrice[i]);
}
System.out.println("\n\nTotal Stock Value: "+numForm.format(totalValue));
}
}
Jul 15 '08 #1
1 1616
JosAH
11,448 Expert 8TB
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
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*...
5
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...
5
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...
4
by: Gonzalo Aguirre | last post by:
i have a classes diagram like this ------------ * ---------- | Vehicles | ------------------------- | Parking| ------------ vehicles ---------- ^ |...
3
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...
5
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...
9
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
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.