模仿银行简单的存取业务项目分析

一,项目分析:
银行对于个人主要的业务,是存款,取款,以及查询余额。

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();

}

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值