亲宝软件园·资讯

展开

Java从零实现超市会员管理系统

Java Fans 人气:0

项目需求

为某超市开发一个超市会员管理系统,实现会员的管理,具体实现功能如下:

(1)积分累计

(2)积分兑换

(3)查询剩余积分

(4)查询所有会员信息

(5)修改密码

(6)开卡(注册会员)

覆盖知识

程序基本概念、数据类型、流程控制、顺序、选择 、循环、跳转语句、变量、类、方法、实用类等

掌握数据库、JDBC、三层架构等相关知识。

掌握Druid连接池、Apache的DBUtils使用 。

开发思路

(1)明确需求

(2)编码顺序

(3)测试

开发步骤

1)、添加需要的jar包到项目中,将lib文件夹中的jar文件通过鼠标右单击选择Build Path的方式添加到你设置的eatJar文件目录里。

2)、创建database.properties文件,用来配置注册驱动和数据库连接对象的相关数据。

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java221804
username=root
password=huanghuang
initialSize=10
maxActive=30
maxIdle=5
maxWait=3000

3)、添加需要的工具类DBUtils类

package cn.book.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DBUtils {
	private static DruidDataSource druidDataSource;
	static {
		Properties properties = new Properties();
		try {
			InputStream is = DBUtils.class
					.getResourceAsStream("/database.properties");
			properties.load(is);
			druidDataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(properties);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static DataSource getDataSource(){
		return druidDataSource;
	}
}

4)、创建数据表:member表

CREATE TABLE `member` (
  `name` varchar(20) NOT NULL,
  `cardId` int(11) NOT NULL,
  `password` varchar(20) NOT NULL,
  `score` int(11) NOT NULL,
  `registDate` varchar(20) NOT NULL,
  UNIQUE KEY `cardId` (`cardId`)
);

5)、编写book类,包含get/set方法、有参/无参构造、toString方法

6)、数据访问层DAO层的接口和实现类的增删改查方法的编写

7)、服务层Service层的接口和实现类的增删改查方法的编写

8)、最后完成视图层View层测试类的编写

全部代码展示

1、Member类

package cn.member.entity;
public class Member {
	// 定义属性
	private String name;
	private int cardId;
	private String password;
	private int score;
	private String registDate;
	public Member() {
		super();
	}
	public Member(String name, int cardId, String password, int score,
			String registDate) {
		super();
		this.name = name;
		this.cardId = cardId;
		this.password = password;
		this.score = score;
		this.registDate = registDate;
	}
	// 定义get/set方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCardId() {
		return cardId;
	}
	public void setCardId(int cardId) {
		this.cardId = cardId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public String getRegistDate() {
		return registDate;
	}
	public void setRegistDate(String registDate) {
		this.registDate = registDate;
	}
	@Override
	public String toString() {
		return "Member [name=" + name + ", cardId=" + cardId + ", password="
				+ password + ", score=" + score + ", registDate=" + registDate
				+ "]";
	}
}

2、DBUtils类

(使用连接池DruidDataSource、Apache的知识)

package cn.member.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DBUtils {
	private static DruidDataSource druidDataSource;
	static {
		Properties properties = new Properties();
		try {
			InputStream is = DBUtils.class
					.getResourceAsStream("/database.properties");
			properties.load(is);
			druidDataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(properties);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static DataSource getDataSource(){
		return druidDataSource;
	}
}

3、数据访问层的MemberDao接口

package cn.member.dao;
import java.util.List;
import cn.member.entity.Member;
public interface MemberDao {
	// 增
	public int insert(Member member);
	// 删
	public int delete(int cardId);
	// 改
	public int update(Member book);
	// 查一个
	public Member select(int cardId);
	// 查全部
	public List<Member> selectAll();
}

4、数据访问层的MemberDaoImpl类

package cn.member.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.member.dao.MemberDao;
import cn.member.entity.Member;
import cn.member.utils.DBUtils;
public class MemberDaoImpl implements MemberDao {
	private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
	@Override
	public int insert(Member member) {
		String sql = "insert into member(name,cardId,password,score,registDate) values(?,?,?,?,?);";
		Object[] args = { member.getName(), member.getCardId(),
				member.getPassword(), member.getScore(), member.getRegistDate() };
		try {
			return queryRunner.update(sql, args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public int delete(int cardId) {
		String sql = "delete from member where cardId=?;";
		try {
			return queryRunner.update(sql, cardId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public int update(Member member) {
		String sql = "update `member` set `name`=?,`password`=?,`score`=?,`registDate`=? where `cardId`=?;";
		Object[] args={member.getName(),member.getPassword(),member.getScore(),member.getRegistDate(),member.getCardId()};
		try {
			return queryRunner.update(sql, args);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public Member select(int cardId) {
		String sql = "select * from member where cardId=?;";
		try {
			return queryRunner.query(sql,
					new BeanHandler<Member>(Member.class), cardId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Member> selectAll() {
		String sql = "SELECT * FROM member;";
		try {
			return queryRunner.query(sql, new BeanListHandler<Member>(
					Member.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}

5、服务层的MemberService接口

package cn.member.service;
import java.util.List;
import cn.member.entity.Member;
public interface MemberService {
	// 增
	public int insertMember(Member member);
	// 删
	public int deleteMember(int cardId);
	// 改
	public int updateMember(Member member);
	// 查一个
	public Member selectMember(int cardId);
	// 查全部
	public List<Member> selectAllMembers();
}

6、服务层的MemberServiceImpl类

package cn.member.service.impl;
import java.util.List;
import cn.member.dao.MemberDao;
import cn.member.dao.impl.MemberDaoImpl;
import cn.member.entity.Member;
import cn.member.service.MemberService;
public class MemberServiceImpl implements MemberService {
	MemberDao memberDao = new MemberDaoImpl();
	@Override
	public int insertMember(Member Member) {
		if (memberDao.select(Member.getCardId()) == null) {
			return memberDao.insert(Member);
		}
		return 0;
	}
	@Override
	public int deleteMember(int cardId) {
		if (memberDao.select(cardId) != null) {
			return memberDao.delete(cardId);
		}
		return 0;
	}
	@Override
	public int updateMember(Member Member) {
		if (memberDao.select(Member.getCardId()) != null) {
			return memberDao.update(Member);
		}
		return 0;
	}
	@Override
	public Member selectMember(int cardId) {
		memberDao.select(cardId);
		return null;
	}
	@Override
	public List<Member> selectAllMembers() {
		List<Member> listMembers = memberDao.selectAll();
		return listMembers;
	}
}

7、视图层MemberMgr测试类

package cn.member.view;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import cn.member.entity.Member;
import cn.member.service.MemberService;
import cn.member.service.impl.MemberServiceImpl;
public class MemberMgr {
	// 定义一个Scanner
	Scanner sc = new Scanner(System.in);
	// 定义Member对象
	Member member = new Member();
	MemberService ms=new MemberServiceImpl();
	//从数据库中查全部会员信息
	List<Member> listM=null;
	// 菜单选择栏
	public void showFirst() {
		System.out
				.println("****************************************欢迎使用超市会员管理系统*****************************************");
		System.out
				.println("1.开卡\t2.修改密码\t3.积分累计\t4.积分兑换\t5.剩余积分查询\t6.查看所有会员信息\t7.退出");
		System.out
				.println("*******************************************************************************************************");
	}
	// 会员卡号随机产生方法
	public int cardRandom() {
		return (int) (Math.random() * (19999999 - 10000000) + 10000000);
	}
	// 注册时判断会员是否存在
	public boolean isCardId(int mm) {
		boolean isCId = false;
		if (listM.size() > 0) {
			for (Member memberC : listM) {
				if (mm == memberC.getCardId()) {
					isCId = true;
					return isCId;
				}
			}
		}
		return isCId;
	}
	// 判断会员卡和密码是否正确
	public boolean isMember(int mm, String pwd) {
		boolean isCId = false;
		if (listM.size() > 0) {
			for (Member memberC : listM) {
//				System.out.println(mm+"/"+pwd+"/"+memberC.getCardId()+"/"+memberC.getPassword());
				if (mm == memberC.getCardId()
						&& pwd.equals(memberC.getPassword())) {
					isCId = true;
					member = memberC;
					return isCId;
				}
			}
		}
		return isCId;
	}
	// 1、开卡
	public void OpenCard() {
		// 每次都初始化一条member对象
		member = new Member();
		do {
			member.setCardId(cardRandom());
		} while (isCardId(member.getCardId()));
		System.out.print("请输入注册姓名:");
		member.setName(sc.next());
		System.out.print("请输入注册密码:");
		String password1 = sc.next();
		do {
			if (password1.length() < 6) {
				System.out.print("会员密码不能小于6位,请重新输入注册密码:");
				password1 = sc.next();
			}
		} while (password1.length() < 6);
		member.setPassword(password1);
		member.setScore(100);
		Date data = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		member.setRegistDate(sdf.format(data));
//		ms.insertMember(member);
		if(ms.insertMember(member)>0){
			System.out.println("恭喜,开通会员卡成功!系统赠送您" + member.getScore()
					+ "积分,您的会员卡号是:" + member.getCardId());
		}else{
			System.out.println("会员开通失败!");
		}
//		listM.add(member);
	}
	// 2、修改密码
	public void changePassword() {
		System.out.print("请输入您的会员卡号:");
		int cardNum = sc.nextInt();
		System.out.print("请输入您的会员密码:");
		String cardPwd = sc.next();
		if (isMember(cardNum, cardPwd)) {
			System.out.print("请输入您的会员新密码:");
			member.setPassword(sc.next());
			if(ms.updateMember(member)>0){
				System.out.println("新密码设置成功!");
			}else{
				System.out.println("新密码设置失败!");
			}
		} else {
			System.out.println("您输入的会员卡号或密码错误,无法修改密码!");
		}
	}
	// 3、积分累计
	public void scoresAdd() {
		System.out.print("请输入您的会员卡号:");
		int cardNum = sc.nextInt();
		System.out.print("请输入您的会员密码:");
		String cardPwd = sc.next();
		if (isMember(cardNum, cardPwd)) {
			System.out.print("请输入本次消费金额(消费1元累计1分):");
			int scoreCurrent = sc.nextInt();
			member.setScore(scoreCurrent + member.getScore());
			if(ms.updateMember(member)>0){
				System.out.println("积分累计成功!你重新累计的积分为:" + member.getScore() + "分");
			}else{
				System.out.println("积分累计失败!");
			}
			
		} else {
			System.out.println("您输入的会员卡号或密码错误,无法累计积分!");
		}
	}
	// 4、积分兑换
	public void scoresConver() {
		System.out.print("请输入您的会员卡号:");
		int cardNum = sc.nextInt();
		System.out.print("请输入您的会员密码:");
		String cardPwd = sc.next();
		if (isMember(cardNum, cardPwd)) {
			System.out.print("请输入您要兑换使用的积分(100积分抵用0.1元,不足100的积分不做抵用):");
			int scoreCurrent = sc.nextInt();
			if (scoreCurrent < member.getScore()&&scoreCurrent>100) {
				member.setScore(member.getScore() - scoreCurrent);
				ms.updateMember(member);
				if(ms.updateMember(member)>0){
					System.out.println("您消费金额中使用会员积分抵扣了0."
							+ (scoreCurrent /100) + "元");
				}else{
					System.out.println("积分兑换失败!");
				}
			}else if(scoreCurrent<100){
				System.out.println("不满足积分兑换规则,不足100的积分不做抵用!");
			}else {			
				System.out.println("积分不足!");
			}
		} else {
			System.out.println("您输入的会员卡号或密码错误,无法进行兑换!");
		}
	}
	// 5、剩余积分查询
	public void scoresRemain() {
		System.out.print("请输入您的会员卡号:");
		int cardNum = sc.nextInt();
		System.out.print("请输入您的会员密码:");
		String cardPwd = sc.next();
		if (isMember(cardNum, cardPwd)) {
			System.out.println("会员信息【会员卡号:" + member.getCardId() + ",姓名:"
					+ member.getName() + ",可用积分:" + member.getScore()
					+ ",开卡时间:" + member.getRegistDate() + "】");
		} else {
			System.out.println("您输入的会员卡号或密码错误,无法查询到信息!");
		}
	}
	// 6、查看所有会员信息
	public void LookMembers() {
		int i = 0;
		for (Member member : listM) {
			i++;
			System.out.println("会员" + i + "信息【会员卡号:" + member.getCardId()
					+ ",姓名:" + member.getName() + ",可用积分:" + member.getScore()
					+ ",开卡时间:" + member.getRegistDate() + "】");
		}
	}
	public void MainMethod() {
		//从数据库中查全部会员信息
		listM=ms.selectAllMembers();
		boolean isIn = true;
		do {
			showFirst();
			System.out.print("请选择:");
			switch (sc.nextInt()) {
			case 1:
				OpenCard();
				break;
			case 2:
				changePassword();
				break;
			case 3:
				scoresAdd();
				break;
			case 4:
				scoresConver();
				break;
			case 5:
				scoresRemain();
				break;
			case 6:
				LookMembers();
				break;
			case 7:
				isIn = false;
				break;
			}
		} while (isIn);
	}
	public static void main(String[] args) {
		MemberMgr memberMgr=new MemberMgr();
		memberMgr.MainMethod();
	}
}

加载全部内容

相关教程
猜你喜欢
用户评论