大四JAVA實驗(一)

          Posted on 2006-10-19 10:57 久城 閱讀(740) 評論(7)  編輯  收藏 所屬分類: JavaTest

          寫一個模擬的銀行存儲系統,能夠實現用戶的登陸,存錢,取錢,轉帳等功能。必須用I/O流在DOS下實現過程。

          起了個大早開始寫,郁悶啊都寫到11點了才出來..

          雖然SIMPLE,但是也有不少收獲!特別是I/O流。以前學的很不扎實,還得好好看看資料再總結下!
          先把代碼帖下,哈哈!

          代碼如下:

          package com.neusoft.test;

          /*
           *Title 模擬實現銀行存儲系統
           *@author realsmy
           *2006-10-19 10:50
           
          */


          import java.io.*;

          class ZhangHu {
              
          private String name;

              
          private String password;

              
          private int money;

              ZhangHu(String name, 
          int money) {
                  
          this.name = name;
                  
          this.money = money;
              }


              ZhangHu(String name, String password, 
          int money) {
                  
          this.name = name;
                  
          this.password = password;
                  
          this.money = money;
              }


              
          public void setM(int a) {
                  money 
          = money + a;
                  System.out.println(
          "存儲了" + a + "元,帳戶" + name + "尚有余額" + money + "");
              }


              
          public void getM(int a) {
                  
          if (a > money) {
                      System.out.println(
          "對不起,您的金額不足" + a + "");
                  }
           else {
                      money 
          = money - a;
                      System.out
                              .println(
          "取得了" + a + "元,帳戶" + name + "尚有余額" + money + "");
                  }

              }


              
          public String getName() {
                  
          return name;
              }


              
          public String getPassword() {
                  
          return password;
              }


              
          public int getMoney() {
                  
          return money;
              }

          }


          // 銀行模擬系統
          public class Bank_Test {
              String name;

              String password;

              String pw;

              
          int money;

              
          int choose;

              
          int a, b;// 存取的金額,臨時變量

              File fl;

              ZhangHu user, user2;
          // 帳戶對象

              String toname;
          // 轉入帳戶名

              String c;
          // 臨時存儲轉入帳戶密碼

              
          public Bank_Test() {
                  login();
                  user 
          = new ZhangHu(name, password, money);
                  
          while (true{
                      cunqu();
                  }

              }


              
          // 實現登陸方法login
              public void login() {
                  System.out.println(
          "您好,歡迎光臨趙家銀行!請輸入您的帳戶號碼:");
                  
          while (true{
                      
          try {
                          BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                                  System.in));
                          name 
          = in.readLine();
                      }
           catch (IOException e) {
                      }

                      fl 
          = new File(name + ".txt");
                      
          // 判斷帳戶是否存在
                      if (!fl.exists()) {
                          System.out.println(
          "對不起,您輸入的帳戶并不存在,請重新輸入:");
                          
          continue;
                      }

                      
          // 帳戶存在,開始判斷密碼
                      try {
                          System.out.println(
          "請輸入您的密碼:");
                          BufferedReader in2 
          = new BufferedReader(new InputStreamReader(
                                  System.in));
                          password 
          = in2.readLine();
                      }
           catch (IOException e) {
                      }

                      
          // 取文件中的密碼
                      try {
                          BufferedReader reader 
          = new BufferedReader(new FileReader(name
                                  
          + ".txt"));
                          pw 
          = reader.readLine();
                          money 
          = Integer.parseInt(reader.readLine());
                      }
           catch (IOException e) {
                      }

                      
          // 判斷密碼
                      if (password.equals(pw)) {
                          System.out.println(
          "登陸成功");
                          System.out.println(
          "您的用戶尚有余額" + money + "");
                          
          break;
                      }
           else {
                          System.out.println(
          "對不起,您輸入的密碼不正確,請重新輸入帳戶:");
                          
          continue;
                      }

                  }

              }


              
          // 實現存取方法cunqu
              public void cunqu() {
                  System.out.println(
          "請選擇您要進行的操作:");
                  System.out.println(
          "1. 存錢 2. 取錢 3. 轉帳 4. 退出 ");
                  
          try {
                      BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                              System.in));
                      choose 
          = Integer.parseInt(in.readLine());
                  }
           catch (IOException e) {
                  }

                  
          // 存錢
                  if (choose == 1{
                      System.out.println(
          "請輸入你要存儲的金額:");
                      
          try {
                          BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                                  System.in));
                          a 
          = Integer.parseInt(in.readLine());
                      }
           catch (IOException e) {
                      }

                      user.setM(a);
                      
          // infile(user);

                  }

                  
          // 取錢
                  if (choose == 2{
                      System.out.println(
          "請輸入你要取得的金額:");
                      
          try {
                          BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                                  System.in));
                          a 
          = Integer.parseInt(in.readLine());
                      }
           catch (IOException e) {
                      }

                      user.getM(a);
                      infile(user);
                  }

                  
          if (choose == 3{
                      System.out.println(
          "請輸入你要轉入的帳戶:");
                      
          while (true{
                          
          try {
                              BufferedReader in 
          = new BufferedReader(
                                      
          new InputStreamReader(System.in));
                              toname 
          = in.readLine();
                          }
           catch (IOException e) {
                          }

                          fl 
          = new File(toname + ".txt");
                          
          // 判斷帳戶是否存在
                          if (!fl.exists()) {
                              System.out.println(
          "對不起,您輸入的帳戶并不存在,請重新輸入:");
                              
          continue;
                          }
           else {
                              
          break;
                          }

                      }

                      System.out.println(
          "請輸入你要轉入的金額:");
                      
          try {
                          BufferedReader in 
          = new BufferedReader(new InputStreamReader(
                                  System.in));
                          a 
          = Integer.parseInt(in.readLine());
                      }
           catch (IOException e) {
                      }

                      user.getM(a);
                      infile(user);
                      
          try {
                          BufferedReader reader 
          = new BufferedReader(new FileReader(
                                  toname 
          + ".txt"));
                          c 
          = reader.readLine();
                          b 
          = Integer.parseInt(reader.readLine());
                      }
           catch (IOException e) {
                      }

                      user2 
          = new ZhangHu(toname, c, b);
                      user2.setM(a);
                      infile(user2);

                  }

                  
          if (choose == 4{
                      System.exit(
          0);
                  }

              }


              
          // 存入文件
              public void infile(ZhangHu p) {
                  
          try {
                      PrintWriter writer 
          = new PrintWriter(new BufferedWriter(
                              
          new FileWriter(p.getName() + ".txt")));
                      writer.println(p.getPassword());
                      writer.println(p.getMoney());
                      writer.flush();
                  }
           catch (IOException e) {
                  }

              }


              
          public static void main(String[] args) {
                  
          new Bank_Test();
              }

          }



          歡迎來訪!^.^!
          本BLOG僅用于個人學習交流!
          目的在于記錄個人成長.
          所有文字均屬于個人理解.
          如有錯誤,望多多指教!不勝感激!

          Feedback

          # re: 大四JAVA實驗(一)  回復  更多評論   

          2006-10-19 14:33 by 馬嘉楠
          在賬戶類ZhangHu中
          方法:public void getM(int a){
          if(a > money){
          System.out.println("對不起,您的金額不足"+a+"元");

          //輸出提示之后,應該有個返回吧,否則下面語句不是還要繼續執行
          return;
          }

          # re: 大四JAVA實驗(一)  回復  更多評論   

          2006-10-19 17:50 by 久城
          改過來了,謝謝!

          # re: 大四JAVA實驗(一)  回復  更多評論   

          2006-10-22 11:08 by 努力
          你們都學了多長時間了!``
          很苦惱..
          有沒有學習的技巧..我現在正在學習JAVA感覺不是很容易!`
          不知道以后能不能安全畢業..

          # re: 大四JAVA實驗(一)  回復  更多評論   

          2007-04-06 14:31 by 糖果
          看到代碼就暈了@.@~ ^-^

          # re: 大四JAVA實驗(一)  回復  更多評論   

          2007-04-19 22:13 by 匿名
          你這個程度 找工作很容易的。05年我畢業的時候,也就懂個語法,struts連是什么都不知道,抱著一本jsp惡啃。大部分東西都是工作以后學的。只要態度積極,這些都不是問題。我看到很多工作很多年的,卻依然很膚淺。所謂有志不在年高,小伙子不用氣餒,認準方向好好學習。

          # re: 大四JAVA實驗(一)[未登錄]  回復  更多評論   

          2007-09-03 20:56 by yy
          呵呵,你是神仙派來的

          # re: 大四JAVA實驗(一)  回復  更多評論   

          2007-09-03 21:04 by 久城
          ??....??

          Copyright © 久城

          主站蜘蛛池模板: 平泉县| 内丘县| 扶沟县| 乌拉特后旗| 醴陵市| 曲靖市| 阿图什市| 中牟县| 阿拉善右旗| 文登市| 金门县| 孟村| 增城市| 澜沧| 白水县| 广丰县| 襄汾县| 北碚区| 宣城市| 永平县| 特克斯县| 买车| 四平市| 长泰县| 黎平县| 马公市| 鄂托克前旗| 梨树县| 苍山县| 凤凰县| 东阿县| 德保县| 恩施市| 河北区| 星座| 凤凰县| 孟州市| 顺平县| 博湖县| 肇庆市| 庄河市|