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

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

import products.BaseProduct;
import workingwithfiles.BaseProductFileReader;

/////////////////////////////////////////////////////////////
//ALL OPERATIONS WILL BE DONE BY DEPARTMENT CONTAINER CLASS
/////////////////////////////////////////////////////////////
public class BaseDepartmentContainer extends BaseContainer {
//all products department container
private BaseProductContainer allproducts_departmentcontainer = new BaseProductContainer();
//bought products department container
private BaseProductContainer boughtproducts_departmentcontainer = new BaseProductContainer();

LinkedList<String> department_Names_For_Unbought_Products = new LinkedList<String>();
LinkedList<String> department_Names_For_Bought_Products = new LinkedList<String>();
//objects that helps buy products

//base constructor
public BaseDepartmentContainer() {

}

public void showInConsoleDepartmentNames() {
for(Iterator<String> iterator = department_Names_For_Unbought_Products.iterator();iterator.hasNext();) {
String str = (String)iterator.next();
System.out.println(str);
}
}

public void updateDepartmentNamesForUnboughtProducts() {
for(Iterator<BaseProduct> iterator = allproducts_departmentcontainer.getAllListOfProducts().iterator();iterator.hasNext();) {
BaseProduct product = iterator.next();

boolean flag = false;
for(Iterator<String> iter = department_Names_For_Unbought_Products.iterator();iter.hasNext();) {
String str = (String)iter.next();
if(str.equals(product.getBaseProductDepartment())) {
flag = true;
break;
}
}
if(!flag) {
department_Names_For_Unbought_Products.add(product.getBaseProductDepartment());
}
}
}

public void updateDepartmentNamesForBoughtProducts() {
for(Iterator<BaseProduct> iterator = boughtproducts_departmentcontainer.getAllListOfProducts().iterator();iterator.hasNext();) {
BaseProduct product = iterator.next();

boolean flag = false;
for(Iterator<String> iter = department_Names_For_Bought_Products.iterator();iter.hasNext();) {
String str = (String)iter.next();
if(str.equals(product.getBaseProductDepartment())) {
flag = true;
break;
}
}
if(!flag) {
department_Names_For_Bought_Products.add(product.getBaseProductDepartment());
}
}
}

public LinkedList<String> getDepartmentNamesForBoughtProducts() {
return department_Names_For_Bought_Products;
}

public LinkedList<String> getDepartmentNamesForUnboughtProducts() {
return department_Names_For_Unbought_Products;
}

//method for updating all products element
public BaseProductContainer updateAllProductsDepartmentContainer() {
//if all products container contains any elements
if(allproducts_departmentcontainer.getAllListOfProducts().size() != 0) {
//clear this container
allproducts_departmentcontainer.deleteAll();
//create object for reading from file
BaseProductFileReader baseproductfilereader = new BaseProductFileReader();
baseproductfilereader.readFromBaseProductFile(allproducts_departmentcontainer, "baseproductdata.xml");
}
//if all product container don't contain any elements
else {
//create object for reading from file
BaseProductFileReader baseproductfilereader = new BaseProductFileReader();
baseproductfilereader.readFromBaseProductFile(allproducts_departmentcontainer, "baseproductdata.xml");
}
//return all products container for cepochnie komandi vizovi
return allproducts_departmentcontainer;
}

//method for updating all bought products element
public BaseProductContainer updateBoughtProductsDepartmentContainer() {
//if bought products container contains any elements
if(boughtproducts_departmentcontainer.getAllListOfProducts().size() != 0) {
//clear this container
boughtproducts_departmentcontainer.deleteAll();
//create object for reading from file
BaseProductFileReader baseproductfilereader = new BaseProductFileReader();
baseproductfilereader.readFromBaseProductFile(boughtproducts_departmentcontainer, "boughtproducts.xml");
}
//if bought product container don't contain any elements
else {
//create object for reading from file
BaseProductFileReader baseproductfilereader = new BaseProductFileReader();
baseproductfilereader.readFromBaseProductFile(boughtproducts_departmentcontainer, "boughtproducts.xml");
}
//return bought product container
return boughtproducts_departmentcontainer;
}

public void generalUpdateDepartmentContainer() {
//update all departments
updateAllProductsDepartmentContainer();
updateBoughtProductsDepartmentContainer();
//update departments names
updateDepartmentNamesForUnboughtProducts();
updateDepartmentNamesForBoughtProducts();
}

public BaseProductContainer getAllProductsContainer() {
return allproducts_departmentcontainer;
}

public BaseProductContainer getBoughtProductsContainer() {
return boughtproducts_departmentcontainer;
}
}
Соседние файлы в папке datacontainers