Java :个人账目管理系统
先貼上項目地址:https://github.com/sunlechuan/PersonTally
這個程序是學期末,實訓老師帶著寫的,主要功能有:1.添加賬目,2.修改賬目,3.刪除帳目,4.查詢賬目。
?
這是程序的主要界面:
?
?
類的構成:1.FileIO主要是創建新的文本文檔和寫入讀取文件。
2.Tool獲取時間,流水賬號,將文本中的String類型轉化成Vector;
3.Operation操作類添加、修改、查詢數據。
4.UI_t界面類,設置頁面,添加監聽器。
程序中有點瑕疵,如果說是存入的數據為空,就會直接報異常,這時候需要把文本文檔刪除才可以,所以我自己在‘增加’的監聽器中增加了一個if語句判斷金額輸入框和備注框是否為空,如果為空的話就跳出方法。
if(money.equals("") || remark.equals(""))
?
????{
?????????return ;
????}
?
如果想要修改數據需要在表格中輸入完成后點擊一下其他表格,再點擊一下'修改'按鈕即可。
以下是代碼:
FileIO類
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 blocke.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 blocke.printStackTrace();}return fileStr;}public String getFilename() {return filename;}public void setFilename(String filename) {this.filename = filename;}}Tool類
package com.ss.util;import java.text.SimpleDateFormat; import java.util.*;import com.slc.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]);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;} }Operation類
?
package com.ss.oper;import java.util.Vector;import com.slc.file.FileIO; import com.slc.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;} }UI_t(界面)類
package com.ss.view;import javax.swing.*; import javax.swing.table.DefaultTableModel;import com.slc.oper.Operation; import com.slc.util.Tool;import java.awt.*; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Vector;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 loteText = new JTextField();private JTextField tjText = 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, 800, 600);init();}public void init(){JScrollPane p1 = new JScrollPane(setTable());final JSplitPane p2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,setInfo(),p1);p2.addComponentListener(new ComponentListener(){@Overridepublic void componentResized(ComponentEvent arg0) {p2.setDividerLocation(0.4);}@Overridepublic void componentHidden(ComponentEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void componentMoved(ComponentEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void componentShown(ComponentEvent arg0) {// TODO Auto-generated method stub}});this.add(p2);}public static void main(String[] args) {new UI_t().setVisible(true);}/** 設置JPanel的界面*/public JPanel setInfo(){JPanel jp = new JPanel();jp.setLayout(null);JLabel label1 = new JLabel("類型");label1.setBounds(100, 50, 30, 30);jp.add(label1);typeCom.addItem("收入");typeCom.addItem("?支出");typeCom.setBounds(150, 50, 60, 30);jp.add(typeCom);JLabel label2 = new JLabel("金額");label2.setBounds(250, 50, 30, 30);jp.add(label2);moneyText.setBounds(300, 50, 60, 30);jp.add(moneyText);JLabel label3 = new JLabel("備注");label3.setBounds(380, 50, 30, 30);loteText.setBounds(430, 50, 60, 30);jp.add(label3);jp.add(loteText);JButton addBut = new JButton("增加");addBut.setBounds(520, 50, 60, 30);jp.add(addBut);addBut.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent arg0) {}@Overridepublic void mousePressed(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent arg0) {}@Overridepublic void mouseEntered(MouseEvent arg0) {}@Overridepublic void mouseClicked(MouseEvent arg0) {String type = (String)typeCom.getSelectedItem();//返回當前所選項String money = moneyText.getText();String remark = loteText.getText();Tool tool = new Tool();String time = tool.getTime();int id = tool.getNewID();String data = id + "," + type + "," + money + "," + time + "," + remark +"\n";Operation oper = new Operation();oper.add(data);select();}});JButton xgBut = new JButton("修改");xgBut.setBounds(610, 50, 60, 30);jp.add(xgBut);xgBut.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseClicked(MouseEvent e) {String data = changeTableData();Operation oper = new Operation();oper.update(data);}});JButton deleteBut = new JButton("刪除");deleteBut.setBounds(700, 50, 60, 30);jp.add(deleteBut);deleteBut.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseClicked(MouseEvent e) {int i = table.getSelectedRow();dtm.removeRow(i);String data = changeTableData();Operation oper = new Operation();oper.delete(data);}});JLabel label4 = new JLabel("按類型查詢");label4.setBounds(100, 100, 70, 30);jp.add(label4);typeCom2.addItem("");typeCom2.addItem("收入");typeCom2.addItem("支出");typeCom2.setBounds(200, 100, 60, 30);jp.add(typeCom2);JLabel label5 = new JLabel("按條件查詢");label5.setBounds(280, 100, 100, 30);jp.add(label5);tjText.setBounds(380,100,100,30);jp.add(tjText);JButton selectBut = new JButton("查詢");selectBut.setBounds(500, 100, 60, 30);jp.add(selectBut);selectBut.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic 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("備注");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<5;j++){line += dtm.getValueAt(i, j)+",";}line += "\n";data += line;}return data;} }從這個項目中學到了很多,首先是關于分包的概念(雖然我還是不會分),然后是每個功能都是一個類調用另一個基礎的類,一層層的調用,寫一個項目之前要構思好從文件或是數據庫的底層開始一層層的向上寫。我之前也寫過一個ATM的,返回來看發現自己的代碼功能要不是凌亂的在外邊,要不就是擠在了監聽器中,總之還有很多需要改進的地方。
?
總結
以上是生活随笔為你收集整理的Java :个人账目管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java(中国邮政储蓄银行个人网上银行系
- 下一篇: 第十章 client windows程