学了四天的 Java基本技术 写了个大作业 ATM 三天时间 好吧 实在周六写了半天 周日没写 今天憋了一天 终于完成了 还是要感谢下帅哥和各位童鞋 说一*验吧 首先是MVC设计模式 好吧 我是第一次听到 应该是吧 写完这个 有点感觉。。。 然后就是对于用一个自定的类来作为另一个方法的参数的使用 实在想一想跟系统定义的int double 或者String类差未几 只是不习惯使用自定的类 再有就是泛型的使用 这个是完全不会。。。 还有是对于HashMap ArrayList等类的应用 不太会用 特别是和自定的类一起应用 最后还有个IO的的使用 这个还行 这是遵循MVC设计模式设计的结构 好吧 不是我自己写的。。。 User.java package *.edu.cust.vo; public class User { private double balance;//余额 private String *;//姓名 private String no;//编号 private String password;//密码 private String recoders;//日志 public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public String getName() { return *; } public void setName(String *) { this.* = *; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRecoders() { return recoders; } public void setRecoders(String temp_1) { this.recoders = temp_1; } } ****************************************** Recoder.java package *.edu.cust.vo; import java.util.Date; public class Recoder { private String userNo; private String no; private Date date; private String instruction; public String getUserNo() { return userNo; } public void setUserNo(String userNo) { this.userNo = userNo; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getInstruction() { return instruction; } public void setInstruction(String instruction) { this.instruction = instruction; } } ****************************************** ConstData.java package *.edu.cust.util; public class ConstData { public final static String *_PATH="G:\ATM\user.txt";//常量路径 } ****************************************** Util.java package *.edu.cust.util; import java.text.SimpleDateFormat; import java.util.Date; public class Util { public final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; public String nowTime(){ Date nowTime = new Date(); SimpleDateFormat *DateFormat = new SimpleDateFormat(DATE_FORMAT); return *DateFormat.format(nowTime); } } ****************************************** FileManager.java package *.edu.cust.dao; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import *.edu.cust.util.ConstData; import *.edu.cust.vo.User; public class FileManager { private List<String> temp = new ArrayList<String>(); private HashMap<String, User> hm = new HashMap<String, User>(); private List<String> list = new ArrayList<String>(); public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public HashMap<String, User> getHm() { return hm; } public void setHm(HashMap<String, User> hm) { this.hm = hm; } public HashMap<String, User> read() { String *Path = ConstData.*_PATH; File file = new File(*Path); BufferedReader bufferedReader = null; String a = null; try { bufferedReader = new BufferedReader(new InputStreamReader( new FileInputStream(file), "UTF-8")); while ((a = bufferedReader.readLine()) != null) { User user = new User(); String temp_1[] = a.split("@"); user.setNo(temp_1[0]); temp.add(temp_1[0]); user.setName(temp_1[1]); user.setPassword(temp_1[2]); user.setBalance(Double.parseDouble(temp_1[3])); user.setRecoders(temp_1[4]); hm.put(user.getNo(), user); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } } return hm; } public boolean write() { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter( new File(ConstData.*_PATH))); Iterator<User> it = hm.values().iterator(); while (it.hasNext()) { User u = it.next(); String temp = u.getNo() + "@" + u.getName()// temp_2:改回txt内的格式 + "@" + u.getPassword() + "@" + u.getBalance() + "@" + u.getRecoders(); bw.write(temp); bw.write("rn"); } bw.flush(); if(bw!=null){ bw.close(); } } catch (IOException e) { e.printStackTrace(); } return true; } public List<String> wrapRecoders(String recoder) { String recoder_1[] = recoder.split("#"); for (int i = 0; i < recoder_1.length; i++) { list.add(recoder_1); } return list; } public String re*poseRecoders(String recoder) { list.add(recoder); String tempRecoder = (String) list.get(0); for (int i = 1; i < list.size(); i++) { tempRecoder = tempRecoder + "#" + list.get(i); } return tempRecoder; } } ****************************************** DataManager.java package *.edu.cust.bo; import java.util.ArrayList; import *.edu.cust.dao.FileManager; import *.edu.cust.util.Util; import *.edu.cust.vo.User; public class DataManager { private boolean a = true; public boolean isA() { return a; } public void setA(boolean a) { this.a = a; } public void enter(String no, String password) { FileManager fileManager = new FileManager(); User user = new User(); fileManager.read(); if (fileManager.getHm().containsKey(no)) { user = fileManager.getHm().get(no); if (password.equals(user.getPassword())) { System.out.println("验证通过!"); a = false; } else { a = true; System.out.println("帐号或密码错误!请重新输进!"); } } else { a = true; System.out.println("帐号或密码错误!请重新输进!"); } } public void putIn(String no, double num) { Util util = new Util(); User user = new User(); FileManager fileManager = new FileManager(); fileManager.read(); user = fileManager.getHm().get(no); user.setBalance(user.getBalance() + num); user.setRecoders(fileManager.re*poseRecoders("于" + util.nowTime() + "存进" + num)); fileManager.re*poseRecoders(user.getRecoders()); fileManager.write(); System.out.println("存款成功!"); } public void withDraw(String no, double num) { Util util = new Util(); User user = new User(); FileManager fileManager = new FileManager(); fileManager.read(); user = fileManager.getHm().get(no); if (num > user.getBalance()) { System.out.println("余额不足!"); } else if (num <= 0) { System.out.println("输进的金额错误!"); } else { user.setBalance(user.getBalance() - num); user.setRecoders(fileManager.re*poseRecoders("于" + util.nowTime() + "取出" + num)); fileManager.write(); System.out.println("取款成功!"); } } public void transferA*ounts(String no, String tempNo, double num) { FileManager fileManager = new FileManager(); User user = new User(); Util util = new Util(); fileManager.read(); user = fileManager.getHm().get(no); if (fileManager.getHm().containsKey(tempNo)) { if (num > user.getBalance()) { System.out.println("余额不足!"); } else if (num <= 0) { System.out.println("输进的金额错误!"); } else { user.setBalance(user.getBalance() - num); user.setRecoders(fileManager.re*poseRecoders("于" + util.nowTime() + "向账户内" + tempNo + "汇进" + num)); fileManager .getHm() .get(tempNo) .setBalance( fileManager.getHm().get(tempNo).getBalance() + num); fileManager .getHm() .get(tempNo) .setRecoders( "于" + util.nowTime() + "收到账户" + no + "向您汇款" + num); fileManager.write(); System.out.println("转账成功!"); } } else { System.out.println("输进的帐号不存在!"); } } public void queryBalance(String no) { User user = new User(); FileManager fileManager = new FileManager(); fileManager.read(); user = fileManager.getHm().get(no); System.out.println("您的余额为" + user.getBalance()); } public void re*posePassword(String no, String oldPassword, String newPassword, String newPassword_1) { User user = new User(); FileManager fileManager = new FileManager(); fileManager.read(); user = fileManager.getHm().get(no); if (oldPassword.equals(user.getPassword())) { if (newPassword.equals(newPassword_1)) { user.setPassword(newPassword_1); System.out.println("修改成功!"); user.setPassword(newPassword); } else { System.out.println("两次新密码不一致!"); } } else { System.out.println("密码错误!"); } fileManager.write(); } public void checkRecoder(String no) { FileManager fileManager = new FileManager(); User user = new User(); fileManager.read(); user = fileManager.getHm().get(no); ArrayList<?> list = (ArrayList<?>) fileManager.wrapRecoders(user .getRecoders()); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } ****************************************** View.java package *.edu.cust.view; import java.util.Scanner; import *.edu.cust.bo.DataManager; public class View { public static void main(String[] args) { try { Scanner cin = new Scanner(System.in); DataManager dataManager = new DataManager(); System.out.println("请输进帐号和密码!"); String no = null; String password = null; while (dataManager.isA() == true) { no = cin.next(); password = cin.next(); dataManager.enter(no, password); } while (dataManager.isA() == false) { System.out .println("请选择服务项目:1、存款 2、取款 3、转账 4、查询余额 5、修改密码 6、查看日志 7、退出"); int a = cin.nextInt(); switch (a) { case 1: System.out.println("请输进存进的金额!"); double tempMoney = cin.nextDouble(); while (tempMoney % 100 != 0) { System.out.println("输进的金额不是100的整数倍,请重新输进!"); tempMoney = cin.nextDouble(); } dataManager.putIn(no, tempMoney); System.out.println("是否继续操纵?是请输进1,否请按任意数字键!"); a = cin.nextInt(); if (a == 1) { dataManager.setA(false); } else { dataManager.setA(true); } break; case 2: System.out.println("请输进取出的金额!"); tempMoney = cin.nextDouble(); while (tempMoney % 100 != 0) { System.out.println("输进的金额不是100的整数倍,请重新输进!"); tempMoney = cin.nextDouble(); } dataManager.withDraw(no, tempMoney); System.out.println("是否继续操纵?是请输进1,否请按任意数字键!"); a = cin.nextInt(); if (a == 1) { dataManager.setA(false); } else { dataManager.setA(true); } break; case 3: System.out.println("请输进转账的帐号和金额"); String tempNo = cin.next(); tempMoney = cin.nextDouble(); while (tempMoney % 100 != 0) { System.out.println("输进的金额不是100的整数倍,请重新输进!"); tempMoney = cin.nextDouble(); } dataManager.transferA*ounts(no, tempNo, tempMoney); System.out.println("是否继续操纵?是请输进1,否请按任意数字键!"); a = cin.nextInt(); if (a == 1) { dataManager.setA(false); } else { dataManager.setA(true); } break; case 4: dataManager.queryBalance(no); System.out.println("是否继续操纵?是请输进1,否请按任意数字键!"); a = cin.nextInt(); if (a == 1) { dataManager.setA(false); } else { dataManager.setA(true); } break; case 5: System.out.println("请输进一次原密码和两次新密码"); String oldPassword = cin.next(); String newPassword = cin.next(); String newPassword_1 = cin.next(); dataManager.re*posePassword(no, oldPassword, newPassword, newPassword_1); System.out.println("是否继续操纵?是请输进1,否请按任意数字键!"); a = cin.nextInt(); if (a == 1) { dataManager.setA(false); } else { dataManager.setA(true); } break; case 6: dataManager.checkRecoder(no); System.out.println("是否继续操纵?是请输进1,否请按任意数字键!"); a = cin.nextInt(); if (a == 1) { dataManager.setA(false); } else { dataManager.setA(true); } break; case 7: dataManager.setA(true); break; } } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("程序YY束?quot;); > > > > ?***************************************** > 还有?txtt的文本文档来充当数据库的作?br /> > 格式说明一下:帐篅@姓聾@密翤@余禓@日?##日?##日?...... > ?@qwe@4@190.0@@?011/08/08 18:56:311存?00.0##?011/08/08 18:56:311存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:244向账户?4汇?00.0##?011/08/08 18:00:144存?00.0 > ?@asd@5@990.0@@?011/08/08 19:29:422存?000.0 > ?@zxc@dd@20390.0@@?011/08/08 18:56:311存?00.0##?011/08/08 18:56:311存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:244向账户?4汇?00.0##?011/08/08 18:00:144存?00.0##?011/08/08 18:56:311存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:244向账户?4汇?00.0##?011/08/08 18:00:144存?00.0##?011/08/08 18:56:277存?00.0##?011/08/08 18:56:244向账户?4汇?00.0##?011/08/08 18:00:144存?00.0##?011/08/08 18:56:244向账户?4汇?00.0##?011/08/08 18:15:266存?0000.0 > ?@mnb@4@1690.0@@?011/08/08 19:26:077收到账?6向您汇?000.0 > ?***************************************** > 当? 还有点小缺? 就是由覴ecoderr不蔐istt? 所以日志写进文本的时候只能写进一个。。。目前还没改出?br /> > 还有文本中的第一个帐号密码不能使? 还未找到原因。。?br /> > 就这样吧、、?br /> > 参考资料vcc设计模式?baike.baidu.*/view/739359.htm