public static void printInput(Product[] objectArray) throws IOException { System.out.println("The input data from the file is: \n"); for (Product item : objectArray) { System.out.println(item.getName() + " " + item.getPrice()); } } // (optional) - displays what was read from the input file
// retrieve stored list of products from products file public Product[] getStoredProducts() { Product[] products = (Product[]) readFile(getProductFileName()); // print names of products being returned for (Product product : products) { System.out.println("getStoredProducts: Returned " + product.getName()); } return products; }
// retrieve all reviews for a product from the database public String[][] getProductReviews(String name) { String[][] reviews = null; Product product = getProductEntry(name); if (product != null) { reviews = product.getRev(); // get product review } return reviews; }
// retrieve product from database public Product getProductEntry(String name) { Product product = null; for (Product entry : getProducts()) { if (entry != null && entry.getName().equals(name)) { product = entry; break; } } return product; }
// remove product review from database public boolean removeReviewEntry(String name, String review_id) { boolean success = true; Product product = getProductEntry(name); // retrieve product if (product != null) { product.deleteReview(review_id); // remove product review } else { success = false; } return success; }
public static void main(String[] args) throws IOException { FileReader fileReader = new FileReader("productsInput.txt"); BufferedReader reader = new BufferedReader(fileReader); FileWriter fileWriter = new FileWriter("productsOutput.txt"); BufferedWriter writer = new BufferedWriter(fileWriter); ArrayList<Product> products = new ArrayList<Product>(); String line = reader.readLine(); while (line != null) { String[] input = line.split(" "); String productName = input[0]; Float productPrice = Float.parseFloat(input[1]); Product product = new Product(productName, productPrice); products.add(product); line = reader.readLine(); } fileReader.close(); Collections.sort( products, new Comparator<Product>() { @Override public int compare(Product firstProduct, Product secondProduct) { if (firstProduct.getNumber() < secondProduct.getNumber()) { return -1; } else if (firstProduct.getNumber() > secondProduct.getNumber()) { return 1; } return 0; } }); for (Product product : products) { writer.write(product.toString()); writer.write("\n"); } writer.close(); }
// remove product from database public boolean deleteProduct(String name) { boolean success = true; Iterator<Product> products = this.product_list.iterator(); while (products.hasNext()) { Product entry = products.next(); if (entry.getName().equals(name)) { success = this.product_list.remove(entry); removeStoredLocation(name); // remove product entry break; } } return success; }
// add product review public boolean addProductReview(String name, String user, String score, String review) { boolean success = true; Product product = getProductEntry(name); // find product entry System.out.println("addProductReview: Retrived Entry " + product); if (product != null) { // add product review product.addReview(user, score, review); } else { success = false; } return success; }
/** Finds the product combinations of numItems items with $1 of the totalPrice */ private void findProductCombination( ArrayList<Product> productList, double target, ArrayList<Product> partial) { // Keeping +-2 dollar as the price search window. int priceBuffer = 2; // if partial size > numItems, you already have too many items, so stop if (partial.size() > itemCount) { return; } double sum = 0; for (Product x : partial) sum += x.getPrice(); if (Math.abs(sum - target) < priceBuffer && partial.size() == itemCount && desiredProductList.size() < 25) { // if no price combos yet, just add it on if (desiredProductList.size() == 0) { desiredProductList.add(new Combination(partial, totalPrice)); } // otherwise, check it against the most recent product combo to make sure you're not repeating // TODO: check all product combos else { Combination testerCombo = desiredProductList.get(desiredProductList.size() - 1); Combination partialCombo = new Combination(partial, totalPrice); if (!partialCombo.equals(testerCombo)) { desiredProductList.add(partialCombo); } } } // if sum is at or within $1 of target, then stop - done! if (sum >= target + priceBuffer) { return; } // otherwise, recursively continue adding another product to combo and test it for (int i = 0; i < productList.size() && !(partial.size() == itemCount && sum < target); i++) { ArrayList<Product> remaining = new ArrayList<Product>(); Product n = productList.get(i); for (int j = i + 1; j < productList.size(); j++) { remaining.add(productList.get(j)); } ArrayList<Product> partial_rec = new ArrayList<Product>(partial); partial_rec.add(n); findProductCombination(remaining, target, partial_rec); } }
// add product to database using a product object public boolean addProductEntry(Product product, String server_name) { boolean success = true; this.product_list.add(product); // add product to database addStoredLocation(product.getName(), server_name); // update stored locations of product return success; }
/** * Displays all Product entries in Inventory line by line, or prints an empty notification if * there are no Products in Inventory. */ public void displayInventory() { Collections.sort( inv, new Comparator<Product>() { public int compare(Product p1, Product p2) { return p1.getSku() - p2.getSku(); } }); if (inv.isEmpty()) { System.out.println( "Inventory is empty, returning to Store " + "Menu..."); // Inventory is empty } else for (Product p : inv) { System.out.println(p.toStringGeneric()); // Display Inventory // contents in order they appear in ArrayList } System.out.println(); }
/** * Test driver for class <code>OrderItem</code>. * * @param args not used. */ public static void main(String[] args) { stdOut.println(""); stdOut.println("Testing class OrderItem..."); String code = "C001"; String description = "Colombia, Whole, 1 lb"; double price = 17.99; int quantity = 5; int newQuantity = 10; Product product = new Product(code, description, price); // Test accessors OrderItem item = new OrderItem(product, quantity); assertTrue("1: testing method getProduct", product.equals(item.getProduct())); assertTrue("2: testing method getQuantity", quantity == item.getQuantity()); // Test mutator item = new OrderItem(product, quantity); item.setQuantity(newQuantity); assertTrue("3: testing method setQuantity", newQuantity == item.getQuantity()); // Test method valueOf item = new OrderItem(product, quantity); double value = price * (double) quantity; assertTrue("4: testing method valueOf", value == item.getValue()); // Test method toString item = new OrderItem(product, quantity); String result = quantity + " " + code + " " + price; assertTrue("5: testing method toString", result.equals(item.toString())); stdOut.println("done"); }
public static void main(String[] args) { try { int n = readLinesNumber(); Product[] menu = readInputFile(n); printInput(menu); String[] output = Product.priceSort(menu, n); writeToFile(output); } catch (IOException err) { System.err.println(err.getMessage()); } }
/* * Converts the Review Object into an TriG String * representation. */ private String convertReview(Review review, ObjectBundle bundle) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(Review.getPrefixed(review.getNr(), bundle.getPublisherNum())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Review"))); // bsbm:reviewFor result.append( createTriplePO( BSBM.prefixed("reviewFor"), Product.getPrefixed(review.getProduct(), review.getProducerOfProduct()))); // rev:reviewer result.append( createTriplePO( REV.prefixed("reviewer"), Person.getPrefixed(review.getPerson(), review.getPublisher()))); // dc:title result.append(createTriplePO(DC.prefixed("title"), createLiteral(review.getTitle()))); // rev:text result.append( createTriplePO( REV.prefixed("text"), createLanguageLiteral(review.getText(), ISO3166.language[review.getLanguage()]))); // bsbm:ratingX Integer[] ratings = review.getRatings(); for (int i = 0, j = 1; i < ratings.length; i++, j++) { Integer value = ratings[i]; if (value != null) result.append( createTriplePO( BSBM.getRatingPrefix(j), // createDataTypeLiteral(value.toString(), // XSD.prefixed("integer")) value.toString())); } // bsbm:reviewDate GregorianCalendar reviewDate = new GregorianCalendar(); reviewDate.setTimeInMillis(review.getReviewDate()); String reviewDateString = DateGenerator.formatDateTime(reviewDate); result.append( createTriplePO( BSBM.prefixed("reviewDate"), createDataTypeLiteral(reviewDateString, XSD.prefixed("dateTime")))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), RatingSite.getPrefixed(review.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(review.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
/* * Converts the Product Object into an TriG String * representation. */ private String convertProduct(Product product) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(Product.getPrefixed(product.getNr(), product.getProducer())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Product"))); // rdfs:label result.append(createTriplePO(RDFS.prefixed("label"), createLiteral(product.getLabel()))); // rdfs:comment result.append(createTriplePO(RDFS.prefixed("comment"), createLiteral(product.getComment()))); // bsbm:productType if (forwardChaining) { ProductType pt = product.getProductType(); while (pt != null) { result.append(createTriplePO(RDF.prefixed("type"), pt.getPrefixed())); pt = pt.getParent(); } } else { result.append(createTriplePO(RDF.prefixed("type"), product.getProductType().getPrefixed())); } // bsbm:productPropertyNumeric Integer[] ppn = product.getProductPropertyNumeric(); for (int i = 0, j = 1; i < ppn.length; i++, j++) { Integer value = ppn[i]; if (value != null) result.append( createTriplePO( BSBM.getProductPropertyNumericPrefix(j), // createDataTypeLiteral(value.toString(), // XSD.prefixed("integer")) value.toString())); } // bsbm:productPropertyTextual String[] ppt = product.getProductPropertyTextual(); for (int i = 0, j = 1; i < ppt.length; i++, j++) { String value = ppt[i]; if (value != null) result.append( createTriplePO( BSBM.getProductPropertyTextualPrefix(j), createDataTypeLiteral(value, XSD.prefixed("string")))); } // bsbm:productFeature Iterator<Integer> pf = product.getFeatures().iterator(); while (pf.hasNext()) { Integer value = pf.next(); result.append( createTriplePO(BSBM.prefixed("productFeature"), ProductFeature.getPrefixed(value))); } // bsbm:producer result.append( createTriplePO(BSBM.prefixed("producer"), Producer.getPrefixed(product.getProducer()))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), Producer.getPrefixed(product.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(product.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
/* * Converts the Offer Object into an TriG String * representation. */ private String convertOffer(Offer offer) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(offer.getPrefixed()); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Offer"))); // bsbm:product int productNr = offer.getProduct(); int producerNr = Generator.getProducerOfProduct(productNr); result.append( createTriplePO(BSBM.prefixed("product"), Product.getPrefixed(productNr, producerNr))); // bsbm:vendor result.append(createTriplePO(BSBM.prefixed("vendor"), Vendor.getPrefixed(offer.getVendor()))); // bsbm:price result.append( createTriplePO( BSBM.prefixed("price"), // createDataTypeLiteral(offer.getPriceString(),BSBM.prefixed("USD")) offer.getPriceString() + "e0")); // bsbm:validFrom GregorianCalendar validFrom = new GregorianCalendar(); validFrom.setTimeInMillis(offer.getValidFrom()); String validFromString = DateGenerator.formatDateTime(validFrom); result.append( createTriplePO( BSBM.prefixed("validFrom"), createDataTypeLiteral(validFromString, XSD.prefixed("dateTime")))); // bsbm:validTo GregorianCalendar validTo = new GregorianCalendar(); validTo.setTimeInMillis(offer.getValidTo()); String validToString = DateGenerator.formatDateTime(validTo); result.append( createTriplePO( BSBM.prefixed("validTo"), createDataTypeLiteral(validToString, XSD.prefixed("dateTime")))); // bsbm:deliveryDays result.append( createTriplePO( BSBM.prefixed("deliveryDays"), // createDataTypeLiteral(offer.getDeliveryDays().toString(), // XSD.prefixed("integer")) offer.getDeliveryDays().toString())); // bsbm:offerWebpage result.append( createTriplePO(BSBM.prefixed("offerWebpage"), createURIref(offer.getOfferWebpage()))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), Vendor.getPrefixed(offer.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(offer.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }