Created
September 23, 2022 08:56
-
-
Save ProfAndreaPollini/d97ceb9c21328bda98ab8e90a660fc57 to your computer and use it in GitHub Desktop.
20220923 - ESEMPIO AUTO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package veicoli; | |
public class Automobile { | |
private final Motore motore; | |
private final Fanale fanalePosteriore; | |
private final Fanale fanaleAnteriore; | |
private final Fanale frecciaSx; | |
private final Fanale frecciaDx; | |
private boolean quadroAcceso; | |
private boolean motoreAcceso; | |
public Automobile(Motore motore) { | |
this.motore = motore; | |
fanaleAnteriore = new Fanale(Fanale.BIANCO); | |
fanalePosteriore = new Fanale(Fanale.ROSSO); | |
frecciaDx =new Fanale(Fanale.GIALLO); | |
frecciaSx =new Fanale(Fanale.GIALLO); | |
quadroAcceso = false; | |
motoreAcceso = false; | |
} | |
public void accendiQuadro() { | |
quadroAcceso = true; | |
motoreAcceso = false; | |
} | |
public void accendimotore() { | |
accendiQuadro(); | |
motoreAcceso = true; | |
} | |
public int getPotenza() { | |
return motore.getPotenza(); | |
} | |
public void frecciaDestra() { | |
if(!quadroAcceso) return; // guard | |
frecciaSx.spegni(); | |
frecciaDx.accendi(); | |
} | |
public void frecciaSinistra() { | |
if(!quadroAcceso) return; // guard | |
frecciaDx.spegni(); | |
frecciaSx.accendi(); | |
} | |
public void frecceSpente() { | |
frecciaSx.spegni(); | |
frecciaDx.spegni(); | |
} | |
@Override | |
public String toString() { | |
return "Automobile{" + | |
"motore=" + motore + | |
", fanalePosteriore=" + fanalePosteriore + | |
", fanaleAnteriore=" + fanaleAnteriore + | |
", frecciaSx=" + frecciaSx + | |
", frecciaDx=" + frecciaDx + | |
", quadroAcceso=" + quadroAcceso + | |
", motoreAcceso=" + motoreAcceso + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package veicoli; | |
public class Fanale { | |
private static final boolean SPENTO = false; | |
private static final boolean ACCESO = true; | |
public static final String ROSSO = "ROSSO"; | |
public static final String GIALLO = "GIALLO"; | |
public static final String BIANCO = "BIANCO"; | |
private final String colore; | |
private boolean stato = SPENTO; | |
public boolean isAcceso() { | |
return stato; | |
} | |
public void accendi() { | |
setStato(ACCESO); | |
} | |
public void spegni() { | |
setStato(SPENTO); | |
} | |
private void setStato(boolean stato) { | |
this.stato = stato; | |
} | |
public Fanale(String colore) { | |
this.colore = colore; | |
} | |
@Override | |
public String toString() { | |
String isAcceso; | |
if (stato) { | |
isAcceso = "ACCESO"; | |
} else { | |
isAcceso = "SPENTO"; | |
} | |
return "Fanale{" + | |
"colore='" + colore + '\'' + | |
", " + isAcceso + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package veicoli; | |
public class Motore { | |
private final int potenza; | |
private final int cilindrata; | |
private final int numCilindri; | |
private final String tipoCarburante; | |
public Motore(int potenza, int cilindrata, int numCilindri, String tipoCarburante) { | |
this.potenza = potenza; | |
this.cilindrata = cilindrata; | |
this.numCilindri = numCilindri; | |
this.tipoCarburante = tipoCarburante; | |
} | |
public int getPotenza() { | |
return potenza; | |
} | |
public int getCilindrata() { | |
return cilindrata; | |
} | |
public int getNumCilindri() { | |
return numCilindri; | |
} | |
public String getTipoCarburante() { | |
return tipoCarburante; | |
} | |
@Override | |
public String toString() { | |
return "Motore{" + | |
"potenza=" + potenza + | |
", cilindrata=" + cilindrata + | |
", numCilindri=" + numCilindri + | |
", tipoCarburante='" + tipoCarburante + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package veicoli; | |
public class Test { | |
public static void main(String[] args) { | |
var m = new Motore(100,1600,4,"BENZINA"); | |
var panda = new Automobile(m); | |
var namachina = new Automobile(new Motore(200,2000,6,"DIESEL")); | |
System.out.println(String.format("potenza = %d", | |
namachina.getPotenza()) ); | |
namachina.accendiQuadro(); | |
namachina.frecciaDestra(); | |
System.out.println(namachina); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment