*家庭软件记账项目
*(简单的小项目)
基本功能:
1.查询
1.1查询所有
1.2多条件查询
1.2.1模糊查询(根据姓名)
1.2.2根据日期查询
2.添加
3.修改
4.删除
项目:
程序的主入口
package com.gjp.app;
/*
* 程序主入口
* */
import com.gjp.view.MainView;
public class MainApp {
public static void main(String[] args) {
new MainView().run();
}
}
view层
package com.gjp.view;
import java.util.List;
import java.util.Scanner;
import com.gjp.account.Account;
import com.gjp.controller.AccountController;
public class MainView {
Scanner sc = new Scanner(System.in);
AccountController controller = new AccountController();
public void run(){
while(true){
System.out.println("<<<<<<<<<<欢迎使用日常生活软件记账系统>>>>>>>>>>");
System.out.println("1.查询 2.添加 3.修改 4.删除 5.退出系统");
System.out.println("请选择需要操作的功能,输入相应的序号【1-5】:");
int choose = sc.nextInt();
switch (choose) {
case 1:
//bSystem.out.println("查询");
selectAccount();
break;
case 2:
//System.out.println("添加");
insertAccount();
break;
case 3:
//System.out.println("修改");
editAccount();
break;
case 4:
//System.out.println("删除");
deleteAccount();
break;
case 5:
System.out.println("退出系统");
System.exit(0);
break;
default:
System.out.println("你输入的序号不正确,请重新输入【1-5】的序号!");
break;
}
}
}
//删除
private void deleteAccount() {
//在删除之前,先查询所有
selectAll();
//在删除之前,要找到删除的那条记录(根据ID找)
System.out.println("你选择的是删除功能,请输入需要删除的ID:");
int zwid = sc.nextInt();
Account ac = controller.selectById(zwid);
if(ac != null){
System.out.println(ac);
int i = controller.delete(zwid);
if(i > 0){
System.out.println("删除成功!");
selectAll();
}else{
System.out.println("删除失败!");
}
}else{
System.out.println("删除的记录不存在!");
}
}
//修改
private void editAccount() {
//在修改之前,先查询所有
selectAll();
//在修改之前,要找到修改的那条记录(根据ID找)
System.out.println("你选择的是修改功能,请输入需要修改的ID:");
int zwid = sc.nextInt();
Account account = controller.selectById(zwid);
System.out.println(account);
if(account != null){
System.out.println("请按要求修改以下内容:");
System.out.println("请修改消费名称:");
String flname = sc.next();
System.out.println("请修改消费金额:");
double money = sc.nextDouble();
System.out.println("请修改支付方式:");
String zhanghu = sc.next();
System.out.println("请修改消费日期:");
String createtime = sc.next();
System.out.println("请修改消费说明:");
String description = sc.next();
Account ac = new Account(zwid, flname, money, zhanghu, createtime, description);
int i = controller.edit(ac);
if(i > 0){
System.out.println("修改成功!");
selectAll();
}else{
System.out.println("修改失败!");
}
}else{
System.out.println("修改的记录不存在!");
}
}
//添加
private void insertAccount() {
System.out.println("你选择的是添加功能,请按要求添加以下内容:");
System.out.println("请添加消费名称:");
String flname = sc.next();
System.out.println("请添加消费金额:");
double money = sc.nextDouble();
System.out.println("请添加支付方式:");
String zhanghu = sc.next();
System.out.println("请添加消费日期:");
String createtime = sc.next();
System.out.println("请添加消费说明:");
String description = sc.next();
Account ac = new Account(0, flname, money, zhanghu, createtime, description);
int i = controller.insert(ac);
if(i > 0){
System.out.println("添加成功!");
selectAll();
}else{
System.out.println("添加失败!");
}
}
//查询
private void selectAccount() {
System.out.println("请选择你需要的查询【1-2】:1.查询所有 2.多条件查询");
int choose = sc.nextInt();
switch (choose) {
case 1:
//System.out.println("查询所有");
selectAll();
break;
case 2:
//System.out.println("多条件查询");
selectDouble();
break;
default:
System.out.println("你选择的查询不存在,请重新输入【1-2】!");
break;
}
}
//多条件查询
private void selectDouble() {
System.out.println("请选择需要查询的功能【1-2】:1.模糊查询 2.根据日期查询");
int choose = sc.nextInt();
switch (choose) {
case 1:
//System.out.println("模糊查询");
select();
break;
case 2:
//System.out.println("根据日期查询");
selectDate();
break;
default:
break;
}
}
//根据日期查询
private void selectDate() {
System.out.println("请输入消费的开始时间:");
String startDate = sc.next();
System.out.println("请输入消费的截止时间:");
String endDate = sc.next();
List<Account> list = controller.selectDate(startDate,endDate);
if(list != null & list.size() != 0){
for (Account ac : list) {
System.out.println(ac.getZwid()+"\t\t"+ac.getFlname()+"\t\t"+ac.getMoney()+"\t\t"+ac.getZhanghu()+"\t\t"+ac.getCreatetime()+"\t\t"+ac.getDescription());
}
}else{
System.out.println("查询的记录不存在!");
}
}
//根据姓名模糊查询
private void select() {
System.out.println("根据姓名模糊查询:");
String name = sc.next();
List<Account> list = controller.select(name);
//把查询所有的数据打印出来
print(list);
}
//查询所有
private void selectAll() {
List<Account> list = controller.selectAll();
//把查询所有的数据打印出来
print(list);
}
//打印查询到的数据
private void print(List<Account> list) {
if(list.size() != 0 && list != null){
System.out.println("ID\t\t消费记录\t\t消费金额\t\t支付方式\t\t消费日期\t\t\t消费说明");
for (Account ac : list) {
System.out.println(ac.getZwid()+"\t\t"+ac.getFlname()+"\t\t"+ac.getMoney()+"\t\t"+ac.getZhanghu()+"\t\t"+ac.getCreatetime()+"\t\t"+ac.getDescription());
}
}else{
System.out.println("没有查询到数据!");
}
}
}
controller层
package com.gjp.controller;
import java.util.List;
import com.gjp.account.Account;
import com.gjp.service.AccountService;
import com.gjp.service.impl.AccountServiceImpl;
public class AccountController {
AccountService service = new AccountServiceImpl();
//查询所有
public List<Account> selectAll() {
return service.selectAll();
}
//模糊查询
public List<Account> select(String name) {
return service.select(name);
}
//添加
public int insert(Account ac) {
return service.insert(ac);
}
//根据ID查询
public Account selectById(int zwid) {
return service.selectById(zwid);
}
//修改
public int edit(Account ac) {
return service.edit(ac);
}
//删除
public int delete(int zwid) {
return service.delete(zwid);
}
//根据日期查询
public List<Account> selectDate(String startDate, String endDate) {
return service.selectDate(startDate,endDate);
}
}
service层(接口)
package com.gjp.service;
import java.util.List;
import com.gjp.account.Account;
public interface AccountService {
//查询所有
List<Account> selectAll();
//模糊查询
List<Account> select(String name);
//添加
int insert(Account ac);
//修改
int edit(Account ac);
//根据ID查询
Account selectById(int zwid);
//删除
int delete(int zwid);
//根据日期查询
List<Account> selectDate(String startDate, String endDate);
}
实现service层的接口
package com.gjp.service.impl;
import java.util.List;
import com.gjp.account.Account;
import com.gjp.dao.AccountDao;
import com.gjp.dao.impl.AccountDaoImpl;
import com.gjp.service.AccountService;
import com.gjp.tools.JDBCUtil;
public class AccountServiceImpl implements AccountService{
AccountDao dao = new AccountDaoImpl();
//查询所有
@Override
public List<Account> selectAll() {
return dao.selectAll();
}
//模糊查询
@Override
public List<Account> select(String name) {
return dao.select(name);
}
//添加
@Override
public int insert(Account ac) {
return dao.insert(ac);
}
//根据ID查询
@Override
public Account selectById(int zwid) {
return dao.selectById(zwid);
}
//修改
@Override
public int edit(Account ac) {
return dao.edit(ac);
}
//删除
@Override
public int delete(int zwid) {
return dao.delete(zwid);
}
//根据日期查询
@Override
public List<Account> selectDate(String startDate, String endDate) {
return dao.selectDate(startDate,endDate);
}
}
dao层
package com.gjp.dao;
import java.util.List;
import com.gjp.account.Account;
public interface AccountDao {
//查询所有
List<Account> selectAll();
//模糊查询
List<Account> select(String name);
//添加
int insert(Account ac);
//根据ID查询
Account selectById(int zwid);
//修改
int edit(Account ac);
//删除
int delete(int zwid);
//根据日期查询
List<Account> selectDate(String startDate, String endDate);
}
实现dao层的接口
package com.gjp.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 com.gjp.account.Account;
import com.gjp.dao.AccountDao;
import com.gjp.tools.JDBCUtil;
public class AccountDaoImpl implements AccountDao{
QueryRunner qr = new QueryRunner(JDBCUtil.getdDataSource());
//查询所有
@Override
public List<Account> selectAll() {
try {
String sql = "select * from account";
List<Account> list = qr.query(sql, new BeanListHandler<Account>(Account.class));
return list;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//模糊查询
@Override
public List<Account> select(String name) {
try {
String sql = "select * from account where flname like ?";
return qr.query(sql,new BeanListHandler<Account>(Account.class) ,"%"+name+"%");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//添加
@Override
public int insert(Account ac) {
try {
String sql = "insert into account values(null,?,?,?,?,?)";
Object[] params = {ac.getFlname(),ac.getMoney(),ac.getZhanghu(),ac.getCreatetime(),ac.getDescription()};
return qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
//修改
@Override
public int edit(Account ac) {
try {
String sql = "update account set flname = ?,money = ?,zhanghu = ?,createtime = ?,description = ? where zwid = ?";
Object[] params = {ac.getFlname(),ac.getMoney(),ac.getZhanghu(),ac.getCreatetime(),ac.getDescription(),ac.getZwid()};
return qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
//根据ID查询
@Override
public Account selectById(int zwid) {
try {
String sql = "select * from account where zwid = ?";
return qr.query(sql, new BeanHandler<Account>(Account.class), zwid);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//删除
@Override
public int delete(int zwid) {
try {
String sql = "delete from account where zwid = ?";
return qr.update(sql, zwid);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
//根据日期查询
@Override
public List<Account> selectDate(String startDate, String endDate) {
try {
String sql = "select * from account where createtime between ? and ?";
Object[] params = {startDate,endDate};
return qr.query(sql, new BeanListHandler<Account>(Account.class), params);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
管家婆数据库