Write a driver program implementing an ArrayList of CD object
I have written the CDException class and the CD class, and now I'm trying to write a driver class to run the other classes. I have to
Read the artist name from user
Read the album name from user
Read the price from user
Read how many in stock from user
And print what they wrote but only the valid entries. Other entries will be ignored upon printing if they just enter blanks.
I know I have to use the scanner. But I have three different classes, the exception, CD class, and the driver class. And I have my constructors in the CD class, but when I attempted to enter the questions in the driver such as
System.out.print("Enter Artist name: ");
sArtist = reader.nextLine( );
sArtist is the constructor in the CD class, and when I try to use it in the driver, it says it hasn't been declared or something like that. How do I link these all three classes, or how would I go about assigning the sArtist to what the user enters? Do i have to re-declare it in the driver as well even tho I done so in the CD class
Here's what I have for my driver class.
Code:
public class CDStore{
public static void main (String[ ] arg) throws Exception{
ArrayList (CD) CD = new ArrayList( );
try{
CD cd1 = new CD("Muse", "The Resistance", 11.99, 20);
System.out.println(cd1.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
try{
CD cd2 = new CD("Yanni", "The Live at the Acropolis", 11.99, 0);
System.out.println(cd2.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
try{
CD cd3 = new CD("Muse", "Black Holes and Revelations", 10.99, 15);
System.out.println(cd3.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
try{
CD cd4 = new CD("Paul Mc Cartney", "All the Best", -0.99, 12);
System.out.println(cd4.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
try{
CD cd5 = new CD("Lady Gaga", "The Fame Monster", 14.99, 44);
System.out.println(cd5.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
try{
CD cd6 = new CD("", "California Gurls", 1.29, 4);
System.out.println(cd6.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
try{
CD cd7 = new CD("Pitbull", "", 11.99, 12);
System.out.println(cd7.toString( ));
System.out.println("=========================");
}
catch(CDException cde){
System.out.println(cde.getMessage( ));
System.out.println("=========================");
}
}//main method ends
} //program ends
Re: Write a driver program implementing an ArrayList of CD object
//you need to gather all the info for the constructor first so do something like
System.out.print("Enter Artist name: ");
String artist = reader.nextLine( );
System.out.print("Enter CD name: ");
String cdName = reader.nextLine( );
System.out.print("Enter Cost : ");
double cost = reader.nextDouble( );
System.out.print("Enter quantity: ");
String quantity = reader.nextInt( );
//Then call your constructor using your variables
CD cd9 = new CD(artist, cdName, cost, quanity);
If you already have the the CD object already made and are trying to add each element to it then you need to identify which CD object you are adding it to such as....
CD cd9 = new CD();
System.out.print("Enter Artist name: ");
cd9.sArtist = reader.nextLine( );
Hope I understood your question and this helps.