用Java编写简单的银行系统

2024-05-15

1. 用Java编写简单的银行系统

  这个有点复杂,说点简单的思路
  1.首先要建立数据库表,保存数据需要保存到表中
  create table Account(
  账号ID
  密码
  有多少钱
  )

  如果要保存流水记录的话,还的建张表保存记录

  2.java
  账号类
  class Account{
  账号ID
  密码
  有多少钱
  }

  一个操作类
  class Test{
  开户,登录,存款,取款,查询余额,修改密码
  各写一个方法
  }

用Java编写简单的银行系统

2. JAVA 简单银行系统的代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AccountDemo {

	public static double MONEY = 0;// 初始化金额是100元。

	public static void main(String[] args) {
		final String USER_NAME = "zhangsan";// 用户名
		final String PASSWORD = "123456";// 密码

		while (true) {
			System.out.print("请输入用户名:");
			String user_name = getString();
			System.out.print("请输入密码:");
			String password = getString();
			if (user_name != null && user_name.equals(USER_NAME)
					&& password != null && password.equals(PASSWORD)) {
				System.out.println("登陆成功!你要干什么?");
				while (true) {
					System.out.println("1:存款");
					System.out.println("2:取款");
					System.out.println("3:查询余额");
					System.out.println("q:退出程序");
					System.out.print("请选择:");
					String userIn = getString();
					int in = 0;
					if (userIn != null && userIn.equals("1")) {
						in = Integer.parseInt(userIn);
					} else if (userIn != null && userIn.equals("2")) {
						in = Integer.parseInt(userIn);
					} else if (userIn != null && userIn.equals("3")) {
						in = Integer.parseInt(userIn);
					} else if (userIn != null
							&& userIn.trim().toUpperCase().equals("Q")) {
						in = 4;
					} else {
						System.out.println("你输入的指令不正确!请重新输入。");
						continue;
					}
					switch (in) {
					case 1:
						double add_money = 0;
						while (true) {
							System.out.print("请输入你要存入的金额:");
							try {
								add_money = Double.parseDouble(getString());
							} catch (Exception e) {
								System.out.println("金额输入不正确!");
								continue;
							}
							break;
						}
						MONEY += add_money;
						System.out.println("存入的金额是" + add_money
								+ "\r\n请选择你要的操作:");
						break;
					case 2:
						double money = 0;
						while (true) {
							System.out.print("请输入你要取出的金额:");
							try {
								money = Double.parseDouble(getString());
							} catch (Exception e) {
								System.out.println("金额输入不正确!");
								continue;
							}
							if (money > MONEY) {
								System.out.println("取出的金额大于现有存款,请重新输入要取出的金额!");
								continue;
							}
							break;
						}
						MONEY -= money;
						System.out.println("取出的金额是" + money + "\r\n请选择你要的操作:");
						break;
					case 3:
						System.out.println("你的余额是:" + MONEY + "\r\n请选择你要的操作:");
						break;
					case 4:
						System.out.println("程序退出!");
						return;
					}
				}

			} else {
				System.out.println("错误:用户名与密码不匹配!\r\n");
				System.out.println("按任意键:重新输入用户名和密码。");
				System.out.println("q:退出程序。");
				System.out.print("请选择:");
				String in = getString();
				if (in.trim().toUpperCase().equals("Q")) {
					break;
				}
			}
		}
	}

	public static String getString() {
		String str = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			str = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
}

3. 求:用JAVA语言编写的银行家算法的源代码

import java.util.*;

class ThreadTest {
  static int type = 4, num = 10; //定义资源数目和线程数目
  static int[] resource = new int[type]; //系统资源总数
  //static int[] copyResource = new int[type]; //副本
  static Random rand = new Random();
  static Bank[] bank = new Bank[num]; //线程组
  Bank temp = new Bank();

  public void init() {
    //初始化组中每个线程,随机填充系统资源总数
    for(int i = 0; i < type; i++)
      resource[i] = rand.nextInt(10) + 80;
    System.out.print("Resource:");
    for(int i = 0; i < type; i++)
      System.out.print(" " + resource[i]);
    System.out.println("");
    for(int i = 0; i < bank.length; i++)
      bank[i] = new Bank("#" + i);
  }
  public ThreadTest4() {
    init();
  }

  class Bank extends Thread {
    //银行家算法避免死锁
    public int[]
      max = new int[type], //总共需求量
      need = new int[type], //尚需资源量
      allocation = new int[type]; //已分配量
    private int[] 
      request = new int[type], //申请资源量
      copyResource = new int[type]; //资源副本
    private boolean isFinish = false; //线程是否完成
    int[][] table = new int[bank.length][type*4]; //二维资源分配表

    private void init() {
      // 随机填充总共、尚需、已分配量
      synchronized(resource) {
        for(int i = 0; i < type; i++) {
          max[i] = rand.nextInt(5) + 10;
          need[i] = rand.nextInt(10);
          allocation[i] = max[i] - need[i];
          resource[i] -= allocation[i]; //从系统资源中减去已分配的
        }
        printer();
        for(int i = 0; i < type; i++) {
          if(resource[i] < 0) {
            //若出现已分配量超出系统资源总数的错误则退出
            System.out.println("The summation of Threads' allocations is out of range!");
            System.exit(1);
          }
        }
      }
    }

    public Bank(String s) {
      setName(s);
      init();
      start();
    }
    public Bank() {
      //none
    }

    public void run() {
      try {
        sleep(rand.nextInt(2000));
      }
      catch(InterruptedException e) {
        throw new RuntimeException(e);
      }
      while(true) {
        //程序没有完成时一直不断申请资源
        if(askFor() == false) {
          try {
            sleep(1000);
          }
          catch(InterruptedException e) {
            throw new RuntimeException(e);
          }
        }
        else
          tryRequest();
        if(noNeed() == true)
          break;
      }
      //休眠一段时间模拟程序运行
      try {
        sleep(1000);
      }
      catch(InterruptedException e) {
        throw new RuntimeException(e);
      }
      System.out.println(getName() + " finish!");
      synchronized(resource) {
        //运行结束释放占有资源
        for(int i = 0; i < type; i++) {
          resource[i] += allocation[i];
          need[i] = allocation[i] = max[i] = 0;
        }
      }
    }

    private void printer() {
      //打印当前资源信息
      System.out.print(getName() + " Max:");
      for(int i = 0; i < type; i++)
        System.out.print(" " + max[i]);
      System.out.print(" Allocation:");
      for(int i = 0; i < type; i++)
        System.out.print(" " + allocation[i]);
      System.out.print(" Need:");
      for(int i = 0; i < type; i++)
        System.out.print(" " + need[i]);
      System.out.print(" Available:");
      for(int i = 0; i < type; i++)
        System.out.print(" " + resource[i]);
      System.out.println("");
    }
    private boolean askFor() {
      //随机产生申请资源量并检测是否超标
      boolean canAsk = false;
      for(int i = 0; i < type; i++) {
        request[i] = rand.nextInt(20);
        //防止申请量超过所需量
        if(request[i] > need[i])
          request[i] = need[i];
      }
      for(int i = 0; i < type; i++) //防止随机申请资源全为0
        if(request[i] > 0)
          canAsk = true;
      synchronized(resource) {
        //锁住可供资源检查是否超标
        for(int i = 0; i < type; i++) {
          if(request[i] > resource[i])
            //如果申请资源超过可供资源则等待一段时间后重新申请
            return false;
        }
      }
      return canAsk;
    }
    private void tryRequest() {
      //创建副本尝试分配请求
      synchronized(resource) {
        for(int i = 0; i < type; i++)
          //依然要防止请求量超出范围
          if(request[i] > resource[i])
            return;
        for(int i = 0; i < type; i++) {
          //复制资源量并减去需求量到一个副本上
          copyResource[i] = resource[i];
          copyResource[i] -= request[i];
        }
        System.out.print(getName() + " ask for:");
        for(int i = 0; i < type; i++)
          System.out.print(" " + request[i]);
        System.out.println("");
        if(checkSafe() == true) {
          //如果检查安全则将副本值赋给资源量并修改占有量和需求量
          for(int i = 0; i < type; i++) {
            resource[i] = copyResource[i];
            allocation[i] += request[i];
            need[i] -= request[i];
          }
          System.out.println(getName() + " request succeed!");
        }
        else
          System.out.println(getName() + " request fail!");
      }
    }
    private boolean checkSafe() {
      //银行家算法检查安全性
      synchronized(bank) {
        //将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type*2所需资源/type*2~type*3占有资源/type*3~-1可用+占用资源
        for(int i = 0; i < bank.length; i++) {
          for(int j = type; j < type*2; j++) {
            table[i][j] = bank[i].need[j%type];
          }
          for(int j = type*2; j < type*3; j++) {
            table[i][j] = bank[i].allocation[j%type];
          }
        }
        //冒泡排序按需求资源从小到大排
        for(int i = 0; i < bank.length; i++) {
          for(int j = i; j < bank.length-1; j++) {
            sort(j, 4);
          }
        }
        //进行此时刻的安全性检查
        for(int i = 0; i < type; i++) {
          table[0][i] = copyResource[i];
          table[0][i+type*3] = table[0][i] + table[0][i+type*2];
          if(table[0][i+type*3] < table[1][i+type])
            return false;
        }
        for(int j = 1; j < bank.length-1; j++) {
          for(int k = 0; k < type; k++) {
            table[j][k] = table[j-1][k+type*3];
            table[j][k+type*3] = table[j][k] + table[j][k+type*2];
            if(table[j][k+type*3] < table[j+1][k+type])
              return false;
          }
        }
      }
      return true;
    }
    private void sort(int j, int k) {
      //递归冒泡排序
      int tempNum;
      if(table[j][k] > table[j+1][k]) {
        for(int i = type; i < type*2; i++) {
          tempNum = table[j][i];
          table[j][i] = table[j+1][i];
          table[j+1][i] = tempNum;
        }
        /*temp = bank[j];
        bank[j] = bank[j+1];
        bank[j+1] = temp;*/
      }
      else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围
        sort(j, k+1);
    }
    private boolean noNeed() {
      //是否还需要资源
      boolean finish = true;
      for(int i = 0; i < type; i++) {
        if(need[i] != 0) {
          finish = false;
          break;
        }
      }
      return finish;
    }
  }

  public static void main(String[] args) {
    ThreadTest t = new ThreadTest();
    //后台线程,设定程序运行多长时间后自动结束
    new Timeout(30000, "---Stop!!!---");
  }
}

求:用JAVA语言编写的银行家算法的源代码

4. 用java写一个仿银行系统

这个也就太简单了, 不牵涉实际的业务逻辑的话 ,实际上就是创建数据库,写一个用户界面, 脱离了柜台业务逻辑后 事实上也就跟一个论坛的用户管理形式差不多了

实在是没写的心情, lz 如果这是你的作业, 最好自己写一下 , 印象会深很多

5. 使用JAVA编写一个简单的银行存取款程序

package com.lw.thread;

/*
 银行账户类Account(不能透支),
 包含账号id(10~16位数字),密码password(6位数字),户主姓名name,余额balence
 */
public class Account {
	private String id;
	private int password;
	private String name;
	private double balence;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getPassword() {
		return password;
	}

	public void setPassword(int password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getBalence() {
		return balence;
	}

	public void setBalence(double balence) {
		this.balence = balence;
	}

	/*
	* 默认构造账户信息为:1111111111111111,666666,钱三多,888888.88。
	*/
	public Account() {
		super();
		this.id = "1111111111111111";
		this.password = 666666;
		this.name = "钱三多";
		this.balence = 888888.88;
	}

	/*
	* 另一个构造方法带4个参数分别初始化4个属性(带数据有效性验证)。
	*/
	public Account(String id, int password, String name, double balence) {

		this.id = id;
		this.password = password;
		this.name = name;
		this.balence = balence;
	}

	
	/*
	* 查询余额
	*/
	public static double selectMoney(Account account) {

		return account.getBalence();
	}

	/*
	* 存钱
	*/
	public static String setMoney(Account account, double balence) {
		if (balence < 0) {
			return "存钱失败,请正确放入!";
		}
		double d = balence + account.getBalence();
		account.setBalence(d);
		return "您存入了" + balence + "元,现账户余额为+" + d;
	}

	/*
	* 取钱
	*/
	public static String getMoney(Account account, double balence) {
		double d = account.getBalence();
		if (balence > d) {
			return "您的余额不足!";
		}
		account.setBalence(d - balence);
		return "您取出了" + balence + "元,现账户余额为+" + account.getBalence();
	}
}

使用JAVA编写一个简单的银行存取款程序

6. 求用JAVA语言编写的银行家算法的源代码

import java.util.*; class ThreadTest { static int type = 4, num = 10; //定义资源数目和线程数目 static int[] resource = new int[type]; //系统资源总数 //static int[] copyResource = new int[type]; //副本 static Random rand = new Random(); static Bank[] bank = new Bank[num]; //线程组 Bank temp = new Bank(); public void init() { //初始化组中每个线程,随机填充系统资源总数 for(int i = 0; i  need[i]) request[i] = need[i]; } for(int i = 0; i  0) canAsk = true; synchronized(resource) { //锁住可供资源检查是否超标 for(int i = 0; i  resource[i]) //如果申请资源超过可供资源则等待一段时间后重新申请 return false; } } return canAsk; } private void tryRequest() { //创建副本尝试分配请求 synchronized(resource) { for(int i = 0; i  resource[i]) return; for(int i = 0; i  table[j+1][k]) { for(int i = type; i < type*2; i++) { tempNum = table[j][i]; table[j][i] = table[j+1][i]; table[j+1][i] = tempNum; } /*temp = bank[j]; bank[j] = bank[j+1]; bank[j+1] = temp;*/ } else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围 sort(j, k+1); } private boolean noNeed() { //是否还需要资源 boolean finish = true; for(int i = 0; i < type; i++) { if(need[i] != 0) { finish = false; break; } } return finish; } } public static void main(String[] args) { ThreadTest t = new ThreadTest(); //后台线程,设定程序运行多长时间后自动结束 new Timeout(30000, "---Stop!!!---"); } }

7. 求一个面向对象编写的一个简易JAVA的银行管理系统

添加一个静态全局遍历Map  ACCOUNTINFO ;
创建一个用户实体类User 包括以下几个字段id(辨识用户唯一性的) ,name ,password,money
在静态块中初始化为ACCOUNTINFO里面存值也就存储User对象
然后就可以做登录账户 存储 转款的操作了。

求一个面向对象编写的一个简易JAVA的银行管理系统

8. 用Java编写一个银行管理系统,包括数据库等详细东西!!!

哦,那个我以前写过一个,给别人发去了,后来电脑出了毛病,东西都格式化了,现在电脑里没有了。不好意思。。。。