Advertisement
pkopy

babcia

Apr 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package projectgrandmother;
  2.  
  3. public class Product {
  4.     private String name;
  5.     private int amount;
  6.     private float price;
  7.     private float value;
  8.  
  9.     public Product(String name, int amount, float price) {
  10.         this.name = name;
  11.         this.amount = amount;
  12.         this.price = price;
  13.  
  14.     }
  15.  
  16.     public Product() {
  17.  
  18.     }
  19.  
  20.     public float getValue() {
  21.         return amount * price;
  22.     }
  23.  
  24.     public void setValue(float value){
  25.         this.value = value;
  26.     }
  27.  
  28.     public String getName() {
  29.         return name;
  30.     }
  31.  
  32.     public void setName(String name) {
  33.         this.name = name;
  34.     }
  35.  
  36.     public int getAmount() {
  37.         return amount;
  38.     }
  39.  
  40.     public void setAmount(int amount) {
  41.         this.amount = amount;
  42.     }
  43.  
  44.     public float getPrice() {
  45.         return price;
  46.     }
  47.  
  48.     public void setPrice(float price) {
  49.         this.price = price;
  50.     }
  51.  
  52.     @Override
  53.     public String toString() {
  54.         return "Product{" +
  55.                 "name='" + name + '\'' +
  56.                 ", amount=" + amount +
  57.                 ", price=" + price +
  58.                 ", value=" + value +
  59.                 '}';
  60.     }
  61. }
  62. /////////////////////////////
  63.  
  64. import java.util.ArrayList;
  65. import java.util.List;
  66.  
  67.  
  68. public class ListGrandpa {
  69.     private String listString;
  70.     private List<Product> products;
  71.  
  72.     public ListGrandpa(String listString) {
  73.         this.listString = listString;
  74.         products = new ArrayList<>();
  75.         splitList();
  76.     }
  77.  
  78.     public void splitList() {
  79.         String[] list = listString.split("/");
  80.         for(String s : list) {
  81.             String[] properties  = s.split(":");
  82.             Product product = new Product();
  83.             product.setName(properties[0]);
  84.             product.setAmount(Integer.valueOf(properties[1]));
  85.             product.setPrice(Float.valueOf(properties[2]));
  86.             float value = product.getPrice() * product.getAmount();
  87.             product.setValue(value);
  88.             if(isExist(product.getName())){
  89.                 System.out.println("Produkt: " + product.getName() + " już był kupiony");
  90.             }else{
  91.                 products.add(product);
  92.             }
  93.         }
  94.     }
  95.  
  96.     public Product maxAmount() {
  97.         int max = 0;
  98.         Product product = null;
  99.  
  100.         for(Product p : products) {
  101.             if(p.getAmount() > max) {
  102.                 max = p.getAmount();
  103.                 product = p;
  104.             }
  105.         }
  106.         return product;
  107.     }
  108.  
  109.  
  110.  
  111.     public boolean isExist(String name){
  112.         for(Product p : products){
  113.             if(p.getName().equals(name)) {
  114.                 return true;
  115.             }
  116.         }
  117.         return false;
  118.     }
  119.  
  120.     public float valueOfShopping(){
  121.         float value = 0;
  122.         for(Product p : products){
  123.             value += p.getValue();
  124.         }
  125.  
  126.         return value;
  127.     }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.     public List<Product> getProducts() {
  134.         return products;
  135.     }
  136.  
  137.     public void setProducts(List<Product> products) {
  138.         this.products = products;
  139.     }
  140.  
  141.     @Override
  142.     public String toString() {
  143.         return "ListGrandpa{" +
  144.                 "products=" + products +
  145.                 '}';
  146.     }
  147. }
  148.  
  149. ///////////////////////////
  150.  
  151. public class Main {
  152.     public static void main(String[] args) {
  153.  
  154.         try {
  155.         StringBuilder shoppingList = new StringBuilder();
  156.  
  157.             FileInputStream fileInputStream = new FileInputStream("C:\\Bootcamp\\babcia.txt");
  158.             int read;
  159.             while ((read = fileInputStream.read()) != -1) {
  160.                 shoppingList.append((char)  read);
  161.             }
  162.  
  163.  
  164.  
  165.         ListGrandpa listGrandpa = new ListGrandpa(shoppingList.toString());
  166.         System.out.println("Najwięcej kupionych " + listGrandpa.maxAmount().getName() + " wartość to: " + listGrandpa.maxAmount().getAmount());
  167.         System.out.println("Wartość zakupów" + listGrandpa.valueOfShopping());
  168.  
  169.         } catch (IOException e) {
  170.             e.printStackTrace();
  171.         }
  172.  
  173.  
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement