读书人

搜寻二叉树的应用

发布时间: 2013-03-13 10:56:58 作者: rapoo

搜索二叉树的应用


左侧为仍然拥有的东西以及每个东西的数量,右侧是已经借出的东西,这些东西从上至下按字母顺序排列,Tool文本框输入你想要借的东西,

底端两个按钮分别负责借和还,底端右侧文本框用来显示操作状态。

搜寻二叉树的应用

当输入java后点击rent按钮,效果如上图所示。

搜寻二叉树的应用

输入php点击rent若干次,如上图

搜寻二叉树的应用

输入java点击return效果如上图

搜寻二叉树的应用

假如php被借完了,再次输入php点击rent则显示已经没有php了


附上源代码:

package demo;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;import java.util.Scanner;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import java.util.Iterator;public class View extends JFrame implements ActionListener{public static final int HEIGHT=300;public static final int WIDTH=400;private JTextField inputToolField;private JTextArea tools;private JTextArea rentals;private JButton rentButton;private JButton returnButton;private JTextField showInformationField;private Stree toolsTree;private Stree rentalsTree;private PrintWriter outputStream=null;private Scanner inputStream=null;public View(){setTitle("Rent and Return");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(WIDTH, HEIGHT);this.setLocation(430, 100);setLayout(new BorderLayout());JPanel inputPanel=new JPanel();inputPanel.setLayout(new FlowLayout());JLabel toolLabel=new JLabel("Tool");inputToolField=new JTextField(20);inputPanel.add(toolLabel);inputPanel.add(inputToolField);JPanel showPanel=new JPanel();showPanel.setLayout(new GridLayout(1,2));tools=new JTextArea(5,10);JScrollPane scrollpane=new JScrollPane(tools);scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);rentals=new JTextArea(5,10);showPanel.add(scrollpane);JScrollPane scrollpaneRentals=new JScrollPane(rentals);scrollpaneRentals.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);scrollpaneRentals.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);showPanel.add(scrollpaneRentals);JPanel operationPanel=new JPanel();rentButton=new JButton("Rent");rentButton.addActionListener(this);returnButton=new JButton("Return");returnButton.addActionListener(this);showInformationField=new JTextField(20);operationPanel.add(rentButton);operationPanel.add(returnButton);operationPanel.add(showInformationField);add(inputPanel,BorderLayout.NORTH);add(showPanel,BorderLayout.CENTER);add(operationPanel,BorderLayout.SOUTH);this.addWindowListener(new WindowsCloseListener());toolsTree=new Stree();rentalsTree=new Stree();}private class WindowsCloseListener extends WindowAdapter{public void windowClosing(WindowEvent e){try {PrintWriter outputStream=new PrintWriter("tools.txt");Iterator it=toolsTree.iterator();String content="";while(it.hasNext()){Tool tool=(Tool)it.next();while(toolsTree.remove(tool)){content+=tool.getName()+"\n";toolsTree.remove(tool);}}outputStream.println(content);outputStream.close();outputStream=new PrintWriter("rentals.txt");it=rentalsTree.iterator();content="";while(it.hasNext()){Tool tool=(Tool)it.next();while(rentalsTree.remove(tool)){content+=tool.getName()+"\n";rentalsTree.remove(tool);}}outputStream.println(content);outputStream.close();} catch (FileNotFoundException e1) {e1.printStackTrace();}}}public static void main(String[]args){View view=new View();view.setVisible(true);view.setupInventory(view.toolsTree);}public void setupInventory(Stree t) {try {inputStream=new Scanner(new File("tools.txt"));while(inputStream.hasNext()){Tool tool=new Tool(inputStream.next());t.add(tool);}inputStream.close();Iterator it=t.iterator();String content="";while(it.hasNext()){content+=it.next()+"\n";}tools.setText(content);inputStream=new Scanner(new File("rentals.txt"));while(inputStream.hasNext()){Tool tool=new Tool(inputStream.next());rentalsTree.add(tool);}inputStream.close();it=rentalsTree.iterator();content="";while(it.hasNext()){content+=it.next()+"\n";}rentals.setText(content);} catch (FileNotFoundException e) {e.printStackTrace();}}public void actionPerformed(ActionEvent e) {String command=e.getActionCommand();Tool tool;if(command.equals("Rent")){String rentItem=inputToolField.getText();tool=new Tool(rentItem);if(toolsTree.remove(tool)){Iterator toolsit=toolsTree.iterator();String content="";while(toolsit.hasNext()){content+=toolsit.next().toString()+"\n";}tools.setText(content);rentalsTree.add(tool);showInformationField.setText("RENTED : "+tool);}else{showInformationField.setText("OUT OF STOCK : "+tool);}Iterator rentalsit=rentalsTree.iterator();String content="";while(rentalsit.hasNext()){content+=rentalsit.next().toString()+"\n";}rentals.setText(content);}else if(command.equals("Return")){String returnItem=inputToolField.getText();tool=new Tool(returnItem);if(rentalsTree.remove(tool)){toolsTree.add(tool);showInformationField.setText("RETURNED : "+tool);}else{showInformationField.setText("OUT OF STOCK : "+tool);}Iterator toolsit=toolsTree.iterator();Iterator rentalsit=rentalsTree.iterator();String content="";while(rentalsit.hasNext()){content+=rentalsit.next().toString()+"\n";}rentals.setText(content);content="";while(toolsit.hasNext()){content+=toolsit.next().toString()+"\n";}tools.setText(content);}}}


读书人网 >编程

热点推荐