2013-04-17 190 views
0

我被困在一個作業,這是我最後一個學期的項目。我必須創建一個包含Song類和MP3_Player類的程序。我遇到的問題是從用戶處獲取輸入並將其放入arrayList中,並將其打印出來。有人能幫助我朝着正確的方向前進嗎?掃描儀和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); 
} 
} 
} 
+0

用戶需要輸入什麼內容?只是一首歌?或者是一首歌的所有細節呢? –

+0

用戶確實需要輸入所有內容。該節目應該要求提供歌曲標題和文件大小。然後,如果有空閒空間,那麼程序應該將該歌曲添加到ArrayList。另外,如果用戶輸入一個已經存在的標題,那麼它應該從ArrayList中移除標題(我認爲我已經正確的刪除方法)。 – user2288502

+0

然後,您必須使用文件大小參數更新您的構造函數。 –

回答

1

因此,這裏是你的問題的解決方案......(據我):-)

  1. 開始通過詢問用戶,他要多少歌曲add.say N.
  2. 現在循環N次重複下面的步驟
  3. 製作用戶輸入一首歌曲的細節說首歌有4兩件事要添加標題,藝術家,somemoredetails,大小
  4. 使聲音類中添加的所有細節,因此現在在這個循環之後你w生病有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; 
} 

} **

歡呼聲