11 Commits

  1. 46
      src/main/java/device/cdPlayer/CDPlayer.java
  2. 26
      src/main/java/device/cdPlayer/CDWechseler.java
  3. 8
      src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java
  4. 7
      src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java
  5. 7
      src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java
  6. 7
      src/main/java/device/cdPlayer/exceptions/NegativeInputException.java
  7. 104
      src/test/java/device/cdPlayer/CDPlayerTest.java

46
src/main/java/device/cdPlayer/CDPlayer.java

@ -18,7 +18,10 @@ public class CDPlayer implements Device {
private List<String> supportedFormats = new ArrayList<String>(Arrays.asList(new String[] {"Audio","MP3","WMA","AAC"})); private List<String> supportedFormats = new ArrayList<String>(Arrays.asList(new String[] {"Audio","MP3","WMA","AAC"}));
private String actualPlayTrack=""; private String actualPlayTrack="";
private CD loadedCD;
private CD loadedCD=null;
private CDWechseler cdWechseler=null;
private String activeSource="CDDrive";//or CDWechseler
@Override @Override
public void louder() { public void louder() {
@ -48,9 +51,7 @@ public class CDPlayer implements Device {
List<String> playList = Arrays.asList(loadedCD.getPlayList()); List<String> playList = Arrays.asList(loadedCD.getPlayList());
int indexOfActualPlayTrack=playList.indexOf(actualPlayTrack); int indexOfActualPlayTrack=playList.indexOf(actualPlayTrack);
int indexOfNextPlayTrack=(indexOfActualPlayTrack+1)%loadedCD.getPlayList().length; int indexOfNextPlayTrack=(indexOfActualPlayTrack+1)%loadedCD.getPlayList().length;
//System.out.println("actualIndex= "+indexOfActualPlayTrack);
this.actualPlayTrack=playList.get(indexOfNextPlayTrack ); this.actualPlayTrack=playList.get(indexOfNextPlayTrack );
//System.out.println("nextSong= "+actualPlayTrack);
} }
@Override @Override
@ -86,7 +87,17 @@ public class CDPlayer implements Device {
@Override @Override
public String chooseItem(int itemNr) { public String chooseItem(int itemNr) {
return null;
if(this.loadedCD==null){
throw new ReturnValueNullException();
}
if(itemNr<0){
throw new NegativeInputException();
}
if(!(itemNr>=0 && itemNr<loadedCD.getPlayList().length)){
throw new ItemNumberNotFoundException();
}
return loadedCD.getPlayList()[itemNr];
} }
@Override @Override
@ -121,6 +132,28 @@ public class CDPlayer implements Device {
} }
return null; return null;
} }
public String changePlaySource(){
if(activeSource.equals("CDDrive")) {
this.activeSource = "CDWechseler";
this.cdWechseler=new CDWechseler();
this.cdWechseler.activate();
}else this.activeSource="CDDrive";
return activeSource;
}
public void loadCDWechseler(CD cd,int index){
if(cdWechseler==null||!cdWechseler.isRunning()){
throw new CDWechselerNotRunningException();
}
if(!(this.supportedFormats.contains(cd.getFormat()))){
throw new FormatNotSupportedException();
}
if(index>10){
throw new MaxCapacityExceededException();
}
cdWechseler.loadOneCD(cd,index);
System.out.println(index);
}
//Getters, Setters und SupportMethods //Getters, Setters und SupportMethods
public void setCD(CD _loadedCD) { public void setCD(CD _loadedCD) {
@ -165,8 +198,11 @@ public class CDPlayer implements Device {
this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running.";
} }
public CD getCD() { public CD getCD() {
return this.loadedCD; return this.loadedCD;
} }
public CDWechseler getCdWechseler() {
return cdWechseler;
}
} }

26
src/main/java/device/cdPlayer/CDWechseler.java

@ -0,0 +1,26 @@
package device.cdPlayer;
import java.util.ArrayList;
import java.util.List;
public class CDWechseler {
private CD[]CDList=new CD[10];
private boolean running=false;
public boolean isRunning() {
return running;
}
public void activate() {
this.running = true;
}
public CD[] getCDList() {
return CDList;
}
public void loadOneCD(CD cd,int index){
CDList[index]=cd;
}
}

8
src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java

@ -0,0 +1,8 @@
package device.cdPlayer.exceptions;
public class CDWechselerNotRunningException extends RuntimeException {
public CDWechselerNotRunningException() {
super("CDWechseler must be active for using.");
}
}

7
src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java

@ -0,0 +1,7 @@
package device.cdPlayer.exceptions;
public class ItemNumberNotFoundException extends RuntimeException {
public ItemNumberNotFoundException() {
super("Given ItemNumber ist not in the Album.");
}
}

7
src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java

@ -0,0 +1,7 @@
package device.cdPlayer.exceptions;
public class MaxCapacityExceededException extends RuntimeException{
public MaxCapacityExceededException(){
super("Max capacity of 10-CDs is already exceeded.");
}
}

7
src/main/java/device/cdPlayer/exceptions/NegativeInputException.java

