##Java 类和对象的实验

一 实验内容

1、设计一个名为Account的类,它包括:

(1)一个名为id的int类型私有账户数据域(默认值为0);

(2)一个名为balance的double类型私有账户数据域(默认值为0);

(3)一个名为annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。

(4)一个名为dateCreate的Date类型私有数据域存储账户的开户日期;

(5)一个能创建默认账户的无参构造方法;

(6)一个能创建带特定id和初始余额的账户的构造方法;

(7)id、balance和annualInterestRate的访问器和修改器;

(8)dateCreated的访问器;

(9)一个名为getMonthInterestRate()的方法返回月利率;

(10)一个名为withDraw的方法从账户提取特定数额;

(11)一个名为deposit的方法向账户存储特定数额;

画出该类的UML图,实现这个类,编写一个测试程序,创建一个账户ID为1122、余额为20000元,年利率为5%的Account对象,使用withDraw方法取款3000元,使用deposit方法存款5000元,然后打印余额、月利息以及这个账户的开户日期。

2、定义一个Account类来建模一个银行账户。一个账户有账号、余额、年利率、开户日期等属性,以及存款和取款等方法,创建两个检测支票账户(checking account)和储蓄账户(saving account)的子类,支票账户有个透支限定额,但储蓄账户不能透支。

画出这些类的UML图,实现这些类,编写一个测试程序,创建Account、SavingAccount和CheckingAccount的对象,然后调用它们的toString()方法。

3、根据要求设计一个新的Account类:

(1)添加一个String类型的新数据name类存储客户的名字;

(2)添加一个新的构造方法,该方法创建一个带指定名字、id和收支额的账户;

(3)添加一个名为transactions的新数据域,它的类型是ArrayList,可以为账户存储交易,每笔交易都是一个Transaction类的实例,Transaction类的定义如下图所示。

(4)修改withdraw和deposit方法,向transactions数组线性表添加一笔交易。

(5)其它所有属性和方法都和练习题2一样。

编写一个测试程序,创建一个年利率为1.5%、收支额为10000、id为1122而名字为江山的Account。向该账户存入500元、1000元和5000元并从该账户中取出100元、2000元和3000元。打印账户清单,显示账户持有者名字、利率、收支额和所有交易。

用Java写一个Account类 java设计一个名为account的类_java

Transaction类描述银行账户的一笔交易

4、利用上述的Account类,模拟一台ATM机。创建一个有10个账户的数组,其id为0,1,…,9,并初始化收支为100元人民币,系统提示用户输入一个id,如果输入的id不正确,提示用户输入正确的id,一旦接受一个id,就显示下图所示的主菜单,可以选择1来查看当前的收支,选择2表示取钱,选择3表示存钱,选择4表示退出主菜单。一旦退出,系统就会提示再次输入id。因此系统一旦启动就不会停止。
画出类的UML图,实现这个类,编写一个测试程序完成上述功能。

Enter an id: 3
Main menu
 1: check balance
 2: withdraw
 3: deposit
 4: exit
 Enter a choice: 1
 The balance is 100.0Main menu
 1: check balance
 2: withdraw
 3: deposit
 4: exit
 Enter a choice: 2
 Enter an amount to withdraw: 1000
 The amount is too large, ignored
 Main menu
 1: check balance
 2: withdraw
 3: deposit
 4: exit
 Enter a choice: 2
 Enter an amount to withdraw: 30Main menu
 1: check balance
 2: withdraw
 3: deposit
 4: exit
 Enter a choice: 3
 Enter an amount to deposit: 12Main menu
 1: check balance
 2: withdraw
 3: deposit
 4: exit
 Enter a choice: 4
 Enter an id: 3
import java.util.Date;
public class Account {
	protected int id;//账户数据域
	protected double balance=0;//账户余额数据域
	protected double annualInterestRate=0;//数据域存储当前率,假设每个账户都有相同的利率
	protected Date dateCreate;//开户日期
	public  Account()//创建默认账户的无参构造
	{
		super();
		this.dateCreate=new Date();
	}
	protected Account(int id,double balance) {
		this.id = id;
		this.balance = balance;
		dateCreate = new Date();
	}
	protected Account(int id,double balance,double annualIntegerRate) {
		this(id,balance);
		dateCreate = new Date();
		this.annualInterestRate = annualIntegerRate;
	}
	//账户,余额,利率访问器和修改器
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id=id;
	}
	public double getBalance()
	{
		return balance;
	}
	public void setBalance(double balancce)
	{
		this.balance=balance;
	}
	public double getAnnualInterestRate()
	{
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate)
	{
		this.annualInterestRate=annualInterestRate;
	}
	//访问器和修改器结束
	//开户日期访问器
	public Date getDateCreate()
	{
		return dateCreate;
	}
	public void setDateCreate(Date dateCreate)
	{
		this.dateCreate=dateCreate;
	}
	public double getMonthInterRate()
	{
		return annualInterestRate;
	}
	public void withDraw(double Decreasemoney)//取钱
	{
		if(Decreasemoney>balance)
		{
			System.out.println("您的账户余额不足!");
		}
		else
		this.balance-=Decreasemoney;
	}
	public void deposit(double Increasemoney)//存钱
	{
		this.balance+=Increasemoney;
	}
}
public class Checking_account extends Account {//支票账户,有透支限定额
public Checking_account() {
		
	}		
	public Checking_account(int id,double balance) {
		super(id,balance);
	}
	
