`

给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;
	}
	
}

 

  • 大小: 61.5 KB
分享到:
评论

相关推荐

    三年JavaEE开发积累的那些代码之一:JavaSE篇完整实例源码

    三年JavaEE开发积累的那些代码之一:JavaSE篇 有什么? 1)自己写的例子:或是为了项目中应用写的demo,或是为了学习某项技术写的demo。 2)网上下载的例子:或改过或没改过,或完善过或原封没动。 没什么? 1)公司...

    JavaSE_J2SE_5.0_API_中文文档_html格式

    JavaSE_J2SE_5.0_API_中文文档_html格式

    javaSE商品管理系统

    javaSE商品管理系统 开发工具:MyEclipse10.7,数据库:Mysql5.5,。 简单对商品的增删改查

    JAVASE初学者教程菜鸟入门思维导图(详细版)

    内容涵盖javase基本语法、面向对象、集合框架、内部类常用类、IO流、多线程,适合学习一阶段的同学平时学习和复习,个人感觉写的比较细了。一起进步吧

    javase编写的学生管理系统--适合初学者学习使用

    javase编写的学生管理系统。因为涉及不到数据库,jsp,servlet等知识,适合初学se的学生借鉴学习

    超市管理系统 纯javase 对初学者很有帮助,涵盖大部分知识点

    超市管理系统 纯javase 对初学者很有帮助,涵盖大部分知识点

    JavaSE之多态体系

    这个文件里的内容适合Java初学者,这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太多的官方话,绝对是你初学JavaSE的宝典,针对人群:刚入门的...

    javase下cmd使用例子.java

    javase下cmd的一个例子,相信给大家很大的帮助

    JavaSE之入门必备

    这这文件里的内容适合Java初学者,这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太多的官方话,绝对是你初学JavaSE的宝典,针对人群:刚入门的...

    JavaSE之继承的概述

    这个文件里的内容适合Java初学者,这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太多的官方话,绝对是你初学JavaSE的宝典,针对人群:刚入门的...

    JavaSE之抽象的概念

    这这文件里的内容适合Java初学者,这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太多的官方话,绝对是你初学JavaSE的宝典,针对人群:刚入门的...

    用javaSE做的购物系统

    用javaSE做的购物系统 适用初学者 简单的购物系统 没有web技术

    JavaSE之日期时间类

    这个文件里的内容适合Java初学者,里边包含了日期以及时间的实现以及使用,可以获取时间戳以及当前系统时间以及多种用法。这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是...

    JavaSE之接口概述

    这里文件里的内容适合Java初学者,里边包含了接口的概念以及相关的用法,这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太多的官方话,绝对是你初学...

    JavaSE之Java基本语法

    这这文件里的内容适合Java初学者,这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太多的官方话,绝对是你初学JavaSE的宝典,针对人群:刚入门的...

    220个JAVA初学者实例

    220个JAVA初学者实例 对于刚接触java的帮助很大

    J2SE(javaSE基础知识).doc

    javaSE基础知识大全,java简介、java不同版本、java特点、jdk、第一个程序、jdk、jre、jvm、eclipse配置、注释、标识符、变量、数据类型、运算符、分支语句、命名规范、循环语句、数组、main方法、面向对象、成员...

    JavaSE之数组与集合类

    这个文件里的内容适合Java初学者,里边包含了JavaSE阶段用到的多个数组以及集合的用法和声明方式。这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE入门者学习,都是浅显易懂的话语,没有太...

    JavaSE之面对程序语言

    这个文件里的内容适合Java初学者,这是一些关乎JavaSE面向程序的相关知识以及一些Java的基础使用规则、类的定义规则、方法的定义方式等等。这也是小编在刚开学学习JavaSE时记录的一些笔记,里面的内容非常适合JavaSE...

    狂神笔记,b站狂神说课程笔记大全(最新)

    b站狂神说课程笔记大全,每个部分都有 狂神说java系列笔记(java基础+javaweb+ssm+微服务)全套 狂神说上课笔记未删减 Java基础到技术升级 1、JavaSE:Java入门 2、JavaSE:基础语法 3、JavaSE:流程控制 4、JavaSE...

Global site tag (gtag.js) - Google Analytics