要求:
要实现开户、取款、存款、转账的功能;
分析:
①用户对象-User:身份证号,姓名,性别,年龄;
②账户对象-Account:账号,密码,余额;
方法:
①根据身份证找到用户对象
②自动生成账号
规则自己定义:比如随机数
③开户方法
要求:
首先根据身份证,看看用户是否存在;
如果不存在,注册用户(添加到用户数组),然后生成账户信息,执行开户;
如果存在,直接生成账户信息,执行开户;
开户:创建账户对象,放到用户的账户数组中;
开户成功后,输出所有用户和用户的账户信息;
④登录
根据账号和密码登录;
登录成功后,可以执行取款、存款、转账操作
⑤取款方法
⑥存款方法
⑦转账方法
在Myeclipse中完成,新建4个类
User
package ebank; public class User { private String cardId; private String name; private String gender; private int age; private Account[] acts = new Account[5]; private int count = 0; public User() { super(); } public User(String cardId, String name, String gender, int age) { super(); this.cardId = cardId; this.name = name; this.gender = gender; this.age = age; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Account[] getActs() { return acts; } public void setActs(Account[] acts) { this.acts = acts; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public void addAct(Account act){ if(count<acts.length){ acts[count] = act; count++; }else{ System.out.println("账户已满!"); } } }
Account
package ebank; public class Account { private String actNo; private String passwd; private double balance; public Account() { super(); } public Account(String actNo, String passwd, double balance) { super(); this.actNo = actNo; this.passwd = passwd; this.balance = balance; } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
AccountService
package ebank; import java.util.Scanner; import java.util.UUID; public class AccountService { private User[] users; private int size; private int count = 0; public AccountService() { size = 10; users = new User[size]; } public AccountService(int size) { this.size = size; users = new User[size]; } public User getUserById(String cardId){ User user = null; for(int i=0;i<users.length;i++){ User u = users[i]; if(u!=null){ if(u.getCardId().equals(cardId)){ user = u; } }else{ break; } } return user; } public void printInfo(){ System.out.println("身份证\t姓名\t性别\t年龄"); for(int i=0;i<users.length;i++){ User u = users[i]; if(u!=null){ System.out.println(u.getCardId()+"\t"+u.getName()+"\t"+u.getGender()+"\t"+u.getAge()); Account[] acts = u.getActs(); if(acts!=null){ for(int j=0;j<acts.length;j++){ Account a = acts[j]; if(a!=null){ System.out.println("\t\t账户:"+a.getActNo()+","+a.getPasswd()+","+a.getBalance()); }else{ break; } } } }else{ break; } } } public String generateActNo(){ String uuidStr = UUID.randomUUID().toString(); String actNo = uuidStr.substring(0, 8); return actNo; } public void registUser(User user){ if(count<users.length){ users[count] = user; count++; System.out.println("开户成功!"); }else{ System.out.println("开户数量已满!"); } } public Account getActByNo(String actNo){ Account act = null; for(int i=0;i<users.length;i++){ User u = users[i]; if(u!=null){ Account[] acts = u.getActs(); if(acts!=null){ for(int j=0;j<acts.length;j++){ Account a = acts[j]; if(a!=null){ if(a.getActNo().equals(actNo)){ act = a; break; } }else{ break; } } } }else{ break; } } return act; } public Account login(String actNo,String passwd){ Account act = null; for(int i=0;i<users.length;i++){ User u = users[i]; if(u!=null){ Account[] acts = u.getActs(); if(acts!=null){ for(int j=0;j<acts.length;j++){ Account a = acts[j]; if(a!=null){ if(a.getActNo().equals(actNo)&&a.getPasswd().equals(passwd)){ act = a; break; } }else{ break; } } } }else{ break; } } return act; } public void getMoney(Account act,double money){ double balance = act.getBalance(); if(balance>=money){ act.setBalance(act.getBalance()-money); System.out.println(act.getActNo()+"成功取出"+money+"元!"); }else{ System.out.println("账户余额不足!"); } } public void saveMoney(Account act,double money){ act.setBalance(act.getBalance()+money); System.out.println(act.getActNo()+"成功存入"+money+"元!"); } public void transfer(Account outAct,Account inAct,double money){ double balance = outAct.getBalance(); if(balance>=money){ outAct.setBalance(outAct.getBalance()-money); inAct.setBalance(inAct.getBalance()+money); System.out.println(outAct.getActNo()+"成功转出"+money+"元到"+inAct.getActNo()+"!"); }else{ System.out.println("账户余额不足!"); } } public void run(){ System.out.println("欢迎来到风云银行!"); Scanner sc = new Scanner(System.in); Account loginAct = null; while(true){ System.out.println("请输入你的操作:(1.开户,2.登录,3.退出)"); String comd = sc.next(); if(comd.equals("1")||comd.equals("开户")){ System.out.println("请输入身份证号码:"); String cardId = sc.next(); User user = getUserById(cardId); if(user!=null){ String actNo = generateActNo(); Account act = new Account(actNo, "000000", 0.0); user.addAct(act); printInfo(); }else{ System.out.println("请依次输入用户信息(姓名,性别,年龄)"); String name = sc.next(); String gender = sc.next(); int age = sc.nextInt(); user = new User(cardId, name, gender, age); String actNo = generateActNo(); Account act = new Account(actNo, "000000", 0.0); user.addAct(act); registUser(user); printInfo(); } }else if(comd.equals("2")||comd.equals("登录")){ System.out.println("请依次输入账号和密码:"); String actNo = sc.next(); String passwd = sc.next(); loginAct = login(actNo, passwd); if(loginAct!=null){ while(true){ System.out.println("1.存款,2.取款,3.转账,4.余额,5.back"); String oper = sc.next(); if(oper.equals("1")||oper.equals("存款")){ System.out.println("请输入存款金额:"); double money = sc.nextDouble(); saveMoney(loginAct, money); }else if(oper.equals("2")||oper.equals("取款")){ System.out.println("请输入取款金额:"); double money = sc.nextDouble(); getMoney(loginAct, money); }else if(oper.equals("3")||oper.equals("转账")){ System.out.println("请输入转入账号:"); String inActNo = sc.next(); Account inAct = getActByNo(inActNo); if(inAct!=null){ System.out.println("请输入转账金额:"); double money = sc.nextDouble(); transfer(loginAct, inAct, money); }else{ System.out.println("转入账号不存在!"); } }else if(oper.equals("4")||oper.equals("余额")){ System.out.println(loginAct.getActNo()+"账户余额为:"+loginAct.getBalance()+"元!"); }else if(oper.equals("5")||oper.equals("back")){ break; }else{ System.out.println("命令不合法!"); } } }else{ System.out.println("登录失败,请检查账号和密码后,重新登录!"); } }else if(comd.equals("3")||comd.equals("退出")){ System.out.println("谢谢使用,系统退出!"); System.exit(0); }else{ System.out.println("命令不合法!"); } } } }
App
package ebank; public class App { public static void main(String[] args){ AccountService as = new AccountService(); as.run(); } }
时间: 2024-10-08 21:47:52