當前位置:首頁 » 操作系統 » Java的hashtable的源碼

Java的hashtable的源碼

發布時間: 2023-09-23 21:51:16

① 求java編寫的倉庫管理系統源代碼或詳細設計

import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
class 商品 extends Panel
{String 代號,名稱;int 庫存;float 單價;
商品(String 代號,String 名稱,int 庫存,float 單價)
{this.代號=代號;this.名稱=名稱;this.庫存=庫存;this.單價=單價;
}
}
class ShowWin extends JFrame implements ActionListener
{ Hashtable hashtable=null;
JTextField 代號文本框=new JTextField(),
名稱文本框=new JTextField(),
庫存文本框=new JTextField(),
單價文本框=new JTextField(),
查詢文本框=new JTextField(),
查詢信息文本框=new JTextField(),
刪除文本框=new JTextField();
JButton b_add=new JButton("添加商品"),
b_del=new JButton("刪除商品"),
b_xun=new JButton("查詢商品"),
b_xiu=new JButton("修改商品"),
b_show=new JButton("顯示商品清單");
JTextArea 顯示區=new JTextArea(25,10);
ShowWin()
{super("倉庫管理窗口");
hashtable=new Hashtable();
Container con=getContentPane();
JScrollPane pane=new JScrollPane(顯示區);
顯示區.setEditable(false);
JPanel save=new JPanel();
save.setLayout(new GridLayout(8,2));
save.add(new Label("輸入代號:"));
save.add(代號文本框);
save.add(new Label("輸入名稱:"));
save.add(名稱文本框);
save.add(new Label("輸入庫存:"));
save.add(庫存文本框);
save.add(new Label("輸入單價:"));
save.add(單價文本框);
save.add(new Label("單擊添加:"));
save.add(b_add);
save.add(new Label("單擊修改:"));
save.add(b_xiu);
save.add(new Label("輸入查詢代號:"));
save.add(查詢文本框);
save.add(new Label("單擊查詢:"));
save.add(b_xun);
JPanel del=new JPanel();
del.setLayout(new GridLayout(2,2));
del.add(new Label("輸入刪除的代號:"));
del.add(刪除文本框);
del.add(new Label("單擊刪除:"));
del.add(b_del);
JPanel show=new JPanel();
show.setLayout(new BorderLayout());
show.add(pane,BorderLayout.CENTER);
show.add(b_show,BorderLayout.SOUTH);
JSplitPane split_one,split_two;
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,save,del);
split_two=new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,show);
con.add(split_two,BorderLayout.CENTER);
JPanel xun=new JPanel();
xun.add(new Label("所得信息:"));
xun.add(查詢信息文本框);
xun.setLayout(new GridLayout(2,1));
con.add(xun,BorderLayout.SOUTH);
b_add.addActionListener(this);
b_del.addActionListener(this);
b_xun.addActionListener(this);
b_xiu.addActionListener(this);
b_show.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==b_add)
{String daihao=null,mingcheng=null;int kucun=0;float danjia=0.0f;
daihao=代號文本框.getText();mingcheng=名稱文本框.getText();
kucun=Integer.parseInt(庫存文本框.getText());
danjia=Float.valueOf(單價文本框.getText()).floatValue();
商品 goods=new 商品(daihao,mingcheng,kucun,danjia);
hashtable.put(daihao,goods);
try{FileOutputStream file=new FileOutputStream("goods.txt");
ObjectOutputStream out=new ObjectOutputStream(file);
out.writeObject(hashtable); out.close();
}
catch(IOException event){}
}
else if(e.getSource()==b_del)
{String daihao1=刪除文本框.getText();
try{FileInputStream come_in=new FileInputStream("goods.txt");
ObjectInputStream in=new ObjectInputStream(come_in);
hashtable=(Hashtable)in.readObject(); //////
in.close();
}
catch(ClassNotFoundException event){}
catch(IOException event){}
商品 temp=(商品)hashtable.get(daihao1);
{hashtable.remove(daihao1);}
try{FileOutputStream file=new FileOutputStream("goods.txt");
ObjectOutputStream out =new ObjectOutputStream(file);
out.writeObject(hashtable);//
out.close();
}
catch(IOException event){}
}
//
else if(e.getSource()==b_xun)
{ String aa;
aa=查詢文本框.getText();
查詢信息文本框.setText(null);
try{FileInputStream come_in=new FileInputStream("goods.txt");
ObjectInputStream in =new ObjectInputStream(come_in);
hashtable=(Hashtable)in.readObject(); ////
in.close();
}
catch(ClassNotFoundException event){}
catch(IOException event){}
商品 a=(商品)hashtable.get(aa);
查詢信息文本框.setText(" 代號:"+a.代號+" 名稱:"+a.名稱+" 庫存:"+a.庫存+" 單價:"+a.單價);
}
//
else if(e.getSource()==b_xiu)
{ String bb;
bb=代號文本框.getText();
try{FileInputStream come_in=new FileInputStream("goods.txt");
ObjectInputStream in=new ObjectInputStream(come_in);
hashtable=(Hashtable)in.readObject(); //////
in.close();
}
catch(ClassNotFoundException event){}
catch(IOException event){}
商品 temp=(商品)hashtable.get(bb);
{hashtable.remove(bb);}
try{FileOutputStream file=new FileOutputStream("goods.txt");
ObjectOutputStream out =new ObjectOutputStream(file);
out.writeObject(hashtable);//
out.close();
}
catch(IOException event){}
String daihao1=null,mingcheng1=null;int kucun1=0;float danjia1=0.0f;
daihao1=代號文本框.getText();mingcheng1=名稱文本框.getText();
kucun1=Integer.parseInt(庫存文本框.getText());
danjia1=Float.valueOf(單價文本框.getText()).floatValue();
商品 goods1=new 商品(daihao1,mingcheng1,kucun1,danjia1);
hashtable.put(daihao1,goods1);
try{FileOutputStream file=new FileOutputStream("goods.txt");
ObjectOutputStream out=new ObjectOutputStream(file);
out.writeObject(hashtable); out.close();
}
catch(IOException event){}

}
//
else if(e.getSource()==b_show)
{ 顯示區.setText(null);
try{FileInputStream come_in=new FileInputStream("goods.txt");
ObjectInputStream in =new ObjectInputStream(come_in);
hashtable=(Hashtable)in.readObject(); ////
}
catch(ClassNotFoundException event){}
catch(IOException event){}
Enumeration enum=hashtable.elements();
while(enum.hasMoreElements())
{ 商品 te=(商品)enum.nextElement();
顯示區.append("商品代號:"+te.代號+" ");
顯示區.append("商品名稱:"+te.名稱+" ");
顯示區.append("商品庫存:"+te.庫存+" ");
顯示區.append("商品單價:"+te.單價+" ");
顯示區.append("\n ");
}
}
}
}
public class LinkListFour
{public static void main(String args[])
{ ShowWin win=new ShowWin();
win.setSize(400,350);
win.setVisible(true);
win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}});
}
}

