Hi,
Im fairly new to java and im working my way through a load of exercises and im a little bit stuck on something.
I have two classes, one of which holds the main, which creates 3 objects.
I would like to know how to how to count the total number of classes, and total the value held within each object.
Basically, count how many CDs exist, then sum the value of each CD.
Ive added the variables to the CD class, but im then required to add something into the creation of the object, which i dont want to do.\
I was thinking i may need another class?
Thanks for any help recieved
class CDDriver
{
public static void main(String args[])
{
CD cd1 = new CD("Kaiser "," up the khazi ",(double) 9.99);
CD cd2 = new CD("Oasis "," morning glory ",(double) 3.99);
CD cd3 = new CD("Bob Dylan "," Alreet Sunna ",(double) 6.99);
System.out.println("Artist \t\tTitle\t\tCost");
System.out.println("====================================");
System.out.println(cd1.toString());
System.out.println(cd2.toString());
System.out.println(cd3.toString());
}
}
public class CD {
private double totalPrice;
private int totalCDs;
private String Artist;
private String Title;
private double Cost;
public CD(String Artist, String Title, double Cost)
{
this.Artist = Artist;
this.Title = Title;
this.Cost = Cost;
}
public CD(double totalPrice, int totalCDs)
{
this.totalPrice = totalPrice;
this.totalCDs = totalCDs;
}
public String toString()
{
return "CD details: " + Title +" By: " + Artist + "Price:" + Cost;
}
}