先放个题目哈:java2021programming.pdf

首先要吐槽两个点:一个是机房的eclipse太难用了(为什么会有这么鸡肋的 Code Assistant),还有一个就是都1202年了还有人在学swing。

怎么说呢,期末第一门考试就来了个下马威(X)

直到11点机房里还是一片敲击键盘的声音…… 总体来说比去年要的工作量大一点,两个小时紧打紧敲完了,不过有不少同学都没写完。

凑巧的是文本域和下拉列表之前大作业里面用过(没错,大作业也是写swing),开卷考试准备的代码模板有个大概,算是没啥阻碍地写了过来~~,除了时间有点不够用~~。

还有是10分钟做完选择题之后一直没发题目,坐那干等;最后我都提交了才通知延长15分钟,有点搞人心态(

最后附上将近俩小时的成果:

// Main.java
import java.io.File;

// 主类
public class Main {
	// 文件路径
	public static final String inFile = System.getProperty("user.dir") + File.separator + "product.txt";
	public static final String outFile = System.getProperty("user.dir") + File.separator + "result.txt";

	public static void main(String[] args) {
		// 创建文件对象
		File in = new File(inFile);
		File out = new File(outFile);
		new FrmProduct(in, out);  // 启动GUI
	}

}

// FrmProduct.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class FrmProduct extends JFrame {
	
	private JRadioButton pidBtn; // 产品编号
	private JRadioButton numBtn; // 产品库存
	private ButtonGroup select; // 产品编号 or 产品库存
	private JComboBox<String> typeBox; // 类型
	private JTextArea jta;  // 文本域
	private JButton saveBtn;  // 保存
	
	private ArrayList<Product> list, show;  // 数据
	private File saveTo;

	public FrmProduct(File from, File to) {
		pidBtn = new JRadioButton("产品编号");
		numBtn = new JRadioButton("产品库存");
		select = new ButtonGroup();
		select.add(pidBtn);
		select.add(numBtn);
		typeBox = new JComboBox<>(Product.TYPES);
		jta = new JTextArea();
		saveBtn = new JButton("保存");
		
		saveTo = to;		
		list = new ArrayList<>();
		show = new ArrayList<>();
		
		Container content = getContentPane();
		
		// 顶部
		Box topLine = Box.createHorizontalBox();
		topLine.add(Box.createHorizontalStrut(10));
		JLabel label1 = new JLabel("排序方式:");
		topLine.add(label1);
		topLine.add(Box.createHorizontalStrut(10));
		topLine.add(pidBtn);
		topLine.add(Box.createHorizontalStrut(10));
		topLine.add(numBtn);
		topLine.add(Box.createHorizontalGlue());
		JLabel label2 = new JLabel("选择类型:");
		topLine.add(label2);
		topLine.add(Box.createHorizontalStrut(10));
		topLine.add(typeBox);
		topLine.add(Box.createHorizontalStrut(10));
		content.add(topLine, BorderLayout.NORTH);
		
		// 显示滚动条的文本域
		JScrollPane jsp = new JScrollPane(jta);
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		content.add(jsp, BorderLayout.CENTER);
		
		// 确认按钮
		Box buttomLine = Box.createHorizontalBox();
		buttomLine.add(Box.createHorizontalGlue());
		buttomLine.add(saveBtn);
		buttomLine.add(Box.createHorizontalGlue());
		content.add(buttomLine, BorderLayout.SOUTH);
		
		setSize(500, 400);
		setTitle("查看产品");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		loadData(from);  // 加载数据
		initListener();  // 初始化监听器
		
		pidBtn.setSelected(true);
		typeBox.setSelectedIndex(0);
		
		// 显示数据
		sortShown();
		setToShow();
		
		setVisible(true);
	}

	// 加载数据
	private void loadData(File file) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			String aline = null;
			while (null != (aline = br.readLine())) {
				if (aline.trim().isEmpty()) {
					continue;
				}
				Product p = new Product(aline);
				list.add(p);
				show.add(p);
			}
			br.close();
		} catch(IOException e) {
			System.err.println(e.getMessage());
		}
	}

	// 初始化监听器
	private void initListener() {
		ItemListener litener1 = new SortListener();
		pidBtn.addItemListener(litener1);
		numBtn.addItemListener(litener1);
		ItemListener listener2 = new FilterListener();
		typeBox.addItemListener(listener2);
		saveBtn.addActionListener(new ClickListener());
	}
	
	// 显示
	private void setToShow() {
		StringBuilder str = new StringBuilder();
		for (Product p : show) {
			str.append(p.toString());
			str.append("\n");
		}
		jta.setText(str.toString());
	}
	
	// 显示数据
	private void sortShown() {
		if (pidBtn.isSelected()) {
			Collections.sort(show, new ProductIdComparator());// 按照id排序的比较器
		} else if (numBtn.isSelected()) {
			Collections.sort(show, new ProductNumberComparator());// 按照库存排序的比较器
		}
	}
	
	// 排序选项监听器
	class SortListener implements ItemListener {
		@Override
		public void itemStateChanged(ItemEvent e) {
			if (e.getSource() == pidBtn) {
				sortShown();
			} else if (e.getSource() == numBtn) {
				sortShown();
			}
			setToShow();
		}
	}
	
	// 类型监听
	class FilterListener implements ItemListener {
		@Override
		public void itemStateChanged(ItemEvent e) {
			String selected = (String) typeBox.getSelectedItem();
			show.clear();
			for (Product p : list) {
				if (selected.equals("全部") || p.getType().equals(selected)) {
					show.add(p);
				}
			}
			sortShown();
			setToShow();			
		} 
	}
	
	// 点击监听
	class ClickListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveTo), "UTF-8"));
				for (Product p : show) {
					br.write(p.toString());
					br.newLine();
				}
				br.close();
			} catch(IOException e) {
				System.err.println(e.getMessage());
			}
			JOptionPane.showMessageDialog(null, "文件成功保存");
		}
	}
	
	
}

// Product.java
public class Product {
	public static String[] TYPES = { "全部", "电脑", "手机", "书籍" }; // 所有类型

	private String id;  //产品编号
	private String name; // 名称
	private String type; // 类型
	private double price; // 价格
	private int number; // 库存

	public Product(String line) {
		String[] args = line.split(";");
		id = args[0];
		name = args[1];
		type = args[2];
		price = Double.parseDouble(args[3]);
		number = Integer.parseInt(args[4]);
	}

	public String toString() {
		return id + ";" + name + ";" + type + ";" + price + ";" + number;
	}
	
	public String getType() {
		return type;
	}
	
	public String getId() {
		return id;
	}
	
	public int getNumber() {
		return number;
	}
	
}

// ProductIdComparator.java
import java.util.Comparator;

// 按照id排序的比较器
public class ProductIdComparator implements Comparator<Product> {
	@Override
	public int compare(Product arg0, Product arg1) {
		return arg0.getId().compareTo(arg1.getId());
	}
}

// ProductNumberComparator.java
import java.util.Comparator;

//按照库存排序的比较器
public class ProductNumberComparator implements Comparator<Product> {
	@Override
	public int compare(Product arg0, Product arg1) {
		return arg0.getNumber() - arg1.getNumber();
	}
}

运行结果和题目要求差不多,就不放出来了。

最后一条建议,Java这门课的重点可以调整一下,以适应当前主流的开发。