#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep 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:
    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:
    /**
    *
    *
    */

    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):
    // 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 !!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    42
    Rep Power
    4
    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?

IMN logo majestic logo threadwatch logo seochat tools logo