Скачиваний:
62
Добавлен:
15.06.2014
Размер:
14.9 Кб
Скачать
package datacontainers;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import products.BaseProduct;
import sorting.SortingByArrivalDate;
import sorting.SortingByCountry;
import sorting.SortingByDepartment;
import sorting.SortingByMaker;
import sorting.SortingByMakerAddress;
import sorting.SortingByMakerMail;
import sorting.SortingByMakerPhone;
import sorting.SortingByMakerSite;
import sorting.SortingByName;
import sorting.SortingByNumber;
import sorting.SortingByPrice;
import sorting.SortingByShelfLife;
import sorting.SortingByType;
import sorting.SortingByWeight;

public class BaseProductContainer extends BaseContainer {
//queue with list of BaseProduct elements
private LinkedList<BaseProduct> baseproductcontainer = new LinkedList<BaseProduct>();

public void setBaseProductContainer(BaseProductContainer container) {
baseproductcontainer = container.getAllListOfProducts();
}

//get the number of products
public int getSize() {
return baseproductcontainer.size();
}

public BaseProductContainer() {
}

public BaseProductContainer(BaseProductContainer container) {
for(Iterator<BaseProduct> iterator = container.getAllListOfProducts().iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
baseproductcontainer.add(baseproduct);
}
}
//method for adding element into BaseProductContainer
public void addElement(BaseProduct product) {
baseproductcontainer.add(product);
}

//get element with deleting
public BaseProduct getElementWithPopping() {
//check there if no elements in linked list
//if LinkedList is empty
if(baseproductcontainer.isEmpty())
//return empty BaseProduct that was created by base constructor
return new BaseProduct();
return baseproductcontainer.pop();
}

//get all list of products
public LinkedList<BaseProduct> getAllListOfProducts() {
return baseproductcontainer;
}

//method for showing all elements into console
public void showInConsole() {
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
baseproduct.showInConsole();
System.out.println();
}
}

//method for deleting all elements from BaseProduct object
public void deleteAll() {
baseproductcontainer.removeAll(baseproductcontainer);
}

//method for sorting by name
public void sortingByName() {
SortingByName comp = new SortingByName();
Collections.sort(baseproductcontainer, comp);
}

//method for sorting by country
public void sortingByCountry() {
SortingByCountry comp = new SortingByCountry();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by type
public void sortingByType() {
SortingByType comp = new SortingByType();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by maker site
public void sortingByMakerSite() {
SortingByMakerSite comp = new SortingByMakerSite();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by number
public void sortingByNumber() {
SortingByNumber comp = new SortingByNumber();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by maker mail
public void sortingByMakerMail() {
SortingByMakerMail comp = new SortingByMakerMail();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by price
public void sortingByPrice() {
SortingByPrice comp = new SortingByPrice();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by weight
public void sortingByWeight() {
SortingByWeight comp = new SortingByWeight();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by shelf life
public void sortingByShelfLife() {
SortingByShelfLife comp = new SortingByShelfLife();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by maker phone
public void sortingByMakerPhone() {
SortingByMakerPhone comp = new SortingByMakerPhone();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by maker address
public void sortingByMakerAddress() {
SortingByMakerAddress comp = new SortingByMakerAddress();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by department
public void sortingByDepartment() {
SortingByDepartment comp = new SortingByDepartment();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by maker
public void sortingByMaker() {
SortingByMaker comp = new SortingByMaker();
Collections.sort(baseproductcontainer,comp);
}

//method for sorting by arrival data
public void sortingByArrivalDate() {
SortingByArrivalDate comp = new SortingByArrivalDate();
Collections.sort(baseproductcontainer,comp);
}

//method for searching by name and deleting element
public void searchAndDelete(String inp_name) {
int position = 0;
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
if(inp_name.equals(baseproduct.getBaseProductName())) {
baseproductcontainer.remove(position);
return;
}
position++;
}
}

public void searchandEditNumber(String name,String number) {
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
if(name.equals(baseproduct.getBaseProductName())) {
baseproduct.setBaseProductNumber(number);
break;
}
}
}

public boolean searchByName(String name) {
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
if(name.equals(baseproduct.getBaseProductName()))
return true;
}
return false;
}

public String searchAndGetNumber(String name) {
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
if(name.equals(baseproduct.getBaseProductName()))
return baseproduct.getBaseProductNumber();
}
return ""; //empty string return but it can't to be
}

//SEARCHING METHODS
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

//search by name and
public BaseProductContainer searchByProductName(String name) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(name.equals(product.getBaseProductName())) {
container.addElement(product);
}
}
return container;
}

//search by arrival date
public BaseProductContainer searchByProductArrivalDate(String arrivaldate) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(arrivaldate.equals(product.getBaseProductArrivalDate())) {
container.addElement(product);
}
}
return container;
}

//search by maker
public BaseProductContainer searchByProductMaker(String maker) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(maker.equals(product.getBaseProductMaker())) {
container.addElement(product);
}
}
return container;
}

//search by price
public BaseProductContainer searchByProductPrice(String price) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(price.equals(product.getBaseProductPrice())) {
container.addElement(product);
}
}
return container;
}

