📄 netbar.java
字号:
Java写的一个简单的网吧计费系统(一)业务逻辑包和实体包 和库 [置顶]
6下面是业务逻辑方面 和数据库包
package business;
import java.sql.DriverManager;
import java.sql.Connection;
/**
*
* class DatabaseConnect 实现连接方法
* @author SUN
*
*/
public class DatabaseConnect {
private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String DATASOURCE = "jdbc:odbc:NetBar";
public static Connection getConnection(){
Connection dbConnection = null;
try{
Class.forName(DRIVER_CLASS);
dbConnection = DriverManager.getConnection(DATASOURCE);
}catch(Exception e){
e.printStackTrace();
}
return dbConnection;
}
}
____________________________________________________________________
package business;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import entity.Record;
/**
*
* class DatabaseEnd 实现下机功能
* @author SUN
*
*/
public class DatabaseEnd {
public DatabaseEnd(){
}
public void doEndUseComputerBusiness(Record record){
Connection con = null;
PreparedStatement pStatement1 = null;
PreparedStatement pStatement2 = null;
try{
//获取连接
con = DatabaseConnect.getConnection();
//修改record表上机机录,添加下机时间
String strSql = "update Record set EndTime = (?) where ;
pStatement1 = con.prepareStatement(strSql);
pStatement1.setString(1, record.getEndTime());
pStatement1.setInt(2, record.getNumberId());
pStatement1.executeUpdate();
//修改此机器的使用标志为不使用
String strSql2 = "update Computer set OnUse = 0 where ;
pStatement2 = con.prepareStatement(strSql2);
pStatement2.setString(1, record.getComputerId());
pStatement2.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
pStatement2.close();
pStatement1.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
____________________________________________________________________
package business;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import entity.Record;
import entity.Computer;
/**
*
* class DatabaseStart 实现上机功能
* @author SUN
*
*/
public class DatabaseStart {
public DatabaseStart(){
}
public int doStartUseComputerBusiness(Record record, Computer computer){
Connection con = null;
PreparedStatement pStatement1 = null;
PreparedStatement pStatement2 = null;
PreparedStatement pStatement3 = null;
ResultSet rest = null;
int i = 0;
try{
//获取连接
con = DatabaseConnect.getConnection();
//插入record表一条上机记录
String strSql1 = "insert into Record(CardId,ComputerId,BeginTime) values (?,?,?)";
pStatement1 = con.prepareStatement(strSql1);
pStatement1.setString(1, record.getCardId());
pStatement1.setString(2, record.getComputerId());
pStatement1.setString(3, record.getBeginTime());
pStatement1.executeUpdate();
//修改此机器的使用标志为已经使用
String strSql2 = "update Computer set OnUse = 1 where ;
pStatement2 = con.prepareStatement(strSql2);
pStatement2.setString(1, computer.getComputerId());
pStatement2.executeUpdate();
//获取纪录编号
String strSql3 = "select Id from Record where CardId = (?) AND ComputerId = (?) AND BeginTime = (?)";
pStatement3 = con.prepareStatement(strSql3);
pStatement3.setString(1, record.getCardId());
pStatement3.setString(2, record.getComputerId());
pStatement3.setString(3, record.getBeginTime());
rest = pStatement3.executeQuery();
rest.next();
i = rest.getInt("Id");
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
rest.close();
pStatement3.close();
pStatement2.close();
pStatement1.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return i;
}
}
____________________________________________________________________
package business;
/**
*
* class FeeAccount 计算上机费用
* @author SUN
*
*/
public class FeeAccount {
static int calFee(String beginTime, String endTime){
int fee = 0;
//从字符串形式的日期获取年、月、日、小时、分
int beginYear = Integer.parseInt(beginTime.substring(0, 4));
int beginMonth = Integer.parseInt(beginTime.substring(5, 7));
int beginDay = Integer.parseInt(beginTime.substring(8, 10));
int beginHour = Integer.parseInt(beginTime.substring(11, 13));
int beginMinute = Integer.parseInt(beginTime.substring(14, 16));
int endYear = Integer.parseInt(endTime.substring(0, 4));
int endMonth = Integer.parseInt(endTime.substring(5, 7));
int endDay = Integer.parseInt(endTime.substring(8, 10));
int endHour = Integer.parseInt(endTime.substring(11, 13));
int endMinute = Integer.parseInt(endTime.substring(14, 16));
//计算上机的时间,单位为分钟
int playMinutes = 0;
playMinutes = ( (endYear - beginYear) * 365 * 24 * 60 +
(endMonth - beginMonth) * 30 * 24 * 60 +
(endDay - beginDay) * 24 * 60 +
(endHour - beginHour) * 60 + (endMinute - beginMinute));
//计算费用
int modNum = playMinutes % 60;
int playHours = 0;
playHours = playMinutes / 60;
if(playHours == 0 || (modNum > 5 && playHours > 0)){
playHours++;
}
fee = playHours * 2;
return fee;
}
}
____________________________________________________________________
package business;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import entity.Card;
import entity.Record;
import frame.pnlInfo;
/**
*
* class Info 显示上机后的消费信息
* @author SUN
*
*/
public class Info {
public void showInfo(pnlInfo p,Record r){
Connection con = null;
PreparedStatement pStatement1 = null;
PreparedStatement pStatement2 = null;
PreparedStatement pStatement3 = null;
ResultSet rest = null;
Card objCard = null;
try{
//用类FeeAccount中的calFee方法计算上机的费用并给Record类型的对象赋值
r.setFee(FeeAccount.calFee(r.getBeginTime(), r.getEndTime()));
//创建Card对象,计算上机消费后卡中的余额
objCard = new Card();
con = DatabaseConnect.getConnection();
//更新表Record中的消费项
String strSql1 = "update Record set Fee = (?) where ;
pStatement1 = con.prepareStatement(strSql1);
pStatement1.setInt(1, r.getFee());
pStatement1.setInt(2, r.getNumberId());
pStatement1.executeUpdate();
//获取消费前卡中的余额
String strSql2 = "select Balance from Card where ;
pStatement2 = con.prepareStatement(strSql2);
pStatement2.setString(1, r.getCardId());
rest = pStatement2.executeQuery();
rest.next();
objCard.setBalance(rest.getInt("Balance"));
//更新消费后卡中的余额
String strSql3 = "update Card set Balance = (?) where ;
pStatement3 = con.prepareStatement(strSql3);
pStatement3.setInt(1, objCard.getBalance()-r.getFee());
pStatement3.setString(2, r.getCardId());
pStatement3.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
pStatement3.close();
rest.close();
pStatement2.close();
pStatement1.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
//在界面pnlInfo中显示消费信息
p.lblGetComputerId.setText(r.getComputerId());
p.lblGetCardId.setText(r.getCardId());
p.lblGetBeginTime.setText(r.getBeginTime());
p.lblGetEndTime.setText(r.getEndTime());
p.lblGetFee.setText(new Integer(r.getFee()).toString());
p.lblGetBalance.setText(new Integer(objCard.getBalance()-r.getFee()).toString());
}
}
______________________________________________________________________
package business;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
* class PasswordCheck 检证密码
* @author SUN
*
*/
public class PasswordCheck {
public PasswordCheck(){
}
public boolean check(String cardid,String password){
Connection con = null;
PreparedStatement pStatement = null;
ResultSet rest = null;
String s = null;
try{
con = DatabaseConnect.getConnection();
String str = "select Password from Card where ;
pStatement = con.prepareStatement(str);
pStatement.setString(1, cardid);
rest = pStatement.executeQuery();
if(rest.next()){
s = rest.getString(1);
}else{
s = "*&^$@@!!)&**&&(*^^&$%$:P)&*^&^*";
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
rest.close();
pStatement.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return (s.equals(password));
}
}
_____________________________________________________________________
实体类包
package entity;
import java.io.Serializable;
public class Card implements Serializable{
public Card(){
}
/**
* 卡号
*/
private String cardId;
/**
* 密码
*/
private String password;
/**
* 余额
*/
private int balance;
/**
* 用户姓名
*/
private String userName;
/**
* set cardId
*/
public void setCardId(String s){
cardId = s;
}
/**
* get cardId
*/
public String getCardId(){
return this.cardId;
}
/**
* set password
*/
public void setPassword(String s){
password = s;
}
/**
* get password
*/
public String getPassword(){
return this.password;
}
/**
* set balance
*/
public void setBalance(int i){
balance = i;
}
/**
* get balance
*/
public int getBalance(){
return this.balance;
}
/**
* set userName
*/
public void setUserName(String s){
userName = s;
}
/**
* get userName
*/
public String getUserName(){
return this.userName;
}
}
__________________________________________________________________
package entity;
import java.io.Serializable;
public class Computer implements Serializable{
public Computer(){
}
/**
* 计算机号
*/
private String computerId;
/**
* 使用标志---是否在使用
*/
private int onUse;
/**
* 备注信息
*/
private String notes;
/**
* set computerId
*/
public void setComputerId(String s){
computerId = s;
}
/**
* get computerId
*/
public String getComputerId(){
return this.computerId;
}
/**
* set onUse
*/
public void setOnUse(int i){
onUse = i;
}
/**
* get onUse
*/
public int getOnUse(){
return this.onUse;
}
/**
* set notes
*/
public void setNotes(String s){
notes = s;
}
/**
* get notes
*/
public String getNotes(){
return this.notes;
}
}
__________________________________________________________________
package entity;
import java.io.Serializable;
public class Record implements Serializable{
public Record(){
}
/**
* 记录编号
*/
private int numberId;
/**
* 卡号
*/
private String cardId;
/**
* 机器号
*/
private String computerId;
/**
* 上机时间
*/
private String beginTime;
/**
* 下机时间
*/
private String endTime;
/**
* 费用
*/
private int fee;
/**
* set numberId
*/
public void setNumberId(int i){
numberId = i;
}
/**
* get numberId
*/
public int getNumberId(){
return this.numberId;
}
/**
* set cardId
*/
public void setCardId(String s){
cardId = s;
}
/**
* get cardId
*/
public String getCardId(){
return this.cardId;
}
/**
* set computerId
*/
public void setComputerId(String s){
computerId = s;
}
/**
* get computerId
*/
public String getComputerId(){
return this.computerId;
}
/**
* set beginTime
*/
public void setBeginTime(String s){
beginTime = s;
}
/**
* get beginTime
*/
public String getBeginTime(){
return this.beginTime;
}
/**
* set endTime
*/
public void setEndTime(String s){
endTime = s;
}
/**
* get endTime
*/
public String getEndTime(){
return this.endTime;
}
/**
* set fee
*/
public void setFee(int i){
fee = i;
}
/**
* get fee
*/
public int getFee(){
return this.fee;
}
}
___________________________________________________________________
在数据库中建立一个Card表 ID(帐号) Passwore()密码 balance(余额) UserName(姓名)
computer表 id(机器号) onuse(机器状态上机为1 下机为0) notes(注释)Record表用于显示上机的记录 id Cardid computerid begintime endtime fee
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -