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.

232 lines
6.2 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. private CDWechseler cdWechseler=null;
  18. private String activeSource="CDDrive";//or CDWechseler
  19. private List<String>favoriteSongs=new ArrayList<>();
  20. @Override
  21. public void louder() {
  22. if(volume < 100) {
  23. volume += 1;
  24. } else {
  25. volume = 100;
  26. }
  27. }
  28. @Override
  29. public void quieter() {
  30. if(volume > 0) {
  31. volume -= 1;
  32. } else {
  33. volume = 0;
  34. }
  35. }
  36. @Override
  37. public int getVolume() {
  38. return volume;
  39. }
  40. @Override
  41. public void next() {
  42. List<String> playList = Arrays.asList(loadedCD.getPlayList());
  43. int indexOfActualPlayTrack=playList.indexOf(actualPlayTrack);
  44. int indexOfNextPlayTrack=(indexOfActualPlayTrack+1)%loadedCD.getPlayList().length;
  45. this.actualPlayTrack=playList.get(indexOfNextPlayTrack );
  46. }
  47. @Override
  48. public void prev() {
  49. List<String> playList = Arrays.asList(loadedCD.getPlayList());
  50. int actualIndex=playList.indexOf(actualPlayTrack);
  51. if(actualIndex>0){
  52. actualIndex-=1;
  53. }else{
  54. actualIndex=0;
  55. }
  56. this.actualPlayTrack=playList.get(actualIndex);
  57. }
  58. @Override
  59. public String getInfoText() {
  60. if(infoText==null){
  61. throw new ReturnValueNullException();
  62. }
  63. return infoText;
  64. }
  65. @Override
  66. public String[] getOptions() {
  67. Method[] methods=this.getClass().getDeclaredMethods();
  68. String[] outMethods=new String[methods.length];
  69. for (int i = 0; i <methods.length ; i++) {
  70. outMethods[i]=methods[i].getName();
  71. }
  72. return outMethods;
  73. }
  74. @Override
  75. public String chooseItem(int itemNr) {
  76. if(this.loadedCD==null){
  77. throw new ReturnValueNullException();
  78. }
  79. if(itemNr<0){
  80. throw new NegativeInputException();
  81. }
  82. if(!(itemNr>=0 && itemNr<loadedCD.getPlayList().length)){
  83. throw new ItemNumberNotFoundException();
  84. }
  85. return loadedCD.getPlayList()[itemNr];
  86. }
  87. @Override
  88. public String[] getItemList() {
  89. return loadedCD.getPlayList();
  90. }
  91. @Override
  92. public String mute() {
  93. return null;
  94. }
  95. @Override
  96. public String unmute() {
  97. return null;
  98. }
  99. @Override
  100. public String play() {
  101. if(isCdFlapOpen()){
  102. throw new CdFlapIsOpenException();
  103. }
  104. else if(!isCdFlapOpen()&&getCdDriveContent()==0){
  105. throw new NoCDException();
  106. }
  107. else if(!isCdFlapOpen()&&getCdDriveContent()==1&&!checkFormat(loadedCD.getFormat())){
  108. throw new FormatNotSupportedException();
  109. }
  110. else if(!isCdFlapOpen()&&getCdDriveContent()==1&&checkFormat(loadedCD.getFormat())){
  111. System.out.println(loadedCD.getFormat()+" "+this.actualPlayTrack+" is playing.");
  112. return "AllConditionsForPlayMet";
  113. }
  114. return null;
  115. }
  116. public String changePlaySource(){
  117. if(activeSource.equals("CDDrive")) {
  118. this.activeSource = "CDWechseler";
  119. this.cdWechseler=new CDWechseler();
  120. this.cdWechseler.activate();
  121. }else this.activeSource="CDDrive";
  122. return activeSource;
  123. }
  124. public void loadCDWechseler(CD cd,int index){
  125. if(cdWechseler==null||!cdWechseler.isRunning()){
  126. throw new CDWechselerNotRunningException();
  127. }
  128. if(!(this.supportedFormats.contains(cd.getFormat()))){
  129. throw new FormatNotSupportedException();
  130. }
  131. if(index>10){
  132. throw new MaxCapacityExceededException();
  133. }
  134. cdWechseler.loadOneCD(cd,index);
  135. this.loadedCD=cdWechseler.getCDList().get(0);
  136. }
  137. public void changeToNextCD(){
  138. if(activeSource.equals("CDDrive")){
  139. throw new ChangeCDOnlyOnCDWechselerException();
  140. }
  141. List<CD> CDList = cdWechseler.getCDList();
  142. int indexOfLoadedCD=CDList.indexOf(loadedCD);
  143. int nextIndex=(indexOfLoadedCD+1)%CDList.size();
  144. this.loadedCD=CDList.get(nextIndex);
  145. }
  146. public void addToFavoriteSongs(String song){
  147. if(activeSource.equals("CDDrive")){
  148. throw new CDWechselerNotRunningException();
  149. }
  150. if(!(favoriteSongs.contains(song))) {
  151. favoriteSongs.add(song);
  152. }
  153. }
  154. //Getters, Setters und SupportMethods
  155. public void setCD(CD _loadedCD) {
  156. loadedCD=_loadedCD;
  157. this.setCdDriveContent();
  158. this.actualPlayTrack=loadedCD.getPlayList()[0];
  159. }
  160. private void setCdDriveContent() {
  161. CdDriveContent = 1;
  162. }
  163. public void tapOnCdFlap(){
  164. this.CdFlap+=1;
  165. }
  166. private boolean checkFormat(String format){
  167. if(this.supportedFormats.contains(format))
  168. return true;
  169. else return false;
  170. }
  171. private int getCdDriveContent() {
  172. return CdDriveContent;
  173. }
  174. private boolean isCdFlapOpen() {
  175. if(CdFlap==1) {
  176. return true;
  177. }
  178. else
  179. return false;
  180. }
  181. public String getSongByNumber(int songNr){
  182. List<String> playList = Arrays.asList(loadedCD.getPlayList());
  183. return playList.get(songNr);
  184. }
  185. public String getActualPlayTrack() {
  186. return actualPlayTrack;
  187. }
  188. public void setInfoText() {
  189. this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running.";
  190. }
  191. public CD getCD() {
  192. return this.loadedCD;
  193. }
  194. public CDWechseler getCdWechseler() {
  195. return cdWechseler;
  196. }
  197. public List<String> getFavoriteSongs() {
  198. return favoriteSongs;
  199. }
  200. }