用VS2010写一个简单的记账工具(四)综合代码

11 篇文章 0 订阅

阿贝云提供免费的虚拟主机和免费的云服务器,独享IP,高在线,免备案,永久免费续期

地址都是国内的,速度十分快,实测云服务器免费,空间蛮大。适合学习和个人试用。极力推荐

阿贝云还有 2G2H5M、15元/月 4G4H5M、25元/月 优惠云服务器 可以购买,很实惠的https://www.abeiyun.com
--------------------- 

至于申请到免费云服务器和虚拟主机以后,要干什么,请参考我的博客   https://blog.csdn.net/DLXG001
 

经过前面的步骤,小工具基本就成型了。

现在把整个代码贴出来,方便大家研究。哈哈

    

using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    //自定义引用
    using System.Text.RegularExpressions;
    using System.Data.OleDb;
    
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
    LoadData();
    }
    
    private void LoadData()
    {
    dateTimePicker_ShiJian.Text = DateTime.Now.ToShortDateString();//把今天的日期显示在时间控件里,默认的就是今天,不用每次在选了。
    GlobalData.SQL_Str = "select * from " + GlobalData.Excel_Sheet;//从数据库中查询记录
    OleDbConnection conn = new OleDbConnection(GlobalData.DbConn_Str);//打开数据库连接
    try
    {
    OleDbDataAdapter DAa = new OleDbDataAdapter(GlobalData.SQL_Str, conn);//实例化OleDbDataAdapter来读取记录
    DataTable DTt = new DataTable();//创建DataTable
    DAa.Fill(DTt);//把OleDbDataAdapter结果赋值给DataTable
    dataGridView.DataSource = DTt;//把DataTable指定为dataGridView数据源
    GlobalData.Id = DTt.Rows.Count;//获取当前已有记录条数,也就是序号,下次写入前+1即可
    GlobalData.SQL_Str = "select sum(小计) from " + GlobalData.Excel_Sheet;//从Excel中统计小计列的和
    conn.Open();
    OleDbCommand cmd_TMDr = new OleDbCommand(GlobalData.SQL_Str, conn);//执行统计sql语句
    object TM = cmd_TMDr.ExecuteScalar();//用object来存储结果
    GlobalData.TotalMoney = int.Parse(TM.ToString());//把合计结果赋值给全局变量
    conn.Close();
    label_TotalMoney.Text = "¥" + GlobalData.TotalMoney.ToString();//把合计结果显示到label中
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
    DataSet Ds = new DataSet();
    GlobalData.SQL_Str = "select 产品 from" + GlobalData.Excel_Sheet + "group by 产品";
    OleDbDataAdapter da = new OleDbDataAdapter(GlobalData.SQL_Str, conn);
    da.Fill(Ds, "产品");
    comboBox_ChanPin.DataSource = Ds.Tables["产品"];
    comboBox_ChanPin.DisplayMember = "产品";
    HuoQuDanJia();
    }
    }
    
    private void HuoQuDanJia()
    {
    if (comboBox_ChanPin.Text != "System.Data.DataRowView")
    {
    //获取产品单价NLRR
    GlobalData.SQL_Str = "select 单价 from " + GlobalData.Excel_Sheet + " where 产品= '" + comboBox_ChanPin.Text + "'";
    OleDbConnection conn = new OleDbConnection(GlobalData.DbConn_Str);
    conn.Open();
    OleDbCommand cmd_TMDr = new OleDbCommand(GlobalData.SQL_Str, conn);
    object DJ = cmd_TMDr.ExecuteScalar();
    textBox_DanJia.Text = DJ.ToString();
    conn.Close();
    return;
    }
    
    
    }
    
    private void textBox_ShuLiang_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (Char.IsControl(e.KeyChar))
    return;
    if (Char.IsDigit(e.KeyChar) && ((e.KeyChar & 0xFF) == e.KeyChar))
    return;
    e.Handled = true;
    }
    
    private void textBox_DanJia_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (Char.IsControl(e.KeyChar))
    return;
    if (Char.IsDigit(e.KeyChar) && ((e.KeyChar & 0xFF) == e.KeyChar))
    return;
    e.Handled = true;
    }
    
    private void textBox_ShuLiang_TextChanged(object sender, EventArgs e)
    {
    textBox_DanJia_TextChanged(sender, e);
    }
    
    private void textBox_DanJia_TextChanged(object sender, EventArgs e)
    {
    if (textBox_ShuLiang.Text != "" && textBox_DanJia.Text != "")
    {
    int temp = 0;
    temp = int.Parse(textBox_ShuLiang.Text.Trim()) * int.Parse(textBox_DanJia.Text.Trim());
    textBox_XiaoJi.Text = temp.ToString();
    }
    }
    
    private void button_XinZeng_Click(object sender, EventArgs e)
    {
    OleDbConnection conn = new OleDbConnection(GlobalData.DbConn_Str);//打开excel数据库
    int tempid = GlobalData.Id + 1;
    string tempDate = dateTimePicker_ShiJian.Value.Year.ToString() + "年";
    tempDate += dateTimePicker_ShiJian.Value.Month.ToString() + "月";
    tempDate += dateTimePicker_ShiJian.Value.Day.ToString() + "日";
    GlobalData.SQL_Str = "insert into " + GlobalData.Excel_Sheet + " values (" + tempid + ",'" + comboBox_ChanPin.Text + "'," + int.Parse(textBox_ShuLiang.Text.Trim()) + "," + int.Parse(textBox_DanJia.Text.Trim()) + "," + int.Parse(textBox_XiaoJi.Text.Trim()) + ",'" + textBox_JingShouRen.Text + "','" + textBox_SongHuoRen.Text + "','" + tempDate + "')";
    OleDbCommand cmd = new OleDbCommand(GlobalData.SQL_Str, conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    MessageBox.Show("添加成功!该条目序号为:【" + tempid.ToString() + "】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    LoadData();
    }
    
    private void comboBox_ChanPin_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (comboBox_ChanPin.Text != "1035硒鼓")
    {
    HuoQuDanJia();
    }
    }
    
    private void button_ChongZhi_Click(object sender, EventArgs e)
    {
    comboBox_ChanPin.Text = "";
    textBox_ShuLiang.Text = "";
    textBox_DanJia.Text = "";
    textBox_JingShouRen.Text = "";
    textBox_SongHuoRen.Text = "";
    }
    }
    }


    
