Você está na página 1de 4

package factory;

import java.util.*;
import java.io.*;

public class VendingUtils


{

public static Double[] montanteVendido = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0};
public static double saldo = 0;
public static double valorFalta = 0;
private static String[] nomeProdutos = {"Sandes de coirato", "Coca-cola",
"M&Ms", "Twix", "Bolachas", "Café frio", "Salame", "Bolachas de milho", "Belga"};
private static Double[] valorProdutos = {1.20, 1.00, 0.90, 0.90, 1.20, 1.30,
1.00, 0.80, 1.10};
public static Double[] quantidade = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0};
private static String[] tiposMoeda = {"1 Centimo", "2 Centimos", "5
Centimos", "10 Centimos", "20 Centimos", "50 Centimos", "1 Euro", "2 Euros", "5
Euros"};
private static Double[] valorMonetario = {0.01, 0.02, 0.05, 0.10, 0.20, 0.50,
1.00, 2.00, 5.00};

public static void setSaldo(double valor)


{
saldo = valor;
}

public static void initMachine()


{
setSaldo(0.00);
}

public static Double[] getMontante()


{
return montanteVendido;
}

public static double getSaldo()


{
return saldo;
}

public static String[] getNomeProdutos()


{
return nomeProdutos;
}

public static Double[] getQuantidade()


{
return quantidade;
}

public static Double[] getValorProdutos()


{
return valorProdutos;
}
public static String[] getTiposMoeda()
{
return tiposMoeda;
}

public static Double[] getValorMonetario()


{
return valorMonetario;
}

public static String insertCoin(String moedaEscolhida)


{
double saldo = getSaldo();
String retorno = "";

String[] tipoMoeda = getTiposMoeda();


Double[] valorMonetario = getValorMonetario();

for (int i = 0; i < tipoMoeda.length; i++)


{
if (Objects.equals(moedaEscolhida, tipoMoeda[i]))
{
saldo += valorMonetario[i];
}
}

setSaldo(saldo);
retorno = "Saldo: " + String.format("%.2f", saldo) + "€\n";

return retorno;
}

public static String chooseProduct(String produtoEscolhido)


{
double saldo = getSaldo();
double valorProduto = 0.00;
String troco = "";

String[] nomeProdutos = getNomeProdutos();


Double[] valorProdutos = getValorProdutos();

for (int i = 0; i < nomeProdutos.length; i++)


{
if (Objects.equals(produtoEscolhido, nomeProdutos[i]))
{
System.out.println("Valor adicionado!");
++quantidade[i];
}
}

for (int i = 0; i < nomeProdutos.length; i++)


{
if (Objects.equals(produtoEscolhido, nomeProdutos[i]))
{
valorProduto = valorProdutos[i];
montanteVendido[i] = valorProduto * quantidade[i];
}
}
if (saldo < valorProduto)
{
for (int i = 0; i < nomeProdutos.length; i++)
{
if (Objects.equals(produtoEscolhido, nomeProdutos[i]))
{
valorProduto = valorProdutos[i];
valorFalta = valorProduto - saldo;
--quantidade[i];
montanteVendido[i] = montanteVendido[i] -
valorProdutos[i];

}
}
return ("Saldo insuficiente, o custo do produto é " +
String.format("%.2f", valorProduto) + "€" + "\nPara adquirir o produto faltam-lhe "
+ String.format("%.2f", valorFalta) + "€" + "\nPor
favor insira mais dinheiro ou selecione outro produto.\n");
}

if (valorProduto > saldo)


troco = produtoEscolhido + ": " + String.format("%.2f",
valorProduto) + "€";
else
{
saldo -= valorProduto;
troco = String.format("%.2f", saldo) + "€";
troco = "A dispensar: " + produtoEscolhido + "\nTroco: " + troco
+ "\n";
setSaldo(0.00);
}

return troco;
}

public static void issueReport() throws IOException {

Double[] montanteVendido = getMontante();


String[] nomeProdutos = getNomeProdutos();
Double[] quantidade = getQuantidade();

try
{
FileOutputStream writer = new FileOutputStream("Relatorio.csv");
writer.write("Produto,Quantidade Vendida,Montantante Vendido\
n".getBytes());

System.out.println("Produtos" + " " + "Quantidade" + "


" + "Montante Vendida");
for (int i = 0; i < nomeProdutos.length; i++) {
String report = "";
report += nomeProdutos[i] + ',' +
String.valueOf(quantidade[i]) + ',' + String.valueOf(montanteVendido[i]) + "\n";
writer.write(report.getBytes());
System.out.println(nomeProdutos[i] + " " + '|' + " " +
String.valueOf(quantidade[i]) + " " + '|' + " " +
String.valueOf(montanteVendido[i]));
}
writer.close();

}
catch (IOException e)
{
e.printStackTrace();
}

Você também pode gostar