Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tp2_tarefaa;
- import java.io.IOException;
- class Product {
- private String name;
- double price, profitTax;
- int quantity;
- public Product(String name, double price, double profitTax) {
- this.name = name;
- this.price = price;
- this.profitTax = profitTax;
- quantity = 0;
- }
- public void increaseQuantity(int quantity) {
- this.quantity += quantity;
- }
- public double getProfit() {
- return (quantity * (price * profitTax));
- }
- public double getIncome() {
- return (quantity * price);
- }
- public String getName() {
- return name;
- }
- }
- public class TP2_TarefaA {
- public static void main(String[] args) {
- new TP2_TarefaA();
- }
- public TP2_TarefaA() {
- String args[];
- //Adiciona à base de dados
- int cmds = Integer.parseInt(readLn(200));
- Product products[] = new Product[cmds];
- for (int i = 0; i < cmds; i++) {
- args = readLn(200).split(" ");
- products[i] = new Product(args[0], Double.parseDouble(args[1]), Double.parseDouble(args[2]));
- }
- //Produtos vendidos
- cmds = Integer.parseInt(readLn(200));
- for (int i = 0; i < cmds; i++) {
- args = readLn(200).split(" ");
- for (int j = 0; j < products.length; j++) {
- if (products[j].getName().equals(args[0])) {
- products[j].increaseQuantity(Integer.parseInt(args[1]));
- }
- }
- }
- bubbleSort(products, 0);
- System.out.println("Lucro");
- for(int i=0; i<products.length; i++) {
- System.out.println(products[i].getName());
- }
- bubbleSort(products, 1);
- System.out.println("Receita");
- for(int i=0; i<products.length; i++) {
- System.out.println(products[i].getName());
- }
- }
- public void bubbleSort(Product products[], int sortType) {
- Product temp;
- switch (sortType) {
- case 0: //lucro
- for (int i = 0; i < products.length; i++) {
- for (int j = 1; j < (products.length - i); j++) {
- if (products[j - 1].getProfit() < products[j].getProfit()) {
- temp = products[j - 1];
- products[j - 1] = products[j];
- products[j] = temp;
- }
- }
- }
- break;
- case 1: //receita
- for (int i = 0; i < products.length; i++) {
- for (int j = 1; j < (products.length - i); j++) {
- if (products[j - 1].getIncome() < products[j].getIncome()) {
- temp = products[j - 1];
- products[j - 1] = products[j];
- products[j] = temp;
- }
- }
- }
- break;
- }
- }
- static String readLn(int maxLg) {
- {
- byte lin[] = new byte[maxLg];
- int lg = 0, car = -1;
- String line = "";
- try {
- while (lg < maxLg) {
- car = System.in.read();
- if ((car < 0) || (car == '\n')) {
- break;
- }
- lin[lg++] += car;
- }
- } catch (IOException e) {
- return (null);
- }
- if ((car < 0) && (lg == 0)) {
- return (null); // eof
- }
- return (new String(lin, 0, lg));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement