Refactor Radio class constructor and volume control methods for improved readability and functionality
This commit is contained in:
parent
82bdd728c3
commit
257b063674
1 changed files with 13 additions and 5 deletions
|
@ -12,9 +12,9 @@ public class Radio {
|
|||
}
|
||||
|
||||
public Radio(boolean on, int volume, double frequency) {
|
||||
this.on = on;
|
||||
this.volume = Math.max(Math.min(volume, 10), 0);
|
||||
this.frequency = Math.max(Math.min(frequency, 110.0), 85.0);
|
||||
this.turnOn();
|
||||
this.setVolume(volume);
|
||||
this.setFrequency(frequency);
|
||||
}
|
||||
|
||||
public void turnOn() {
|
||||
|
@ -26,17 +26,25 @@ public class Radio {
|
|||
}
|
||||
|
||||
public void incVolume() {
|
||||
if (this.volume < 10) {
|
||||
if (this.volume < 10 && this.on) {
|
||||
this.volume++;
|
||||
}
|
||||
}
|
||||
|
||||
public void decVolume() {
|
||||
if (this.volume > 0) {
|
||||
if (this.volume > 0 && this.on) {
|
||||
this.volume--;
|
||||
}
|
||||
}
|
||||
|
||||
private void setVolume(int volume) {
|
||||
if (volume >= 0 && volume <= 10) {
|
||||
this.volume = volume;
|
||||
} else {
|
||||
this.volume = 5;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFrequency(double frequency) {
|
||||
if (frequency >= 85.0 && frequency <= 110.0) {
|
||||
this.frequency = frequency;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue