package cn.hncu.mygjp;
public class MainApp {
public static void main(String[] args) {
new MainView().run();
}
}
package cn.hncu.mygjp;
import java.util.List;
import java.util.Scanner;
public class MainView {
Scanner scanner;
ZhangWuDao dao = new ZhangWuDao();
public void run() {
scanner = new Scanner(System.in);
System.out.println("---------------管家婆家庭记账软件---------------");
while (true) {
System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");
System.out.println("请输入要操作的功能序号[1-5]:");
int choose = scanner.nextInt();
switch (choose) {
case 1:
addZhangWu();
break;
case 2:
editZhangWu();
break;
case 3:
deleteZhangWu();
break;
case 4:
selectZhangWu();
break;
case 5:
System.exit(0);
default:
System.out.println("输入有误,请重新输入");
}
}
}
private void deleteZhangWu() {
selectAll();
System.out.println("请输入需要删除的账务ID");
scanner = new Scanner(System.in);
int choose = scanner.nextInt();
dao.deleteZhangWu(choose);
System.out.println("删除账务成功");
}
private void editZhangWu() {
selectAll();
System.out.println("选择的是编辑账务功能,请输入以下内容");
System.out.print("请输入ID:");
int zwid = scanner.nextInt();
scanner = new Scanner(System.in);
System.out.println("输入分类名称:");
String flname = scanner.next();
System.out.println("输入金额:");
double money = scanner.nextDouble();
System.out.println("输入账户:");
String zhanghu = scanner.next();
System.out.println("输入日期:格式XXXX-XX-xx");
String createtime = scanner.next();
System.out.println("输入具体描述:");
String description = scanner.next();
ZhangWu zw = new ZhangWu(zwid, flname, money, zhanghu, createtime, description);
dao.editZhangWu(zw);
System.out.println("编辑账务成功");
}
private void selectZhangWu() {
System.out.println("1. 查询所有 2. 条件查询");
scanner = new Scanner(System.in);
int selectChooser = scanner.nextInt();
switch (selectChooser) {
case 1:
selectAll();
break;
case 2:
select();
break;
default:
System.out.println("输入有误");
}
}
private void select() {
System.out.println("选择条件查询,输入日期格式XXXX-XX-XX");
scanner = new Scanner(System.in);
System.out.print("请输入开始日期:");
String startDate = scanner.nextLine();
System.out.print("请输入结果日期:");
String endDate = scanner.nextLine();
List list = dao.select(startDate,endDate);
if (list.size() != 0) {
print(list);
}else {
System.out.println("没有查询到数据");
}
}
private void selectAll() {
List list = dao.selectAll();
if(list.size() != 0){
print(list);
}else {
System.out.println("没有查询到数据");
}
}
private void addZhangWu() {
System.out.println("选择的是添加账务功能,请输入以下内容");
scanner = new Scanner(System.in);
System.out.println("输入分类名称:");
String flname = scanner.next();
System.out.println("输入金额:");
double money = scanner.nextDouble();
System.out.println("输入账户:");
String zhanghu = scanner.next();
System.out.println("输入日期:格式XXXX-XX-xx");
String createtime = scanner.next();
System.out.println("输入具体描述:");
String description = scanner.next();
ZhangWu zw = new ZhangWu(0, flname, money, zhanghu, createtime, description);
dao.addZhangWu(zw);
System.out.println("添加账务成功");
}
private void print(List list) {
System.out.println("ID\t\t类别\t\t账户\t\t金额\t\t时间\t\t说明");
for(ZhangWu zhangWu :list){
System.out.println(zhangWu.getZwid()+"\t\t"+zhangWu.getFlname()+"\t\t"+zhangWu.getZhanghu()+"\t\t"+
zhangWu.getMoney()+"\t\t"+zhangWu.getCreatetime()+"\t"+zhangWu.getDescription());
}
}
}
package cn.hncu.mygjp;
public class ZhangWu {
private int zwid;
private String flname;
private double money;
private String zhanghu;
private String createtime;
private String description;
public ZhangWu(){
super();
}
public ZhangWu(int zwid, String flname, double money, String zhanghu, String createtime, String description) {
super();
this.zwid = zwid;
this.flname = flname;
this.money = money;
this.zhanghu = zhanghu;
this.createtime = createtime;
this.description = description;
}
public int getZwid() {
return zwid;
}
public void setZwid(int zwid) {
this.zwid = zwid;
}
public String getFlname() {
return flname;
}
public void setFlname(String flname) {
this.flname = flname;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getZhanghu() {
return zhanghu;
}
public void setZhanghu(String zhanghu) {
this.zhanghu = zhanghu;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String toString() {
return "ZhangWu [zwid=" + zwid + ", flname=" + flname + ", money=" + money + ", zhanghu=" + zhanghu
+ ", createtime=" + createtime + ", description=" + description + "]";
}
}
package cn.hncu.mygjp;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCUtils {
private static BasicDataSource dataSource = new BasicDataSource();
static {
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/gjp");
dataSource.setUsername("root");
dataSource.setPassword("951218");
dataSource.setMaxActive(10);
dataSource.setMaxIdle(5);
dataSource.setMinIdle(2);
dataSource.setInitialSize(10);
}
public static DataSource getDataSource() {
return dataSource;
}
}
package cn.hncu.mygjp;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
public class ZhangWuDao {
private QueryRunner qRunner = new QueryRunner(JDBCUtils.getDataSource());
public void addZhangWu(ZhangWu zw) {
String sqlString = "INSERT INTO gjp_zhangwu (flname,money,zhanghu,createtime,description) VALUES(?,?,?,?,?)";
Object[] param = { zw.getFlname(), zw.getMoney(), zw.getZhanghu(), zw.getCreatetime(), zw.getDescription() };
try {
qRunner.update(sqlString, param);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("添加账务失败");
throw new RuntimeException();
}
}
public List selectAll() {
String sql = "SELECT * FROM gjp_zhangwu";
List list;
try {
list = qRunner.query(sql, new BeanListHandler<>(ZhangWu.class));
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询失败");
}
}
public List select(String startDate, String endDate) {
String sql = "SELECT * FROM gjp_zhangwu WHERE createtime BETWEEN ? AND ?";
List list;
try {
Object[] params = {startDate,endDate};
list = qRunner.query(sql,new BeanListHandler<>(ZhangWu.class),params);
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("条件查询失败");
}
}
public void editZhangWu(ZhangWu zw) {
String sql = "UPDATE gjp_zhangwu SET flname=?,money=?,zhanghu=?,createtime=?,description=? WHERE zwid=?";
Object []params = {zw.getFlname(),zw.getMoney(),zw.getZhanghu(),zw.getCreatetime(),zw.getDescription(),zw.getZwid()};
try {
qRunner.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("编辑失败");
}
}
public void deleteZhangWu(int choose) {
String sql = "DELETE FROM gjp_zhangwu WHERE zwid=?";
try {
qRunner.update(sql, choose);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("删除账务失败");
}
}
}