1.设计要求
(1)要求每个学生独立完成期末作品,在遇到问题时,同学之间可以相互讨论,但切忌复制他人程序。
(2)根据期末作品题目,自己编写程序,上机调试程序,并得到正确的运行结果。
(3)认真撰写期末作品文档,给出核心源代码以及程序的运行结果,并分析运行结果,给出遇到的问题和解决办法。
2.运行环境
Eclipse+JDK1.8及以上+MySQL
3.主要内容
要求完成一个景区售票信息管理系统,使用者能够登录并完成对信息的添加、删除、修改、查询等操作。
4.核心代码
4.1 代码结构
4.2 实体类
AdminInfo实体类AdminInfo.java
package com.sell.model;
/**
* 管理员信息
*/
public class AdminInfo {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public AdminInfo(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public AdminInfo() {
}
@Override
public String toString() {
return "AdminInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
Order实体类Order.java
package com.sell.model;
public class Order {
private Integer id;
private String cusname;
private String cardID;
private String count;
private String price;
private String buydate;
private Integer mpId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCusname() {
return cusname;
}
public void setCusname(String cusname) {
this.cusname = cusname;
}
public String getCardID() {
return cardID;
}
public void setCardID(String cardID) {
this.cardID = cardID;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getBuydate() {
return buydate;
}
public void setBuydate(String buydate) {
this.buydate = buydate;
}
public Integer getMpId() {
return mpId;
}
public void setMpId(Integer mpId) {
this.mpId = mpId;
}
public Order(Integer id, String cusname, String cardID, String count, String price, String buydate, Integer mpId) {
this.id = id;
this.cusname = cusname;
this.cardID = cardID;
this.count = count;
this.price = price;
this.buydate = buydate;
this.mpId = mpId;
}
public Order() {
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", cusname='" + cusname + '\'' +
", cardID='" + cardID + '\'' +
", count='" + count + '\'' +
", price='" + price + '\'' +
", buydate='" + buydate + '\'' +
", mpId=" + mpId +
'}';
}
}
Ticket实体类Ticket.java
package com.sell.model;
public class Ticket {
private Integer id;
private String name;
private String price;
private Integer sum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Integer getSum() {
return sum;
}
public void setSum(Integer sum) {
this.sum = sum;
}
public Ticket(Integer id, String name, String price, Integer sum) {
this.id = id;
this.name = name;
this.price = price;
this.sum = sum;
}
public Ticket() {
}
@Override
public String toString() {
return "Ticket{" +
"id=" + id +
", name='" + name + '\'' +
", price='" + price + '\'' +
", sum=" + sum +
'}';
}
}
4.3 操作数据库的代码
(1)获得连接、关闭代码Db.java
package com.sell.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Db {
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/a?characterEncoding=utf-8","root","rootfenfen");
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败!");
}
return connection;
}
/**
* 关闭资源
* @param connection
* @param statement
* @param resultSet
*/
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
try {
if(resultSet!=null){
resultSet.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
(1)管理员登录代码
package com.sell.dao;
import com.sell.db.Db;
import com.sell.model.AdminInfo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AdminInfoDaoImpl implements AdminInfoDao {
@Override
public AdminInfo login(AdminInfo adminInfo) throws SQLException {
AdminInfo adminInfo1 = new AdminInfo();
String sql = "select * from admininfo where username = 'admin' and password='123456'";
Connection connection = Db.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
adminInfo1.setId(resultSet.getInt(1));
adminInfo1.setUsername(resultSet.getString(2));
adminInfo1.setPassword(resultSet.getString(3));
}
return adminInfo1;
}
}
- 门票表的操作代码
package com.sell.dao;
import com.sell.db.Db;
import com.sell.model.Ticket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class TicketDaoImpl implements TicketDao{
@Override
public List<Ticket> selectTickets(Ticket ticket) throws SQLException {
List list=new ArrayList();
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql="select * from s_ticket where 1=1";
if(ticket!=null){
if(ticket.getName()!=null){
sql+=" and name like '%"+ticket.getName()+"%'";
}
}
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
Ticket ticket1=new Ticket();
ticket1.setId(resultSet.getInt(1));
ticket1.setName(resultSet.getString(2));
ticket1.setPrice(resultSet.getString(3));
ticket1.setSum(resultSet.getInt(4));
list.add(ticket1);
}
return list;
}
@Override
public Ticket selectTicketById(Ticket ticket) throws SQLException {
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql="select * from s_ticket where id="+ticket.getId();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
ticket.setId(resultSet.getInt(1));
ticket.setName(resultSet.getString(2));
ticket.setPrice(resultSet.getString(3));
ticket.setSum(resultSet.getInt(4));
}
return ticket;
}
@Override
public int insertTicketById(Ticket ticket) throws SQLException {
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql = "INSERT INTO s_ticket (name,price,sum) VALUES('"+ticket.getName()+"','"+ticket.getPrice()+"',"+ticket.getSum()+")";
return statement.executeUpdate(sql);
}
@Override
public int updateTicketByid(Ticket ticket) throws SQLException {
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql="UPDATE s_ticket SET name='"+ticket.getName()+"', price='"+ticket.getPrice()+"',sum="+ticket.getSum()+" WHERE id="+ticket.getId();
return statement.executeUpdate(sql);
}
@Override
public int deleteTicketById(Ticket ticket) throws SQLException {
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql="delete from s_ticket where id="+ticket.getId();
return statement.executeUpdate(sql);
}
}
(4)订单表的操作代码
package com.sell.dao;
import com.sell.db.Db;
import com.sell.model.Order;
import com.sell.model.Ticket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class OrderDaoImpl implements OrderDao{
@Override
public List selectOrders(Order order) throws SQLException {
List list=new ArrayList();
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql="select * from s_ticket where 1=1";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
Order order1=new Order();
order1.setId(resultSet.getInt(1));
order1.setCusname(resultSet.getString(2));
order1.setCardID(resultSet.getString(3));
order1.setCount(resultSet.getString(4));
order1.setPrice(resultSet.getString(5));
order1.setBuydate(resultSet.getString(6));
list.add(order1);
}
return list;
}
@Override
public int insertOrder(Order order) throws SQLException {
Connection connection = Db.getConnection();
Statement statement =connection.createStatement();
String sql = "INSERT INTO orderinfo (cusname, cardID,count,price,buydate,menpiaoid) VALUES('"+order.getCusname()+"', '"+order.getCardID()+"',"+order.getCount()+",'"+order.getPrice()+"','"+order.getBuydate()+"','"+order.getMpId()+"')";
return statement.executeUpdate(sql);
}
}
4.4 主类run.java
package com.sell.run;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import com.sell.dao.AdminInfoDao;
import com.sell.dao.AdminInfoDaoImpl;
import com.sell.dao.OrderDao;
import com.sell.dao.OrderDaoImpl;
import com.sell.dao.TicketDao;
import com.sell.dao.TicketDaoImpl;
import com.sell.model.AdminInfo;
import com.sell.model.Order;
import com.sell.model.Ticket;
public class Run {
public static void main(String[] args) throws SQLException {
Run run=new Run();
run.runlogin();
}
public void runlogin() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎登录××××景区售票系统");
System.out.println("请输入账号:");
String account=scanner.next();
System.out.println("请输入密码:");
String password=scanner.next();
AdminInfo adminInfo = new AdminInfo();
adminInfo.setUsername(account);
adminInfo.setPassword(password);
AdminInfoDao adminInfoDao=new AdminInfoDaoImpl();
adminInfo=adminInfoDao.login(adminInfo);
if(adminInfo.getId()!=0){
//登录成功
System.out.println("登录成功");
success();
}else{
//成功失败
System.out.println("登录失败!");
}
}
public void success() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("1.添加门票信息");
System.out.println("2.删除门票信息");
System.out.println("3.修改门票信息");
System.out.println("4.查询门票信息");
System.out.println("5.浏览门票信息");
System.out.println("6.出售门票");
System.out.println("7.退出系统");
System.out.println("请输入1-8");
int flag=scanner.nextInt();
TicketDao ticketDao=new TicketDaoImpl();
OrderDao orderDao=new OrderDaoImpl();
if(flag==1){
//添加门票信息
System.out.println("门票名称");
String mp=scanner.next();
System.out.println("门票价格");
String price=scanner.next();
System.out.println("门票数量");
Ticket ticket1=new Ticket();
int sum=scanner.nextInt();
ticket1.setName(mp);
ticket1.setPrice(price);
ticket1.setSum(sum);
int insertFlag=ticketDao.insertTicketById(ticket1);
if(insertFlag==1){
//新增成功
System.out.println("新增成功");
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
if(status.equals("y")){
success();
}
}else{
//新增失败
System.out.println("新增失败");
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
}
}else if(flag==2){
//删除
List tickets=ticketDao.selectTickets(null);
System.out.println("门票id 名称 价格 库存");
for(int i=0;i<tickets.size();i++){
Ticket ticket=(Ticket)tickets.get(i);
System.out.println(ticket.getId()+" "+ticket.getName()+" "+ticket.getPrice()+" "+ticket.getSum());
}
System.out.println("请输入删除的id");
int id=scanner.nextInt();
Ticket ticket1=new Ticket();
ticket1.setId(id);
int deleteFlag=ticketDao.deleteTicketById(ticket1);
if(deleteFlag==1){
//删除成功
System.out.println("删除成功");
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
if(status.equals("y")){
success();
}
}else{
//删除失败
System.out.println("删除失败");
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
}
}else if(flag==3){
//修改
List tickets=ticketDao.selectTickets(null);
System.out.println("门票id 名称 价格 库存");
for(int i=0;i<tickets.size();i++){
Ticket ticket=(Ticket)tickets.get(i);
System.out.println(ticket.getId()+" "+ticket.getName()+" "+ticket.getPrice()+" "+ticket.getSum());
}
System.out.println("请输入修改的id");
int id=scanner.nextInt();
Ticket ticket1=new Ticket();
ticket1.setId(id);
System.out.println("门票名称");
String mp=scanner.next();
System.out.println("门票价格");
String price=scanner.next();
System.out.println("门票数量");
int sum=scanner.nextInt();
ticket1.setName(mp);
ticket1.setPrice(price);
ticket1.setSum(sum);
int deleteFlag=ticketDao.updateTicketByid(ticket1);
if(deleteFlag==1){
//修改成功
System.out.println("修改成功");
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
if(status.equals("y")){
success();
}
}else{
//修改失败
System.out.println("修改失败");
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
}
}else if(flag==4){
//查询
Ticket ticket1=new Ticket();
System.out.println("请输入门票名称:");
String keyname=scanner.next();
ticket1.setName(keyname);
List tickets=ticketDao.selectTickets(ticket1);
System.out.println("门票id 名称 价格 库存");
for(int i=0;i<tickets.size();i++){
Ticket ticket=(Ticket)tickets.get(i);
System.out.println(ticket.getId()+" "+ticket.getName()+" "+ticket.getPrice()+" "+ticket.getSum());
}
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
if(status.equals("y")){
success();
}
}else if(flag==5){
//浏览
List tickets=ticketDao.selectTickets(null);
System.out.println("门票id 名称 价格 库存");
for(int i=0;i<tickets.size();i++){
Ticket ticket=(Ticket)tickets.get(i);
System.out.println(ticket.getId()+" "+ticket.getName()+" "+ticket.getPrice()+" "+ticket.getSum());
}
System.out.println("是否返回上一级:y/n");
String status=scanner.next();
if(status.equals("y")){
success();
}
}else if(flag==6){
//出售
List tickets=ticketDao.selectTickets(null);
System.out.println("门票id 名称 价格 库存");
for(int i=0;i<tickets.size();i++){
Ticket ticket=(Ticket)tickets.get(i);
System.out.println(ticket.getId()+" "+ticket.getName()+" "+ticket.getPrice()+" "+ticket.getSum());
}
System.out.println("请输入购买门票id");
int status=scanner.nextInt();
Ticket ticket=new Ticket();
ticket.setId(status);
ticket=ticketDao.selectTicketById(ticket);
System.out.println("请输入购票人名:");
String gprm=scanner.next();
System.out.println("请输入身份证号:");
String sfz=scanner.next();
System.out.println("请输入票数:");
int ps=scanner.nextInt();
System.out.println("请输入购票时间:");
String sj=scanner.next();
Order order=new Order();
order.setCusname(gprm);
order.setCardID(sfz);
order.setCount(String.valueOf(ps));
order.setPrice(String.valueOf(ps*Double.valueOf(ticket.getPrice())));
order.setBuydate(sj);
order.setMpId(status);
orderDao.insertOrder(order);
//根据id查询票数
ticket.setSum(ticket.getSum()-ps);
ticketDao.updateTicketByid(ticket);
System.out.println("出票成功");
System.out.println("是否返回上一级:y/n");
String status1=scanner.next();
if(status1.equals("y")){
success();
}
}else if(flag==7){
System.out.println("退出成功");
runlogin();
}
}
}
5.运行结果与分析
(1)登录
(2) 添加
(3)删除
(4)修改
(5)查询
(6)浏览
(7)出售门票
(8)退出