	public Checking_account(int id,double balance,double annualInterestRate) 
	{
		super(id,balance,annualInterestRate);
	}
	public String toString() {
		return "Account typte:CheckingAccount\n"+super.toString();
	}
}
public class Saving_account extends Account {//储蓄账户,不能透支
public Saving_account() {

		}

		public Saving_account(int id,double balance) {
			super(id,balance);
		}

		public Saving_account(int id,double balance,double annualIntegerRate) {
			super(id,balance,annualIntegerRate);
		}
		protected void withDaraw(double withDarawBalance) {
			if(balance-withDarawBalance<0) {
				System.out.println("账户余额不足!余额为:"+getBalance());
			}
			
			else {
				balance = balance - withDarawBalance;
			}	
		}
 
		public String toString() {
			return "Account typte:SavingAccount\n"+super.toString();
		}
}
package tu;

public class Test_Account {
public static void main(String args[])
{
	int id1 = 1122;
	int id2 = 2211;
	double balance = 10000;
	double annualInterestRate = 0.0025;
	
	System.out.println("*****************************************Checking_account*************************************\n");
	Checking_account check1 = new Checking_account();
	Checking_account check2 = new Checking_account(id1,balance);
	Checking_account check3 = new Checking_account(id2,balance,annualInterestRate);
	
	System.out.println("---------------------Check1---------------------\n");
	System.out.println(check1.getDateCreate());
	System.out.println(check1.getId());
	System.out.println(check1.getBalance());
	System.out.println(check1.getAnnualInterestRate());
	
	double depositBalance = 1000;
	check1.deposit(depositBalance);
	System.out.println(check1.getBalance());
	
	double  withDarawBalance = 1000;
	check1.withDraw(withDarawBalance);
	System.out.println(check1.getBalance());
	System.out.println();
	System.out.println(check1.toString());
	
	
	System.out.println("---------------------Check2---------------------\n");
	System.out.println(check2.getDateCreate());
	System.out.println(check2.getId());
	System.out.println(check2.getBalance());
	System.out.println(check2.getAnnualInterestRate());
	
	check2.deposit(depositBalance);
	System.out.println(check2.getBalance());
	
	check2.withDraw(withDarawBalance);
	System.out.println(check2.getBalance());
	System.out.println();
	System.out.println(check2.toString());
	
	
	System.out.println("---------------------Check3---------------------\n");
	System.out.println(check3.getDateCreate());
	System.out.println(check3.getId());
	System.out.println(check3.getBalance());
	System.out.println(check3.getAnnualInterestRate());
	
	check3.deposit(depositBalance);
	System.out.println(check2.getBalance());
	
	check3.withDraw(withDarawBalance);
	System.out.println(check3.getBalance());
	System.out.println();
	System.out.println(check3.toString());
	
	
	System.out.println("---------------------Check3---------------------\n");
	System.out.println(check3.getDateCreate());
	System.out.println(check3.getId());
	System.out.println(check3.getBalance());
	System.out.println(check3.getAnnualInterestRate());
	
	check3.deposit(depositBalance);
	System.out.println(check2.getBalance());
	
	check3.withDraw(200000);
	System.out.println(check3.getBalance());
	System.out.println();
	System.out.println(check3.toString());
	
	
	System.out.println("*****************************************Saving_account*************************************");
	Saving_account saving1 = new Saving_account();
	Saving_account saving2 = new Saving_account(id1,balance);
	Saving_account saving3 = new Saving_account(id2,balance,annualInterestRate);
	
	System.out.println("---------------------saving1---------------------\n");
	System.out.println(saving1.getDateCreate());
	System.out.println(saving1.getId());
	System.out.println(saving1.getBalance());
	System.out.println(saving1.getAnnualInterestRate());
	
	saving1.deposit(depositBalance);
	System.out.println(check1.getBalance());
	
	saving1.withDaraw(withDarawBalance);
	System.out.println(saving1.getBalance());
	System.out.println();
	System.out.println(saving1.toString());
	
	System.out.println("---------------------saving2---------------------\n");
	System.out.println(saving2.getDateCreate());
	System.out.println(saving2.getId());
	System.out.println(saving2.getBalance());
	System.out.println(saving2.getAnnualInterestRate());
	
	saving2.deposit(depositBalance);
	System.out.println(check2.getBalance());
	
	saving2.withDraw(withDarawBalance);
	System.out.println(saving2.getBalance());
	System.out.println();
	System.out.println(saving2.toString());
	
	System.out.println("---------------------saving3---------------------\n");
	System.out.println(saving3.getDateCreate());
	System.out.println(saving3.getId());
	System.out.println(saving3.getBalance());
	System.out.println(saving3.getAnnualInterestRate());
	
	saving3.deposit(depositBalance);
	System.out.println(saving3.getBalance());
	
	saving3.withDraw(withDarawBalance);
	System.out.println(saving3.getBalance());
	System.out.println();
	System.out.println(saving3.toString());
	
	System.out.println("---------------------saving4---------------------\n");
	System.out.println(saving1.getDateCreate());
	System.out.println(saving1.getId());
	System.out.println(saving1.getBalance());
	System.out.println(saving1.getAnnualInterestRate());
	
	saving1.deposit(depositBalance);
	System.out.println(check1.getBalance());
	
	saving1.withDraw(20000);
	System.out.println(saving1.getBalance());
	System.out.println();
	System.out.println(saving1.toString());
	
}
}
3.
public class Account {
	private String name;
	private int id;
	private double balance;
	private double annualInterestRate;
	private Date dateCreate=new Date();
	public List<Integer> transactions=new ArrayList<Integer>();
	public Account() {
		;
	}
	public Account(String name,int id,double balance)
	{
		this.name=name;
		this.id=id;
		this.balance=balance;
	}
	public double getMonthInterestRate()
	{
		return balance*annualInterestRate/12;
	}
	public void withDraw(int money) {
		transactions.add(-money);
		this.balance-=money;
	}
	public void deposit(int money) {
		transactions.add(money);
		this.balance+=money;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	public Date getDateCreate() {
		return dateCreate;
	}
	class checkingAccount{
		checkingAccount(int money)
		{
			balance-=money;
		}

		@Override
		public String toString() {
			return "checkingAccount [toString()=" + "可以透支限额度" + "]";
		}
	}
	class savingAccount{
		savingAccount(int money)
		{
			if(money>balance)
				System.out.println("透支银行账户余额");
			else
				balance-=money;
		}

		@Override
		public String toString() {
			return "savingAccount [toString()=" +" 不可以透支限额度 "+ "]";
		}
		
	}
	public static void main(String[] args) {
		Account people=new Account("江山",1122,10000);
		people.setAnnualInterestRate(1.5*0.01);

		people.deposit(500);
		people.deposit(1000);
		people.deposit(5000);
		
		people.withDraw(100);
		people.withDraw(2000);
		people.withDraw(3000);
		SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 
	    sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记  
	    System.out.println("名字:"+people.name+" id:"+people.id);
		System.out.println("余额 "+people.getBalance()+"月利息 "+people.getMonthInterestRate()
		+"开户日期 "+sdf.format(people.getDateCreate()));
		for(Integer money:people.transactions)
			{
				int i=1;
				System.out.println("第"+i+"笔交的交易金额为:"+money);
				i++;
			}
}

}
4.
public class Account {
	private int id;
	private double balance;
	private double annualInterestRate;
	private Date dateCreate=new Date();
	public List<Integer> transactions=new ArrayList<Integer>();
	public Account() {
		;
	}
	public Account(int id,double balance)
	{
		this.id=id;
		this.balance=balance;
	}
	public double getMonthInterestRate()
	{
		return balance*annualInterestRate/12;
	}
	public void withDraw(int money) {
		transactions.add(-money);
		this.balance-=money;
	}
	public void deposit(int money) {
		transactions.add(money);
		this.balance+=money;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	public Date getDateCreate() {
		return dateCreate;
	}
	class checkingAccount{
		checkingAccount(int money)
		{
			balance-=money;
		}

		@Override
		public String toString() {
			return "checkingAccount [toString()=" + "可以透支限额度" + "]";
		}
	}
	class savingAccount{
		savingAccount(int money)
		{
			if(money>balance)
				System.out.println("透支银行账户余额");
			else
				balance-=money;
		}

		@Override
		public String toString() {
			return "savingAccount [toString()=" +" 不可以透支限额度 "+ "]";
		}
		
	}
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		List<Account> list=new ArrayList<Account>();
		for(int i=0;i<=9;i++)
		{
			list.add(new Account(i,100));
		}
		System.out.println("请输入你要选择的id:");
		int id=input.nextInt();
		while(true) {
		System.out.println("Main menu\r\n" + 
				"1: check balance\r\n" + 
				"2: withdraw\r\n" + 
				"3: deposit\r\n" + 
				"4: exit\r\n" + 
				"Enter a choice: ");
		int n=input.nextInt();
		if(n==1)
			{
				System.out.println("账户余额"+list.get(id).balance);
			}
		if(n==2)
		{
			System.out.println("请输入你要取出的钱:");
			int money=input.nextInt();
			if(money>list.get(id).balance)
				System.out.println("超过最大余额");
			else
				list.get(id).withDraw(money);
		}
		if(n==3)
		{
			System.out.println("请输入你要存入的钱:");
			int money=input.nextInt();
			list.get(id).deposit(money);
		}
		if(n==4)break;
		}
}

}

运行结果截图:

用Java写一个Account类 java设计一个名为account的类_System_02


用Java写一个Account类 java设计一个名为account的类_测试程序_03