Tuesday 18 March 2008

Java Interface concept example2

===== TVBoard.java ===========
public interface TVBoard {

public void channelDown();
public void channelUp();
public void volumeDown();
public void volumeUp();
public void powerOnOff(boolean power);
}

========== SSgTV.java ===========

public class SSgTV implements TVBoard {

private String name = "SSg TV";
private int volume = 5;
private int channel = 7;
private boolean power = false;

public SSgTV() {
System.out.println("SSg TV is created.");
}

@Override
public void channelDown() {
this.channel -= 1;
System.out.println(this.name+"- channelDown");
}

@Override
public void channelUp() {
this.channel += 1;
System.out.println(this.name+" + channelIp");
}

@Override
public void volumeDown() {
this.volume -= 1;
System.out.println(this.name+" - volumnDown");
}

@Override
public void volumeUp() {
this.volume += 1;
System.out.println(this.name+" + volumnUp");
}

@Override
public void powerOnOff(boolean power) {
this.power = power;

if (this.power==false)
System.out.println(this.name+" power off");
else
System.out.println(this.name+" power on");
}

public void SleepTimer(int time) {
System.out.println(time+" please wait for power on or off");

this.powerOnOff(false);
}
}

============== LGsTV.java ==============

public class LGsTV implements TVBoard {

private String name = "LGg TV";
private int volume = 5;
private int channel = 7;
private boolean power = false;

public LGsTV() {
System.out.println("LGs TV is created.");
}

@Override
public void channelDown() {
this.channel -= 1;
System.out.println(this.name+"- channelDown");
}

@Override
public void channelUp() {
this.channel += 1;
System.out.println(this.name+" + channelIp");
}

@Override
public void volumeDown() {
this.volume -= 1;
System.out.println(this.name+" - volumnDown");
}

@Override
public void volumeUp() {
this.volume += 1;
System.out.println(this.name+" + volumnUp");
}

@Override
public void powerOnOff(boolean power) {
this.power = power;

if (this.power==false)
System.out.println(this.name+" power off");
else
System.out.println(this.name+" power on");
}

public void SleepTimer(int time) {
System.out.println(time+" please wait for power on or off");

this.powerOnOff(false);
}

}

=============== TVTestMain.java =============

public class TVTestMain {

/**
* @param args
*/
public static void main(String[] args) {
TVBoard tvBoard = new SSgTV();
tvBoard.powerOnOff(true);
tvBoard.channelUp();
tvBoard.channelDown();
tvBoard.volumeUp();
tvBoard.volumeUp();
tvBoard.volumeUp();
tvBoard.volumeUp();
tvBoard.powerOnOff(false);
System.out.println();

TVBoard gTvBoard = new LGsTV();
gTvBoard.powerOnOff(true);
gTvBoard.channelUp();
gTvBoard.channelDown();
gTvBoard.volumeUp();
gTvBoard.volumeUp();
gTvBoard.volumeUp();
gTvBoard.volumeUp();
gTvBoard.powerOnOff(false);
}

}