//search by type
public BaseProductContainer searchByProductType(String type) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(type.equals(product.getBaseProductType())) {
container.addElement(product);
}
}
return container;
}

//search by shelf life
public BaseProductContainer searchByProductShelfLife(String shelflife) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(shelflife.equals(product.getBaseProductShelfLife())) {
container.addElement(product);
}
}
return container;
}

//search by weight
public BaseProductContainer searchByProductWeight(String weight) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(weight.equals(product.getBaseProductWeight())) {
container.addElement(product);
}
}
return container;
}

//search by number
public BaseProductContainer searchByProductNumber(String number) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(number.equals(product.getBaseProductNumber())) {
container.addElement(product);
}
}
return container;
}

//search by country
public BaseProductContainer searchByProductCountry(String country) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(country.equals(product.getBaseProductCountry())) {
container.addElement(product);
}
}
return container;
}

//search by maker address
public BaseProductContainer searchByProductMakerAddress(String makeraddress) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(makeraddress.equals(product.getBaseProductMakerAddress())) {
container.addElement(product);
}
}
return container;
}

//search by maker site
public BaseProductContainer searchByProductMakerSite(String makersite) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(makersite.equals(product.getBaseProductMakerSite())) {
container.addElement(product);
}
}
return container;
}

//search by maker mail
public BaseProductContainer searchByProductMakerMail(String makermail) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(makermail.equals(product.getBaseProductMakerMail())) {
container.addElement(product);
}
}
return container;
}

//search by maker phone
public BaseProductContainer searchByProductMakerPhone(String makerphone) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(makerphone.equals(product.getBaseProductMakerPhone())) {
container.addElement(product);
}
}
return container;
}

//search by department
public BaseProductContainer searchByProductDepartment(String department) {
BaseProductContainer container = new BaseProductContainer();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(department.equals(product.getBaseProductDepartment())) {
container.addElement(product);
}
}
return container;
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

public void filterByDepartment(String filter_by) {
LinkedList<BaseProduct> tmp_container = new LinkedList<BaseProduct>();
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
if(filter_by.equals(product.getBaseProductDepartment())) {
tmp_container.add(product);
}
}
baseproductcontainer = tmp_container;
}

public String getOveralPrice() {
Double result = 0.0;
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
result += Double.parseDouble(product.getBaseProductPrice());
}
return result.toString();
}

public String getOveralNumberOfProducts() {
Integer result = 0;
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
result += Integer.parseInt(product.getBaseProductNumber());
}
return result.toString();
}

public String getOveralWeightOfProducts() {
Double result = 0.0;
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
result += Integer.parseInt(product.getBaseProductWeight());
}
return result.toString();
}

public String getAverageShelfLifeOfProducts() {
Double sum = 0.0;
Double result = 0.0;
int counter = 0;
for(Iterator<BaseProduct> iterator = baseproductcontainer.iterator();iterator.hasNext();) {
BaseProduct product = (BaseProduct)iterator.next();
sum += Double.parseDouble(product.getBaseProductShelfLife());
counter++;
}
if(counter == 0)
return "No data";
if(counter == 1)
return sum.toString();
result = sum / (double)counter;
return result.toString();
}
}
Соседние файлы в папке datacontainers