望采纳,谢谢!
package 银行账户演示;
//import java.util.*;
//(1)定义一个账户类,包含用户名、密码、余额等属性、存钱、取钱等方法。
public class Account {
private String user;//用户名
private String passcode;//密码
private double balance;//余额
public Account(String user, String passcode, double balance) {
this.user = user;
this.passcode = passcode;
this.balance = balance;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPasscode() {
return passcode;
}
public void setPasscode(String passcode) {
this.passcode = passcode;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//存钱
public void savemoney(double money)
{
balance += money;
System.out.printf("%s存了%.2f元,账户余额为%.2f元。\n",user,money,balance);
}
//取钱
public void drawmoney(double money)
{
if( balance - money < 0)
{
System.out.println("账户余额不足,无法取钱");
}
else
{
balance -= money;
System.out.printf("%s取了%.2f元,账户余额为%.2f元。\n",user,money,balance);
}
}
public void show(){
//显示账户信息
System.out.printf("user:%s\n",user);
System.out.printf("balance:%.2f\n",balance);
}
}
package 银行账户演示;
import java.util.*;
//(2)定义一个银行类,包含银行名、账户列表等属性,开账户、查账户的功能。账户列表可以使用集合相关的类,如ArrayList,最好同时使用泛型。
public class Bank {
public String BankName;
private ArrayList<Account> account;
Bank(String bankName){
this.BankName=bankName;
account = new ArrayList<Account>();
}
public Bank(String BankName, ArrayList<Account> account) {
this.BankName = BankName;
this.account = account;
}
//开账户
public void OpenAccount(String user,String passcode,double money)
{
account.add(new Account(user,passcode,money));
}
//查找账户按用户名,若找到,返回该账户,否则,返回null
public Account SeekAccount(String username)
{
for(Account account : account)
{
if(username.equals(account.getUser()))
return account;
}
return null;
}
//显示该用户账户信息
public void showAccount(String userName){
Account account = SeekAccount(userName);
if(account == null)
System.out.println("该账户不存在。");
else
account.show();
}
//向特定账户存钱
public void saveMoney(String userName,double money){
Account account = SeekAccount(userName); //查找
if(account == null)
System.out.println("该账户不存在。");
else
account.savemoney(money);
}
//从特定账户取钱,需验证密码
public void drawMoney(String userName,double money,String password){
Account account = SeekAccount(userName); //查找
if(account==null)
System.out.println("该账户不存在。");
else
{
if(password.equals(account.getPasscode()))
{
account.drawmoney(money);
}
else
{
System.out.println("密码不正确,取钱失败。");
}
}
}
}
package 银行账户演示;
/*
做一个简单的银行、账户演示程序。
(1)定义一个账户类,包含用户名、密码、余额等属性、存钱、取钱等方法。
(2)定义一个银行类,包含银行名、账户列表等属性,开账户、查账户的功能。账户列表可以使用集合相关的类,如ArrayList,最好同时使用泛型。
(3)定义一个测试类及main函数,在其中创建一个银行,开设一些账号,模拟存钱、取钱、显示余额等。
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Bank bank = new Bank("中国农业银行");
bank.OpenAccount("梅西", "666666", 999999);
bank.OpenAccount("梅东", "99999", 10000);
bank.OpenAccount("梅方", "222222", 4000);
//存钱
bank.saveMoney("梅方", 100);
//从特定账户取钱,需验证密码
bank.drawMoney("梅方",1000,"222222");
//显示账户信息
bank.showAccount("梅方");
//存钱
bank.saveMoney("梅东", 10000);
//密码错误,取钱失败的情况
bank.drawMoney("梅东",20000,"222222");
//余额不足的情况
bank.drawMoney("梅西",1000000,"666666");
}
}