Você está na página 1de 5

package homebroker;

import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import java.util.Scanner;

/*
* UFABC BC&T - BC0505
* Algoritmo: Classe Holding
*
* Responsável: Walace Ribeiro Santana
*
* Data: 10/04/2011.
*/
class Holding {
private String code;
private Double unitValue;
private Integer quantity;
public Holding(String code, Integer quantity) {
this.code = code;
this.unitValue = getHoldingValue(code);
this.quantity = quantity;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Double getUnitValue() {
return unitValue;
}
public void setUnitValue(Double value) {
this.unitValue = value;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Double getTotalValue() {
return quantity * unitValue;
}
private Double getHoldingValue(String code) {
if(code.equals("petr4")){
return 28.01;
} else if (code.equals("petr3")){
return 31.47;
} else if (code.equals("vale5")){
return 47.43;
} else if (code.equals("vale3")){
return 53.00;
} else if (code.equals("bbas3")){
return 29.39;
} else if (code.equals("rsid3")){
return 14.94;
} else if (code.equals("cyre3")){
return 16.36;
} else if (code.equals("gfsa3")){
return 11.00;
} else if (code.equals("fibr3")){
return 25.68;
} else if (code.equals("mmxm3")){
return 11.05;
} else if (code.equals("usim3")){
return 28.45;
} else if (code.equals("bbdc4")){
return 32.45;
} else if (code.equals("itub4")){
return 31.83;
} else if (code.equals("goll4")){
return 20.87;
} else if (code.equals("tamm4")){
return 30.15;
} else {
return 0.0;
}
}
}
//==============================================================================
==
/*
* UFABC BC&T - BC0505
* Algoritmo: Classe principal Intro
*
* Responsável: Fernando Soler
*
* Data: 12/04/2011.
*/
class Investor {
private String name;
private String cpf;
private Double deposit;
private Double tax;
private Map<String, Holding> investorHoldingMap;
public Investor() {
tax = new Double(0.0);
investorHoldingMap = new HashMap<String, Holding>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public Double getDeposit() {
return deposit;
}
public void setDeposit(Double deposit) {
this.deposit = deposit;
}
public Holding getInvestorHolding(String code) {
return investorHoldingMap.get(code);
}
public void setInvestorHolding(String code, Holding investorHolding) {
this.investorHoldingMap.put(code, investorHolding);
}
public Map<String, Holding> getInvestorHoldingMap() {
return investorHoldingMap;
}
public Double getTax() {
return tax;
}
public void addTax(Double tax) {
this.tax += tax;
}
public Double getTotalValueHolding() {
Double total = new Double(0.0);
Collection<Holding> holdings = investorHoldingMap.values();
for (Holding holding : holdings)
total += holding.getTotalValue();
return total;
}
}
/*
* UFABC BC&T - BC0505
* Algoritmo: Classe principal Intro
*
* Responsável: Fernando Soler
*
* Data: 12/04/2011.
*/
public class Main {
private static Double tax;
private static String code;
private static String option;
private static Double salePrice;
private static Integer holdingQuantity;
private static String commands[];
public static void main(String[] args) {
Holding holding;
Scanner scanner = new Scanner(System.in);
Investor investor = new Investor();
System.out.print("Nome do investidor: ");
investor.setName(scanner.nextLine());
System.out.print("CPF: ");
investor.setCpf(scanner.nextLine());
System.out.print("Depósito inicial: ");
investor.setDeposit(Double.valueOf(scanner.nextLine()));
System.out.println("Selecionar opções:");
System.out.println("[a] Compra de ações");
System.out.println("[b] Venda de ações");
System.out.println("[s] Sair");
option = scanner.nextLine();
while (!option.equals("s")) {
if (option.equals("a")) {
System.out.println("Código das ações - Quantidade de ações
");
commands = scanner.nextLine().split(" - ");
code = commands[0];
holdingQuantity = Integer.valueOf(commands[1]);
holding = new Holding(code, holdingQuantity);
if (investor.getDeposit() < holding.getTotalValu
e()) {
System.out.println("Impossivel efetuar c
ompra: saldo insuficiente");
} else {
tax = 6.0;
investor.setInvestorHolding(code, holdin
g);
investor.setDeposit(investor.getDeposit(
) - holding.getTotalValue() - tax);
investor.addTax(tax);
}
} else {
System.out.println("Código das ações - Quantidade de ações
- Preço para venda");
commands = scanner.nextLine().split(" - ");
code = commands[0];
holdingQuantity = Integer.valueOf(commands[1]);
salePrice = Double.valueOf(commands[2]);
tax = 0.2 * (holdingQuantity * salePrice - holdi
ngQuantity * investor.getInvestorHolding(code).getUnitValue()) + 6;
holding = investor.getInvestorHolding(code);
holding.setQuantity(holding.getQuantity() - Inte
ger.valueOf(holdingQuantity));
investor.setDeposit(investor.getDeposit() + (hol
dingQuantity * salePrice) - tax);
investor.addTax(tax);
}
System.out.println();
System.out.println("Digite a próxima opção([a], [b] ou [s]):")
;
option = scanner.nextLine();
}
System.out.println("-------------------------------------------"
);
System.out.println("Nome do investidor: " + investor.getName());
System.out.println("Saldo em dinheiro: " + investor.getDeposit()
);
System.out.println("Saldo em ações: " + investor.getTotalValueHoldin
g());
Collection<Holding> holdings = investor.getInvestorHoldingMap().
values();
for (Holding h : holdings)
System.out.println("-> Código: " + h.getCode() + " - Quant
idade: " + h.getQuantity());
System.out.println("Imposto a pagar: " + investor.getTax());
}
}

Você também pode gostar