Created numberOfSongsValidator and toString method for ipod.

This commit is contained in:
Trishaan committed 2025-09-29 02:08:14 -07:00
1 parent 92d3c13c15
commit 0385837c0d
1 file changed
+27 -4
+27 -4
View File
@@ -11,6 +11,7 @@ package ca.bcit.comp2522.lab03;
*/ */
public class IPod extends IDevice { public class IPod extends IDevice {
private static final int NO_SONGS = 0;
private static final int IPOD_HASH = 7; private static final int IPOD_HASH = 7;
private static final int IPOD_HASH_CODE = 97; private static final int IPOD_HASH_CODE = 97;
@@ -28,9 +29,9 @@ public class IPod extends IDevice {
) { ) {
super("music"); super("music");
// TODO: validate numberOfSongs input numberOfSongsValidator(numberOfSongs);
this.numberOfSongs = numberOfSongs; this.numberOfSongs = numberOfSongs;
this.maxVolumeDecibels = maxVolumeDecibels; this.maxVolumeDecibels = maxVolumeDecibels;
} }
@@ -68,8 +69,17 @@ public class IPod extends IDevice {
@Override @Override
public String toString() { public String toString() {
// TODO: change this to StringBuilder, refer to IPad final StringBuilder ipodDetails;
return super.toString() + " " + this.numberOfSongs + " " + this.maxVolumeDecibels;
ipodDetails = new StringBuilder();
ipodDetails.append(super.toString());
ipodDetails.append(" ");
ipodDetails.append(this.numberOfSongs);
ipodDetails.append(" ");
ipodDetails.append(this.maxVolumeDecibels);
return ipodDetails.toString();
} }
/** /**
@@ -113,4 +123,17 @@ public class IPod extends IDevice {
return hash; return hash;
} }
/**
* numberOfSongsValidator makes sure the number of songs being
* passed are not negative.
* @param songNumber passes the value of the number of songs.
* */
public void numberOfSongsValidator(int songNumber)
{
if(songNumber < NO_SONGS)
{
throw new IllegalArgumentException("Number of songs cannot be less than 0");
}
}
} }