Java实现管理系统

public class Util {
	private static DecimalFormat formatTool=new DecimalFormat("#,###.00");
	//将用户输入实数转化为字符串类型的货币值
	public static String dollar(double num){
		return "¥"+formatTool.format(num);
	}
}
public class List {
	//定义数组可以包含对象的最大个数
	private int maxItems=100;
	//该数组包含对象的实际个数
	private int numItems=0;
	//定义该类中用来存储对象的数组
	protected Object[] list=null;
	//类的构造方法
	public List(){
		//初始化数组
		list=new Object[maxItems];
	}
	//该构造方法,可以修改数组可以包含对象的最大个数
	public List(int _maxItems){
		maxItems=_maxItems;
		list=new Object[maxItems];
	}
	public void add(Object obj){
		//假定还有空间增加一个对象
		list[numItems]=obj;
		numItems++;
	}
	public void delete(int pos){
		//假设pos在0与numItems之间
		for(int i=pos+1;i<numItems;i++){
			list[i-1]=list[i];
		}
		numItems--;
	}
	//检索数组中pos位置的对象
	public Object get(int pos){
		return list[pos];
	}
	//返回数组中包含对象的个数
	public int getSize(){
		return numItems;
	}
	//判断数组是否满
	public boolean isFull(){
		return(numItems>=maxItems);
	}
	//以string形式返回list类
	public String toString(){
		String s=new String();
		for(int i=0;i<numItems;i++){
			s+="\n"+list[i].toString();
		}
		return s+"\n";
	}
}
public interface Comparable {
public abstract int compare(Object obj1,Object obj2);
}
public class FlexSorter {
	//返回A[start],A[start+1]...A[stop-1]中最小元素的序号
	private static int findMin(Object[] A,int start,int stop,Comparable comp){
		int min=start;
		for(int i=start+1;i<stop;i++){
			if(comp.compare(A[i], A[min])<0)
				min=i;
		}
		return min;
	}
	//交换A[pos1],WITH A[pos2]
	private static void swap(Object[] A,int pos1,int pos2){
		Object temp=A[pos1];
		A[pos1]=A[pos2];
		A[pos2]=temp;
	}
	//选择排序算法
	public static void sort(Object[] A,int stop,Comparable comp){
		for(int i=0;i<stop;i++){
			swap(A, i, findMin(A, i, stop, comp));
		}
	}
	public static void sort(Object[] A,Comparable comp){
		sort(A,A.length,comp);
	}
}
public class SortedList extends List{
	//排序时用来进行比较时的接口
	private Comparable comp;
	//类的构造方法,以comparable为参数
	public SortedList(Comparable _comp){
		comp=_comp;
	}
	//类的构造方法,以可以存储对象的最大对象个数和_comp为参数
	public SortedList(int _maxItems,Comparable _comp){
		super(_maxItems);
		comp=_comp;
	}
	//向数组中添加一个对象的同时,对数组中的对象进行排序
	public void add(Object obj){
		super.add(obj);
		FlexSorter.sort(list, getSize(),comp);
	}
	//返回对象在数组中的位置否则返回-1
	public int indexOf(Object obj){
		for(int i=0;i<getSize();i++){
			if(comp.compare(get(i),obj)==0)
				return i;
		}
		return -1;
	}
}
public class Item {
	//条目的id desc
	protected String ID,desc;
	//条目的价格
	protected double price;
	//此条目中商品的数量
	protected int units;
	public Item(String _ID){
		ID=_ID;
	}
	//返回条目的字符串表示形式
	public String toString(){
		return ID+"\t"+units+" units X"+Util.dollar(price)+"\t";
	}
}
public class InventoryItem extends Item{
	public InventoryItem(String _ID,String _desc,double _price,int _units) {
		super(_ID);
		desc=_desc;
		price=_price;
		units=_units;
	}
	//在存货条目中减少商品的数量
	public void remove(int num){
		units-=num;
	}
	//在存货条目中增加商品数量
	public void add(int num){
		units+=num;
	}
	//以字符串形式返回存货条目的描述
	public String toString(){
		return super.toString()+"\t"+desc;
	}
	
}
public class InvoiceItem extends Item{
	//提货条目的构造方法,以相关的存货条目和提货数量为参数
	public InvoiceItem(InventoryItem item,int _units) {
		 super(item.ID);
		 desc=item.desc;
		 price=item.price;
		 units=_units;
		 total=price*units;
	}
	//定义提货清单中每个条目的价格
	protected double total;
	//以字符串形式返回提货清单中的一条提货条目
	public String toString(){
		return super.toString()+"="+Util.dollar(total)+"\t("+desc+")";
	}
}
public class ItemComparer implements Comparable{
	@Override
	public int compare(Object obj1, Object obj2) {
		if((obj1 instanceof Item)&&(obj2 instanceof Item)){
			Item item1=(Item)obj1;
			Item item2=(Item)obj2;
			return item1.ID.compareTo(item2.ID);
		}
		else
		 return -1;
	}
	
}
public class Inventory extends SortedList{
	//存货清单的构造函数
	public Inventory(){
		super(4,new ItemComparer());
		add(new InventoryItem("0001", "电池", 2.30, 15));
		add(new InventoryItem("0002", "彩电", 1280.00, 10));
		add(new InventoryItem("0015", "电扇", 128.00, 10));
		add(new InventoryItem("2001", "台灯", 32.00, 20));
	}
	//根据用户输入的ID,返回相应的存货条目,如果不存在返回null
	public InventoryItem getPart(String _id){
		int index=indexOf(new Item(_id));
		if(index>=0)
			return (InventoryItem)get(index);
		else
			return null;
	}
	//在存货清单中,根据用户输入的id和用户输入的数量增加响应商品的数量
	public void addUnits(String _id,int _number){
		InventoryItem item=getPart(_id);
		if(item!=null)
			item.add(_number);
		else
			JOptionPane.showMessageDialog(null, "没有找到和输入ID对应的项","警告",JOptionPane.ERROR_MESSAGE);
	}
	//在存货清单中,根据用户输入的id和用户输入的数量减少相应商品的数量
	public void removeUnits(String _id,int _number){
		InventoryItem item=getPart(_id);
		if((item!=null)&&(item.units>=_number)&&(_number>=0))
			item.remove(_number);
		else
			JOptionPane.showMessageDialog(null, "没有找到和输入ID对应的项","警告",JOptionPane.ERROR_MESSAGE);
	}
}
public class Invoice extends SortedList {
	//定义与提货清单关联的存货清单
	private Inventory inventory;
	//提货清单构造函数
	public Invoice(Inventory _inventory){
		super(new ItemComparer());
		inventory=_inventory;
	}
	//在提货清单中增加提货条目,同时在存货清单中对应的存货条目上减少用户输入的商品数量
	public void addItem(String _id,int _number){
		InventoryItem item=inventory.getPart(_id);
		if((item!=null)&&(item.units>=_number)&&(_number>=0)&&!isFull()){
			item.remove(_number);
			add(new InvoiceItem(item, _number));
		}else{
			JOptionPane.showMessageDialog(null, "列表满,项没有找到或者商品已经订购完","警告",JOptionPane.ERROR_MESSAGE);;
			System.out.println("List full,item not found ot invalid units");
		}
	}
	//根据用户输入的id在提货清单上删除提货条目
	public void delItem(String _id){
		int index=indexOf(new Item(_id));
		if(index>=0)
			delete(index);
	}
	//返回提货清单上商品的价格总和
	public double getTotal(){
		double total=0.0;
		for(int i=0;i<getSize();i++){
			total+=((InvoiceItem)get(i)).total;
		}
		return total;
	}
	//以字符串形式返回提货清单
	public String toString(){
		return "提货清单"+Util.dollar(getTotal())+super.toString();
	}
}

