给JAVASE初学者的一个例子:J2SE版本的 商家信息记录系统

功能基本实现完成,可以编译执行,来查看效果.

 

程序基本实现了 显示代码与业务逻辑相分离,并模拟了一个数据库的实现机制.

 

package cn.iamsese.product.custom.company;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
/**
 * 商家信息记录系统
 * cn.iamsese.product.custom.company
 * Author: vb2005xu [JAVA菜鸟]
 */
public class Main {
	
	public Main(){
		this.init();
	}
	
	private SellerOPInterface sellerOP ; 
	
	private void init(){
		this.sellerOP = new SellerOP();
		this.initSellersData(this.sellerOP) ;
		
	}
	
	private void initTellLineRecordData(Seller seller){
		TellLineRecordOPInterface tellLineRecordOP = this.sellerOP.getTellLineRecordOP(seller);
		tellLineRecordOP.addTellLineRecord( new TellLineRecord( 1 , MessageFormat.format("{0,date,full}",new Date()) , "抬高K钱1" ,"" +
				"虚太高价,故意坑人" ) ) ;
		tellLineRecordOP.addTellLineRecord( new TellLineRecord( 2 , MessageFormat.format("{0,date,full}",new Date()) , "抬高K钱2" ,"" +
		"虚太高价,故意坑人" ) ) ;
		tellLineRecordOP.addTellLineRecord( new TellLineRecord( 3 , MessageFormat.format("{0,date,full}",new Date()) , "抬高K钱3" ,"" +
		"虚太高价,故意坑人" ) ) ;
		tellLineRecordOP.addTellLineRecord( new TellLineRecord( 4 , MessageFormat.format("{0,date,full}",new Date()) , "抬高K钱4" ,"" +
		"虚太高价,故意坑人" ) ) ;
		//System.out.println(tellLineRecordOP.findAllTellLineRecords());
	}
	
	private void initSellersData(SellerOPInterface sellerOP){
		sellerOP.addSeller(new Seller(0,"商家1","鼎好111") ) ;
		
		Seller seller1 = new Seller(1,"商家2","鼎好112") ;
		sellerOP.addSeller(seller1) ;
		this.initTellLineRecordData(seller1);
		
		sellerOP.addSeller(new Seller(2,"商家3","鼎好103") ) ;
	}
	
	private void printSellersInfo(Enumeration<Seller> sellers){
		//列出所有商家信息
		String formatMSG = "| ID: {0} | Name: {1} | Adderss: {2} | Level: {3} |" ;
		while (sellers.hasMoreElements()){
			Seller seller = sellers.nextElement();
			System.out.println("----------Start-------------------------------------------------------------------------");
			System.out.println(MessageFormat.format(formatMSG, seller.getId(),seller.getName(),seller.getAddress(),this.sellerOP.getSellerLevel(seller)  ));
			
			this.printTellRecords(seller);
			System.out.println("----------Stop-----------------------------------------------------------------");
						
		}
	}
	
	private void printTellRecords(Seller seller){		
		int recordCount = this.sellerOP.getTellLineRecordOP(seller).getRecordsCount() ;
		System.out.println("记录: " + recordCount);
		
		int i = 0 ;
		String formatMSG = " ID: {0} Date: {1} Title: {2} \n 描述 {3} " ;
		for(TellLineRecord record : this.sellerOP.getTellLineRecordOP(seller).findAllTellLineRecords())
		{
			System.out.println( (i++) + "-----");
			System.out.println(MessageFormat.format(formatMSG,record.getId(),record.getDate(),record.getTitle(),record.getDescription()));
		}
	}
	
	private void buildWebUI(){
		this.printSellersInfo(this.sellerOP.findAllSeller());
	}
	
	public static void main(String[] args) {
		Main console = new Main();
		console.buildWebUI();
	}
}

/**
 * 商家所在的大楼
 * cn.iamsese.product.custom.zgcpzcompany
 * Author: vb2005xu [JAVA菜鸟]
 */
class Building {
	public final static int HAI_LONG = 0 ; //海龙 
	public final static int DING_HAO = 0 ; //鼎好 
	public final static int E_WORLD = 0 ; //E世界 
}

/**
 * 商家
 * cn.iamsese.product.custom.zgcpzcompany
 * Author: vb2005xu [JAVA菜鸟]
 */
class Seller {
	private int id ; //公司ID
	private String name ;//公司名称
	private String address ;//办公地点
	
	public Seller(int id,String name,String address)
	{
		this.id = id ;
		this.name = name ;
		this.address = address ;
	}
	
	//public ArrayList<TellLineRecord> records ;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

/**
 * 记录
 * cn.iamsese.product.custom.zgcpzcompany
 * Author: vb2005xu [JAVA菜鸟]
 */
class TellLineRecord {
	private int id ; //ID
	private String date ; //时间
	private String title ; //标题 
	private String description ; //描述
	
	public TellLineRecord(int id,String date,String title,String description){
		this.id = id ;
		this.date = date ;
		this.title = title ;
		this.description = description ;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
}


//逻辑层

/**
 * 商家 操作接口
 */
interface SellerOPInterface {
	public Enumeration<Seller> findAllSeller();
	public boolean addSeller(Seller seller) ;
	public Seller findSellerByName(String name);
	public boolean removeSellerByName(String name) ;
	public void removeAllSeller();
	
