【常用】查询输入框:一个关键字绑定多个属性(包括int、string不同的类型)

11 篇文章 0 订阅

从input获取输入一个待查询关键字key,前端不区分,传到后台再进行处理,基本逻辑如下:

//根据订单号(int)或者供应商名称(string)查询订单
if (!string.IsNullOrEmpty(key))
{
    var Id = MathTools.ToInt(key);
    if (Id > 0)
    {
        var res = details.Where(p=>p.Id==Id).ToList();
    }
    else
    {
        var res = details.Where(p=>(p.SupplierName.Contains(key))).ToList();
    }
}
//巧用Int32.Parse方法(将数字的字符串表示形式转换为其等效的32位整数。)
public class MathTools
{ 
    public static int ToInt(string str)
    {
       if (str != "" && str != null)
       {
            try
            {
                return int.Parse(str);
            }
            catch
            {
                 return 0;
            }
        }
        else return 0;
    }
}

上面的方法很垃圾........抛出异常已经到了毫秒级了,性能差!

直接用:TryParse();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Java代码实现: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.text.*; public class CalendarApp extends JFrame implements ActionListener { private JLabel dateLabel, timeLabel; private JButton prevMonthBtn, nextMonthBtn, prevYearBtn, nextYearBtn, addBtn, queryBtn, summaryBtn, exitBtn; private JComboBox<Integer> monthComboBox; private JTextField yearField, idField, typeField, costField, keywordField; private JTable accountTable; private DefaultTableModel accountTableModel; private Calendar calendar; private SimpleDateFormat dateFormat, timeFormat; private HashMap<String, Double> summaryMap; public CalendarApp() { super("Calendar App"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // 创建日期和时间标签 dateLabel = new JLabel(); timeLabel = new JLabel(); // 创建按钮 prevMonthBtn = new JButton("<<"); nextMonthBtn = new JButton(">>"); prevYearBtn = new JButton("<<<"); nextYearBtn = new JButton(">>>"); addBtn = new JButton("Add"); queryBtn = new JButton("Query"); summaryBtn = new JButton("Summary"); exitBtn = new JButton("Exit"); // 绑定按钮事件监听器 prevMonthBtn.addActionListener(this); nextMonthBtn.addActionListener(this); prevYearBtn.addActionListener(this); nextYearBtn.addActionListener(this); addBtn.addActionListener(this); queryBtn.addActionListener(this); summaryBtn.addActionListener(this); exitBtn.addActionListener(this); // 创建月份下拉框和年份文本框 monthComboBox = new JComboBox<Integer>(); for (int i = 1; i <= 12; i++) { monthComboBox.addItem(i); } yearField = new JTextField(); // 创建记账信息输入框和表格 idField = new JTextField(); typeField = new JTextField(); costField = new JTextField(); keywordField = new JTextField(); accountTableModel = new DefaultTableModel(new Object[]{"ID", "Type", "Cost", "Keyword"}, 0); accountTable = new JTable(accountTableModel); // 创建日期和时间格式化器 dateFormat = new SimpleDateFormat("yyyy年MM月dd日 E"); timeFormat = new SimpleDateFormat("HH:mm:ss"); // 创建日历对象 calendar = Calendar.getInstance(); // 初始化显示 updateUI(); } // 更新用户界面 private void updateUI() { // 更新日期和时间标签 Date date = new Date(); dateLabel.setText(dateFormat.format(calendar.getTime())); timeLabel.setText(timeFormat.format(date)); // 更新月份下拉框和年份文本框 monthComboBox.setSelectedItem(calendar.get(Calendar.MONTH) + 1); yearField.setText(Integer.toString(calendar.get(Calendar.YEAR))); // 更新记账信息表格 updateAccountTable(); // 清空记账信息输入框 idField.setText(""); typeField.setText(""); costField.setText(""); keywordField.setText(""); // 更新汇总信息 summaryMap = calculateSummary(); } // 更新记账信息表格 private void updateAccountTable() { // 清空表格 accountTableModel.setRowCount(0); // 获取当天日期 int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); // 从文件或数据库中读取记账信息 // 这里使用示例数据 Object[][] data = { {"001", "Food", 10.0, "Rice;Noodle"}, {"002", "Transport", 5.0, "Bus"}, {"003", "Shopping", 20.0, "Clothes;Shoes"}, {"004", "Food", 15.0, "Pizza"}, {"005", "Entertainment", 30.0, "Movie"} }; for (Object[] row : data) { String id = (String)row[0]; String type = (String)row[1]; Double cost = (Double)row[2]; String keyword = (String)row[3]; // 如果是当天的记录,就添加到表格中 if (year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1 && id.startsWith(Integer.toString(day))) { accountTableModel.addRow(new Object[]{id, type, cost, keyword}); } } } // 计算当月记账类型汇总信息 private HashMap<String, Double> calculateSummary() { // 获取当月日期信息 int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // 初始化汇总信息 HashMap<String, Double> summaryMap = new HashMap<String, Double>(); for (String type : new String[]{"Food", "Transport", "Shopping", "Entertainment"}) { summaryMap.put(type, 0.0); } // 遍历每天的记账记录,进行汇总 for (int i = 1; i <= days; i++) { // 从文件或数据库中读取记账信息 // 这里使用示例数据 Object[][] data = { {"001", "Food", 10.0, "Rice;Noodle"}, {"002", "Transport", 5.0, "Bus"}, {"003", "Shopping", 20.0, "Clothes;Shoes"}, {"004", "Food", 15.0, "Pizza"}, {"005", "Entertainment", 30.0, "Movie"} }; for (Object[] row : data) { String type = (String)row[1]; Double cost = (Double)row[2]; // 如果是当天的记录,就进行汇总 if (year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1 && i == Integer.parseInt((String)row[0])) { if (summaryMap.containsKey(type)) { summaryMap.put(type, summaryMap.get(type) + cost); } else { summaryMap.put(type, cost); } } } } return summaryMap; } // 处理按钮事件 public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == prevMonthBtn) { // 上一月 calendar.add(Calendar.MONTH, -1); updateUI(); } else if (source == nextMonthBtn) { // 下一月 calendar.add(Calendar.MONTH, 1); updateUI(); } else if (source == prevYearBtn) { // 上一年 calendar.add(Calendar.YEAR, -1); updateUI(); } else if (source == nextYearBtn) { // 下一年 calendar.add(Calendar.YEAR, 1); updateUI(); } else if (source == addBtn) { // 添加记账信息 int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String id = String.format("%02d%02d%s", day, accountTableModel.getRowCount() + 1, year); String type = typeField.getText(); Double cost = Double.parseDouble(costField.getText()); String keyword = keywordField.getText(); accountTableModel.addRow(new Object[]{id, type, cost, keyword}); // 将记账信息保存到文件或数据库中 System.out.println("Add account: " + id + ", " + type + ", " + cost + ", " + keyword); } else if (source == queryBtn) { // 查询记账信息 String keyword = keywordField.getText(); // 从文件或数据库中读取记账信息,并根据关键字进行过滤 // 这里使用示例数据 Object[][] data = { {"001", "Food", 10.0, "Rice;Noodle"}, {"002", "Transport", 5.0, "Bus"}, {"003", "Shopping", 20.0, "Clothes;Shoes"}, {"004", "Food", 15.0, "Pizza"}, {"005", "Entertainment", 30.0, "Movie"} }; accountTableModel.setRowCount(0); for (Object[] row : data) { String type = (String)row[1]; Double cost = (Double)row[2]; String keywords = (String)row[3]; if (keywords.contains(keyword)) { accountTableModel.addRow(new Object[]{row[0], type, cost, keywords}); } } } else if (source == summaryBtn) { // 显示记账类型汇总信息 JOptionPane.showMessageDialog(this, summaryMap, "Summary", JOptionPane.INFORMATION_MESSAGE); } else if (source == exitBtn) { // 退出程序 System.exit(0); } } public static void main(String[] args) { CalendarApp app = new CalendarApp(); app.setVisible(true); } } ``` 这个程序使用了Swing库来创建用户界面,并使用了Java日期库(java.util.Date和java.text.SimpleDateFormat)来格式化日期和时间。程序中还使用了HashMap来存储记账类型和对应的费用,并使用JOptionPane来显示汇总信息。注意,这个程序只是一个示例程序,实际应用中需要根据具体需求进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值