The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
How do i add the following to an ArrayList please?
Discuss How do i add the following to an ArrayList please? in the Java Help forum on Dev Shed. How do i add the following to an ArrayList please? Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 25th, 2012, 09:56 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 1
Time spent in forums: 21 m 14 sec
Reputation Power: 0
|
|
|
How do i add the following to an ArrayList please?
Hi, im doing a small bit of work and i cannot figure this out.
I want to create a new 'CD' object in a ArrayList belonging to the Store class by allowing the user to input the data.
Example: The user enters in the name of the artist, track name, album name, year of release and the price of the cd.
Is this possible? It's been killing me trying to figure it out
Any help would be much appreciated :P
Sorry about the mess, not allowed to post urls for the pastebin =(
CD class:
Quote:
package storetestapp;
public class CD
{
//instance variables.
public int cdId;
public String artist;
private String albumName;
private String trackName;
private int year;
private double price;
public CD() // default constructor
{
cdId = 000;
artist ="no title";
albumName = "No director";
trackName ="No lead actor";
year =0000;
price =0;
}
public CD(int newCdId, String newArtist, String newAlbumName, String newTrackName, int newYear, double newPrice)
{
this.cdId = newCdId;
this.artist = newArtist;
this.albumName =newAlbumName;
this.trackName= newTrackName;
this.year = newYear;
this.price = newPrice;
}
//These methods return information about the instance variables.
public int getCdId()
{
return cdId;
}
public String getArtist()
{
return artist;
}
public String getAlbum()
{
return albumName;
}
public String getTrack()
{
return trackName;
}
public int getYear()
{
return year;
}
public double getPrice()
{
return price;
}
// These methods set the instance variables to the value.
public void setCdId (int cdId)
{
this.cdId = cdId;
}
public void setArtist(String artist)
{
this.artist= artist;
}
public void setAlbumName(String albumName)
{
this.albumName = albumName;
}
public void setTrackName (String trackName)
{
this.trackName = trackName;
}
public void setYear (int year)
{
this.year = year;
}
public void setPrice (double price)
{
this.price = price;
}
}
|
Store Class:
Quote:
/**
*
*
*/
package storetestapp; //This is the package for my project.
//These are the imported native Java classes that I will need.
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Store{
Scanner in = new Scanner(System.in);
private List<CD> storeCdList;
public Store() // Default Constructor.
{
storeCdList = new ArrayList<>();
}
/*This method returns all the details about every CD in the Store.
It will print out the information below with the appropiate values
*/
public void printCd(PrintStream prstr)
{
for (int i = 1; i != storeCdList.size(); i++)
{
CD cd = storeCdList.get(i);
prstr.println("Store R:" + i + "; CD ID: " + cd.getCdId() +" Artist: " + cd.getArtist() + ", Track Name: " + cd.getTrack()
+ ", Album Name: " + cd.getAlbum() + ", Year released: " + cd.getYear() + ", Price: "+ cd.getPrice() + ".");
}
}
//This Method will allow me to print out the information of a single CD object.
public List<CD> getCdList()
{
return this.storeCdList;
}
//This Method will allow me to add a CD to the Store. The new CD will automatically be placed last.
public void addCd(CD cd)
{
this.storeCdList.add(cd);
}
public void userAddCd(PrintStream prstr)
{
//DO FOR THE REST - PRINT OUTS
System.out.print("Enter CD ID: ");
int userCdId = in.nextInt();
System.out.print("Enter Artist Name: ");
String userArtist = in.nextLine();
System.out.print("Enter Track Name: ");
String userAlbumName = in.nextLine();
System.out.print("Enter Album Name: ");
String userTrackName = in.nextLine();
System.out.print("Enter Year: ");
int userYear = in.nextInt();
System.out.print("Enter the Price: ");
double userPrice = in.nextDouble();
this.storeCdList.add(null);
}
public void searchStore()
{
}
//This method will allow me to remove a CD from the Store by inputting the CD's Store ID.
public CD removeCd(CD cd)
{
int index = in.nextInt();
return this.storeCdList.remove(index);
}
}
|
Main class(StoreTestApp):
Quote:
// This is to test the program.
package storetestapp;
//These are the classes that are
import java.util.Scanner;
//import java.io.PrintStream;
public class StoreTestApp
{
private static CD i;
Scanner in = new Scanner(System.in);
public static void main(String []args)
{
Store sto = new Store();
// PrintStream ps = new PrintStream("");
//Scanner in = new Scanner(System.in);
// TODO code application logic here
System.out.println("Welcome to our CD store");
System.out.println("We have the following CDs in-store right now!");
sto.addCd(new CD(010, "Game", "Put You On The Game", "The Documentary", 2005, 11.99));
//CD cd1 = new CD("Game", "Put You On The Game", "The Documentary", 2005, 14.99);
//sto.addCd(cd1);
sto.addCd(new CD(101, "Nas", "One Mic", "Stillmatic", 2001, 12.99));
//CD cd2 = new CD("Nas", "One Mic", "Stillmatic", 2001, 12.99);
sto.addCd(new CD(102, "DMX", "Slippin'", "Blood Of My Blood, Flesh Of My Flesh", 1998, 13.99));
sto.addCd(new CD(103, "Eminem", "Like Toy Soldiers", "Encore", 2005, 14.99));
sto.addCd(new CD(104, "Nas", "Ether", "Stillmatic", 2001, 15.99));
sto.addCd(new CD(105, "Game", "The City", "R.E.D", 2011, 16.99));
sto.addCd(new CD(106, "DMX", "X Gon' Give It To Ya", "X The Beast", 2004, 17.99));
sto.addCd(new CD(107, "Tupac", "Changes", "All Eyez On Me", 1996, 18.99));
sto.addCd(new CD(108, "Nas", "N.Y State Of Mind", "Illmatic", 1994, 19.99));
//sto.addCd(cd2);
sto.printCd(System.out);
System.out.print("Enter the Store ID to remove the CD: ");
sto.removeCd(i);
sto.printCd(System.out);
sto.userAddCd(System.out);
sto.printCd(System.out);
//System.out.println(sto.printCd());
//System.out.println(cd1.printCd(PrintStream ps));
}
}
|
Any help would be much appreciated really. Thanks !!
|

October 26th, 2012, 05:33 AM
|
|
Contributing User
|
|
Join Date: Oct 2010
Posts: 38

Time spent in forums: 1 Day 2 h 4 m 8 sec
Reputation Power: 3
|
|
|
I didnt look through all the code but from what I saw, I dont see why your code shouldnt work. What problem are you having?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|