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.
and
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.
Code:
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
Code:
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
Comment