模仿银行简单的存取业务项目分析
2015-11-20 20:26
323 查看
一,项目分析:
银行对于个人主要的业务,是存款,取款,以及查询余额。
二,项目代码:
public class Account {
}
import java.util.ArrayList;
import java.util.List;
public class Bank {
private List list=new ArrayList();
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class BankService implements Runnable {
private Socket s;
private Bank b;
private PrintWriter pw;
private Scanner sc;
private boolean flag;
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class BackClient {
}
银行对于个人主要的业务,是存款,取款,以及查询余额。
1建立顾客类:Account里面存顾客的id money,以及提供存款,取款,查询的方法。 2建立银行类 Bank: A,在构建Bank()类时,创建100个银行用户,每个用户存1块钱。存到一个List中 B,提供查询,存款,取款,方法, 注意:在存款,和取款时要加同步锁保持数据的一致(synchronized)。 3,银行服务类 BankService 该类实现了Runnable接口,其构造方法需要传入Socket,和一个Bank 另外在声明一个输出流pw 和一个输入流sc 在其run()方法中,根据协议,这里用sc.next()接受数据的; sc.next()和sc.nextLine()的区别: sc.next():如果下一个标记与从指定字符串构造的模式匹配,则返回下一个标记即它是逐个扫描,如果匹配到对应的模式,则返回这个字符串,因为这里协议是,通过空格将字符串分开的,故可以用这个方法。 sc.nextLine():是通过扫描整行的字符,然后再返回。 然后根据对应的字符,匹配到对应的方法。 4,服务器Server: 1,建立一个银行 2,创建一个服务器,并设定端口号 3,不断接受来自客户端的消息 4,建立银行服务,启动线程 5,客户端:BankClient A,建立与服务器的连接 B,建立流 C,发出各种协议
二,项目代码:
public class Account {
/** * @param args */ private int id; private double balance; //存钱 public void deposite(double money){ this.balance+=money; }; //取钱 public void withdraw(double money){ this.balance-=money; }; //查询余额 public double getBalance(){ return balance; } public Account(int id, double balance) { super(); this.id = id; this.balance = balance; } public Account() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public void setBalance(double balance) { this.balance = balance; }
}
import java.util.ArrayList;
import java.util.List;
public class Bank {
private List list=new ArrayList();
public Bank(){ for(int i=1;i<=100;i++){ list.add(new Account(i,1)); } } public Account find(int id){ Account account=null; for(Account a:list){ if(a.getId()==id){ account=a; break; } } return account; } //存钱 public Account deposite(int id,double money){ Account account=find(id); if(account==null){ throw new RuntimeException("查无此用户"+id); } synchronized (this) { account.deposite(money); return account; } } //取钱 public Account withdraw(int id,double money){ Account account=find(id); if(account==null){ throw new RuntimeException("查无此用户"+id); } synchronized (this) { if(account.getBalance()<money+1){ throw new RuntimeException("此用户"+id+",当前余额为:"+money+"余额不足,操作失败"); } account.withdraw(money); return account; } }
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class BankService implements Runnable {
private Socket s;
private Bank b;
private PrintWriter pw;
private Scanner sc;
private boolean flag;
public BankService(Socket s,Bank b){ this.s=s; this.b=b; try { pw=new PrintWriter(s.getOutputStream()); sc=new Scanner(s.getInputStream()); flag=true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { while(flag){ // TODO 1读取客户端传过来的数据 if(!sc.hasNext()){ flag=false; return; } //2解析协议 完成操作 String command =sc.next();//next() 如果下一个标记与从指定字符串构造的模式匹配,则返回下一个标记 //System.out.println(sc+"是什么。。。。。。。"); if("QUIT".equals(command)){ flag=false; break; }else{ Account account=null; if("DEPOSIT".equals(command)){ int id=sc.nextInt(); double money=sc.nextDouble(); account=b.deposite(id, money); }else if("WITHDRAW".equals(command)){ int id=sc.nextInt(); double money=sc.nextDouble(); account=b.withdraw(id, money); }else if(!"BALANCE".equals(command)){ pw.println("无效的协议......."); pw.flush(); break; }else{ int id=sc.nextInt(); account=b.find(id); } pw.println(account.getId()+" "+account.getBalance()); pw.flush(); } } }
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Bank b=new Bank(); ServerSocket ss=new ServerSocket(9999); while(true){ Socket s=ss.accept(); BankService bs=new BankService(s,b); Thread t=new Thread(bs); t.start(); } }
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class BackClient {
/** * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { //创建一个与服务器的连接 套接字 Socket s=new Socket("localhost",9999); //建立流 PrintWriter pw=new PrintWriter(s.getOutputStream()); Scanner sc=new Scanner(s.getInputStream()); //发出各种协议 //1,存 pw.println("DEPOSIT 1 1000"); pw.flush(); if(sc.hasNext()){ int accountid=sc.nextInt(); double money=sc.nextDouble(); System.out.println("存款成功,当前账户"+accountid+"余额为:"+money); } //2,取 pw.println("WITHDRAW 1 100"); pw.flush(); if(sc.hasNext()){ int accountid=sc.nextInt(); double money=sc.nextDouble(); System.out.println("取款成功,当前账户"+accountid+"余额为:"+money); } //3查询 pw.println("BALANCE 1"); pw.flush(); if(sc.hasNext()){ int accountid=sc.nextInt(); double money=sc.nextDouble(); System.out.println("查询当前账户"+accountid+"余额为:"+money); } //退出 pw.println("QUIT"); pw.flush(); sc.close(); }
}
相关文章推荐
- 以yyyyMMddHHmmss这种格式取得系统时间
- 关于c的4种算法以及实例演示
- nginx负载均衡的5策略
- JQuery判断radio是否选中并获取选中值的示例代码
- Python调用MongoDB使用心得
- 《掌握需求过程》阅读笔记二
- 黑苹果安装后不能调节分辨率
- Oracle 删除表中记录 如何释放表及表空间大小
- python调用Mongodb笔记
- Nginx绑定多个域名
- 信息安全系统设计基础第十一周
- MyEclipse中配置Strut.xml自动提示功能
- ASP.NET MVC - loop model data in javascript
- 计算机科学概论
- TextView使用SpannableString设置复合文本
- Java多线程同步与synchronized
- spring AOP
- HDOJ 2020 绝对值排序
- 二叉树n0 = n2 + 1
- 读取imei手机串号