0

I am stuck on a homework assignment that is my last project of the semester. I have to create a program with a Song class and a MP3_Player class. The problem I am having is taking the input from the user and putting it into an arrayList and also printing it out. Could someone help push me in the right direction.

package program3;

import java.util.Scanner;





/**
 *
 * @author Seth
 */
 public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here


    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the song's title:");
        String title = sc.nextLine();
    System.out.print("Enter the song's artist:");
        String artist = sc.nextLine();
    System.out.print("Enter the song's duration:");
        float duration = sc.nextFloat(); 
    System.out.print("Enter the size of the songs file: "); 
        int fileSize = sc.nextInt(); 

    Song song = new Song(title, artist, duration, fileSize);
        song.add(song);

    for (int i = 0; i<song.length; i++){
        System.out.println("Song " + i + " title is: " + 
            song.getTitle() + ", the artist is " + 
            song.getArtist() + " and the duration is " + 
            String.valueOf(song.getDuration()) + ".");
    }

}

}






package program3;

import java.util.ArrayList;



 /**
 *
 * @author Seth
 */
public class MP3_Player {

private int freeSpace;
private int storageCapacity;

ArrayList<Song> songList = new ArrayList<Song>(); 



public MP3_Player( )
{ 
   storageCapacity = 10;
   freeSpace = storageCapacity; 

}

public MP3_Player(int storage)
{
    storageCapacity = storage; 
    freeSpace = storageCapacity;

}    

public String toString()
{
    return "MP3 player with capacity " + storageCapacity + 
            ", " + freeSpace + " left, contains:\n" + 
    songList.toString().replace("[","").replace("]","").replace(",","\n");
}

public int getFreeSpace()
{
    return freeSpace; 
}

public int getStorageCapacity()
{
    return storageCapacity; 
}

public ArrayList<Song> getSongList()
{
    return songList; 
}

public void setStorageCapacity(int newStorageCapacity)
{
    storageCapacity = newStorageCapacity; 
}

public void setFreeSpace(int newFreeSpace)
{
    if (newFreeSpace >= 0) {
        freeSpace = newFreeSpace; 
    }
}


public void addSong(Song s)
{
     if(s.getFileSize() <= freeSpace)
     {
         songList.add(s);
         System.out.println(s.getTitle() + " added to the MP3 player."); 
         freeSpace -= s.getFileSize(); 
     }
     else
     {
         System.out.println("Cannot add " + s.getTitle() + 
                 " not enough free space.");
     }

}


public void removeSong(Song s)
{
    boolean songRemoval = false; 
    for (Song i : songList)
    {
        if(s.equals(i))
        {
            freeSpace += s.getFileSize(); 
            songRemoval = true; 
            System.out.println(s.getTitle() + 
                    " is removed from the MP3 Player."); 
        }
        else if(s.equals(i))
        {
            System.out.println("This song isn't stored in the MP3 Player."); 
        }
    }
    if(songRemoval)
    {songList.remove(s); 
}
}
}
user2288502
  • 11
  • 1
  • 6
  • What does the user need to input? Just a Song? Or all the details of a Song rather? – Cy Pangilinan Apr 17 '13 at 00:26
  • The user does need to input everything. The program is supposed to ask for a song title and the size of the file. Then if there is free space then the program is supposed to add the song to the ArrayList. Also, if the user inputs a title that already exists then it is supposed to remove the title from the ArrayList (the remove method I think I have properly). – user2288502 Apr 17 '13 at 00:44
  • You have to update your constructor with a filesize argument then. – Cy Pangilinan Apr 17 '13 at 00:58
  • Are you talking about public MP3_Player( ) { storageCapacity = 10; freeSpace = storageCapacity; } – user2288502 Apr 17 '13 at 01:06
  • I meant the fileSize of a Song, since your constructor only has 3 arguments (title, artist and duration). – Cy Pangilinan Apr 17 '13 at 01:11
  • I edited what I have now. I had to change what Mr D posted because we haven't covered BufferedReader or InputStreamReader. I am only familiar with using Scanner. – user2288502 Apr 17 '13 at 01:26
  • Consult this SO question for to read input using Scanner http://stackoverflow.com/questions/12999899/getting-user-input-with-scanner – Cy Pangilinan Apr 17 '13 at 01:36

1 Answers1

1

So here's a solution to your problem...(according to me) :-)

  1. Start by asking the user ,how many songs he wants to add.say N.
  2. Now loop N times and repeat the following steps
  3. Make user enter the details for a song say song has 4 things to be added title,artist,somemoredetails,size
  4. Make the sound class add all the details and so now after this loop you will have N song objects.
  5. So how do you print all the songs with their information.
  6. Here's the code:

    //loop through the arraylist like this 
    for (Song s:arraylistofobjcts){
       s.o.p("Songs"+s.getTitle()+s.getArtist()+s.getWriter()+s.getFileSize())
    }
    

    //Thats it..

    **So this the song class**
    

**public class Song {

private String title,writer,artist;
    private int size;

public Song(String title,String writer, String artist,int size){

    this.setTitle(title);
    this.setWriter(writer);
    this.setArtist(artist);
            this.setSize(size); 
}
public int getSize() {
    return this.size;
}
public void setSize(int size) {
    this.size = size;
}
public String getTitle() {
    return this.title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getArtist() {
    return this.artist;
}
public void setArtist(String artist) {
    this.artist = artist;
}
public String getWriter() {
    return this.writer;
}
public void setWriter(String writer) {
    this.writer = writer;
}

}**

cheers

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42