模拟银行存储实验---GUI实现

Posted on 2006-10-26 08:15 久城 阅读(560) 评论(0)  编辑  收藏 所属分类: JavaTest
把前两天做的实验用GUI实现...

主要的接口都没有变,所以实现比较容易...

代码如下:
  1/**
  2*title 模拟银行存储实验——GUI实现
  3*@author realsmy
  4*date 2006-10-26 8:10
  5*/

  6
  7import java.io.*;
  8import java.awt.*;
  9import javax.swing.*;
 10import java.awt.event.*;
 11import javax.swing.JPanel.*;
 12
 13//login applicatiion
 14class Login extends JFrame{
 15    Container c;
 16    JTextField text;
 17    JPasswordField password;
 18    MyPanel panel;
 19    JLabel label_name,label_password;
 20    JButton button1,button2;
 21    String lg_name,lg_password;
 22    File fl;//帐户资料文件声明
 23    Login(){
 24        super("模拟银行存储系统");
 25        c = getContentPane();
 26        c.setLayout(new BorderLayout());
 27        //初始化组件
 28        text = new JTextField();
 29        password = new JPasswordField();
 30        label_name = new JLabel("教工号 :");
 31        label_password = new JLabel("密    码 :");
 32        panel = new MyPanel();
 33        button1 = new JButton("LOGIN");
 34        button2 = new JButton("RESET");
 35        //添加监听
 36        button1.addActionListener(new ActionListener()
 37        {
 38            public void actionPerformed(ActionEvent e)
 39            {
 40                lg_name = text.getText();
 41                lg_password  = password.getText();
 42                if ( lg_name.equals("admin"&& lg_password.equals("admin")){
 43                    setVisible(false);
 44                    GuanLi gl = new GuanLi(); 
 45                    gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 46                }

 47                else{
 48                    try{
 49                        fl = new File("frozen",lg_name+".txt");
 50                        if(fl.exists()){
 51                            JOptionPane.showMessageDialog(null,"对不起,您的帐户已被冻结!");
 52                        }

 53                        else{
 54                            fl = new File(lg_name+".txt");
 55                            //判断帐户是否存在
 56                            if(!fl.exists()){
 57                                JOptionPane.showMessageDialog(null,"对不起,您输入的帐户并不存在,请重新输入:");
 58                            }

 59                            else{
 60                            //帐户存在,开始判断密码
 61                                BufferedReader reader = new BufferedReader(new FileReader( lg_name + ".txt"));
 62                                String pw = reader.readLine();
 63                                int money = Integer.parseInt(reader.readLine());
 64                                //判断密码
 65                                if(lg_password.equals(pw)){
 66                                    JOptionPane.showMessageDialog(null,"登陆成功\n"+"您的用户尚有余额"+money+"");
 67                                    ZhangHu zh = new ZhangHu(lg_name,lg_password,money);
 68                                    setVisible(false);
 69                                    YongHu yh = new YongHu(zh);
 70                                    yh.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 71                                }

 72                                else{
 73                                    JOptionPane.showMessageDialog(null,"对不起,您输入的密码不正确,请重新输入:");
 74                                }

 75                            }

 76                        }
        
 77                    }
catch(HeadlessException a){}catch(IOException b){}
 78                }

 79            }

 80        }
);
 81        button2.addActionListener(new ActionListener()
 82        {
 83            public void actionPerformed(ActionEvent e)
 84            {
 85                text.setText("");
 86                password.setText("");
 87            }

 88        }
);
 89
 90        //设置容器
 91        panel.setLayout(new GridLayout(3,2,10,15));
 92        panel.add(label_name);
 93        panel.add(text);
 94        panel.add(label_password);
 95        panel.add(password);
 96        panel.add(button1);
 97        panel.add(button2);
 98        
 99        c.add(panel,BorderLayout.CENTER);
100        setSize(300,200);
101        setLocation(300,200);
102        setVisible(true);
103    }

104}

105
106//定义管理界面
107class GuanLi extends JFrame{
108    private Container c;
109    JButton button1,button2,button3,button4;
110    MyPanel panel;
111    JLabel label1,label2;
112    Manager manager;
113    //String name,password,password2;
114    //int money;
115    GuanLi(){
116        super("模拟银行存储系统");
117        c = getContentPane();
118        c.setLayout(new BorderLayout());
119        //数据初始化
120        //button = new JButton("");
121        panel = new MyPanel();
122        label1 = new JLabel("欢迎光临赵家银行!");
123        label2 = new JLabel("请选择你要进行的操作:");
124        button1 = new JButton("1.添加帐户");
125        button2 = new JButton("2.删除用户");
126        button3 = new JButton("3.冻结用户");
127        button4 = new JButton("4.退出");
128        
129        //按钮监听
130        button1.addActionListener(new ActionListener(){
131            public void actionPerformed(ActionEvent e){
132                manager = new Manager();
133                manager.add();
134            }

135        }
);
136        button2.addActionListener(new ActionListener(){
137            public void actionPerformed(ActionEvent e){
138                manager = new Manager();
139                manager.del();
140            }

141        }
);
142        button3.addActionListener(new ActionListener()
143        {
144            public void actionPerformed(ActionEvent e){
145                manager = new Manager();
146                manager.froze();
147            }

148        }
);
149        button4.addActionListener(new ActionListener()
150        {
151            public void actionPerformed(ActionEvent e)
152            {
153                System.exit(0);
154            }

155        }
);
156        panel.setLayout(new GridLayout(6,1,50,5));
157        //c.setLayout(new GridLayout(4,2));
158        panel.add(label1);
159        panel.add(label2);
160        panel.add(button1);
161        panel.add(button2);
162        panel.add(button3);
163        panel.add(button4);
164
165        c.add(panel);
166        setSize(400,300);
167        setLocation(300,200);
168        setVisible(true);
169    }

170}

171
172//定义用户界面
173class YongHu extends JFrame{
174    private Container c;
175    JButton button1,button2,button3,button4;
176    MyPanel panel;
177    JLabel label1,label2;
178    ZhangHu zhanghu,zhanghu2;
179    //String name,password,password2;
180    String toname,password;
181    int money,money2,money3;
182    File fl;
183    YongHu(ZhangHu zh){
184        super("模拟银行存储系统");
185        zhanghu = zh;
186        c = getContentPane();
187        c.setLayout(new BorderLayout());
188        //数据初始化
189        panel = new MyPanel();
190        label1 = new JLabel("欢迎光临赵家银行!");
191        label2 = new JLabel("请选择你要进行的操作:");
192        button1 = new JButton("1.存钱");
193        button2 = new JButton("2.取钱");
194        button3 = new JButton("3.转帐");
195        button4 = new JButton("4.退出");
196        
197        //按钮监听
198        button1.addActionListener(new ActionListener(){
199            public void actionPerformed(ActionEvent e){
200                try{
201                    money = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入您要存入的金额:"));
202                }
catch(HeadlessException o){}
203                zhanghu.setM(money);
204                infile(zhanghu);
205            }

206        }
);
207        button2.addActionListener(new ActionListener(){
208            public void actionPerformed(ActionEvent e){
209                try{
210                    money = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入您要取得的金额:"));
211                }
catch(HeadlessException o){}
212                zhanghu.getM(money);
213                infile(zhanghu);
214            }

215        }
);
216        button3.addActionListener(new ActionListener()
217        {
218            public void actionPerformed(ActionEvent e){
219                try{
220                    while(true){
221                        toname = JOptionPane.showInputDialog(null,"请输入你要转入的帐户:");
222                        fl = new File(toname+".txt");
223                        //判断帐户是否存在
224                        if(!fl.exists()){
225                            System.out.println("对不起,您输入的帐户并不存在,请重新输入:");
226                            continue;
227                        }

228                        else{
229                            break;
230                        }

231                    }

232                    money2 = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入你要转入的金额:"));
233                    zhanghu.getM(money2);
234                    infile(zhanghu);
235                    BufferedReader reader = new BufferedReader(new FileReader( toname + ".txt"));
236                    password = reader.readLine();
237                    money3 = Integer.parseInt(reader.readLine());
238                    zhanghu2 = new ZhangHu(toname,password,money3);
239                    zhanghu2.setM(money2);
240                    infile(zhanghu2);
241                }
catch(HeadlessException o){}catch(IOException p){}
242            }

243        }
);
244        button4.addActionListener(new ActionListener()
245        {
246            public void actionPerformed(ActionEvent e)
247            {
248                System.exit(0);
249            }

250        }
);
251        panel.setLayout(new GridLayout(6,1,50,5));
252        panel.add(label1);
253        panel.add(label2);
254        panel.add(button1);
255        panel.add(button2);
256        panel.add(button3);
257        panel.add(button4);
258
259        c.add(panel);
260        setSize(400,300);
261        setLocation(300,200);
262        setVisible(true);
263    }

264    public void infile(ZhangHu p){
265        try{
266            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(p.getName()+".txt")));
267            writer.println(p.getPassword());
268            writer.println(p.getMoney());
269            writer.flush() ;
270        }
catch(IOException e){}
271    }

272}

273
274//定义帐户类
275class ZhangHu {
276    private String name;
277    private String password;
278    private int money;
279    ZhangHu(){
280        
281    }

282    ZhangHu(String name,String password, int money){
283        this.name = name;
284        this.password = password;
285        this.money = money;
286    }

287    public void setM(int a){
288        money = money + a;
289        try{
290            JOptionPane.showMessageDialog(null,"存储了"+a+"元,帐户"+name+"尚有余额"+money+"");
291        }
catch(HeadlessException E){}
292    }

293    public void getM(int a){
294        if(a > money){
295            try{
296                JOptionPane.showMessageDialog(null,"对不起,您的金额不足"+a+"");
297            }
catch(HeadlessException E){}
298        }

299        else{
300            money = money - a;
301            try{
302                JOptionPane.showMessageDialog(null,"取得了"+a+"元,帐户"+name+"尚有余额"+money+"");
303            }
catch(HeadlessException E){}
304        }

305    }

306    public String getName(){
307        return name;
308    }

309    public String getPassword(){
310        return password;
311    }

312    public int getMoney(){
313        return money;
314    }

315    public void setName(String name){
316        this.name = name;
317    }

318    public void setPassword(String password){
319        this.password = password;
320    }

321    public void setMoney(int money){
322        this.money = money;
323    }

324}

325
326//定义管理员类
327class Manager
328{
329    public void add(){
330        String pw;
331        ZhangHu zh = new ZhangHu();
332        try{
333            zh.setName(JOptionPane.showInputDialog(null,"请输入您要添加的帐户名:"));
334            zh.setPassword(JOptionPane.showInputDialog(null,"请输入您要设置的密码:"));
335            while(true){
336                pw = JOptionPane.showInputDialog(null,"请再次输入密码:");
337                if ( zh.getPassword().equals(pw)){
338                    break;
339                }

340                else{
341                    zh.setPassword(JOptionPane.showInputDialog(null,"两次输入的密码不一致,请重新输入密码:"));
342                    continue;
343                }

344            }

345            zh.setMoney(Integer.parseInt(JOptionPane.showInputDialog(null,"请输入您的金额:")));
346            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(zh.getName()+".txt")));//创建文件
347            writer.println(zh.getPassword());
348            writer.println(zh.getMoney());
349            writer.flush() ;
350            JOptionPane.showMessageDialog(null,"帐户"+zh.getName()+"已经创建成功");
351        }
catch(HeadlessException e){}catch(IOException c){}
352    }

353    public void del(){
354        String name = null;
355        try{
356            name = (JOptionPane.showInputDialog(null,"请输入您要删除的帐户名:"));
357            while(true){
358                File fl = new File(name+".txt");
359                if(fl.exists()){
360                    fl.delete();
361                    JOptionPane.showMessageDialog(null,"帐户"+name+"删除成功");
362                    break;
363                }

364                else{
365                    name = JOptionPane.showInputDialog(null,"您所输入的帐户并不存在,请重新输入:");
366                }

367            }
    
368        }
catch(HeadlessException e){}
369    }

370    public void froze(){
371        ZhangHu zh = new ZhangHu();
372        String name = null;        
373        name = (JOptionPane.showInputDialog(null,"请输入您要冻结的帐户:"));
374        try{
375            while(true){
376                File fl = new File(name+".txt");
377                if (fl.exists()){//如果该帐户存在,则读取该帐户的数据
378                    BufferedReader reader = new BufferedReader(new FileReader( name + ".txt"));
379                    zh.setPassword(reader.readLine());
380                    zh.setMoney(Integer.parseInt(reader.readLine()));
381                    File fr = new File("frozen");
382                    if(!fr.exists()){
383                        fr.mkdir();//创建一个此目录的文件夹
384                    }

385                    File frfl = new File(fr,name+".txt");
386                    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(frfl)));
387                    writer.println(zh.getPassword());
388                    writer.println(zh.getMoney());
389                    writer.flush() ;
390                    fl.delete();
391                    JOptionPane.showMessageDialog(null,"帐户"+name+"冻结成功!");
392                    break;
393                }

394                else{
395                    name = JOptionPane.showInputDialog(null,"您所输入的帐户并不存在,请重新输入:");
396                }
    
397            }

398        }
catch(SecurityException e){}catch(HeadlessException e){}catch(IOException e){}
399    }

400}

401
402//自定义面扳类
403class MyPanel extends JPanel
404{
405    public Insets insets()
406    {
407        return new Insets(40,40,40,40);
408    }

409}

410
411//主类
412public class Bank_Test {
413    public static void main(String[] args){
414        Login lg = new Login();
415        lg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
416    }
    
417}


欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!


只有注册用户登录后才能发表评论。


网站导航:
 

Copyright © 久城