public class MainFrame extends JFrame{
	private Inventory inventory=new Inventory();
	private Invoice invoice=new Invoice(inventory);
	final JTextArea textArea_1=new JTextArea();//显示存货单的信息
	final JTextArea textArea_2=new JTextArea();//显示提货单的信息
	public static void main(String args[]){
		MainFrame frame=new MainFrame();
		frame.setBounds(100,100,450,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	public MainFrame(){
		super();
		setTitle("商品库存管理系统");
		final JTabbedPane tabbedPane=new JTabbedPane();
		final JPanel panel_1=new JPanel();
		final JPanel panel_2=new JPanel();
		final JScrollPane scrollPane_1=new JScrollPane(textArea_1);
		final JScrollPane scrollPane_2=new JScrollPane(textArea_2);
		final JTextField id_1=new JTextField(4);//存货单某项的id
		final JTextField number_1=new JTextField(4);//欲修改的存货单的某项的数量
		final JTextField id_2=new JTextField(4);//提货单的id
		final JTextField number_2=new JTextField(4);//欲修改的提货单的某项的数量
		final JPanel panelButton_1=new JPanel();
		final JPanel panelButton_2=new JPanel();
		final JPanel panelInput_1=new JPanel();
		final JPanel panelInput_2=new JPanel();
		final JButton inventoryAddButton=new JButton();
		final JButton inventoryRemoveButton=new JButton();
		final JButton invoiceAddButton=new JButton();
		final JButton invoiceDeleteButton=new JButton();
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		tabbedPane.add("存货单",panel_1);
		tabbedPane.add("提货单",panel_2);
		
		panel_1.setLayout(new BorderLayout());
		panel_1.add(scrollPane_1,BorderLayout.CENTER);
		panel_1.add(panelButton_1, BorderLayout.SOUTH);
		panel_1.add(panelInput_1, BorderLayout.NORTH);
		panelInput_1.add(new JLabel("存货单某项的id:"));
		panelInput_1.add(id_1);
		panelInput_1.add(new JLabel("商品数量:"));
		panelInput_1.add(number_1);
		panelButton_1.add(inventoryAddButton);
		inventoryAddButton.setText("增加商品数量");
		panelButton_1.add(inventoryRemoveButton);
		inventoryRemoveButton.setText("减少商品数量");
		textArea_1.setEditable(false);
		scrollPane_1.setViewportView(textArea_1);
		
		
		panel_2.setLayout(new BorderLayout());
		panel_2.add(scrollPane_2,BorderLayout.CENTER);
		panel_2.add(panelButton_2, BorderLayout.SOUTH);
		panel_2.add(panelInput_2, BorderLayout.NORTH);
		panelInput_2.add(new JLabel("提货单某项的id:"));
		panelInput_2.add(id_2);
		panelInput_2.add(new JLabel("商品数量:"));
		panelInput_2.add(number_2);
		panelButton_2.add(invoiceAddButton);
		invoiceAddButton.setText("增加提货单");
		panelButton_2.add(invoiceDeleteButton);
		invoiceDeleteButton.setText("删除提货单");
		textArea_2.setEditable(false);
		scrollPane_2.setViewportView(textArea_2);
		
		refreshInventory();
		refreshInvoice();
		
		inventoryAddButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					inventory.addUnits(id_1.getText(),Integer.parseInt(number_1.getText()));
					id_1.setText(null);
					number_1.setText(null);
				}catch(NumberFormatException e1){
					JOptionPane.showMessageDialog(null, "输入的商品数量错误","警告",JOptionPane.ERROR_MESSAGE);
				}
				refreshInventory();
			}		
		});
		inventoryRemoveButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					inventory.removeUnits(id_1.getText(),Integer.parseInt(number_1.getText()));
					id_1.setText(null);
					number_1.setText(null);
				}catch(NumberFormatException e1){
					JOptionPane.showMessageDialog(null, "输入的商品数量错误","警告",JOptionPane.ERROR_MESSAGE);
				}
				refreshInventory();
			}
			
		});
		invoiceAddButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					invoice.addItem(id_2.getText(),Integer.parseInt(number_2.getText()));
					id_2.setText(null);
					number_2.setText(null);
				}catch(NumberFormatException e1){
					JOptionPane.showMessageDialog(null, "输入的商品数量错误","警告",JOptionPane.ERROR_MESSAGE);
				}
				refreshInventory();
				refreshInvoice();
			}
			
		});
		invoiceDeleteButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				invoice.delItem(id_2.getText());
				id_2.setText(null);
				number_2.setText(null);
				refreshInvoice();
			}
			
		});
	}
	//刷新存货单的显示结果
	public void refreshInventory(){
		String str=null;
		for(int i=0;i<inventory.list.length;i++){
			str=inventory.toString()+"\n";
		}
		textArea_1.setText(str);
	}
	//刷新提货单的显示结果
	public void refreshInvoice(){
		String str=null;
		for(int i=0;i<invoice.list.length;i++){
			str=invoice.toString()+"\n";
		}
		textArea_2.setText(str);
	}
}




相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页