Issue
I am a beginner and working on turning on and off a light bulb. I have some working Java code. I have a constructor and Javascript code. The codes are working separately but how do I connect all of them together so they actually communicate together?
package com.example.restservice.Model;
import java.awt.Color;
public class Bulb {
private int bulbId;
private boolean on;
//private Color color;
private String colorName;
private String location;
public Bulb(int id, boolean on, Color c, String cn, String location) {
this.bulbId = id;
this.on = on;
//color = c;
colorName = cn;
this.location = location;
}
public int getId() {
return bulbId;
}
public void setId(int id) {
this.bulbId = id;
}
public boolean isOn() {
return on;
}
public boolean isOff() {
return !on;
}
public String getcolorName() {
return colorName;
}
public void setColorName(String cn) {
colorName = cn;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public void turnOn() {
on = true;
}
public void turnOff() {
on = false;
}
public String toString() {
String result;
if (on) {
result = "The " + getcolorName() + " light is on.";
}
else {
result = "The " + getcolorName() + " light is off.";
}
return result;
}
}
Javascript:
function change() {
var image = document.getElementById('switch');
if (imageTracker==='on') {
image.src ='img/light-on.jpg';
imageTracker = 'off';
}
else {
image.src ='img/light-off.jpg';
imageTracker = 'on';
}
}
Controller
@GetMapping("/bulbs")
public Bulb[] bulps() {
return bulbs;
}
@GetMapping("/Bulb")
public Bulb bulb(int bulbId) {
for (Bulb bulb : bulbs) {
if (bulb.getId() == bulbId) {
return bulb;
}
}
return null;
}
Solution
The solution was to use the function (fedge).
Answered By - Danielle