“\ T”选项卡,当输出数据显示,几乎以表格形式显示,目的是为了使数据看起来整齐点。
“\ n”换行,其他行显示下一个(),目的是换行字符或字符串,使用Systemoutprintln();效果是一样的。
定义Person类作为存储数据的基础
定义一个银行的数据库,模拟初始化银行的客户和资金,并定义一个查询的方法
定义一个类似ATM机,让用户输入并查找
先构建一个客户端,再构建一个服务器端,其实做一个简单的界面,建立一个数据库,调用SQl 语句,,实现单机,模拟多线程的就可以了,服务器部分不做也可以模拟出来。。
PS:这不会是程序专题训练一吧。。。
//您可以使用重入锁实现排队。
package comlw;
import javautilconcurrentlocksLock;
import javautilconcurrentlocksReentrantLock;
public class ReentrantLockDemo implements Runnable {
private int number = 0;// 创建一个变量
private Lock lock = new ReentrantLock();// 创建重入锁对象
@Override
public void run() {
locklock();// 打开锁
try {
for (int i = 0; i < 5; i++) {
try {
Threadsleep(100);// 线程休眠01秒
} catch (InterruptedException e) {
eprintStackTrace();
}
// 输出当前线程的名称和number的值,每次循环之后number的值都会加一
Systemoutprintln(ThreadcurrentThread()getName() + ": "
+ number++);
}
} finally {
lockunlock();// 释放锁
}
}
public static void main(String[] args) {
ReentrantLockDemo run = new ReentrantLockDemo();// 获得ReentrantLockDemo对象
Thread thread1 = new Thread(run);// 创建线程1
Thread thread2 = new Thread(run);// 创建线程2
thread1start();// 运行线程1
thread2start();// 运行线程2
}
}
public class Account {
protected String accId;
protected String name;
protected double money;
public Account (String accId,String name){
this(accId,name,0);
}
public Account (String accId,String name,double money){
thisaccId = accId;
thisname = name;
thismoney = money;
}
public void saveMoney(double money){
if(money <= 0){
Systemoutprintln("存款金额必须大于0");
}
thismoney += money;
Systemoutprintln("存款成功");
}
public double getMoney(double money){
if(money <= 0){
Systemoutprintln("取款金额必须大于0");
return 0;
}
if(thismoney <= money){
Systemoutprintln("余额不足,无法取款");
return 0;
}
thismoney -= money;
Systemoutprintln("取款成功");
return money;
}
public double getBalance(){
return thismoney;
}
protected double getOverdraft(){
return 0;
}
// 实现了equals方法,list比较时才能正确
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(this == obj){
return true;
}
if(obj instanceof Account){
return thisaccIdequals(((Account)obj)accId);
}
return false;
}
@Override
public String toString() {
return "账户=" + accId + ",名字=" + name + ",余额=" + money;
}
}
public class Bank {
// Account实现了equals方法,list查找时才能正确
private List<Account> usersAccounts;
public Bank() {
usersAccounts = new ArrayList<Account>();
}
public void addAccount(Account account) {
if (usersAccountscontains(account)) {
Systemoutprintln("添加失败,不能添加同样的账户");
return;
}
usersAccountsadd(account);
}
public boolean delAccount(Account account) {
return usersAccountsremove(account);
}
public boolean delAccount(String accId) {
return delAccount(new Account(accId, null));
}
public boolean existAccount(Account account) {
return usersAccountscontains(account);
}
public boolean existAccount(String accId) {
return existAccount(new Account(accId, null));
}
public Account getAccount(String accId){
return usersAccountsget(usersAccountsindexOf(new Account(accId, null)));
}
public double getAllMoney() {
// 不考虑是否溢出,只是把所有用户余额相加
double result = 0;
for (Account account : usersAccounts) {
result += accountgetBalance();
}
return result;
}
public double getAllOverdraft() {
// 不考虑是否溢出
double result = 0;
for (Account account : usersAccounts) {
result += accountgetOverdraft();
}
return result;
}
public int getAccountNum() {
return usersAccountssize();
}
public int getCreditAccountNum() {
int num = 0;
for (Account account : usersAccounts) {
// instanceof 性能没有简单的方法快。
if (account instanceof CreditAccount) {
num++;
}
}
return num;
}
public int getSavingAccountNum() {
int num = 0;
for (Account account : usersAccounts) {
if (account instanceof SavingAccount) {
num++;
}
}
return num;
}
public List<Account> getAllAccount() {
return usersAccounts;
}
}
public class CreditAccount extends Account{
private double overdraft;
public CreditAccount(String accId,String name){
super(accId, name);
thisoverdraft = 1000;
}
public CreditAccount(String accId,String name,double money){
this(accId, name,money,1000);
}
public CreditAccount(String accId,String name,double money,double overdraft){
super(accId, name,money);
thisoverdraft = overdraft;
}
@Override
public double getMoney(double money) {
if(money <= 0){
Systemoutprintln("取款金额必须大于0");
return 0;
}
if(thismoney + overdraft <= money){
Systemoutprintln("余额不足,无法取款");
return 0;
}
thismoney -= money;
Systemoutprintln("取款成功");
return money;
}
@Override
public double getOverdraft(){
return overdraft;
}
@Override
public String toString() {
return "账户=" + accId + ",名字=" + name + ",余额=" + money + ",透支=" + overdraft;
}
}
public class SavingAccount extends Account {
public SavingAccount(String accId, String name) {
super(accId, name);
}
public SavingAccount(String accId, String name, double money) {
super(accId, name, money);
}
@Override
public double getMoney(double money) {
return supergetMoney(money);
}
@Override
public double getOverdraft() {
return supergetOverdraft();
}
}
public class Test {
private static Bank bank = new Bank();
public static void main(String[] args) {
TestgenAccount();
// 开户
Account a1 = new CreditAccount("1", "1", 200, 2000);
Account a2 = new SavingAccount("2", "2", 300);
Account a3 = new SavingAccount("3", "3", 400);
Account a4 = new CreditAccount("4", "4", 500, 2000);
Account a5 = new CreditAccount("4", "5", 600, 2000); // 帐号4重
bankaddAccount(a1);
bankaddAccount(a2);
bankaddAccount(a3);
bankaddAccount(a4);
bankaddAccount(a5);
showNowAccount();
// 销户
bankdelAccount("1");
bankdelAccount("2");
showNowAccount();
// 存款
if(bankexistAccount("3")){
Account acc = bankgetAccount("3");
accsaveMoney(100);
}
showNowAccount();
// 取款
if(bankexistAccount("3")){
Account acc = bankgetAccount("3");
Systemoutprintln("余额=" + accgetBalance());
accgetMoney(100);
Systemoutprintln("余额=" + accgetBalance());
accgetMoney(1000);
Systemoutprintln("余额=" + accgetBalance());
}
if(bankexistAccount("4")){
Account acc = bankgetAccount("4");
Systemoutprintln("余额=" + accgetBalance());
accgetMoney(100);
Systemoutprintln("余额=" + accgetBalance());
accgetMoney(1000);
Systemoutprintln("余额=" + accgetBalance());
accgetMoney(10000);
Systemoutprintln("余额=" + accgetBalance());
}
Systemoutprintln(bankgetAccountNum());
Systemoutprintln(bankgetAllMoney());
Systemoutprintln(bankgetAllOverdraft());
Systemoutprintln(bankgetCreditAccountNum());
Systemoutprintln(bankgetSavingAccountNum());
}
public static void genAccount(){
String s = "1000 0000 0000 000";
Account a = null;
for(int i = 1; i < 11; i ++){
if((i & 2) == 0){
a = new CreditAccount(s + StringvalueOf(i), "账户" + StringvalueOf(i));
} else {
a = new SavingAccount(s + StringvalueOf(i), "账户" + StringvalueOf(i));
}
bankaddAccount(a);
}
}
public static void showNowAccount(){
for(Account account : bankgetAllAccount()){
Systemoutprintln(account);
}
}
}
以上就是关于java需要写一个JAVA程序,模拟银行账户的查询、转账功能。全部的内容,包括:java需要写一个JAVA程序,模拟银行账户的查询、转账功能。、java银行余额查询程序设计、用java编写多线程银行ATM 模拟程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)