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

import java.util.Iterator;

import products.BaseProduct;
import workingwithfiles.BaseProductFileReader;
import workingwithfiles.BaseProductFileWriter;
import datacontainers.BaseExceptionContainer;
import datacontainers.BaseProductContainer;
import exceptions.BaseException;

public class Buyer {
private BaseProductContainer boughtbaseproductcontainer = new BaseProductContainer();
private BaseProductFileWriter baseproductfilewriter = new BaseProductFileWriter();
private BaseExceptionContainer baseexceptioncontainer = new BaseExceptionContainer();
private BaseProductFileReader baseproductfilereader = new BaseProductFileReader();

//constructor
public Buyer() {

}

//get boughtbaseproductcontainer
public BaseProductContainer getBoughtContainer() {
return boughtbaseproductcontainer;
}
public BaseProductContainer buyProduct(BaseProductContainer container, String nameofproduct, int buyingnumber) {
try {
if(container.getAllListOfProducts().isEmpty()) {
throw new BaseException("Can't to buy.Empty input ProductContainer.",1);
}

else {
for(Iterator<BaseProduct> iterator = container.getAllListOfProducts().iterator();iterator.hasNext();) {
BaseProduct baseproduct = (BaseProduct)iterator.next();
if(baseproduct.getBaseProductName().equals(nameofproduct)) {
//when products that you want to buy equal products that there are in magazine
if(buyingnumber == Integer.parseInt(baseproduct.getBaseProductNumber())) {
//read from our bought file
readFromBoughtProductsFile();
//check how many elements in our file
if(boughtbaseproductcontainer.getAllListOfProducts().size() == 0) {
//clear this container
boughtbaseproductcontainer.deleteAll();
//write in container object
boughtbaseproductcontainer.addElement(baseproduct);
}
//if container have elements
else {
//we find if there is element with this name in container
if(boughtbaseproductcontainer.searchByName(baseproduct.getBaseProductName())) {
int new1_num = buyingnumber + Integer.parseInt(boughtbaseproductcontainer.searchAndGetNumber(baseproduct.getBaseProductName()));
boughtbaseproductcontainer.searchandEditNumber(baseproduct.getBaseProductName(), Integer.toString(new1_num));
}
//if we can't find element with this name
else {
boughtbaseproductcontainer.addElement(baseproduct);
}
}
//delete element from source container that contains all products
container.searchAndDelete(nameofproduct);
break;
}
//if inp products under than products that there are in magazine
if(buyingnumber < Integer.parseInt(baseproduct.getBaseProductNumber())) {
//read from our bought file
readFromBoughtProductsFile();
//check how many elements in our file
if(boughtbaseproductcontainer.getAllListOfProducts().size() == 0) {
//clear this container
boughtbaseproductcontainer.deleteAll();
//write in container object
BaseProduct copy_baseproduct = new BaseProduct(baseproduct);
//set new number of product
copy_baseproduct.setBaseProductNumber(Integer.toString(buyingnumber));
//add element to our bought container
boughtbaseproductcontainer.addElement(copy_baseproduct);
}
//if container have elements
else {
//we find if there is element with this name in container
if(boughtbaseproductcontainer.searchByName(baseproduct.getBaseProductName())) {
int new2_num = buyingnumber + Integer.parseInt(boughtbaseproductcontainer.searchAndGetNumber(baseproduct.getBaseProductName()));
boughtbaseproductcontainer.searchandEditNumber(baseproduct.getBaseProductName(), Integer.toString(new2_num));
}
//if we can't find element with this name
else {
BaseProduct copy2_baseproduct = new BaseProduct(baseproduct);
copy2_baseproduct.setBaseProductNumber(Integer.toString(buyingnumber));
boughtbaseproductcontainer.addElement(copy2_baseproduct);
}
}
//update number in source container
int new3_number = Integer.parseInt(baseproduct.getBaseProductNumber()) - buyingnumber;
container.searchandEditNumber(nameofproduct, Integer.toString(new3_number));
break;
}
//if you want buy more products that there are in magazine
if(buyingnumber > Integer.parseInt(baseproduct.getBaseProductNumber())) {
throw new BaseException("You can't to buy.You want to buy the number of products that more than there are in magazine.",3);
}
}
}
}
}
catch(BaseException exc) {
baseexceptioncontainer.addElement(exc);
}
baseproductfilewriter.writeToFile(boughtbaseproductcontainer, "boughtproducts.xml");
return container;
}

//read from bought products file
public BaseProductContainer readFromBoughtProductsFile() {
boughtbaseproductcontainer.deleteAll();
baseproductfilereader.readFromBaseProductFile(boughtbaseproductcontainer, "boughtproducts.xml");
return boughtbaseproductcontainer;
}
}