Advertisement
OverSkillers

Untitled

Apr 3rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package tp2_tarefaa;
  2.  
  3. import java.io.IOException;
  4.  
  5. class Product {
  6.  
  7.     private String name;
  8.     double price, profitTax;
  9.     int quantity;
  10.  
  11.     public Product(String name, double price, double profitTax) {
  12.         this.name = name;
  13.         this.price = price;
  14.         this.profitTax = profitTax;
  15.         quantity = 0;
  16.     }
  17.  
  18.     public void increaseQuantity(int quantity) {
  19.         this.quantity += quantity;
  20.     }
  21.  
  22.     public double getProfit() {
  23.         return (quantity * (price * profitTax));
  24.     }
  25.  
  26.     public double getIncome() {
  27.         return (quantity * price);
  28.     }
  29.  
  30.     public String getName() {
  31.         return name;
  32.     }
  33. }
  34.  
  35.  
  36. public class TP2_TarefaA {
  37.  
  38.     public static void main(String[] args) {
  39.         new TP2_TarefaA();
  40.     }
  41.  
  42.     public TP2_TarefaA() {
  43.         String args[];
  44.         //Adiciona à base de dados
  45.         int cmds = Integer.parseInt(readLn(200));
  46.         Product products[] = new Product[cmds];
  47.         for (int i = 0; i < cmds; i++) {
  48.             args = readLn(200).split(" ");
  49.             products[i] = new Product(args[0], Double.parseDouble(args[1]), Double.parseDouble(args[2]));
  50.         }
  51.         //Produtos vendidos
  52.         cmds = Integer.parseInt(readLn(200));
  53.         for (int i = 0; i < cmds; i++) {
  54.             args = readLn(200).split(" ");
  55.             for (int j = 0; j < products.length; j++) {
  56.                 if (products[j].getName().equals(args[0])) {
  57.                     products[j].increaseQuantity(Integer.parseInt(args[1]));
  58.                 }
  59.             }
  60.         }
  61.        
  62.         bubbleSort(products, 0);
  63.         System.out.println("Lucro");
  64.         for(int i=0; i<products.length; i++) {
  65.             System.out.println(products[i].getName());
  66.         }
  67.         bubbleSort(products, 1);
  68.         System.out.println("Receita");
  69.         for(int i=0; i<products.length; i++) {
  70.             System.out.println(products[i].getName());
  71.         }
  72.     }
  73.  
  74.     public void bubbleSort(Product products[], int sortType) {
  75.         Product temp;
  76.         switch (sortType) {
  77.             case 0: //lucro
  78.                 for (int i = 0; i < products.length; i++) {
  79.                     for (int j = 1; j < (products.length - i); j++) {
  80.                         if (products[j - 1].getProfit() < products[j].getProfit()) {
  81.                             temp = products[j - 1];
  82.                             products[j - 1] = products[j];
  83.                             products[j] = temp;
  84.                         }
  85.                     }
  86.                 }
  87.                 break;
  88.             case 1: //receita
  89.                 for (int i = 0; i < products.length; i++) {
  90.                     for (int j = 1; j < (products.length - i); j++) {
  91.                         if (products[j - 1].getIncome() < products[j].getIncome()) {
  92.                             temp = products[j - 1];
  93.                             products[j - 1] = products[j];
  94.                             products[j] = temp;
  95.                         }
  96.                     }
  97.                 }
  98.                 break;
  99.         }
  100.     }
  101.  
  102.     static String readLn(int maxLg) {
  103.         {
  104.             byte lin[] = new byte[maxLg];
  105.             int lg = 0, car = -1;
  106.             String line = "";
  107.             try {
  108.                 while (lg < maxLg) {
  109.                     car = System.in.read();
  110.                     if ((car < 0) || (car == '\n')) {
  111.                         break;
  112.                     }
  113.                     lin[lg++] += car;
  114.                 }
  115.             } catch (IOException e) {
  116.                 return (null);
  117.             }
  118.             if ((car < 0) && (lg == 0)) {
  119.                 return (null); // eof
  120.             }
  121.             return (new String(lin, 0, lg));
  122.         }
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement