import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class shop extends Panel{
String number,name;
int shuliang;
float danjia;
shop(String number,String name,int shuliang,float danjia){
this.number=number;
this.name=name;
this.shuliang=shuliang;
this.danjia=danjia;
}
}
class shopping extends JFrame implements ActionListener{
LinkedList goods_list=null;
JTextField numbertxt=new JTextField();
JTextField nametxt=new JTextField();
JTextField danjiatxt=new JTextField();
JTextField shuliangtxt=new JTextField();
JTextField deletetxt=new JTextField();
JButton b_add=new JButton("添加商品");
JButton b_del=new JButton("删除商品");
JButton b_show=new JButton("显示商品");
JTextArea show=new JTextArea();
shopping(){
goods_list=new LinkedList();
Container con=getContentPane();
JScrollPane pane=new JScrollPane(show);
show.setEditable(false);
JPanel save=new JPanel();
save.setLayout(new GridLayout(5,2));
save.add(new Label("输入代码"));
save.add(numbertxt);
save.add(new Label("输入名称"));
save.add(nametxt);
save.add(new Label("输入库存"));
save.add(shuliangtxt);
save.add(new Label("输入单价"));
save.add(danjiatxt);
save.add(new Label("单击添加"));
save.add(b_add);
JPanel del=new JPanel();
del.setLayout(new GridLayout(2,2));
del.add(new Label("输入删除的代码"));
del.add(deletetxt);
del.add(new Label("单击删除"));
del.add(b_del);
JPanel show1=new JPanel();
show1.setLayout(new BorderLayout());
show1.add(pane,BorderLayout.CENTER);
show1.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,show1);
con.add(split_two,BorderLayout.CENTER);
b_add.addActionListener(this);
b_del.addActionListener(this);
b_show.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b_add){
String daihao,mingcheng;
int kucun;
float danjia;
daihao=numbertxt.getText();
mingcheng=nametxt.getText();
kucun=Integer.parseInt(shuliangtxt.getText());
danjia=Float.valueOf(danjiatxt.getText()).floatValue();
shop goods=new shop(daihao,mingcheng,kucun,danjia);
goods_list.add(goods);
try{
FileOutputStream file=new FileOutputStream("goods.txt");
ObjectOutputStream out=new ObjectOutputStream(file);
out.writeObject(goods_list);
out.close();
}catch(IOException ex1){}
}
else if(e.getSource()==b_del){
String daihao=deletetxt.getText();
try{
FileInputStream come_in= new FileInputStream("goods.txt");
ObjectInputStream in=new ObjectInputStream(come_in);
goods_list=(LinkedList)in.readObject();
in.close();
}catch(ClassNotFoundException e1){}
catch(IOException e2){}
int num=goods_list.size();
for(int i=0;i<num;i++){
shop temp=(shop)goods_list.get(i);
if(temp.number.equals(daihao)){
goods_list.remove(i);}
try{
FileOutputStream file_out=new FileOutputStream("goods.txt");
ObjectOutputStream out=new ObjectOutputStream(file_out);
out.writeObject(goods_list);
out.close();
}catch(IOException ex){}
}
}
else if(e.getSource()==b_show){
show.setText(null);
try{
FileInputStream come_in= new FileInputStream("goods.txt");
ObjectInputStream in=new ObjectInputStream(come_in);
goods_list=(LinkedList)in.readObject();
}catch(ClassNotFoundException e1){}
catch(IOException e2){}
Iterator iter=goods_list.iterator();
while(iter.hasNext()){
shop te=(shop)iter.next();
show.append("商品代码:"+te.number);
show.append("商品名称:"+te.name);
show.append("商品库存:"+te.shuliang);
show.append("商品单价:"+te.danjia);
show.append("\n");
}
}
}
}
public class LinkListFour{
public static void main(String args[]){
shopping sp=new shopping();
sp.setSize(100,100);
sp.setVisible(true);
sp.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
}
}
java 数据结构 遍历链表程序
4星 · 超过85%的资源 需积分: 34 175 浏览量
2009-02-07
18:26:09
上传
评论
收藏 1KB RAR 举报
zhuseahui
- 粉丝: 63
- 资源: 419
最新资源
- 407000001.fasset
- J-20240402998-HTE-HTE-2947110DA-D3_整车运动控制器-电压渐变(缓升缓降)-检测报告-XS-汽车及零部件检测中心.pdf.crdownload
- Excel表格VBA-获取文件夹内所有文件路径.zip
- 文件移动.zip
- 反射源代码,源代码,源代码,源代码
- CAJViewer 9.0_x64-setup.exe
- servant C++语言框架rpc的源码实现 tools C++语言框架IDL工具的源码实现 util C++语言.7z
- 使用pyqt创建一个登录具有动态背景的登陆界面
- 实验名称 调幅波信号的解调
- 实验名称 振幅调制器(利用乘法器)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