码迷,mamicode.com
首页 > 编程语言 > 详细

java之 ------ 类的封装、继承和多态(二)

时间:2015-05-02 13:55:41      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

问题:声明银行账户类,成员变量包括账号、储户姓名、开启时间、身份证号码、存款余额等账户信息,成员方法包括开户、存款、取款、查询(余额、明细)、销户等操作。

主要成员描述如下

public class Account {                        	//账户类
    private String name;                   	//储户姓名
    private double balance;                  	//账户余额
    public Account(String name)             	//构造方法,参数指定储户姓名
    public String getName()                	//返回账户名
    public double balance()                 	//查看账户余额
    public boolean put(double value)         	//存款操作,参数为存入金额
    public double get(double value)           //取款操作,参数为取款金额,返回实际取到金额
}



import java.util.Scanner;
import java.math.*;
class Account{
	String name;
	static int count=0;
	static String[] opea=new String[10000];
	static double[] val=new double[1000];
	double balance;
	static  String accountID="6215000000000000000";
	String id;
	public Account(String name,double balance,String id){
		this.set(name,balance,id);
	}
	public void set(String name,double balance,String id){
		this.name=name;
		this.balance=balance;
		if(id.length()>18||id.length()<1){
			System.out.println("Input ID Error !");
			return ;
		}
		this.id=id;
		accountID=add(accountID);
	}
	public void inquire(){
		System.out.println("-------balance and trad information -------");
		System.out.println("balance : "+this.balance);
		for(int i=0;i<count;i++){
			System.out.println(opea[i]+" "+val[i]);
		}
	}
	public String getName(){
		return "AccoutName : "+this.name+"\nAccountID "+this.accountID+"\nID: "+id.substring(0,6)+"********"+id.substring(14,id.length());
	}
	public double balance(){
		return this.balance;
	}
	public boolean put(double value){
		this.balance+=value;
		if(value==0){
			return false;
		}
		opea[count]="put";
		val[count++]=value;
		return true;
	}
	public double get(double value){
		if(value>this.balance){
			System.out.println("lack of balance !!!");
			return 0;
		}
		opea[count]="get";
		val[count++]=value;
		this.balance-=value;
		return value;
	}
	public boolean exit(){
		return true;
	}
	public static String add(String str){
			char[] c=new char[19];
			int leng=19;
			int sign=0,i;
			for(i=0;i<19;i++){
				leng=leng-1;
				int sum=0;
				sum+=str.charAt(leng)-'0';
				if(i==0){
					sum+=1;
				}
				sum+=sign;
				c[leng]=(char)(sum+48);
				if(sum>=10){
					sign=1;
					c[leng]=(char)(sum%10+48);
				}else{
					sign=0;
				}
			}
			return String.valueOf(c);
	}
}
class Main{
	public static void main(String[] args){
		Account p=new Account("Tom",1000,"340822123409231190");
		System.out.println("-------Accoutn information-------");
		System.out.printf(p.getName());
		System.out.println();
		System.out.println("balance : "+p.balance());
		p.put(123213);
		p.inquire();
		System.out.println();
		
		Account p2=new Account("Sex",100000,"340822123434533454");
		System.out.println("-------Accoutn information-------");
		System.out.printf(p2.getName());
		System.out.println();
		System.out.println("balance : "+p2.balance());
		p2.get(12);
		System.out.println("balance : "+p2.balance());
		p2.get(12000000);
		System.out.println("balance : "+p2.balance());
		p2.inquire();
		System.out.println();
	}
}


java之 ------ 类的封装、继承和多态(二)

标签:

原文地址:http://blog.csdn.net/u011479875/article/details/45438751

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!