Shop.java

jmud-0.11/src/Shop.java · 3.4 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.io.BufferedReader;
import java.io.IOException;

import util.list.Vector;
import util.file.InvalidFileFormatException;
import util.log.Log;
import util.list.Vector;
import util.bit.Bit;
import string.StrUtil;
import string.InvalidFlagException;
import misc.Separators;


class Shop implements Sheetable {

    private int    theId;
    private String theTitle = null;
    private float  buyTax;
    private float  sellTax;
    private int    toBuy;
    private int    keeper;
    private Vector products = new Vector();
    private int    goodMoney;

    Shop(int zoneBase, int shopId, BufferedReader shopFile) 
	throws InvalidFileFormatException {

	theId = shopId + zoneBase;

	StringTokenizer tok = null;
	try {
	    theTitle = shopFile.readLine();

	    Log.debug("Loja: " + theTitle);

	    tok     = new StringTokenizer(shopFile.readLine());
	    buyTax  = Float.valueOf(tok.nextToken()).floatValue();
	    sellTax = Float.valueOf(tok.nextToken()).floatValue();

	    toBuy = StrUtil.parseFlags(shopFile.readLine());
	    goodMoney = StrUtil.parseFlags(shopFile.readLine());

	    keeper = Integer.parseInt(shopFile.readLine()) + zoneBase;
	    
	    while (shopFile.ready()) {
		String line = shopFile.readLine();
		if (line.startsWith(Separators.EOR))
		    break;
		if (line.startsWith(Separators.EOF))
		    throw new InvalidFileFormatException();

		products.insert(new Integer(Integer.parseInt(line) + zoneBase));
	    }

	    products.trim();
	}
	catch(IOException e) {
	    throw new InvalidFileFormatException();
	}
	catch(NoSuchElementException e) {
	    throw new InvalidFileFormatException();
	}
	catch(NumberFormatException e) {
	    throw new InvalidFileFormatException();
	}
	catch(InvalidFlagException e) {
	    throw new InvalidFileFormatException();
	}
    }

    int getId() {
	return theId;
    }

    boolean hasId(int id) {
	return getId() == id;
    }

    int getKeeperId() {
	return keeper;
    }

    float getBuyTax() {
	return buyTax;
    }

    float getSellTax() {
	return sellTax;
    }
    
    boolean producesItem(int itemId) {
	for (Enumeration enum = products.elements(); enum.hasMoreElements(); ) {
	    Integer it = (Integer) enum.nextElement();
	    if (it.intValue() == itemId)
		return true;
	}
	return false;
    }

    boolean buyItem(Item it) {
	return Bit.isSet(toBuy, 1 << it.getType()); 
    }

    boolean acceptCurrency(int currKind) {
	return Bit.isSet(goodMoney, 1 << currKind);
    }

    /////////////
    // Sheetable:

    String getShortSheet() {
	return StrUtil.leftPad(String.valueOf(theId), 5) + " " + StrUtil.leftPad(String.valueOf(keeper), 8) + " " + StrUtil.leftPad(String.valueOf(buyTax), 6) + " " + StrUtil.leftPad(String.valueOf(sellTax), 5) + " " + StrUtil.rightPad(StrUtil.makeFlags(toBuy), 32);
    }

    String getProductList() {
	return StrUtil.listVector(products, ", ", "Nenhum");
    }

    public String getSheet() {
	return "LOJA - ID: [" + theId + "]" + Separators.NL +
	    "Título: [" + theTitle + "]"+ Separators.NL +
	    "Compra: " + buyTax + "  Venda: " + sellTax + Separators.NL +
	    "Itens comprados: " + StrUtil.niceListFlags(toBuy, Item.typeLabels, "Nenhum") + Separators.NL +
	    "Moedas aceitas: " + StrUtil.niceListFlags(goodMoney, Currency.kindLabel, "Nenhuma") + Separators.NL +
	    "Vendedor: [" + keeper + "]" + Separators.NL +
	    "Produtos: " + getProductList(); 
    }
}

Site Stats

Snippets: 1791
Codebases: 571