	public int getSellerLevel(Seller seller) ; // 获取商家的级别
	public TellLineRecordOPInterface getTellLineRecordOP(Seller seller);
}

/**
 * 记录集 操作接口
 * cn.iamsese.product.custom.zgcpzcompany
 * Author: vb2005xu [JAVA菜鸟]
 */
interface TellLineRecordOPInterface {
	public int getRecordsCount() ;
	public ArrayList<TellLineRecord> findAllTellLineRecords();
	public boolean addTellLineRecord(TellLineRecord tellLineRecord) ;
	public TellLineRecord findTellLineRecordByID(int id);
	public boolean removeTellLineRecordByID(int id);
	public void removeAllTellLineRecord();
}


class SellerOP implements SellerOPInterface {	
	
	public SellerOP(){}
	
	public Enumeration<Seller> findAllSeller() {		
		return DBStore.getDBStoreInstance().getSellerRecordStore().elements();
	}

	public Seller findSellerByName(String name) {
		return DBStore.getDBStoreInstance().getSellerRecordStore().get(name);
	}

	public void removeAllSeller() {
		DBStore.getDBStoreInstance().getSellerRecordStore().clear();
	}

	public boolean removeSellerByName(String name) {
		boolean state = false ;
		try {
			DBStore.getDBStoreInstance().getSellerRecordStore().remove(name) ;
			state = true ;
		} catch (NullPointerException e) {
		}
		return state;
	}
	public boolean addSeller(Seller seller) {
		boolean state = false ;
		try {
			DBStore.getDBStoreInstance().getSellerRecordStore().put(seller.getName(),seller) ;
			//为seller的记录对象初始化操作
			DBStore.getDBStoreInstance().getTellLineRecordStore().put(seller.getName() , new ArrayList<TellLineRecord>() ) ;
			state = true ;
		} catch (NullPointerException e) {
		}
		return state;
	}
	public TellLineRecordOPInterface getTellLineRecordOP(Seller seller) {		
		return new TellLineRecordOP(seller);
	}

	public int getSellerLevel(Seller seller) {
		//每两次加一个级别
		//System.out.println(this.getTellLineRecordOP(seller));
		return this.getTellLineRecordOP(seller).getRecordsCount() / 2 + 1;
	}

	
}

class TellLineRecordOP implements TellLineRecordOPInterface {
	private Seller seller ;	
	public TellLineRecordOP(Seller seller) {
		this.seller = seller;
	}	
	
	public ArrayList<TellLineRecord> findAllTellLineRecords() {
		return DBStore.getDBStoreInstance().getTellLineRecordStore().get(this.seller.getName());		
	}

	public TellLineRecord findTellLineRecordByID(int id) {
		return DBStore.getDBStoreInstance().getTellLineRecordStore().get(this.seller.getName()).get(id);
	}

	public void removeAllTellLineRecord() {
		DBStore.getDBStoreInstance().getTellLineRecordStore().get(this.seller.getName()).clear();
	}

	public boolean removeTellLineRecordByID(int id) {
		boolean state = false ;
		try {
			DBStore.getDBStoreInstance().getTellLineRecordStore().get(this.seller.getName()).remove(id) ;
			state = true ;
		} catch (NullPointerException e) {
		}
		return state;
	}

	public boolean addTellLineRecord(TellLineRecord tellLineRecord) {
		boolean state = false ;
		try {
			//首先取得TellLineRecordStore,并判断其中是否存在this.seller.getName()
			if (DBStore.getDBStoreInstance().getTellLineRecordStore().containsKey(this.seller.getName())){
				DBStore.getDBStoreInstance().getTellLineRecordStore().get(this.seller.getName()).add(tellLineRecord) ;
				state = true ;
			}
				
//			else { // -- 这个在调用addSeller时已经初始化了
//				DBStore.getDBStoreInstance().getTellLineRecordStore().put(this.seller.getName() , new ArrayList<TellLineRecord>() ) ;
//			}
//			state = true ;
		} catch (NullPointerException e) {
			//e.printStackTrace();
		}
		return state;
	}

	public int getRecordsCount() {
		try {
			return this.findAllTellLineRecords().size();
		}catch (NullPointerException e) {
			//如果this.findAllTellLineRecords()返回null的话
			return 0 ;
		}
		
	}
	
}

//实体层,这里没有使用到数据库,所以缺少了Dao层的实现

/**
 * 模拟的记录存储器存储器
 * cn.iamsese.product.custom.zgcpzcompany
 * Author: vb2005xu [JAVA菜鸟]
 */
class DBStore {
	private static DBStore instance = null ;
	
	/**
	 * < sellerName,ArrayList<TellLineRecord> >
	 */
	private Hashtable<String, ArrayList<TellLineRecord>> tellLineRecordStore ;
	/**
	 * <sellerName,Seller>
	 */
	private Hashtable<String, Seller> sellerRecordStore ;
	
	/*
	 * 仅能使用单态实例对象
	 */
	private DBStore(){
		this.setSellerRecordStore(new Hashtable<String, Seller> ());
		this.setTellLineRecordStore(new Hashtable<String, ArrayList<TellLineRecord>>()) ;
	}
	
	public static DBStore getDBStoreInstance(){
		if (instance == null)
			instance = new DBStore();
		return instance ;
	}
	
	public Hashtable<String, ArrayList<TellLineRecord>> getTellLineRecordStore() {
		return tellLineRecordStore;
	}

	public void setTellLineRecordStore(
			Hashtable<String, ArrayList<TellLineRecord>> tellLineRecordStore) {
		this.tellLineRecordStore = tellLineRecordStore;
	}

	public Hashtable<String, Seller> getSellerRecordStore() {
		return sellerRecordStore;
	}

	public void setSellerRecordStore(Hashtable<String, Seller> sellerRecordStore) {
		this.sellerRecordStore = sellerRecordStore;
	}
	
}

 

你可能感兴趣的:(DAO,J2SE)