Excel数据库下载:点我下载

程序源代码下载:点我下载

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单记账Java代码,主要实现了以下功能:添加账单、删除账单、展示账单、按照时间和金额排序账单。 ```java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class AccountBook { private ArrayList<Bill> bills; // 账单列表 public AccountBook() { bills = new ArrayList<>(); } // 添加账单 public void addBill(Bill bill) { bills.add(bill); } // 删除账单 public void deleteBill(int index) { if (index >= 0 && index < bills.size()) { bills.remove(index); } } // 展示账单 public void showBills() { if (bills.isEmpty()) { System.out.println("没有任何账单!"); return; } System.out.println("序号\t\t时间\t\t金额\t\t备注"); for (int i = 0; i < bills.size(); i++) { System.out.println(i + "\t\t" + bills.get(i)); } } // 按照时间排序账单 public void sortByTime() { Collections.sort(bills, new Comparator<Bill>() { @Override public int compare(Bill o1, Bill o2) { return o1.getTime().compareTo(o2.getTime()); } }); } // 按照金额排序账单 public void sortByAmount() { Collections.sort(bills, new Comparator<Bill>() { @Override public int compare(Bill o1, Bill o2) { return (int) (o1.getAmount() - o2.getAmount()); } }); } public static void main(String[] args) { AccountBook accountBook = new AccountBook(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加账单"); System.out.println("2. 删除账单"); System.out.println("3. 展示账单"); System.out.println("4. 按时间排序账单"); System.out.println("5. 按金额排序账单"); System.out.println("0. 退出程序"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("输入时间(格式:yyyy-MM-dd):"); String time = scanner.next(); System.out.print("输入金额:"); double amount = scanner.nextDouble(); System.out.print("输入备注:"); String remark = scanner.next(); Bill bill = new Bill(time, amount, remark); accountBook.addBill(bill); System.out.println("添加账单成功!"); break; case 2: System.out.print("输入要删除的账单序号:"); int index = scanner.nextInt(); accountBook.deleteBill(index); System.out.println("删除账单成功!"); break; case 3: accountBook.showBills(); break; case 4: accountBook.sortByTime(); System.out.println("按时间排序成功!"); break; case 5: accountBook.sortByAmount(); System.out.println("按金额排序成功!"); break; case 0: System.out.println("退出程序。"); System.exit(0); break; default: System.out.println("输入错误,请重新输入!"); break; } } } } class Bill { private String time; // 时间 private double amount; // 金额 private String remark; // 备注 public Bill(String time, double amount, String remark) { this.time = time; this.amount = amount; this.remark = remark; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return time + "\t\t" + amount + "\t\t" + remark; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值