基于JDBC的账务管理系统(一)https://developer.aliyun.com/article/1433676
MainView类
package cn.yh.test; import cn.yh.dao.AccountDao; import cn.yh.dao.AccountDaoImpl; import cn.yh.dao.UserDao; import cn.yh.dao.UserDaoImpl; import cn.yh.entity.Account; import cn.yh.entity.User; import java.util.Date; import java.util.List; import java.util.Scanner; /* * 4.主界面的类;这里实现类,是其成员变量; * */ public class MainView { UserDao userDao=new UserDaoImpl(); AccountDao accountDao=new AccountDaoImpl(); Scanner sc=new Scanner(System.in); //为了后续,有很多的输入,直接在外面设计sc控制台输入为成员变量; //接下来是界面的设计;简单的界面;操作:登录;其他查询? //4.1构建第一个界面; public void login(){ System.out.println("----------------金算盘账务管理系统----------------"); int i=3; while(i>0) { System.out.println("请输入账户:"); String username = sc.nextLine(); System.out.println("请输入密码:"); String password = sc.nextLine(); i--; //调用实现类: User user = userDao.selectUser(username, password); if (user != null) { //登录 menu(); } else { if(i==0)break; System.out.println("账户名和密码不对!,还剩机会"+(i)+"机会"); //如果不对,则继续输入,升级改进,加循环;! } } } //4.2 构建第二个界面;针对账户,做选择操作;汇总sum(money) ;其他人;当前账户的信息,sum(money) public void menu(){ while(true){ System.out.println("*******************账务管理系统*******************"); System.out.println("请选择您的操作:1.查看余额 2.存款 3.取款 4.转账 5.查看明细 6.修改密码 7.退出"); int choice=sc.nextInt(); //获取方式,和nextLine() 以回车换行读取 ;nextInt(); switch (choice){ case 1: selectBalance();break; case 2: saveMoney();break; case 3: drawMoney();break; case 4: transferMoney();break; case 5: selectDetails();break; case 6: editPwd();break; case 7: System.exit(0); default: break; } } } /** * 修改自己的密码;由于这里是针对用户的操作,需要userDao增加方法; */ private void editPwd() { } /** * 4.7 查看明细界面;一个是查看所有详情信息;另外一个是按时间查询(开始和结束时间); */ private void selectDetails() { System.out.println("---------------账务管理系统---------------------"); System.out.println("请选择您的操作:1.查看所有信息 2.按时间查询 "); int choice=sc.nextInt(); switch (choice){ case 1: selectAll();break; case 2: selectByDate();break; } } //4.7.2条件查询;升级: // 4.7.3 根据所有收入%模糊查询; 所有奖金; // 4.7.4查询收入一共有多少前;支出一共有多少钱;group by分组; mysql // 4.7.5 排序;查询今年支出排序;order by // 子业务: 需要输入一个开始时间,一个结束时间; //输入值,由selectByDate()来接收值; private void selectByDate() { sc.nextLine(); System.out.println("请输入开始时间:"); String startTime=sc.nextLine(); System.out.println("请输入结束时间:"); String endTime=sc.nextLine(); //去调用根据时间的查询; List<Account> accountList = accountDao.selectByDate(startTime, endTime); accountList.forEach(a->{ System.out.println(a.getId()+"\t"+a.getSortname()+"\t"+a.getMoney()+"\t"+a.getType()); }); } //4.7.1查看所有信息;查看所有账户account的信息,需要去调用accountDao信息; private void selectAll() { List<Account> accountList = accountDao.selectAll(); // for (Account a : accountList) { // System.out.println(a.getId()+"\t"+a.getTypeName()+"\t"+a.getMoney()+"\t"+a.getType()); // } //stream forEach 兰布达; accountList.forEach(a->{ System.out.println(a.getId()+"\t"+a.getSortname()+"\t"+a.getMoney()+"\t"+a.getType()); }); } //4.6转账界面 private void transferMoney() { } //4.5取款界面 private void drawMoney() { } //4.4 存款界面;去对账户做处理; private void saveMoney() { System.out.println("---------------账务管理系统---------------------"); sc.nextLine(); System.out.println("请输入存款的类型:"); String typeName=sc.nextLine(); System.out.println("请输入存款金额:"); int money=sc.nextInt(); sc.nextLine(); System.out.println("请输入存款方式:(银行、支付宝、微信、现金)"); //1.xx银行;2.支付宝;3.微信;4.现金 String type=sc.nextLine(); System.out.println("请输入备注信息:"); String desc=sc.nextLine(); //去调用实体类的存款方法; //去对对象做一个封装;a:是一个账户对象; //这里需要重点来理解一下,我用的当前时间:是java系统的时间;java.util包; long nowTime = new Date().getTime(); //存到数据库的时候,java.sql.Date Account a=new Account(typeName,type,money,new java.sql.Date(nowTime),desc); int result=accountDao.saveMoney(a); //将a作为参数传递过去; if(result>0){ System.out.println("存款成功!"); //去哪里? } } //4.3 第三个界面;查看余额; private void selectBalance() { double v = accountDao.selectBalance(); System.out.println("您的余额是:"+v); } }
实体类:
package cn.yh.entity; //2.实体类User public class User { private int id; private String userName; private String password; public User() { } public User(int id, String userName, String password) { this.id = id; this.userName = userName; this.password = password; } public User(String userName, String password) { this.userName = userName; this.password = password; } public int getId() { return id; } public void setId(int 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; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + '}'; } }
Account类
package cn.yh.entity; import java.sql.Date; //后续要存到数据库;sql.Date //2.2实体类Account public class Account { private int id; private String sortname; private String type; private double money; private Date createTime; private String description; public Account() { } public Account(String sortname, String type, double money, Date createTime, String description) { this.sortname = sortname; this.type = type; this.money = money; this.createTime = createTime; this.description = description; } public Account(int id, String sortname, String type, double money, Date createTime, String description) { this.id = id; this.sortname = sortname; this.type = type; this.money = money; this.createTime = createTime; this.description = description; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSortname() { return sortname; } public void setSortname(String sortname) { this.sortname = sortname; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Account{" + "id=" + id + ", typeName='" + sortname + '\'' + ", type='" + type + '\'' + ", money=" + money + ", createTime=" + createTime + ", description='" + description + '\'' + '}'; } }
基于JDBC的账务管理系统(三)https://developer.aliyun.com/article/1433678