remote87

Kochan

Jan 13th, 2021 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.Date;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.Stream;
  10.  
  11. public class Kochan implements Serializable {
  12.     //fields
  13.     private List<Invoice> invoiceList;
  14.     private long counter;
  15.     private User supplier;
  16.  
  17.     //Constructor
  18.     public Kochan(){
  19.  
  20.     }
  21. //    public Kochan(List<Invoice> newInvoiceList, int newCounter, User newSupplier){
  22. //        this.invoiceList = newInvoiceList;
  23. //        this.counter = newCounter;
  24. //        this.supplier = newSupplier;
  25. //    }
  26.  
  27.     //Getters, setters
  28.     public void addToInvoiceList(Invoice invoice){
  29.         this.invoiceList.add(invoice);
  30.         this.counter++; // ???? or standalone setter?
  31.     }
  32.  
  33.     public List<Invoice> getInvoiceList(){
  34.         return this.invoiceList;
  35.     }
  36.  
  37.     public long getCounter(){
  38.         return this.counter;
  39.     }
  40.  
  41.     public void setSupplier(User user){
  42.         this.supplier = user;
  43.     }
  44.  
  45.     public User getSupplier(){
  46.         return this.supplier;
  47.     }
  48.  
  49.     //Methods
  50.     //public getFromFile()
  51.     public boolean getFromFile(){
  52.         List<Invoice> result = new List<Invoice>();
  53.         try (Stream<Invoice> lines = Files.lines(Paths.get("File.txt"))) {
  54.             result.add(lines.collect(Collectors.toList()));
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.             return false;
  58.         }
  59.         return true;
  60.     }
  61.  
  62.     //public sendToFIle()
  63.  
  64.     //search by specific date
  65.     public List<Invoice> searchByDate(Date dateToSearch){
  66.         List<Invoice> invoicesSameDate = new List<Invoice>();
  67.         //Date of issue: simple date format
  68.         for(Invoice invoice : this.invoiceList){
  69.             if(invoice.dateOfIssue.compareTo(dateToSearch) == 0 ) invoicesSameDate.add(invoice);
  70.         }
  71.         return invoicesSameDate;
  72.     }
  73.  
  74.     //overload search by range of dates
  75.     public List<Invoice> searchByDate(Date startDate, Date endDate){
  76.         List<Invoice> invoicesInRange = new List<Invoice>();
  77.         for(Invoice invoice : this.invoiceList){
  78.             if(!(invoice.dateOfIssue.before(startDate) && invoice.dateOfIssue.after(endDate))) invoicesInRange.add(invoice);
  79.         }
  80.         return invoicesInRange;
  81.     }
  82.  
  83.     //search by items
  84.     public List<Invoice> searchByItem(Goods goodsToSearch){
  85. //        List<Invoice> searchedByItem = new List<Invoice>();
  86. //        for(Invoice invoice : this.invoiceList){
  87. //            for(Goods good : invoice.goods){
  88. //                if(good.nameOfGood.equals(goodsToSearch)) searchedByItem.add(invoice);
  89. //            }
  90. //        }
  91. //        return searchedByItem;
  92.  
  93.         List<Invoice> result = this.invoiceList.stream().filter(good -> good.nameOfGood.equals(goodsToSearch));
  94.         return result;
  95.     }
  96.  
  97.     //search by recipient?? search by name, idNumber, id?
  98.     public List<Invoice> searchByRecipientName(String nameToSearch){
  99.         List<Invoice> searchRecipientName = new List<Invoice>();
  100.         for(Invoice invoice : this.invoiceList){
  101.             if(invoice.User.recipient.name.equals(nameToSearch)) searchRecipientName.add(invoice);
  102.         }
  103.         return searchRecipientName;
  104.     }
  105.  
  106.     //get next free number
  107.     public long getNextFreeNumber(){
  108.         return counter + 1;
  109.     }
  110. }
  111.  
Add Comment
Please, Sign In to add comment