java上机 模拟简单的银行系统

Assignment
Part 3: BankSystem
Design BankSystem class. You need to design more than one class. For example: You need to
design a class BankAccount to model users' bank accounts. Probably different bank accounts
(CashAccount, CreditAccount, ...). The account should keep a user's name and
balance, accurate to the nearest cent... The user should be able to make deposits and
withdrawals on his/her account, as well as changing the account's name at any time. Also, the
system needs to be able to find out how many BankAccounts have been created in total. For
each account, only the last 6 Transactions should be able to store in ascending order and
be printed.
Note: The following part should be done after Exception chapter.
1. Add the CheckingAccount class of BankSystem , Designing and Using Classes
to throw an IllegalArgumentException in any of the following circumstances:
when the account is constructed with a negative balance,
when a negative amount is deposited, or
when the account is overdrawn (when the amount withdrawn exceeds the current
balance).
2. An IllegalArgumentException is an unchecked exception that is thrown to
indicate that a method has been passed an illegal or inappropriate argument. Instructions:
Add (modify if you've already finished) the CheckingAccount class to handle
errors and write a test program as indicated above.

需求分析
1.BankAccount
这个系统的基石无疑是先有一个银行账户。
结合生活中的银行账户,一个基本的银行账户应包含如下属性:
ac_id 账户id              
acname 账户名
username 用户名
password 密码
balance 余额
这是最基本的了,当然可能还有绑定电话,邮箱等,对于一个简单基本的bankaccount,其他属性我们不再进行考虑。
 接下来我们可以先将BankAccount类的基本框架创建出来
 为实现其它功能的方法可以在以后根据需要添加

import java.util.Scanner;

/*一个简单基本的银行账户应该包括账户id,账户名,用户名,账户密码,
 * 账户余额等
 * 
 */
public class BankAccount {
	protected long id;//账户id
	protected String name;//账户名
	protected String username;//用户名
	protected String password;//密码
	protected double balance;//余额
	protected String[] transaction=new String[6];//记录最后6条事务
	
	public long getid() {
		return id;
	}
	public void setid(long id) {
		this.id = id;
	}
	public String getAcname() {
		return name;
	}
	public void setname(String name) {
		
		
		this.name = name;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public void  Despoit(){//存款
		Scanner input =new Scanner(System.in);
		System.out.println("Please enter the deposit amount:");
		double money=input.nextDouble();
		double newbalance=this.balance+money;
		this.setBalance(newbalance);
		String type="Despoit";
		String change=""+money;
		Transactions_add(type, change);//添加事务记录
	}
	public void CheckBalance(){//查询余额
		String format = String.format("%.1f",balance);//转换为保留一位小数形式
		System.out.println("Account balance:"+format);
	}
	public void  Withdraw(){//取款
		Scanner input =new Scanner(System.in);
		System.out.println("Please enter the withdrawal amount:");
		double money=input.nextDouble();
		CheckingAccount.check_over(money, balance);
		if(CheckingAccount.check_over(money, balance)==1) {
			System.out.println("Withdraw successfully!");
			this.setBalance(balance-money);
			String type="Withdraw";
			String change=""+money;
			Transactions_add(type, change);//添加事务记录
															}
}
	public void change_acname() {//改变账户name
		
		Scanner input=new Scanner(System.in);
		System.out.println("Please input your new account name");
		String new_name=input.next();
		setname(new_name);
		String type="Change name";
		String change=new_name;
		Transactions_add(type, change);//添加事务记录
	}
	public void Transactions_add(String type,String change) {
		//记录事务  最多六条,按time升序存储,time越大越靠后
		int k=0;
		for(int i=0;i<6;i++) {
			if(transaction[i]==null) {
		transaction[i]="Type: "+type+"  Change: "+change;
		break;
			}	
		}
			for(int j=0;j<6;j++) {
				if(transaction[j]!=null)
					k++;
			}
			if (k==6) {   //无空处时 将后五条前移一位
				for(int m=0;m<5;m++)
					transaction[m]=transaction[m+1];
				transaction[5]="Type: "+type+"  Change: "+change;
			}
		}
		
		
	public void print_transaction() {
		System.out.println("The last 6 transactions are:");
		for(int i=1;i<=6;i++)
			System.out.println("Transaction "+i+": "+transaction[i-1]);
	}
	

 2.CashAccount
有了这样一个基本的银行账户后,我们根据功能可以添加一个现金账户或者说储蓄账户
现金账户记录所有结算资金流水以及因商户充值、代付和转账所产生的资金流水。但就就其成员而言与BankAccount并没有多少改变


public class CashAccount extends BankAccount{
public CashAccount(long id,String name,String username,String password,double balance){
		this.id=id;
		this.name=name;
		this.username=username;
		this.password=password;
		this.balance=balance;
	}

}

 3.CreditAccount
信用卡的使用也是银行系统中重要的一部分,CreditAccount需要多一个属性 ceiling 透支额度

import java.util.Scanner;

public class CreditAccount extends BankAccount {
		 private double ceiling;//信用额度
		 public CreditAccount(long id,String name,String username,String password,double ceiling,double balance){
		this.id=id;
		this.name=name;
		this.username=username;
		this.password=password;
		this.ceiling=ceiling;
		this.balance=balance;
	}
public double getCeiling() {//获取额度
		return ceiling;
	}
 
	public void setCeiling(double ceiling) {//设置额度
		this.ceiling = ceiling;
	}
public void is_over() {//是否超额
	if(balance+ceiling<0)
		System.out.println("Over Ceiling!");
	
}
//同名方法覆盖父类中的Withdraw
public void Withdraw() {//信用卡账户可以透支余额  在不超过额度的情况下 
	Scanner input =new Scanner(System.in);
	System.out.println("Please enter the withdrawal amount");
	double money=input.nextDouble();
CheckingAccount.check_over_ceiling(money, balance, ceiling);
if(CheckingAccount.check_over_ceiling(money, balance, ceiling)==1)
	this.setBalance(balance-money);
	String type="Withdraw";
	String change=""+money;
	Transactions_add(type, change);//添加事务记录

}
}

4.银行系统类

实现注册登录,调用账户类的方法进行查找,记录,更改,存取功能

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BankSystem {
	static int count=1;
	static int same=0;
private static List<BankAccount> list=new ArrayList<BankAccount>();//银行账户容器  上溯到List接口方便修改扩展和自定义
private int choice;
public void  openAccount(){//开户
	Scanner input =new Scanner(System.in);
	System.out.println("Please choose your account type:");
	System.out.println("1: cash account");
	System.out.println("2: credit account");
	
	int choice=input.nextInt();
	switch(choice){
		case 1://cash账户
			long aid=count ;//账户编号随着开户数量增加,新开户的id即为账户总数量
			System.out.println("Please input your cashaccount name:");
			String name=input.next();
			System.out.println("Please input your username:");
			String ausername=input.next();
			for(int i = 0;i < list.size(); i ++){
				 same=0;
		         if( list.get(i).username.equals(ausername)){
		        	 same=1;
		        	 break;
		         }
		        
	        }
				if(same==1) {
			System.out.println("This username has been used!");
			
			break;
				}
			System.out.println("Please input your password:");
			String password=input.next();
			System.out.println("Please input your initial balance:");
			double balance=input.nextDouble();
		
		if(CheckingAccount.check_negative(balance)==1) {//余额不能为负数   这里调用CheckAccount进行检查
			BankAccount ac=new CashAccount(aid,name,ausername,password,balance);
			list.add(ac);
			System.out.println("Your account has been created successfully!"+"\n"+"Your id is "+ac.getid());//开户成功
			System.out.println("There are "+count+" accounts in our system.");
			count++;}
			break;
		case 2://credit账户
			long aid1=count;//账户编号
			System.out.println("Please input your creditaccount name:");
			String name1=input.next();
			System.out.println("Please input your username:");
			String username1=input.next();
			System.out.println("Please input your password:");
			String password1=input.next();
			System.out.println("Please set your ceiling:");
			
			double ceilling=input.nextDouble();
			if(CheckingAccount.check_negative(ceilling)==1) {//额度也不能为负数
			System.out.println("Please input your initial balance:");
			double balance2=input.nextDouble();

			if(CheckingAccount.check_negative(balance2)==1) {
			BankAccount ac1=new CreditAccount(aid1,name1,username1,password1,ceilling,balance2);
			list.add(ac1);//将创建好的账户装入容器
			System.out.println("Your account has been created successfully!"+"\n"+"Your id is "+ac1.getid());
			System.out.println("There are "+count+"accounts in our system.");
			count++;}
			}
			break;
		default:
				System.out.println("Input Error!");
				break;
	}


}
//登录  以用户名和密码进行登录
	public void  login(){
		Scanner input =new Scanner(System.in);
		System.out.println("Please input your id:");
		long id=input.nextLong();
		System.out.println("Please input your password:");
		String password=input.next();
		
		BankAccount at=null;
		for(BankAccount ac:list){
			if(ac.getid()==id&&ac.getPassword().equals(password)){
				at=ac;
			}
		}
		if(at!=null){do {
			System.out.println("Login successfully!");
			System.out.println("Please choose the function you want:");
			System.out.println("1: Deposit");
			System.out.println("2: Withdraw money");
			System.out.println("3: Check the balance");
			System.out.println("4: Change the account name");
			System.out.println("5: Check the last 6 transactions");//查询最近6个事务
			System.out.println("6: Log out");//注销登录
			choice=input.nextInt();
			switch(choice){
		     	case 1:
		     		System.out.println("Welcome to use the deposit function:");
		     		at.Despoit();
		     	
		     		break;
		     	case 2:
		     		System.out.println("Welcome to use the withdrawal function:");
		     		at.Withdraw();
		     		break;
		     	case 3:
		     		System.out.println("Welcome to use the checkbalance function:");
		     		at.CheckBalance();
		     		break;
		     	case 4:
		     		System.out.println("Welcome to use the changename function:");
		     		at.change_acname();
		     		break;
		     	case 5:
		     		System.out.println("Welcome to use the checktransaction function:");
		     		at.print_transaction();
		     		break;
		     	case 6:
		     		break;
		     	default:
		     		System.out.println("ERROR!");
			}
		}while(choice!=6);
			
		}
		else{
			System.out.println("Your username or password is wrong!");
		}
		
	}


}

5.顾客类

作为客户使用的入口。

import java.util.Scanner;
 

//顾客使用id和密码进行登录
//账户name可以随时改变

public class Customer {
 
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		BankSystem bk=new BankSystem();
		int choice;
		do{
		System.out.println("Please choose function:");
		System.out.println("1:Open an account:");//开户
		System.out.println("2:Login:");//登录
		System.out.println("3:End and exit the system");//结束程序
		choice=input.nextInt();
		switch(choice){
			case 1:
				bk.openAccount();
				break;
			case 2:
				bk.login();			
		}
		}while(choice!=3);
	}
}

6.checkaccount类

对输入数据的有效性进行检查,有错则抛出异常

public class CheckingAccount {
public static int check_negative(double amount) {//检查输入负数异常
	int i = 1;
	try {
		if(amount<0)
			throw new IllegalArgumentException ();
	}
catch(IllegalArgumentException e){
	i=0;
	e.printStackTrace();
	System.out.println("Your input amount is negative");
	
}
finally {
	return i;
}
	}
public static int  check_over(double amount,double balance) {//检查现金账户取款溢出
	int i=1;
	try {
		if(amount>balance)
			throw new IllegalArgumentException ();
	}
catch(IllegalArgumentException e){
	i=0;
	e.printStackTrace();
	System.out.println("Your input amount is over balance");
	
}
finally {
	return i;
}
	}
public static int  check_over_ceiling(double amount,double balance,double ceiling) {//检查信用账户取款溢出
	int i=1;
	try { 
		if(amount>balance+ceiling)
			throw new IllegalArgumentException ();
	}
catch(IllegalArgumentException e){
	i=0;
	e.printStackTrace();
	System.out.println("Your input amount is over ceiling");
	
}
finally {
	return i;
}

	}

}

 

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值