Rumah >Java >javaTutorial >案例分享用java开发实现一个记账系统(代码全)
本文是纯代码,很全,用java开发实现的记账系统,由于时间原因也没有做过多的注解,如有不清楚的可以一起讨论,代码有不足之处也欢迎指出。
java记账系统
package com.ss.file; import java.io.*; public class FileIO { private String filename = "tally.txt"; public FileIO() { File file = new File(filename); if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } public void write(String data,boolean mode) { try { FileWriter fw = new FileWriter(filename,mode); BufferedWriter bw = new BufferedWriter(fw); bw.write(data); bw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String read() { FileReader fr; String line = ""; String fileStr = ""; try { fr = new FileReader(filename);//不能一行行的读 BufferedReader br = new BufferedReader(fr);//可以一行行的读效率高 while((line = br.readLine()) != null) { fileStr += line + "\n"; } br.close(); fr.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileStr; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } }
package com.ss.oper; import java.util.Vector; import com.ss.file.FileIO; import com.ss.util.Tool; public class Operation { FileIO io = new FileIO(); public void add(String data) { io.write(data, true); } public void delete(String data) { io.write(data, false); } public void update(String data) { io.write(data, false); } /* * 全部查询 */ public Vector select() { String str = io.read(); Tool tool = new Tool(); return tool.StringToVec(str); } /* * 条件查询 */ public Vector select(String type,String remark) { Vector vecData = new Vector(); Vector vecAll = select(); for(int i = 0;i < vecAll.size();i++) { Vector smallVec = (Vector)vecAll.get(i); boolean select = (smallVec.get(1).toString().equals(type) || type.equals("")) && (smallVec.get(4).toString().equals(remark) || remark.equals("")); if(select) { vecData.add(smallVec); } } return vecData; } }
package com.ss.util; import java.text.SimpleDateFormat; import java.util.*; import com.ss.file.FileIO; public class Tool { public String getTime() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = sdf.format(date); return str; } /* * 将String转化成Vector类型 */ public Vector StringToVec(String data) { Vector bigVec = new Vector(); if(data != null && !data.equals("")) { String[] array = data.split("\n"); for(int i=0;i<array.length;i++) { String[] a = array[i].split(","); Vector smallVec = new Vector(); smallVec.add(a[0]); smallVec.add(a[1]); smallVec.add(a[2]); smallVec.add(a[3]); smallVec.add(a[4]); smallVec.add(a[5]); smallVec.add(a[6]); smallVec.add(a[7]); smallVec.add(a[8]); smallVec.add(a[9]); smallVec.add(a[10]); bigVec.add(smallVec); } } return bigVec; } /* * 获取下一个流水账号 */ public int getNewID() { int id = 1; FileIO io = new FileIO(); String data = io.read(); if(data != null && !data.equals("")) { Vector bigVec = StringToVec(data); Vector smallVecLast = (Vector)bigVec.get(bigVec.size()-1); String str = (String)smallVecLast.get(0); id = Integer.parseInt(str) + 1; } return id; } }
package com.ss.view; import javax.swing.*; import javax.swing.table.DefaultTableModel; import com.ss.oper.Operation; import com.ss.util.Tool; import java.util.Vector; import java.awt.*; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class UI_t extends JFrame{ private JComboBox<String> typeCom = new JComboBox<String>(); private JComboBox<String> typeCom2 = new JComboBox<String>(); private JTextField moneyText = new JTextField(); private JTextField stateText = new JTextField(); private JTextField tjText = new JTextField(); private JTextField kehuText = new JTextField(); private JTextField jishuText = new JTextField(); private JTextField jsmoneyText = new JTextField(); private JTextField jsfenchengText = new JTextField(); private JTextField yingliText = new JTextField(); private JTextField wanchengText = new JTextField(); private JTable table = new JTable(); private DefaultTableModel dtm = new DefaultTableModel(); private Vector<String> colName = new Vector<String>(); public UI_t(){ super("个人账目管理系统"); this.setBounds(300, 200, 1200, 800); init(); } public void init(){ JScrollPane p1 = new JScrollPane(setTable()); final JSplitPane p2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,setInfo(),p1); p2.addComponentListener(new ComponentListener(){ @Override public void componentResized(ComponentEvent arg0) { p2.setpiderLocation(0.4); } @Override public void componentHidden(ComponentEvent arg0) { // TODO Auto-generated method stub } @Override public void componentMoved(ComponentEvent arg0) { // TODO Auto-generated method stub } @Override public void componentShown(ComponentEvent arg0) { // TODO Auto-generated method stub } }); this.add(p2); } public static void main(String[] args) { // TODO Auto-generated method stub new UI_t().setVisible(true); } public JPanel setInfo(){ JPanel jp = new JPanel(); jp.setLayout(null); JLabel label1 = new JLabel("类型"); label1.setBounds(10, 50, 30, 30); jp.add(label1); typeCom.addItem("生旦净末丑"); typeCom.addItem("齐怪了"); typeCom.setBounds(50, 50, 60, 30); jp.add(typeCom); JLabel label2 = new JLabel("客户名"); label2.setBounds(120, 50, 60, 30); jp.add(label2); kehuText.setBounds(160, 50, 60, 30); jp.add(kehuText); JLabel jishulabel = new JLabel("技术名"); jishulabel.setBounds(240, 50, 60, 30); jp.add(jishulabel); jishuText.setBounds(280, 50, 60, 30); jp.add(jishuText); JLabel dingdanlabel = new JLabel("订单金额"); dingdanlabel.setBounds(340, 50, 60, 30); jp.add(dingdanlabel); moneyText.setBounds(400, 50, 60, 30); jp.add(moneyText); JLabel jsjiagelabel = new JLabel("技术价格"); jsjiagelabel.setBounds(460, 50, 60, 30); jp.add(jsjiagelabel); jsmoneyText.setBounds(520, 50, 60, 30); jp.add(jsmoneyText); JLabel jsfenchenglabel = new JLabel("技术分成"); jsfenchenglabel.setBounds(580, 50, 60, 30); jp.add(jsfenchenglabel); jsfenchengText.setBounds(640, 50, 60, 30); jp.add(jsfenchengText); JLabel yinglilabel = new JLabel("盈利"); yinglilabel.setBounds(700, 50, 40, 30); jp.add(yinglilabel); yingliText.setBounds(740, 50, 60, 30); jp.add(yingliText); JLabel wanchenglabel = new JLabel("完成时间"); wanchenglabel.setBounds(820, 50, 60, 30); jp.add(wanchenglabel); wanchengText.setBounds(880, 50, 60, 30); jp.add(wanchengText); JLabel statelabel4 = new JLabel("状态信息"); statelabel4.setBounds(940, 50, 60, 30); stateText.setBounds(1000, 50, 40, 30); jp.add(statelabel4); jp.add(stateText); JButton addBut = new JButton("增加"); addBut.setBounds(400, 100, 60, 30); jp.add(addBut); addBut.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent arg0) { } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { } @Override public void mouseEntered(MouseEvent arg0) { } @Override public void mouseClicked(MouseEvent arg0) { String type = (String)typeCom.getSelectedItem();//返回当前所选项 String kehuname=kehuText.getText(); String jishuname=jishuText.getText(); String dingdanmoney=moneyText.getText(); String jishumoney=jsmoneyText.getText(); String jishufencheng=jsfenchengText.getText(); String yingli=yingliText.getText(); String finnishdata=wanchengText.getText(); String remark = stateText.getText(); Tool tool = new Tool(); String kaishitime = tool.getTime(); int id = tool.getNewID(); //修改部分 String data = id + "," + type + "," + kehuname + "," + jishuname + "," +dingdanmoney + ","+jishumoney + ","+jishufencheng + ","+yingli + ","+kaishitime+","+finnishdata+","+remark +"\n"; Operation oper = new Operation(); oper.add(data); select(); } }); JButton xgBut = new JButton("修改"); xgBut.setBounds(500, 100, 60, 30); jp.add(xgBut); xgBut.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { String data = changeTableData(); Operation oper = new Operation(); oper.update(data); } }); JButton deleteBut = new JButton("删除"); deleteBut.setBounds(600, 100, 60, 30); jp.add(deleteBut); deleteBut.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { int i = table.getSelectedRow(); dtm.removeRow(i); String data = changeTableData(); Operation oper = new Operation(); oper.delete(data); } }); JLabel chaxunlabel = new JLabel("按店铺查询"); chaxunlabel.setBounds(100, 200, 100, 30); jp.add(chaxunlabel); typeCom2.addItem(""); typeCom2.addItem("生旦净末丑"); typeCom2.addItem("齐怪了"); typeCom2.setBounds(180, 200, 60, 30); jp.add(typeCom2); JLabel tianjiaolabel = new JLabel("按条件查询"); tianjiaolabel.setBounds(280, 200, 100, 30); jp.add(tianjiaolabel); tjText.setBounds(380,200,100,30); jp.add(tjText); JButton selectBut = new JButton("查询"); selectBut.setBounds(500, 200, 60, 30); jp.add(selectBut); selectBut.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { String type = (String) typeCom2.getSelectedItem(); String remark = tjText.getText(); Operation oper = new Operation(); Vector data = oper.select(type,remark); dtm.setDataVector(data, colName); table.setModel(dtm); } }); return jp; } public JTable setTable(){ colName.add("流水账号"); colName.add("类型"); colName.add("客户名"); colName.add("技术名"); colName.add("订单金额"); colName.add("技术价格"); colName.add("技术分成"); colName.add("盈利"); colName.add("开始时间"); colName.add("完成时间"); colName.add("状态信息"); Operation oper = new Operation(); Vector data = oper.select(); dtm.setDataVector(data, colName); table.setModel(dtm); return table; } /* * 从文件中重新读取一遍数据相当于刷新 */ public void select(){ Operation oper = new Operation(); Vector data = oper.select(); dtm.setDataVector(data, colName); table.setModel(dtm); } /* * 获取表格内容 */ public String changeTableData(){ String data = ""; int row = table.getRowCount(); for(int i=0;i<row;i++){ String line = ""; for(int j=0;j<11;j++){ line += dtm.getValueAt(i, j)+","; } line += "\n"; data += line; } return data; } }
相关文章:
Atas ialah kandungan terperinci 案例分享用java开发实现一个记账系统(代码全). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!