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.
123 lines
4.2 KiB
123 lines
4.2 KiB
import device.Device;
|
|
import device.cdPlayer.CD;
|
|
import device.cdPlayer.CDPlayer;
|
|
import device.cdPlayer.CD;
|
|
import device.cdPlayer.CDPlayer;
|
|
import device.radioPlayer.RadioPlayer;
|
|
import device.usbPlayer.USB_Stick;
|
|
import device.usbPlayer.UsbPlayer;
|
|
import exception.NoItemEvenAllPreconditionAreMet;
|
|
|
|
import java.io.FileReader;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.util.Arrays;
|
|
import java.util.Properties;
|
|
|
|
public class BordComputer {
|
|
|
|
String[] deviceNames;
|
|
Device[] installedDevices;
|
|
Device activeDevice = null;
|
|
|
|
public void readConfig() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
|
|
|
try (FileReader reader = new FileReader("Geraete.config")) {
|
|
Properties properties = new Properties();
|
|
properties.load(reader);
|
|
String[] values = properties.values().toArray(new String[0]);
|
|
Arrays.sort(values);
|
|
deviceNames = new String[values.length];
|
|
System.arraycopy(values, 0, deviceNames, 0, properties.size());
|
|
installedDevices = new Device[values.length];
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
setDevices();
|
|
activeDevice = installedDevices[0];
|
|
}
|
|
|
|
public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
|
Class<?> c;
|
|
for (int i = 0; i < deviceNames.length; i++) {
|
|
c = Class.forName(deviceNames[i]);
|
|
installedDevices[i] = (Device) c.getConstructor().newInstance();
|
|
}
|
|
}
|
|
|
|
public String changeDevice() {
|
|
Device[] tempDevices = new Device[installedDevices.length];
|
|
tempDevices[0] = installedDevices[1];
|
|
tempDevices[1] = installedDevices[2];
|
|
tempDevices[2] = installedDevices[0];
|
|
//replaces the lists
|
|
installedDevices = tempDevices;
|
|
//setting activeDevice
|
|
activeDevice = installedDevices[0];
|
|
|
|
String infoText="";
|
|
if(activeDevice instanceof CDPlayer){
|
|
CDPlayer cdPlayer=(CDPlayer) activeDevice;
|
|
String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"};
|
|
//some CDs
|
|
CD audioCD1=new CD("Audio",audioPlayList);
|
|
audioCD1.setAlbumName("Love Songs");
|
|
|
|
cdPlayer.tapOnCdFlap();
|
|
cdPlayer.setCD(audioCD1);
|
|
cdPlayer.tapOnCdFlap();
|
|
cdPlayer.setInfoText();
|
|
infoText = cdPlayer.getInfoText();
|
|
}else{
|
|
infoText=activeDevice.getInfoText()+" ist activate";
|
|
}
|
|
return infoText;
|
|
}
|
|
|
|
public String play(){
|
|
String itemToPlay=null;
|
|
try {
|
|
itemToPlay=activeDevice.play();
|
|
}catch (RuntimeException exception) {
|
|
throw new NoItemEvenAllPreconditionAreMet();
|
|
}
|
|
|
|
return itemToPlay;
|
|
}
|
|
|
|
//===== Hilf-Funktionen ======
|
|
|
|
public Device getInstanceByName(String deviceName){
|
|
for (int i = 0; i <installedDevices.length ; i++) {
|
|
if(installedDevices[i].getClass().getSimpleName().equals(deviceName)){
|
|
return installedDevices[i];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public void meetAllPreconditionForPlay(){
|
|
if(activeDevice instanceof CDPlayer) {
|
|
String[] audioPlayList = new String[]{"Audio 01", "Audio 02", "Audio 03", "Audio 04", "Audio 05"};
|
|
CD audioCD1 = new CD("Audio", "PopMusic", audioPlayList, "XAc20044");
|
|
CD audioCD2 = new CD("Audio", "RockMusic", audioPlayList, "BLXPP78");
|
|
|
|
CDPlayer cdPlayer = (CDPlayer) activeDevice;
|
|
cdPlayer.changePlaySource();
|
|
cdPlayer.loadCDWechseler(audioCD1, 0);
|
|
cdPlayer.loadCDWechseler(audioCD2, 1);
|
|
|
|
cdPlayer.setCD(cdPlayer.getCdWechseler().getCDList().get(0));
|
|
activeDevice = cdPlayer;
|
|
}
|
|
if(activeDevice instanceof UsbPlayer) {
|
|
USB_Stick USBDrive = new USB_Stick();
|
|
UsbPlayer up1 = new UsbPlayer(USBDrive);
|
|
activeDevice = up1;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public void setActiveDevice(Device activeDevice) {
|
|
this.activeDevice = activeDevice;
|
|
}
|
|
}
|