Created
June 30, 2016 05:04
-
-
Save desrtfx/06c2d43f08a63030ac7afd74632e5df9 to your computer and use it in GitHub Desktop.
Gelataria
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 src.gelataria; | |
public class GeladoDia { | |
private String sabor; //Sabor do dia | |
private int preco; //Pre�o do gelado | |
// valor total recebido pelas unidades de gelado do dia j� vendidos | |
private int valor_recebido; | |
private int maximo; //n�mero m�ximo de unidades de gelados | |
private int vendidos; //n�mero de gelados vendidos | |
//M�todo construtor | |
public GeladoDia(String csabor, int cpreco, int cmaximo) { | |
sabor = csabor; | |
preco = cpreco; | |
maximo = cmaximo; | |
vendidos = 0; | |
printPanel(); | |
} | |
//Imprime painel informativo | |
public void printPanel(){ | |
System.out.println("##########"); | |
System.out.println(sabor + " : " + preco); | |
System.out.println("##########"); | |
} | |
//Imprime o n� m�ximo de gelados desse dia | |
public int getMaximo() { | |
System.out.println("N�mero m�ximo de unidades do dia: " + maximo); | |
return maximo; | |
} | |
//Valor em caixa | |
public int getValor_recebido(){ | |
System.out.println("Valor em caixa: " + valor_recebido); | |
return valor_recebido; | |
} | |
//N� gelados vendidos | |
public int getVendidos(){ | |
System.out.println("N� de gelados vendidos: " + vendidos); | |
return vendidos; | |
} | |
public void compraGeladoDia(int pagamento){ | |
if (pagamento < preco){ | |
return; | |
} | |
if (vendidos == maximo){ | |
System.out.println(sabor + " :esgotado"); | |
return; | |
} | |
valor_recebido += pagamento; | |
vendidos += 1; | |
} | |
public int fechoDia(){ | |
System.out.println("Total vendas dia: " + valor_recebido); | |
int recebido = valor_recebido; | |
valor_recebido = 0; | |
vendidos = 0; | |
return recebido; | |
} | |
public void mudaPreco(int preco) { | |
this.preco = preco; | |
} | |
} |
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 src.gelataria; | |
public class Gelataria { | |
private GeladoDia g1; | |
private GeladoDia g2; | |
private GeladoDia g3; | |
public Gelataria() { | |
g1 = new GeladoDia("Menta", 2, 100); | |
g2 = new GeladoDia("Caf�", 1, 60); | |
g3 = new GeladoDia("Lim�o", 3, 40); | |
} | |
// Getter methods for the private fields | |
public GeladoDia getG1() { | |
return g1; | |
} | |
public GeladoDia getG2() { | |
return g2; | |
} | |
public GeladoDia getG3() { | |
return g3; | |
} | |
public void geladoMaisBarato() { | |
g2.compraGeladoDia(1); | |
} | |
public int fechaGelataria() { | |
int mTotal = g1.fechoDia() + g2.fechoDia() + g3.fechoDia(); | |
System.out.println("Montante total:" + mTotal); | |
return mTotal; | |
} | |
public void atualizaPreco(int p1, int p2, int p3) { | |
g1.mudaPreco(p1); | |
g2.mudaPreco(p2); | |
g3.mudaPreco(p3); | |
} | |
} |
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 src.gelataria; | |
public class Main { | |
public static void main(String[] args) { | |
/* GeladoDia g1 = new GeladoDia("Menta", 10, 500); | |
g1.printPanel(); | |
g1.compraGeladodia(10); | |
g1.getVendidos(); | |
g1.getValor_recebido(); | |
g1.fechoDia(); | |
g1.getValor_recebido(); */ | |
Gelataria hagen = new Gelataria(); | |
hagen.geladoMaisBarato(); | |
// This is the new part | |
// get a reference to g2 from the hagen instance | |
GeladoDia g2 = hagen.getG2(); | |
g2.getVendidos(); | |
g2.getValor_recebido(); | |
/* instead of the above code (starting from my "this is the new part") comment | |
* you could also write the following: | |
* | |
* hagen.getG2().getVendidos(); | |
* hagen.getG2().getValor_recebido(); | |
* | |
* This way, you don't store a direct reference to g2 - but the code is less readable | |
* and changes will be more complicated | |
* | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment