项目组成
bean包
创建类 完成类的实例化
package com.fs.bean;
public class Consuminfo {
private int id;
private String card_number;
private String type;
private int counsum_data;
private String consume_date;
public Consuminfo(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCard_number() {
return card_number;
}
public void setCard_number(String card_number) {
this.card_number = card_number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getCounsum_data() {
return counsum_data;
}
public void setCounsum_data(int counsum_data) {
this.counsum_data = counsum_data;
}
public String getConsume_date() {
return consume_date;
}
public void setConsume_date(String consume_date) {
this.consume_date = consume_date;
}
public Consuminfo(int id, String card_number, String type, int counsum_data, String consume_date) {
this.id = id;
this.card_number = card_number;
this.type = type;
this.counsum_data = counsum_data;
this.consume_date = consume_date;
}
public Consuminfo(int id, String card_number, String type, int counsum_data) {
this.id = id;
this.card_number = card_number;
this.type = type;
this.counsum_data = counsum_data;
}
}
//必须要创建无参构造 避免使用jdbc时出现错误
dao包
dao包分为两个部分 一个是接口部分 另外一个是接口方法的实现Impl 此目的可以很好的分析用户的需求 根据需求来解决问题 问题的解决也会变得更为清晰 防止弄混业务
以下代码是实现消费的一系列业务 比如 添加消费信息 搜索消费信息等。。。
public class ConsuminfoDaoImpl implements ConsuminfoDao {
private QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
@Override
public List<Consuminfo> findByNumber(String number,int month) throws SQLException {
String sql="select * from tb_consuminfo where card_number=?and MONTH(consume_date)=? ";
return qr.query(sql,new BeanListHandler<Consuminfo>(Consuminfo.class),number,month);
}
@Override
public boolean addConsuminfo(Consuminfo consuminfo) throws SQLException {
String sql="INSERT INTO `tb_consuminfo` (id,card_number,type,consum_data,consume_date) values(?,?,?,?,?)";
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=sdf.format(d);
Object par[]={
consuminfo.getId() , consuminfo.getCard_number(),consuminfo.getType(),consuminfo.getCounsum_data(),time
};
int n= qr.update(sql,par);
return n>0? true:false;
}
@Override
public boolean Consume(String number,Sens sens) throws SQLException {
int n=0;
if (sens.getType().equals("上网")){
String sql="UPDATE tb_monthly_consumption_records SET real_flow = real_flow + ? WHERE card_number = ?";
n= qr.update(sql,sens.getData(),number);
return n>0? true:false;
}
if (sens.getType().equals("短信")){
String sql="update tb_monthly_consumption_records set real_SMS_count=real_SMS_count + ? where card_number=?";
n= qr.update(sql,sens.getData(),number);
return n>0? true:false;
}
if (sens.getType().equals("通话")){
String sql="update tb_monthly_consumption_records set real_talk_time= real_talk_time+? where card_number=?";
n= qr.update(sql,sens.getData(),number);
return n>0? true:false;
}
return false;
}
}
service包
与dao包一样分为两个部分 其原理也与dao包相同 主要区别是异常在此包中处理 避免异常产生的错误影响业务实施 其接口的方法与dao包里的接口方法相同 并且调用dao包中的Impl里的方法来实现此包里的业务
以下是一个套餐的需求实现 其实实现了找出所有的套餐以及更改套餐的业务等。。。
package com.fs.service.impl;
import com.fs.bean.SerPackage;
import com.fs.dao.SerPackageDao;
import com.fs.dao.impl.SerPackageDaoImpl;
import com.fs.service.SerPackageService;
import java.sql.SQLException;
import java.util.List;
public class SerPackageServiceImpl implements SerPackageService {
private SerPackageDao dao = new SerPackageDaoImpl();
@Override
public List<SerPackage> findAllSerPackage() {
try {
return dao.findAllSerPackage();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
@Override
public SerPackage findbyId(int id) throws SQLException {
return dao.findbyId(id);
}
@Override
public boolean changeSp(String number, int i) throws SQLException {
return dao.changeSp(number,i);
}
}
视图包
创建视图来实现业务的可视化以及操作业务
代码如下
package com.fs.view;
import com.fs.bean.*;
import com.fs.service.*;
import com.fs.service.impl.*;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class SosoView {
private static CardService cardService = new CardServiceImpl();
private static SerPackageService serPackageService = new SerPackageServiceImpl();
private static MoboleCardService moboleCardService = new MoboleCardServiceImpl();
private static MonthlyService monthlyService=new MonthlyServiceImpl();
private static ConsuminfoService consuminfoService=new ConsuminfoServiceImpl();
private static String msg=null;
private static SceneService sceneService=new SceneServiceImpl();
public static void mainMenu() throws SQLException {
boolean flag = true;
while(flag){
Scanner input = new Scanner(System.in);
System.out.println("***************欢迎使用嗖嗖移动用户大厅***************");
System.out.println("1.用户登录 2.用户注册 3.使用嗖嗖 ");
System.out.println("4.话费充值 5.资费说明 6.退出系统 ");
System.out.println("请选择:");
String select = input.next();
switch (select){
case "1":
login();
break;
case "2":
register();
break;
case "3":
UseSoso();
break;
case "4":
charge();
break;
case "5":
showSP();
break;
case "6":
flag = false;
break;
default:
System.out.println("输入错误,无此选项请重新输入!");
break;
}
}
}
public static void register(){
MoboleCard moboleCard = new MoboleCard();
while(true){
Scanner input = new Scanner(System.in);
System.out.println("*************** 可选择的卡号 ***************");
List<Card> cardList = cardService.findAllIsOk();
for(int i = 0 ; i < cardList.size();i++){
System.out.println((i+1)+"."+cardList.get(i).getCardNumber());
}
System.out.println("*************** 请选择你的卡号 ***************");
if(!input.hasNextInt()){
System.out.println("输入错误,请重新输入");
continue;
}
int select = input.nextInt();
if(select < 0 || select > cardList.size()){
System.out.println("序号错误,请重新输入");
continue;
}
//注册卡号
Card card = cardList.get(select-1);
//设置手机号
moboleCard.setCard_number(card.getCardNumber());
//获取所有套餐
List<SerPackage> serPackageList = serPackageService.findAllSerPackage();
for(int i = 0 ; i < serPackageList.size();i++){
System.out.print((i+1)+"."+serPackageList.get(i).getName());
}
System.out.println("*************** 请选择你的套餐 ***************");
if(!input.hasNextInt()){
System.out.println("输入错误,请重新输入");
continue;
}
select = input.nextInt();
if(select < 0 || select > serPackageList.size()){
System.out.println("序号错误,请重新输入");
continue;
}
//所选套餐
SerPackage serPackage = serPackageList.get(select-1);
//设置套餐
moboleCard.setSer_package(serPackage.getId());
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名:");
moboleCard.setUsername(scanner.nextLine());
System.out.println("请输入密码:");
moboleCard.setPassword(scanner.nextLine());
System.out.println("请输入预存话费金额:");
if(!input.hasNextDouble()){
System.out.println("输入错误,请重新输入");
continue;
}
double money = input.nextDouble();
if(money < serPackage.getPrice()){
System.out.println("您预存话费不足以支付本月的套餐资费,请重新输入!");
continue;
}
moboleCard.setMoney(money-serPackage.getPrice());
moboleCard.setStatus(0);
//调用业务方法完成注册
System.out.println(moboleCardService.register(moboleCard));
System.out.println("卡号:"+moboleCard.getCard_number());
System.out.println("用户名:"+moboleCard.getUsername());
System.out.println("当前余额:"+moboleCard.getMoney());
System.out.println(serPackage.getName()+serPackage.getTalk_time()+"分钟/月");
return;
}
}
public static void login() throws SQLException {
Scanner input = new Scanner(System.in);
System.out.println("请输入手机号:");
String cardNumber = input.nextLine();
System.out.println("请输入密码:");
String password = input.nextLine();
msg = moboleCardService.login(cardNumber,password);
System.out.println("【友情提示】:"+msg);
if (msg.equals("登录成功!")){
MoboleCard moboleCard=moboleCardService.Check(cardNumber);
Monthly_records Mr=monthlyService.findByNumber(cardNumber);
SerPackage serPackage=serPackageService.findbyId(moboleCard.getSer_package());
Usermenu(moboleCard,serPackage,Mr);
}
}
public static void Usermenu(MoboleCard moboleCard, SerPackage serPackage, Monthly_records monthly_records) throws SQLException {
Boolean flag=true;
while (flag){
System.out.println("*************** 移动用户菜单 ***************");
Scanner in=new Scanner(System.in);
System.out.println("1.本月账单查询");
System.out.println("2.套餐余额查询");
System.out.println("3.打印消费详情");
System.out.println("4.套餐变更");
System.out.println("5.办理网退");
String s=in.nextLine();
switch (s){
case "1":
System.out.println("您的卡号是"+moboleCard.getCard_number()+",单月账单:\n"+"套餐咨询费:"+serPackage.getPrice()+"\n合计:"+(serPackage.getPrice()+monthly_records.getConsum_amount())
+"\n账户余额"+moboleCard.getMoney());in.nextInt();break;
case "2": System.out.println("您的卡号是"+moboleCard.getCard_number()+",套餐内剩余:\n"+"通话时长:"+(serPackage.getTalk_time()- monthly_records.getReal_talk_time())+"\n短信条数:"+(serPackage.getSms_count()-monthly_records.getReal_SMS_time())
+"\n上网流量"+(serPackage.getFlow()-monthly_records.getReal_flow()));in.nextInt();
break;
case "3":
System.out.println("输入月份");
int k=in.nextInt();
List<Consuminfo>list=consuminfoService.findByNumber(moboleCard.getCard_number(),k);
System.out.println("序号"+" "+"类型"+" "+"数据"+" "+"日期");
if (list==null||list.size()==0){
System.out.println("本月没有消费记录");
}
for (int i=0;i<list.size();i++){
System.out.println((i+1)+" "+list.get(i).getType()+" "+list.get(i).getCounsum_data()+" "+list.get(i).getConsume_date());
}
break;
case "4":List<SerPackage> serPackageList=serPackageService.findAllSerPackage();
for (int i=0;i<serPackageList.size();i++){
System.out.print((i+1)+":"+serPackageList.get(i).getName()+" ");
}
System.out.println("选择更换的套餐类型");
int select=in.nextInt();
if (moboleCard.getSer_package()==select){
System.out.println("你已经是这个套餐了");
}else{
if (moboleCard.getMoney()<serPackage.getPrice()){
System.out.println("没米 gun");
}else {
serPackageService.changeSp(moboleCard.getCard_number(),select);
System.out.println("修改成功");
}
}
break;
case "5":
break;
default: flag=false;
}
}
}
public static void UseSoso() throws SQLException {
System.out.println("输入卡号");
Scanner in=new Scanner(System.in);
String number=in.nextLine();
MoboleCard moboleCard=moboleCardService.Check(number);
SerPackage serPackage=serPackageService.findbyId(moboleCard.getSer_package());
Monthly_records monthly_records=monthlyService.findByNumber(number);
if (moboleCard.getStatus()==1){
System.out.println("账户 错误");
return;
}
List<Sens> sensList=sceneService.Selct(number);
int n=sensList.size();
Random rand=new Random();
int idex=rand.nextInt(n);
System.out.println(sensList.get(idex).getDescription());
Sens sens=sensList.get(idex);
Consuminfo consuminfo=new Consuminfo(idex,number,sensList.get(idex).getType(),sensList.get(idex).getData());
consuminfoService.addConsuminfo(consuminfo);
Monthly_records mr=new Monthly_records();
if (sens.getType().equals("上网")){
if (serPackage.getFlow()- monthly_records.getReal_flow()-sens.getData()<=0){
System.out.println("费用不足 请充值");
}else{
consuminfoService.Consume(number,sens);
}
}
if (sens.getType().equals("短信")){
if (serPackage.getSms_count()-monthly_records.getReal_SMS_time()-sens.getData()<=0){
System.out.println("费用不足 请充值");
}else{
consuminfoService.Consume(number,sens);
}
}
if (sens.getType().equals("通话")){
if (serPackage.getTalk_time()-monthly_records.getReal_talk_time()-sens.getData()<=0){
System.out.println("费用不足 请充值");
}else{
consuminfoService.Consume(number,sens);
}
}
}
public static boolean charge() throws SQLException {
Scanner in=new Scanner(System.in);
Scanner inup=new Scanner(System.in);
System.out.println("输入号码");
String number=in.nextLine();
MoboleCard moboleCard=moboleCardService.Check(number);
System.out.println("输入充值金额");
double count=inup.nextDouble();
System.out.println("充值成功");
return moboleCardService.charge(count,moboleCard.getCard_number());
}
public static void showSP(){
List<SerPackage>list=serPackageService.findAllSerPackage();
System.out.println("序号\t"+"套餐名称\t"+"通话时长\t"+"短信条数\t"+"上网流量\t");
for (SerPackage s:list){
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getTalk_time()+" "+s.getSms_count()+" "+s.getFlow());
}
}
}
工具类
方便对数据库的操作 以及事务的处理
package com.fs.util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/*基于c3p0连接池+事务实现JDBC工具类
*
* ThreadLocal:
* 这个类提供线程局部变量。 这些变量与其正常的对应方式不同,
* 因为访问一个的每个线程(通过其get或set方法)都有自己独立初始化的变量副本。
* */
public class JdbcUtil {
private static ComboPooledDataSource ds =
new ComboPooledDataSource("mysql");
//保存不同线程的Connection
private static ThreadLocal<Connection> local = new ThreadLocal<>();
/*返回连接池数据源的对象*/
public static DataSource getDataSource(){
return ds;
}
/*获取连接对象*/
public static Connection getConnection() throws SQLException {
Connection con = local.get();
//当前有事务
if(con != null){
//事务想换的Connection
return con;
}
return ds.getConnection();
}
/*开启事务*/
public static void beginTransaction() throws SQLException {
Connection con = local.get();
if(con != null){
throw new SQLException("事务已经开启,不能重复开启!");
}
con = getConnection();
con.setAutoCommit(false);
local.set(con);
}
/*提交事务*/
public static void commitTransaction() throws SQLException {
Connection con = local.get();
if(con == null){
throw new SQLException("事务已经提交,不能重复提交!");
}
con.commit();
con.close();
local.remove();
}
/*事务回滚*/
public static void rollBackTransaction()throws SQLException {
Connection con = local.get();
if(con == null){
throw new SQLException("事务已经回滚,不能重复回滚!");
}
con.rollback();
con.close();
local.remove();
}
public static void release(Connection connection) throws SQLException {
Connection con = local.get();
if(con == null){
connection.close();
}
if(con != connection){
connection.close();
}
}
}