@ -0,0 +1,7 @@
package device.cdPlayer.exceptions;
public class NegativeInputException extends RuntimeException{
public NegativeInputException() {
super("InputNumber must be positive.");
}
}

104
src/test/java/device/cdPlayer/CDPlayerTest.java

@ -327,4 +327,106 @@ class CDPlayerTest {
); );
} }
}
@ParameterizedTest
@MethodSource("chooseItemTestData")
void chooseItemTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception ) {
if(cases.equals("Case1")) {
Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(1));
assertEquals(ReturnValueNullException.class, newException.getClass());
}
if(cases.equals("Case2")) {
Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(200));
assertEquals(ItemNumberNotFoundException.class, newException.getClass());
}
if(cases.equals("Case3")) {
Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(-1));
assertEquals(NegativeInputException.class, newException.getClass());
}
if(cases.equals("Case4")) {
assertThat(_cdPlayer.chooseItem(1).contains("2")).describedAs(testName).isEqualTo(true);
}
}
static Stream<Arguments> chooseItemTestData () {
CDPlayer cdPlayer1 = new CDPlayer();
String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"};
CD audioCD1=new CD("Audio",audioPlayList);
CDPlayer cdPlayer2 = new CDPlayer();
cdPlayer2.tapOnCdFlap();
cdPlayer2.setCD(audioCD1);
cdPlayer2.tapOnCdFlap();
return Stream.of(
Arguments.of("[chooseItem() by notSetCD ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException()),
Arguments.of("[chooseItem() by invalidInput ] => shouldThrowItemNumberNotFoundException","Case2", cdPlayer2,new ItemNumberNotFoundException()),
Arguments.of("[chooseItem() by invalidInput:negativeNumber] => shouldThrowItemNumberNotFoundException","Case3", cdPlayer2,new NegativeInputException()),
Arguments.of("[chooseItem() by validInput] => shouldReturnMsgWithinTheItemNr","Case4", cdPlayer2,null)
);
}
@ParameterizedTest
@MethodSource("changePlaySourceData")
void changePlaySourceTest(String testName,String cases, CDPlayer _cdPlayer) {
if(cases.equals("Case1")) {
String returnVal = _cdPlayer.changePlaySource();
assertThat(returnVal).describedAs(testName).isEqualTo("CDWechseler");
}
if(cases.equals("Case2")) {
String returnVal = _cdPlayer.changePlaySource();
assertThat(returnVal).describedAs(testName).isEqualTo("CDDrive");
}
}
static Stream<Arguments> changePlaySourceData () {
CDPlayer cdPlayer1 = new CDPlayer();
CDPlayer cdPlayer2 = new CDPlayer();
cdPlayer2.changePlaySource();
return Stream.of(
Arguments.of("[changePlaySource() by activeSource=CDDrive ] => shouldReturnCDWechseler","Case1", cdPlayer1),
Arguments.of("[changePlaySource() by activeSource=CDWechseler ] => shouldReturnCDDrive","Case2", cdPlayer2)
);
}
@ParameterizedTest
@MethodSource("loadCDWechselerData")
void loadCDWechselerTest(String testName,String cases, CDPlayer _cdPlayer,CD cd,Exception exception) {
if(cases.equals("Case1")) {
Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd,0));
assertEquals(CDWechselerNotRunningException.class, newException.getClass());
}
if(cases.equals("Case2")) {
_cdPlayer.changePlaySource();
Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd,0));
assertEquals(FormatNotSupportedException.class, newException.getClass());
}
if(cases.equals("Case3")) {
_cdPlayer.changePlaySource();
_cdPlayer.loadCDWechseler(cd,0);
CD firstCD=_cdPlayer.getCdWechseler().getCDList()[0];
assertThat(firstCD).describedAs(testName).isNotEqualTo(null);
}
if(cases.equals("Case4")) {
_cdPlayer.changePlaySource();
Exception newException=assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd, 12));
assertEquals(MaxCapacityExceededException.class, newException.getClass());
}
}
static Stream<Arguments> loadCDWechselerData () {
CDPlayer cdPlayer1 = new CDPlayer();
String[] videoPlayList=new String[]{"Video 01","Video 02","Video 03","Video 04","Video 05"};
String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"};
CD audioCD1=new CD("Audio",audioPlayList);
CD DVD=new CD("DVD",videoPlayList);
return Stream.of(
Arguments.of("[loadCDWechseler() by notRunning=CDWechseler ] => shouldThrowAnException", "Case1", cdPlayer1,audioCD1,new CDWechselerNotRunningException()),
Arguments.of("[loadCDWechseler() by WrongCDFormat ] => shouldThrowAnException", "Case2", cdPlayer1,DVD,new FormatNotSupportedException()),
Arguments.of("[loadCDWechseler() by leadingFirstCD ] => CDListOfCDWechselerShouldHaveLength1", "Case3", cdPlayer1,audioCD1,null),
Arguments.of("[loadCDWechseler() by MaxCapacityExceeded:Max=10 ] => shouldThrowAnException", "Case4", cdPlayer1,audioCD1,new MaxCapacityExceededException())
);
}
}
Loading…
Cancel
Save