② java中的Hashtable怎麼用,請詳細舉例子說明,拜託了 謝謝

就是哈希表,下面這個示例創建了一個數字的哈希表。它將數字的名稱用作鍵: Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);
要獲取一個數字,可以使用以下代碼:
Integer n = numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}

③ java 多線程操作hashtable(添加,刪除,遍歷)

public class TestThread {
public static void main(String[] args){
Map<Integer, Object> tables = new Hashtable<Integer, Object>();
Thread add = new Thread(new ThreadAdd(tables));
Thread del = new Thread(new ThreadDel(tables));
Thread count = new Thread(new ThreadCount(tables));
//啟動線程
add.start();
del.start();
count.start();
}
/**
*添加對象線程
*/
private static class ThreadAdd implements Runnable{
private Map<Integer, Object> table;

public ThreadAdd(Map<Integer, Object> tables){
this.table=tables;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
table.put(i, new Object());
System.out.println("添加對象,序號為:"+i);
}
}
}
/**
*刪除對象線程
*/
private static class ThreadDel implements Runnable{
private Map<Integer, Object> table;

public ThreadDel(Map<Integer, Object> table){
this.table=table;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table.remove(i);
System.out.println("移除對象,序號為:"+i);
}
}
}
/**
*統計線程
*/
private static class ThreadCount implements Runnable{
private Map<Integer, Object> table;

public ThreadCount(Map<Integer, Object> table){
this.table=table;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("當前隊列還剩"+table.size()+"個對象");
}
}
}
}
這是我的寫的demo,不知道符合不符合你的意思,大家共同交流共同進步。

④ 急需日歷記事本JAVA源代碼

能留下郵箱嗎?給你發過去

熱點內容
少兒編程排行 發布:2025-01-24 04:40:46 瀏覽:698
搭建伺服器怎麼使用 發布:2025-01-24 04:19:34 瀏覽:443
平行進口霸道哪些配置有用 發布:2025-01-24 04:19:32 瀏覽:873
ngram演算法 發布:2025-01-24 04:03:16 瀏覽:658
迷宮游戲c語言 發布:2025-01-24 03:59:09 瀏覽:358
榮耀30pro存儲類型 發布:2025-01-24 03:54:02 瀏覽:556
客戶端文件上傳 發布:2025-01-24 03:48:44 瀏覽:257
推特更改密碼的用戶名是什麼 發布:2025-01-24 03:45:55 瀏覽:596
cc編譯選項 發布:2025-01-24 03:45:18 瀏覽:512
銀行密碼怎麼被鎖 發布:2025-01-24 03:37:02 瀏覽:431