Hello everyone I just finished my last program for class this semester and would love to have some feedback from the memebers here on things that I still need to improve on or good coding practices that I may not know about that can help me become better. I have already turned the program in and as far as the testing I have done the program works as intended so hopefully I will do well on it.
The first section will be the main, followed by a Song class, and an MP3_Player class. This is the longest code I have posted to date so I hope I make it readable and easy to understand. I have also tried to add enough comments to make it easier to follow. Thank you for your time and advice!
package cse1301_program3;
public class CSE1301_Program3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/* This will create an MP3_Player object called ipod. It will then give
* it the initial storageCapacity of 22.
*/
MP3_Player ipod = new MP3_Player(22);
/*This will create 6 Song objects song1-song6. It will then add each
* song to the MP3 player if there is enough freeSpace on the MP3 Player.
*/
Song song1 = new Song("Free Falling", "Tom Petty", 4.54f, 5);
ipod.addSong(song1);
Song song2 = new Song("Fearless", "Taylor Swift", 3.6f, 3);
ipod.addSong(song2);
Song song3 = new Song("Hotel California", "Eagles", 3.9f, 4);
ipod.addSong(song3);
Song song4 = new Song("Paradise City", "Aerosmith", 4.7f, 6);
ipod.addSong(song4);
Song song5 = new Song("Chattahoochee", "Alan Jackson", 3.0f, 3);
ipod.addSong(song5);
// Song6 will not be added due to the freeSpace being used up.
Song song6 = new Song("You Belong with Me", "Taylor Swift", 4.2f, 2);
ipod.addSong(song6);
// This prints the contents of the MP3 player.
System.out.println(ipod);
/* This do loop plays song2 from the beginning of the song until its
* completion. and then prints that it has finished playing.
*/
do {
song2.Play();
} while (song2.getIsPlaying());
// Song2 will be removed for the Arraylist of the MP3 Player.
ipod.removeSong(song2);
/* This prints the contents of the MP3 player after removing song2.
* It shows how the freeSpace has been increased.
*/
System.out.println(ipod);
/* This do loop tries to play song3 at position 5, but returns that it
* is past the duration of the song.
*/
do {
song3.playSong(5);
} while (song3.getIsPlaying());
/* This do loop plays song1 at position 2, and moves the start
* position to 2 and plays the song til the end.
*/
do {
song1.playSong(2);
} while (song1.getIsPlaying());
}
}
package cse1301_program3;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Song {
// The following instantiate the attributes given to the Song() class.
private String title;
private String artist;
private float duration;
private float currentPlayPosition;
private int startPosition;
private boolean isPlaying;
private int fileSize;
/* These are the default, default(with parameters), and toString()
* constructors of the Song() class.
*/
public Song()
{
title = "NO TITLE";
artist = "NO ARTIST";
duration = 0.0f;
currentPlayPosition = 0.0f;
startPosition = 0;
isPlaying = false;
fileSize = 0;
}
public Song(String t, String a, float d, int f)
{
title = t;
artist = a;
duration = d;
currentPlayPosition = 0.0f;
isPlaying = false;
fileSize = f;
}
public String toString()
{
return "'" + title + "' by " + artist + " lasts for " + duration +
" and is " + fileSize + "mb large";
}
// These are the accessor methods of the Song() class.
public String getTitle()
{
return title;
}
public String getArtist()
{
return artist;
}
public float getDuration()
{
return duration;
}
public boolean getIsPlaying()
{
return isPlaying;
}
public int getFileSize()
{
return fileSize;
}
// These are the mutator methods of the Song() class.
public void setTitle(String newTitle)
{
if (newTitle.compareTo("") != 0)
{
title = newTitle;
}
}
public void setArtist(String newArtist)
{
if (newArtist.compareTo("") != 0)
{
artist = newArtist;
}
}
public void setDuration(float newDuration)
{
if (newDuration >= 0)
{
duration = newDuration;
}
}
// This is the standard Play() method of the Song class.
public void Play()
{
isPlaying = true;
currentPlayPosition += 0.1f;
if (currentPlayPosition < duration)
{
System.out.println(title + " is playing at position " +
currentPlayPosition);
}
else
{
isPlaying = false;
System.out.println(title + " has finished playing.");
currentPlayPosition = 0.0f;
}
}
/* This is the added playSong() method to start a song at a certain position
* in the song. If the start position is past the duration it will give an
* error message.
*/
public void playSong(int i)
{
isPlaying = true;
startPosition = i;
currentPlayPosition += 0.1f;
if (currentPlayPosition <= startPosition)
{
currentPlayPosition = startPosition;
}
else if (currentPlayPosition <= duration)
{
System.out.println(title + " is playing at position " +
currentPlayPosition);
}
else if (startPosition > duration)
{
isPlaying = false;
System.out.println("Cannot play song at this position, it is past "
+ "the duration.");
}
else
{
isPlaying = false;
System.out.println(title + " has finished playing.");
currentPlayPosition = 0.0f;
}
}
}
package cse1301_program3;
import java.util.ArrayList;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class MP3_Player {
// This creates the ArrayList that will hold the songs in the MP3 Player.
ArrayList<Song> songList = new ArrayList<Song>();
//This creates the two attributes associated with the MP3 player.
private int storageCapacity;
private int freeSpace;
// This is the default constructor of the MP3 player
MP3_Player()
{
storageCapacity = 0;
freeSpace = storageCapacity;
}
// This is the constructor with parameters for the Mp3 player.
MP3_Player(int t)
{
storageCapacity = t;
freeSpace = storageCapacity;
}
/* This returns a toString() for the Mp3 player with formatting to display
information about storageCapacity, freeSpace, and the list of songs in
the MP3 player.
*/
public String toString()
{
return "MP3 player with capacity " + storageCapacity + ", " + freeSpace
+ " left, contains:\n" + songList.toString().replace("[","").replace("]", "").replace(",", "\n");
}
// These are three accessor methods for the MP3 player.
public int getStorageCapacity()
{
return storageCapacity;
}
public int getFreeSpace()
{
return freeSpace;
}
public ArrayList<Song> getSongList()
{
return songList;
}
/* This is the method for adding a song to the MP3 player, and it checks if
* there is enough space before adding the song and if there is it is added.
* If there is not enough space it returns the error.
*/
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.");
}
}
/* This is the method for removing a song from the MP3 player. It searches
* the ArrayList for the song in question and if a match is found it removes
* it from the ArrayList. It will also adjust the amount of freeSpace that
* is added back to the MP3 player.
*/
public void removeSong(Song s)
{
boolean songWillBeRemoved = false;
for (Song i : songList)
{
if (s.equals(i))
{
freeSpace += s.getFileSize();
songWillBeRemoved = true;
System.out.println(s.getTitle() + " removed from the MP3 Player.");
}
else if (s.equals(i))
{
System.out.println("This song is not stored in the MP3 Player.");
}
}
if (songWillBeRemoved)
{
songList.remove(s);
}
}
}