Project for Continous Integration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.5 KiB

  1. package device.cdPlayer;
  2. import device.Device;
  3. import device.cdPlayer.exceptions.*;
  4. import java.lang.reflect.Method;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class CDPlayer implements Device {
  9. // 0-Leer; true-1
  10. private int CdDriveContent=0;
  11. private int CdFlap=0;
  12. private int volume = 0;
  13. private String infoText=null;
  14. private List<String> supportedFormats = new ArrayList<String>(Arrays.asList(new String[] {"Audio","MP3","WMA","AAC"}));
  15. private String actualPlayTrack="";
  16. private CD loadedCD=null;
  17. @Override
  18. public void louder() {
  19. if(volume < 100) {
  20. volume += 1;
  21. } else {
  22. volume = 100;
  23. }
  24. }
  25. @Override
  26. public void quieter() {
  27. if(volume > 0) {
  28. volume -= 1;
  29. } else {
  30. volume = 0;
  31. }
  32. }
  33. @Override
  34. public int getVolume() {
  35. return volume;
  36. }
  37. @Override
  38. public void next() {
  39. List<String> playList = Arrays.asList(loadedCD.getPlayList());
  40. int indexOfActualPlayTrack=playList.indexOf(actualPlayTrack);
  41. int indexOfNextPlayTrack=(indexOfActualPlayTrack+1)%loadedCD.getPlayList().length;
  42. //System.out.println("actualIndex= "+indexOfActualPlayTrack);
  43. this.actualPlayTrack=playList.get(indexOfNextPlayTrack );
  44. //System.out.println("nextSong= "+actualPlayTrack);
  45. }
  46. @Override
  47. public void prev() {
  48. List<String> playList = Arrays.asList(loadedCD.getPlayList());
  49. int actualIndex=playList.indexOf(actualPlayTrack);
  50. if(actualIndex>0){
  51. actualIndex-=1;
  52. }else{
  53. actualIndex=0;
  54. }
  55. this.actualPlayTrack=playList.get(actualIndex);
  56. }
  57. @Override
  58. public String getInfoText() {
  59. if(infoText==null){
  60. throw new ReturnValueNullException();
  61. }
  62. return infoText;
  63. }
  64. @Override
  65. public String[] getOptions() {
  66. Method[] methods=this.getClass().getDeclaredMethods();
  67. String[] outMethods=new String[methods.length];
  68. for (int i = 0; i <methods.length ; i++) {
  69. outMethods[i]=methods[i].getName();
  70. }
  71. return outMethods;
  72. }
  73. @Override
  74. public String chooseItem(int itemNr) {
  75. if(this.loadedCD==null){
  76. throw new ReturnValueNullException();
  77. }
  78. if(!(itemNr>=0 && itemNr<loadedCD.getPlayList().length)){
  79. throw new ItemNumberNotFoundException();
  80. }
  81. return null;
  82. }
  83. @Override
  84. public String[] getItemList() {
  85. return loadedCD.getPlayList();
  86. }
  87. @Override
  88. public String mute() {
  89. return null;
  90. }
  91. @Override
  92. public String unmute() {
  93. return null;
  94. }
  95. @Override
  96. public String play() {
  97. if(isCdFlapOpen()){
  98. throw new CdFlapIsOpenException();
  99. }
  100. else if(!isCdFlapOpen()&&getCdDriveContent()==0){
  101. throw new NoCDException();
  102. }
  103. else if(!isCdFlapOpen()&&getCdDriveContent()==1&&!checkFormat(loadedCD.getFormat())){
  104. throw new FormatNotSupportedException();
  105. }
  106. else if(!isCdFlapOpen()&&getCdDriveContent()==1&&checkFormat(loadedCD.getFormat())){
  107. System.out.println(loadedCD.getFormat()+" "+this.actualPlayTrack+" is playing.");
  108. return "AllConditionsForPlayMet";
  109. }
  110. return null;
  111. }
  112. //Getters, Setters und SupportMethods
  113. public void setCD(CD _loadedCD) {
  114. loadedCD=_loadedCD;
  115. this.setCdDriveContent();
  116. this.actualPlayTrack=loadedCD.getPlayList()[0];
  117. }
  118. private void setCdDriveContent() {
  119. CdDriveContent = 1;
  120. }
  121. public void tapOnCdFlap(){
  122. this.CdFlap+=1;
  123. }
  124. private boolean checkFormat(String format){
  125. if(this.supportedFormats.contains(format))
  126. return true;
  127. else return false;
  128. }
  129. private int getCdDriveContent() {
  130. return CdDriveContent;
  131. }
  132. private boolean isCdFlapOpen() {
  133. if(CdFlap==1) {
  134. return true;
  135. }
  136. else
  137. return false;
  138. }
  139. public String getSongByNumber(int songNr){
  140. List<String> playList = Arrays.asList(loadedCD.getPlayList());
  141. return playList.get(songNr);
  142. }
  143. public String getActualPlayTrack() {
  144. return actualPlayTrack;
  145. }
  146. public void setInfoText() {
  147. this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running.";
  148. }
  149. public CD getCD() {
  150. return this.loadedCD;
  151. }
  152. }