Chinaunix首页 | 论坛 | 博客
  • 博客访问: 353417
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: Java

2015-07-17 17:49:07

题目:

1、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(gettersetter方法),取款方法withdraw(),存款方法deposit()

Account

private int id

private double balance

private double annualInterestRate

public Account (int id, double balance, double annualInterestRate )

public int getId()

public double getBalance()

public double getAnnualInterestRate()

public void setId( int id)

public void setBalance(double balance)

public void setAnnualInterestRate(double annualInterestRate)

public void withdraw (double amount)

public void deposit (double amount)

 

提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。

 

2. 创建Customer类。

Customer

private String firstName

private String lastName

private Account account

public Customer(String f,String l)

public String getFirstName()

public String getLastName()

public Account getAccount()

public void setAccount(Account account)

 

a. 声明三个私有对象属性:firstNamelastNameaccount

b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(fl

c. 声明两个公有存取器来访问该对象属性,方法getFirstNamegetLastName返回相应的属性。

d. 声明setAccount 方法来对account属性赋值。

e. 声明getAccount 方法以获取account属性。

 

3.写一个测试程序。

(1)       创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23 的账户。

(2)       Jane Smith操作。

存入 100 元,再取出960元。再取出2000元。

打印出Jane Smith 的基本信息

 

成功存入 100.0

成功取出:960.0

余额不足,取款失败

Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23, balance is 1140.0

 ----------------------------------------------------------------------------------------------

 

点击(此处)折叠或打开

  1. package com.troubleshooting.bank5;

  2. //写一个名为Account的类模拟账户
  3. public class Account {
  4.     private int id;
  5.     private double balance;
  6.     private double annualInterestRate;

  7.     public Account(int id, double balance, double annualInterestRate) {
  8.         super();
  9.         this.id = id;
  10.         this.balance = balance;
  11.         this.annualInterestRate = annualInterestRate;
  12.     }

  13.     // public Account(int id, double balance, double annualInterestRate) {
  14.     //
  15.     // }

  16.     public int getId() {
  17.         return id;
  18.     }

  19.     public void setId(int id) {
  20.         this.id = id;
  21.     }

  22.     public double getBalance() {
  23.         return balance;
  24.     }

  25.     public void setBalance(double balance) {
  26.         this.balance = balance;
  27.     }

  28.     public double getAnnualInterestRate() {
  29.         return annualInterestRate;
  30.     }

  31.     public void setAnnualInterestRate(double annualInterestRate) {
  32.         this.annualInterestRate = annualInterestRate;
  33.     }

  34.     // 在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
  35.     public void withdraw(double amount) {
  36.         if (balance >= amount) {
  37.             balance -= amount;
  38.             System.out.println("成功取出:" + amount);
  39.         } else {
  40.             System.out.println("余额不足,取款失败");
  41.         }
  42.     }

  43.     public void deposit(double amount) {
  44.         balance += amount;
  45.         System.out.println("成功存入:" + amount);
  46.     }
  47. }

  48. package com.troubleshooting.bank5;

  49. public class Customer {
  50.     private String firstName;
  51.     private String lastName;
  52.     private Account account;

  53.     // public Customer(String f, String l) {
  54.     // firstName = f;
  55.     // lastName = l;
  56.     // }

  57.     public Customer(String f, String l) {
  58.         super();
  59.         this.firstName = f;
  60.         this.lastName = l;

  61.     }

  62.     public String getFirstName() {
  63.         return firstName;
  64.     }

  65.     public void setFirstName(String firstName) {
  66.         this.firstName = firstName;
  67.     }

  68.     public String getLastName() {
  69.         return lastName;
  70.     }

  71.     public void setLastName(String lastName) {
  72.         this.lastName = lastName;
  73.     }

  74.     public Account getAccount() {
  75.         return account;
  76.     }

  77.     public void setAccount(Account account) {
  78.         this.account = account;
  79.     }

  80. }

  81. package com.troubleshooting.testbank1;
  82. /*
  83.  * 创建一个Customer ,名字叫 Jane Smith,
  84.  * 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户,对Jane Smith操作。    
  85.  * 存入 100 元,再取出960元。再取出2000元。    
  86.  * 打印出Jane Smith 的基本信息
  87.  * 成功存入 :100.0
  88.  * 成功取出:960.0
  89.  * 余额不足,取款失败
  90.  * Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0

  91.  *
  92. */

  93. import com.troubleshooting.bank5.*;

  94. public class TestCustomer {
  95.     public static void main(String[] args) {
  96.         Customer cust = new Customer("Jane", "Smith");
  97.         Account account = new Account(1000, 2000, 0.0123);
  98.         cust.setAccount(account);
  99.         account.deposit(100);
  100.         account.withdraw(960);
  101.         account.withdraw(2000);
  102.         System.out.println("Customer [" + cust.getLastName() + "," + cust.getFirstName() + "] has a acount: id is"
  103.                 + account.getId() + ",annualInterestRate is " + account.getAnnualInterestRate() * 100 + "%"
  104.                 + ", balance is " + account.getBalance());
  105.     }

  106. }
显示结果:
成功存入:100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith,Jane] has a acount: id is1000,annualInterestRate is 1.23%, balance is 1140.0



      

 

阅读(3302) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~