Created
February 23, 2023 08:49
-
-
Save ProfAndreaPollini/0b5f71fb1444306ac329979ec3c51a11 to your computer and use it in GitHub Desktop.
Esercizio Bar e panini
This file contains 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 com.ap.test.sol2; | |
public class Articolo { | |
} |
This file contains 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 com.ap.test.sol2; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Bar { | |
private final List<Articolo> articoli; | |
public Bar() { | |
articoli = new ArrayList<>(); | |
} | |
public static void main(String[] args) { | |
System.out.println("aggiungo un panino e aggiorno quanti panini ci sono"); | |
var bar1 = new Bar(); | |
bar1.addPanino(); | |
var panini = bar1.countPanini(); | |
System.out.println("ho un panino? " + (panini == 1)); | |
} | |
private int countPanini() { | |
int ret = 0; | |
for (var articolo:articoli) { | |
if (articolo instanceof Panino) { | |
ret +=1; | |
} | |
} | |
return ret; | |
} | |
private void addPanino() { | |
articoli.add(new Panino()); | |
} | |
} |
This file contains 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 com.ap.test.sol2; | |
public class Conchiglia extends Articolo{ | |
} |
This file contains 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 com.ap.test.sol2; | |
public class Panino extends Articolo{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment