Everybody knows QR code format, and we can generate it at http://www.the-qrcode-generator.com:
1.
http://kdabrowski.blogspot.com
2.
http://jeeprojectsnippets.com
3.
http://jeeproject.com
4.
Bus ticket:
We focus on barcodes now...
An example of EAN-13 format of barcode:
Another example of CODE39 format of barcode:
CODE128/EAN-128 format of barcode:
EAN-8 format of barcode:
Interleaved 2 of 5:
How can we use it?
Assume that we have barcode scanner, printer and display and we want to build single point of sale.
Let's begin...
-----------------------------------------
package com.jeeprojectsnippets.pos.entity;
import java.io.Serializable;
import com.jeeprojectsnippets.pos.entity.base.EntTable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* Entity class for table: "pos_Product"
*/
@Entity
@Table(name = "pos_Product")
@NamedQueries({
@NamedQuery(name = "EntProduct.findById", query = "SELECT p FROM EntProduct p WHERE id = :id "),
@NamedQuery(name = "EntProduct.findByBarcode", query = "SELECT p FROM EntProduct p WHERE barcode = :barcode ")
})
public class EntProduct extends EntTable implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "barcode")
private Long barcode;
@Column(name = "price")
private double price;
/**
* @return the barcode
*/
public Long getBarcode() {
return barcode;
}
/**
* @param barcode the barcode to set
*/
public void setBarcode(Long barcode) {
this.barcode = barcode;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
}
-------------------------------------------
package com.jeeprojectsnippets.pos.entity.base;So we have a little shop now :-)
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* Auxliary class for Entity classes
*/
public class EntTable implements Serializable {
private static final long serialVersionUID = -2L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
-------------------------------------------
USE `myDB`;
CREATE TABLE pos_Product
(
`id` INTEGER NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`barcode` LONG NULL,
`price` DOUBLE NULL,
PRIMARY KEY (`id`)
) ;
COMMIT;
INSERT INTO `sio2`.`pos_product` (`id`, `name`, `barcode`, `price`) VALUES (33, 'cheese', '1234567890123', 13.3);
INSERT INTO `sio2`.`pos_product` (`id`, `name`, `barcode`, `price`) VALUES (34, 'butter', '1234567890124', 14.44);
INSERT INTO `sio2`.`pos_product` (`id`, `name`, `barcode`, `price`) VALUES (35, 'milk', '1234567890125', 15.0);
INSERT INTO `sio2`.`pos_product` (`id`, `name`, `barcode`, `price`) VALUES (36, 'bread', '1234567890126', 16);
COMMIT;
-------------------------------------------
package com.jeeprojectsnippets.pos.entity.manager;
import javax.ejb.Local;
import com.jeeprojectsnippets.pos.entity.EntProduct;
@Local
/**
* ProductManager interface to DAO operations of EntProduct object
*/
public interface ProductManager {
public EntProduct saveAndRefresh(EntProduct entProduct);
public EntProduct findById(int id);
public EntProduct findByBarcode(Long barcode);
publicT find(Class entityClass, int id);
}
-------------------------------------------
package com.jeeprojectsnippets.pos.entity.manager.impl;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.jeeprojectsnippets.pos.entity.EntProduct;
import com.jeeprojectsnippets.pos.entity.manager.ProductManager;
import com.jeeprojectsnippets.pos.io.PosConstants;
@Stateless(name = "ProductManagerImpl")
/**
* Class, which implements ProductManager interface
*/
public class ProductManagerImpl implements ProductManager, PosConstants {
@PersistenceContext(unitName = "Scanning")
private EntityManager em;
@Override
public EntProduct saveAndRefresh(EntProduct entProduct) {
em.persist(entProduct);
em.refresh(entProduct);
return entProduct;
}
@Override
public EntProduct findById(int id) {
EntProduct entProduct = em.find(EntProduct.class, id);
return entProduct;
}
@Override
publicT find(Class entityClass, int id) {
return em.find(entityClass, id);
}
@Override
public EntProduct findByBarcode(Long barcode)
throws javax.persistence.NoResultException,
javax.persistence.NonUniqueResultException{
Query que = em.createNamedQuery("EntProduct.findByBarcode");
que.setParameter("barcode", barcode);
EntProduct resu = (EntProduct) que.getSingleResult();
return resu;
}
}
-------------------------------------------
package com.jeeprojectsnippets.pos;
import java.util.ArrayList;
import java.util.List;
import com.jeeprojectsnippets.pos.io.PosConstants;
import com.jeeprojectsnippets.pos.entity.EntProduct;
import com.jeeprojectsnippets.pos.entity.manager.ProductManager;
import javax.ejb.EJB;
public class POSApp implements PosConstants {
private static Scanner scanner = null;
private static Display display = null;
private static Printer printer = null;
private static UserButtonListener userButtonListener = null;
private static EntProduct entProduct = null;
private static ListentProductList = null;
@EJB
private static ProductManager productManager = null;
public static void init(){
setScanner(new Scanner());
setDisplay(new Display());
setPrinter(new Printer());
setUserButtonListener(new UserButtonListener());
}
private static void run() throws Exception{
try{
Long barcode = Long.parseLong(getScanner().scan());
setEntProduct(getProductManager().findByBarcode(barcode));
getEntProductList().add(getEntProduct());
getDisplay().display(getEntProduct().getName() + " " + getEntProduct().getPrice() + "\n");
} catch (java.lang.NumberFormatException e){
getDisplay().display(ERROR_MSG_INVALID_BARCODE_NAME);
} catch (javax.persistence.NoResultException e) {
getDisplay().display(ERROR_MSG_PRODUCT_NOT_FOUND_NAME);
} catch (javax.persistence.NonUniqueResultException e) {
getDisplay().display(ERROR_MSG_MORE_THAN_ONE_PRODUCT_FOUND_NAME);
}catch(Exception ex){
// TODO logger.debug(ex.getMessage());
System.out.println(ex.getMessage());
throw new Exception(ex.getMessage());
}finally{
// TODO clean if needed
}
}
protected static String act() throws Exception{
double sum = 0;
while(!EVENT_EXIT.equals(getUserButtonListener().watch())){
setEntProductList(new ArrayList(1));
run();
for (EntProduct entProduc : getEntProductList()){
getPrinter().print(entProduc.getName() + " " + entProduc.getPrice() + "\n");
sum += entProduc.getPrice();
}
}
getPrinter().print("TOTAL: " + sum + "\n");
getDisplay().display("TOTAL: " + sum + "\n");
return Double.toString(sum);
}
public static void main(String[] args) throws Exception {
init();
while(true){
act();
}
}
/**
* @return the scanner
*/
public static Scanner getScanner() {
return scanner;
}
(...)
}
-------------------------------------------
package com.jeeprojectsnippets.pos;
import com.jeeprojectsnippets.pos.io.BarCodeReader;
import com.jeeprojectsnippets.pos.io.impl.BarCodeReaderImpl;
class Scanner implements BarCodeReader {
private BarCodeReader adaptee = new BarCodeReaderImpl();
public String read() {
return adaptee.read();
}
public String scan() {
return read();
}
}
-------------------------------------------
package com.jeeprojectsnippets.pos;
import com.jeeprojectsnippets.pos.io.DisplayWriter;
import com.jeeprojectsnippets.pos.io.impl.DisplayWriterImpl;
class Display implements DisplayWriter
{
private DisplayWriter adaptee = new DisplayWriterImpl();
public boolean write(String msg) {
return adaptee.write(msg);
}
public boolean display(String msg) {
return write(msg);
}
}
-------------------------------------------
package com.jeeprojectsnippets.pos;
import com.jeeprojectsnippets.pos.io.PrinterWriter;
import com.jeeprojectsnippets.pos.io.impl.PrinterWriterImpl;
class Printer implements PrinterWriter {
private PrinterWriter adaptee = new PrinterWriterImpl();
public boolean write(String msg) {
return adaptee.write(msg);
}
public boolean print(String msg) {
return write(msg);
}
}
-------------------------------------------
package com.jeeprojectsnippets.pos;
import com.jeeprojectsnippets.pos.io.UserReactionReader;
import com.jeeprojectsnippets.pos.io.impl.UserReactionReaderImpl;
class UserButtonListener implements UserReactionReader {
private UserReactionReader adaptee = new UserReactionReaderImpl();
public String listen() {
return adaptee.listen();
}
public String watch() {
return listen();
}
}
-------------------------------------------
No comments:
Post a Comment