I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out.
Same story, I am learning Java and have just written a CD Inventory application. It works, does what I want it to and all that, but now I need to put an array in there to store more than one cd at a time. Seems simple enough until I actually start coding. I want to save as much of the code as I can since I worked so hard to get it just the way I want it, but am not exactly sure of how an array will work in to it all.
All the examples I see on arrays have the information contained in that array entered when the array is created, example - int array[] = {1,2,3,4};.
That is great, but I want my array to use the fields I have already defined and store the information the user enters. Can this be done? Do I need to create a new class altogether, or can I use one of the two I already have and just add a method? Here are the two classes I currently have coded, any help would be appreciated. - import java.util.Scanner; //uses class Scanner
-
-
public class Inventory
-
{// begin class Inventory
-
public static void main(String[] args)
-
{//begin method main
-
-
Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class
-
-
Scanner input = new Scanner(System.in); // create scanner
-
-
-
// begin display method
-
System.out.print("Enter CD Name or STOP to Exit: ");
-
thisCompactdisk.setCDName(input.next()); // read cd name
-
-
while (!thisCompactdisk.getCDName().equalsIgnoreCase("STOP"))
-
{// begin main While
-
-
System.out.print("Enter Price of this CD: "); // prompt for price
-
thisCompactdisk.setPrice(input.nextFloat()); // price input from user.
-
while (thisCompactdisk.getPrice()<= 0)
-
{// begin while
-
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
-
thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
-
} // End while
-
-
System.out.print("Enter CD Item Number: "); // prompt for cd item number
-
thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
-
-
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
-
thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
-
-
-
System.out.print("CD "+thisCompactdisk.getCDName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
-
System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
-
System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
-
-
System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
-
thisCompactdisk.setCDName(input.next()); //name input from user
-
-
} // End main While
-
System.out.print("Ending Program.");
-
-
-
}// end method main
-
} // end class Payroll
and - public class Compactdisk
-
{// begin class
-
-
-
//InventoryCD class has 5 fields
-
String cdName; // cd name
-
float price; // price of cd
-
int itemno; // item number of cd
-
int nstock; // how many units in stock
-
-
//Compact disk class constructor
-
public Compactdisk()
-
-
// 4 fields need to be set up
-
{
-
cdName = "";
-
price = 0;
-
itemno = 0;
-
nstock = 0;
-
}
-
-
// set values
-
public void setCDName(String diskName)
-
{
-
cdName = diskName;
-
}
-
public void setPrice(float cdPrice)
-
{
-
price = cdPrice;
-
}
-
public void setItemno(int cdItemno)
-
{
-
itemno = cdItemno;
-
}
-
public void setNstock(int cdStock)
-
{
-
nstock = cdStock;
-
}
-
-
// return values
-
public String getCDName()
-
{
-
return (cdName);
-
}
-
public float getPrice()
-
{
-
return (price);
-
}
-
public int getItemno()
-
{
-
return (itemno);
-
}
-
public int getNstock()
-
{
-
return (nstock);
-
}
-
-
// returns inventory value
-
public float getValue()
-
{
-
return(price * nstock);
-
}
-
-
}// end class
5 2562
Would it be over your head to have a look at [b]ArrayList[b]s? The trouble with
arrays is that they have a fixed length (you have 101 CDs and an array of length
100; that means trouble).
Here's a hypothetical sketch of an example: -
List<CD> inventory= new ArrayList<CD>();
-
...
-
CD cd= new CD( ...);
-
inventory.add(cd);
-
Give it a try.
kind regards,
Jos
Question, don't think this deserves it own thread, just a curiosity thing:
I've heard that arraylist is sort of the "new and improved" vector ... why is that? The only obvious difference I notice is that there aren't 3 methods that do the same things... perhaps it's more efficient in some way? Could someone shed some light on the subject?
Thanks for the help,
-blazed
Question, don't think this deserves it own thread, just a curiosity thing:
I've heard that arraylist is sort of the "new and improved" vector ... why is that? The only obvious difference I notice is that there aren't 3 methods that do the same things... perhaps it's more efficient in some way? Could someone shed some light on the subject?
Thanks for the help,
-blazed
The Vector class is an old class; it already existed in Java 1.0. All of its public
methods are synchronized which seemed like a handy idea but it isn't, The
Collection Framework introduced the ArrayList class which basically did the
same thing; only a bit better: none of the methods are synchronized and the
reallocation scheme (when the array grows) is a bit better as well.
Nowadays the Vector class only exists as a retrofitted class so that it implements
the List interface, same as the ArrayList does. There's no need to use Vectors
anymore.
kind regards,
Jos
Would it be over your head to have a look at [b]ArrayList[b]s? The trouble with
arrays is that they have a fixed length (you have 101 CDs and an array of length
100; that means trouble).
Here's a hypothetical sketch of an example: -
List<CD> inventory= new ArrayList<CD>();
-
...
-
CD cd= new CD( ...);
-
inventory.add(cd);
-
Give it a try.
kind regards,
Jos
Thanks for the reply. I will read anything I can get my hands on. Where is this "ArrayList" you speak of? It may be over my head but I will certainly give it a try none-the-less.
I do understand the fixed lenght confines of an array, but since this is more of a learning exercise I am not too concerned with that right now. I will not enter in more than 5 cds for what I am doing. I just want to learn how to do it.
Here are the API documents. Note that you can download them all.
(look at the top right corner). An ArrayList is so much more convenient and using
Collection classes like this one may even influence your design and drag it away
from a Fortanesque way of thinking about programs to a more object oriented
way of thinking; do give it a try; it'll help you and ArrayLists aren't that difficult.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fotios |
last post by:
Hi guys,
I have put together a flexible client-side user agent detector (written in
js). I thought that some of you may find it useful. Code is here:
http://fotios.cc/software/ua_detect.htm
...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: MBS |
last post by:
Greetings. I'm still pretty new to PHP and I have a question. I know that
variables are preceeded by a "$" (dollar sign). Typically, a variable has
one value, unless it is an array. Then it is...
|
by: Kieran Simkin |
last post by:
Hi,
I wonder if anyone can help me, I've been headscratching for a few hours
over this.
Basically, I've defined a struct called cache_object:
struct cache_object {
char hostname;
char ipaddr;...
|
by: Tom |
last post by:
I am having a really annoying issue with serialization and a .NET User
Control I am writing. For example, let's say I have a couple of classes in
my control - first class is like:
Public Class...
|
by: Rob R. Ainscough |
last post by:
VS 2005 I have:
ClickOnce deployment
User's that hate and or don't want to use an IE Client (don't blame them)
I don't see how ASPX web pages are going to survive? With .NET 2.0 and
clickonce...
|
by: Leszek |
last post by:
Hi.
Is it possible in javascript to operate on an array without knowing how mamy
elements it has?
What i want to do is sending an array to a script, and this script should
add all values from...
|
by: tlyczko |
last post by:
I am developing an Access database that will be used by some users
logging into Citrix servers.
Using the "Code 1" listing, 'fGetFullNameOfLoggedUser' from Dev
Ashish's site, which I found in...
|
by: Sandman |
last post by:
So, I have this content management system I've developed myself. The
system has a solid community part where members can register and then
participate in forums, write weblogs and a ton of other...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |