overtimeRecording

package net.tqm.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.tqm.control.TimeRecording;
import net.tqm.util.DayChooser;

public class TimeRecordRespective extends JFrame implements ActionListener {
	/**
	 * @author tqm
	 */
	private static final long serialVersionUID = 8831501534638274287L;

	private JLabel lblday, lblendTime;
	private JTextField jtfDay;
	private String cardFeedingTime[] = { "7:30", "7:45", "8:00", "8:30",
			"8:45", "9:00", "9:45" };
	private JComboBox jcbCardFeedingTime;
	private JTextArea jtaHistoryData;
	private JButton jbtnSubmit,jbtnQuery;
	private DayChooser chooser;

	public TimeRecordRespective() {
		this.getLayout();
		this.setLayout(new BorderLayout());
		JPanel timeRecordPanel = new JPanel();
		JPanel timeRecordViewPanel = new JPanel();
		timeRecordPanel.setLayout(new FlowLayout());
		timeRecordViewPanel.setLayout(new FlowLayout());

		jbtnSubmit = new JButton("提 交");
		jbtnQuery = new JButton("历史数据");
		jtaHistoryData = new JTextArea(10, 20);
		lblday = new JLabel("overtime日期:");
		lblendTime = new JLabel("终   时:");
		jcbCardFeedingTime = new JComboBox(cardFeedingTime);
		chooser = DayChooser.getInstance(new Date());
		jtfDay = new JTextField();
		jtfDay.setText("                              ");
		chooser.register(jtfDay);

		timeRecordPanel.add(lblday);
		timeRecordPanel.add(chooser);

		timeRecordPanel.add(lblendTime);
		timeRecordPanel.add(jcbCardFeedingTime);

		timeRecordPanel.add(jbtnSubmit);
		timeRecordPanel.add(jbtnQuery);
		timeRecordViewPanel.add(jtaHistoryData);

		jbtnSubmit.addActionListener(this);
		jbtnQuery.addActionListener(this);

		add(timeRecordPanel,BorderLayout.CENTER);
		add(timeRecordViewPanel,BorderLayout.EAST);
		setLocation(400, 300);
		setSize(400, 400);
		setTitle("overtime日期及时间记录");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				int flag = JOptionPane.showConfirmDialog(null,
						"确认关闭?", "确认!", JOptionPane.YES_NO_OPTION,
						JOptionPane.INFORMATION_MESSAGE);
				if (JOptionPane.YES_OPTION == flag) {
					System.exit(0);
				} else {
					return;
				}
			}
		});

		pack();
		setVisible(true);
	}

	public static void main(String[] args) {
		new TimeRecordRespective();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.jbtnSubmit) {
			TimeRecording.insertRecord(this.jtfDay.getText(),
					(String) this.jcbCardFeedingTime.getSelectedItem());
			this.jtfDay.setText("");
		}
		else if (e.getSource() == this.jbtnQuery) {
			TimeRecording.queryHistoryRecord(jtaHistoryData);
			jtfDay.setText("                              ");
		}
	}

}


package net.tqm.control;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import net.tqm.util.DBUtil;
import net.tqm.util.OrderedLinkedHashMap;

public class TimeRecording {
	static File file = new File("db/timeRecord");
	@SuppressWarnings("unchecked")
	static HashMap<String, String> historyRecord = (HashMap<String, String>) DBUtil.readFromFile(file);

	public static void insertRecord(String key, String value){
		if(key.trim().equals("")){
			JOptionPane.showMessageDialog(null, "请选择日期!");
			return;
		}
		if(historyRecord==null){
			historyRecord = new LinkedHashMap<String, String>();
		}
		historyRecord.put(key, value);
		historyRecord = OrderedLinkedHashMap.getOrder(historyRecord);
		DBUtil.writeToFile(historyRecord, file);
	}

	public static void queryHistoryRecord(JTextArea textArea) {
	    if(historyRecord.isEmpty())
	    {
	    	textArea.setText("");
	    	JOptionPane.showMessageDialog(null, "没有历史数据!");
	        return;
	    }
	    textArea.setText("");
		Iterator<String> dayIterator = historyRecord.keySet().iterator();
		while (dayIterator.hasNext()) {
			String key = dayIterator.next();
			textArea.append(key + " ==" + historyRecord.get(key)+"\n");
		}
	}
	
	public static void timeAmount(){//TODO 统计overtime总时间,分月份统计,修改时间,删除时间等
		
	}
}


package net.tqm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class DBUtil {
	public static void writeToFile(Object o,File file) {
		try {
			ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(
					file));
			w.writeObject(o);
			w.flush();
			w.close();
		} catch (Exception e) {
		}
	}
	
	public static Object readFromFile(File file) {
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					file));
			Object o = in.readObject();
			in.close();
			return o;
		} catch (Exception e) {
		}
		return null;
	}
}


package net.tqm.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class OrderedLinkedHashMap {
	
	
	public  static LinkedHashMap<String, String> getOrder(Map<String, String>  map){
			List<Map.Entry<String, String>> infoIds =new ArrayList<Map.Entry<String, String>>(map.entrySet());
			
			//排序
			Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {   
			    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {      
			    	String p1 = (String) o1.getKey();
			    	String p2 = (String) o2.getKey();
			    	
			    	return p1.compareTo(p2);
			    }
			}); 
			
	/*转换成新map输出*/
			LinkedHashMap<String, String> newMap = new LinkedHashMap <String, String>();
			
			for(Map.Entry<String,String> entity : infoIds){
				newMap.put(entity.getKey(), entity.getValue());
			}
			
			return newMap;
		}
	
}


 
 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值