0

我被困在本学期最后一个项目的家庭作业上。我必须创建一个带有 Song 类和 MP3_Player 类的程序。我遇到的问题是从用户那里获取输入并将其放入 arrayList 并打印出来。有人可以帮助我朝着正确的方向前进。

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); 
}
}
}
4

1 回答 1

1

所以这是您问题的解决方案……(据我说):-)

  1. 首先询问用户,他想添加多少首歌曲。说 N.
  2. 现在循环N次并重复以下步骤
  3. 让用户输入歌曲的详细信息说歌曲有 4 件事要添加标题、艺术家、一些详细信息、大小
  4. 让声音类添加所有细节,所以现在在这个循环之后你将拥有 N 个歌曲对象。
  5. 那么如何打印所有歌曲及其信息。
  6. 这是代码:

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

    //而已..

    **So this the song class**
    

**公开课宋{

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;
}

}**

干杯

于 2013-04-17T07:52:37.597 回答