2在Bank类中,做成一个单例模式,对于在Accout类中使id自动生成时,需要创建一个getnextid的方法,并且在类的全局变量里面仍然需要id这个变量,只是需要在加上一个返回sid这个变量 作为getnextid的返回值;深刻理解到在抽象类中不一定有抽象方法,但是含有抽象方法的类一定是抽象类;单例模式就是把创建类对象的过程静态化,使其通过一个静态的方法来创建类对象,同时把无参构造方法的修饰符的权限改为private,使其不会被别的途径所创建
package
Business;
import Account.Account;
import Account.CreditAccount;
import Account.SavingAccount;
public class Bank {
private static Bank bank=null;
private Account[] as =new Account[100];
private int size;
//作为单例模式需要一个方法来构造一个Bank类型的对象
public static Bank newInstance(){
if(bank==null) bank=new Bank();
return bank;
}
private Bank(){}//为了能够使构造出来的Bank类型对象一样,所以把Bank的无参构造函数私有化,防止构造出其它Bank类型对象
//用户注册
public Account Register(String pass1,String pass2,String name,String personId,String email,int type){
if(!(pass1.equals(pass2))){
System.out.print("你输入的密码错误");
return null;
}
Account c=null;
if(type==0) c=new SavingAccount(pass1, name, personId, email);
else if(type==1) c=new CreditAccount(pass1,name,personId,email);
else return null;
//判断数组是否已经满了
if (size==as.length) expand();
as[size]=c;
size++;
return c;
}
//增大数组容量的方法
private void expand() {
Account[] a=new Account[as.length*2];
System.arraycopy(as,0,a,0,as.length);
as=a;
}
//用户登陆
public Account Login(long id,String pass1){
Account c =getAccountById(id);
if(c==null){
System.out.print("此账户不存在");
return null;
}
else if(!(pass1.equals(pass1)))
return null;
else System.out.println("欢迎您"+c.getName());
return c;
}
//通过id得到Account账户
private Account getAccountById(long id) {
for(int i=0;i<size;i++){
if(as[i].getId()==id) return as[i];
}
return null;
}
//设置信用额度
private Account setCeiling(long id,double ceiling){
Account c=getAccountById(id);
if(c instanceof CreditAccount){
CreditAccount ca=(CreditAccount)c;
ca.setCeiling(ceiling);
}
return c;
}
//存款方法
public Account depoist(long id,double money){
Account c=this.getAccountById(id);
c.deposit(money);
return c;
}
//取款方法
public Account withdraw(long id,double money){
Account c=this.getAccountById(id);
c.withdraw(money);
return c;
}
//统计所有账户余额总和
public double getAllBalance(){
double d=0;
for(int i=0;i<size;i++){
d=d+as[i].getBalance();
}
return d;
}
//统计所有信用账户透支额度总数
public double getAllCeilingBalance(){
double d=0;
for(int i=0;i<size;i++){
if(as[i] instanceof CreditAccount){
CreditAccount ca=(CreditAccount)as[i];
d=d+ca.getBalance();
}
}
return d;
}
public static void main(String[] args){
Bank b =newInstance();
b.Register( "123", "123", "jj", "123", "jj@163.com", 1);
b.Register( "456", "456", "ff", "456", "ff@163.com", 0);
b.setCeiling(10001L,100000);
b.depoist(10001L, 1000);
System.out.println(b.getAllBalance());
System.out.println(b.getAllCeilingBalance());
}
}
import Account.Account;
import Account.CreditAccount;
import Account.SavingAccount;
public class Bank {
private static Bank bank=null;
private Account[] as =new Account[100];
private int size;
//作为单例模式需要一个方法来构造一个Bank类型的对象
public static Bank newInstance(){
if(bank==null) bank=new Bank();
return bank;
}
private Bank(){}//为了能够使构造出来的Bank类型对象一样,所以把Bank的无参构造函数私有化,防止构造出其它Bank类型对象
//用户注册
public Account Register(String pass1,String pass2,String name,String personId,String email,int type){
if(!(pass1.equals(pass2))){
System.out.print("你输入的密码错误");
return null;
}
Account c=null;
if(type==0) c=new SavingAccount(pass1, name, personId, email);
else if(type==1) c=new CreditAccount(pass1,name,personId,email);
else return null;
//判断数组是否已经满了
if (size==as.length) expand();
as[size]=c;
size++;
return c;
}
//增大数组容量的方法
private void expand() {
Account[] a=new Account[as.length*2];
System.arraycopy(as,0,a,0,as.length);
as=a;
}
//用户登陆
public Account Login(long id,String pass1){
Account c =getAccountById(id);
if(c==null){
System.out.print("此账户不存在");
return null;
}
else if(!(pass1.equals(pass1)))
return null;
else System.out.println("欢迎您"+c.getName());
return c;
}
//通过id得到Account账户
private Account getAccountById(long id) {
for(int i=0;i<size;i++){
if(as[i].getId()==id) return as[i];
}
return null;
}
//设置信用额度
private Account setCeiling(long id,double ceiling){
Account c=getAccountById(id);
if(c instanceof CreditAccount){
CreditAccount ca=(CreditAccount)c;
ca.setCeiling(ceiling);
}
return c;
}
//存款方法
public Account depoist(long id,double money){
Account c=this.getAccountById(id);
c.deposit(money);
return c;
}
//取款方法
public Account withdraw(long id,double money){
Account c=this.getAccountById(id);
c.withdraw(money);
return c;
}
//统计所有账户余额总和
public double getAllBalance(){
double d=0;
for(int i=0;i<size;i++){
d=d+as[i].getBalance();
}
return d;
}
//统计所有信用账户透支额度总数
public double getAllCeilingBalance(){
double d=0;
for(int i=0;i<size;i++){
if(as[i] instanceof CreditAccount){
CreditAccount ca=(CreditAccount)as[i];
d=d+ca.getBalance();
}
}
return d;
}
public static void main(String[] args){
Bank b =newInstance();
b.Register( "123", "123", "jj", "123", "jj@163.com", 1);
b.Register( "456", "456", "ff", "456", "ff@163.com", 0);
b.setCeiling(10001L,100000);
b.depoist(10001L, 1000);
System.out.println(b.getAllBalance());
System.out.println(b.getAllCeilingBalance());
}
}
package
Account;
public abstract class Account {
private static long sid=10000;
private long id;
private String password;
private String name;
private String personId;
private String email;
private double balance;
public Account() {
}
public Account(String password, String name, String personId, String email) {
this.id=Account.getNextId();
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
}
public static long getNextId(){
sid++;
return sid;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public final void deposit(double money){
this.balance=this.balance+money;
}
public abstract void withdraw(double money);
public long getId() {
// TODO Auto-generated method stub
return id;
}
}
public abstract class Account {
private static long sid=10000;
private long id;
private String password;
private String name;
private String personId;
private String email;
private double balance;
public Account() {
}
public Account(String password, String name, String personId, String email) {
this.id=Account.getNextId();
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
}
public static long getNextId(){
sid++;
return sid;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public final void deposit(double money){
this.balance=this.balance+money;
}
public abstract void withdraw(double money);
public long getId() {
// TODO Auto-generated method stub
return id;
}
}