Im trying to have put in a list of 10 CD's (with informatiom, not just blank ones) that will ready be in the list before the program even runs. I obviously am new to this, so i dont know what to do. Any help?
THANKS!
This is my program this far:
public class CD{
private String artist,title;
private double price;
private int rating;
public CD(String a, String t, double p, int r){//Takes in all four parameters
artist = a;title = t;price = p;rating = r;}
//Default Constructor
public CD(){
this ("Justin Tallio","Greatest Hits",3.5,5);}
//Copy Constructor
public CD(CD that){
this.artist=that.artist;this.price=that.price;
this.title=that.title;this.rating=that.rating;}
//Accessors
public String getArtist(){
return artist;}
public String getTitle(){
return title;}
public double getPrice(){
return price;}
public int getRating(){
return rating;}
//Mutators
public void setArtist(String a){//Sets the artist
artist = a;}
public void setTitle(String t){//Sets the title
title = t;}
public void setPrice(double p){//Sets the price
price = p;}
public void setRating(int r){//Sets the rating b
rating = r;}
public String toString(){//toString
return( "Artist:"+artist+"\n\tTitle:"+title+"\n\tPrice:"+price+"\n\tRating:"+rating);
}
}
public class CD_Collection{
private int size=10;
private CD[]colec;
public CD_Collection(int sz,CD[] holder){//Constructor
size = sz;colec=holder;}
public CD_Collection(){//Defaults to an empty CD_Collection with 20 spaces.
this(0,new CD[20]);}
public void addCD(CD newCD){//Adds a new CD
colec[size++]=newCD;}
public void totalCost(){//Adds up the sum of all the prices
double total=0;
for(int i=0;i<size;i++){
total+=colec[i].getPrice();}
System.out.println("cost of all your cds: "+total+"\n");}
public void avgPrice(){//Finds the average price of the CDs
double Cost=0;
for(int i=0;i<size;i++){
Cost+=colec[i].getPrice();}
System.out.println("Average cost of cds: "+Cost/size+"\n");}
public void searchByArtist(String seachItem){//Searches by artist, and returns all the information for those CDs
String seachCDs="";
for(int i=0;i<size;i++){
if(colec[i].getArtist().equals(seachItem))
seachCDs+=colec[i];}
System.out.println("CD's in your collection by:"+seachItem+": "+ seachCDs);}
public void searchByTitle(String seachItem){//Searches by title, and returns the CD information.
String searchTitle="";
for(int i=0;i<size;i++){
if(colec[i].getArtist().equals(seachItem))
searchTitle+=colec[i];}
System.out.println("CD Title"+seachItem+": "+searchTitle);}
public String toString(){//toString
String allCDS="";
for(int i=0;i<size;i++){
String x=colec[i]+"\n";
allCDS+=x;}
return allCDS;}
}//end class
import TerminalIO.*;
public class CD_Menu{
public static void main(String[] args){
CD_Collection group = new CD_Collection();
KeyboardReader reader = new KeyboardReader();
int choice=0;
String choices="What would you like to do?" +//Displays the user with the 6 choices.
"\n\t1) Add Cd"+
"\n\t2) Print Collection" +
"\n\t3) Total Cost" +
"\n\t4) Average Price" +
"\n\t5) Search By Artist" +
"\n\t6) Search By Title ";
while(choice<6){//If the user enters and number over 6, the program quits.
choice = reader.readInt(choices);//shows choices to user
if(choice == 1){//The option for adding a CD
String title = reader.readLine("Title?");
String artist = reader.readLine("Artist?");
double price = reader.readDouble("Price?");
int rating = reader.readInt("Rating?");
CD newCD = new CD(title,artist,price,rating);
group.addCD(newCD);}
else if(choice==2){//The option that prints out all the CD currently stored in the collection.
System.out.print(group);}
else if(choice==3){//The option for printing the total cost.
group.totalCost();}
else if(choice==4){//The option that finds the average price.
group.avgPrice();}
else if(choice==5){//Search by artist
String artist = reader.readLine("What artist would you like to search for?");
group.searchByArtist(artist);}
else if(choice==6){//Search by title
String title = reader.readLine("What CD are you looking for?");
group.searchByArtist(title);}
}//end while
}//end main
}//end class
Thank you!