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

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

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: Java

2015-07-16 21:25:26

实验题目 2 扩展银行项目添加一个 Customer 类。 Customer 类将包含一个 Account 象。

实验目的 使用引用类型的成员变量。

提 示

1. banking包下的创建Customer类。该类必须实现上面的UML图表中的模 型。

a. 声明三个私有对象属性firstNamelastName account

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

c. 声明两个公有存取器来访问该对象属性方法 getFirstName getLastName

回相应的属性。

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

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

2. exercise2 主目录里编译运行这个 TestBanking 程序。应该看到如下 输出结果:

Creating the customer Jane Smith.

Creating her account with a 500.00 balance.

Withdraw 150.00

Deposit 22.50

Withdraw 47.62

Customer [Smith, Jane] has a balance of 324.88


点击(此处)折叠或打开

  1. package com.troubleshooting.bank1;

  2. //定义一个账户
  3. public class Account {
  4.     // 账户余额
  5.     private double balance;

  6.     public Account(double init_balance) {
  7.         balance = init_balance;
  8.     }

  9.     // 获取账户余额
  10.     public double getBlance() {
  11.         return balance;
  12.     }

  13.     public void setBalance(double balance) {
  14.         this.balance = balance;
  15.     }

  16.     // 存钱
  17.     // amt存钱的额度
  18.     public void deposit(double amt) {
  19.         balance += amt;
  20.     }

  21.     // 取钱
  22.     // amt取钱的额度
  23.     public void withdraw(double amt) {
  24.         if (balance >= amt) {
  25.             balance -= amt;
  26.         } else {
  27.             System.out.println("余额不足!");
  28.         }
  29.     }
  30. }

  31. package com.troubleshooting.bank2;
  32. import com.troubleshooting.bank1.Account;
  33. public class Customer {
  34.     
  35.     private String firstName;
  36.     private String lastName;
  37.     private Account account;
  38.     
  39.     public Customer(String f,String l){
  40.         firstName = f;
  41.         lastName = l;
  42.     }

  43.     public String getFirstName() {
  44.         return firstName;
  45.     }

  46.     public void setFirstName(String firstName) {
  47.         this.firstName = firstName;
  48.     }

  49.     public String getLastName() {
  50.         return lastName;
  51.     }

  52.     public void setLastName(String lastName) {
  53.         this.lastName = lastName;
  54.     }

  55.     public Account getAccount() {
  56.         return account;
  57.     }

  58.     public void setAccount(Account account) {
  59.         this.account = account;
  60.     }

  61. }

  62. package com.troubleshooting.testbank1;
  63. /*
  64.  * This class creates the program to test the banking classes.
  65.  * It creates a new Bank, sets the Customer (with an initial balance),
  66.  * and performs a series of transactions with the Account object.
  67.  */

  68. import com.troubleshooting.bank1.Account;
  69. import com.troubleshooting.bank2.Customer;

  70. public class TestBanking2 {

  71.   public static void main(String[] args) {
  72.     Customer customer;
  73.     Account account;

  74.     // Create an account that can has a 500.00 balance.
  75.     account = new Account(500.00);
  76.     System.out.println("Creating the customer Jane Smith.");
  77.     //code
  78.     customer = new Customer("Jane","Smith");
  79.     System.out.println("Creating her account with a 500.00 balance.");
  80.     //code
  81.     //account.setBalance(500);
  82.     customer.setAccount(account);
  83.     System.out.println("Withdraw 150.00");
  84.    
  85.     //code
  86.     //account.withdraw(150);
  87.     customer.getAccount().withdraw(150);
  88.     System.out.println("Deposit 22.50");
  89.       //code
  90.     //account.deposit(22.50);
  91.     customer.getAccount().deposit(22.50);
  92.     System.out.println("Withdraw 47.62");
  93.        //code
  94.     //account.withdraw(47.62);
  95.     customer.getAccount().withdraw(47.62);
  96.     // Print out the final account balance
  97.     System.out.println("Customer [" + customer.getLastName()
  98.          + ", " + customer.getFirstName()
  99.          + "] has a balance of " + customer.getAccount().getBlance());
  100.   }

  101. private static void customer(String string, String string2) {
  102.     // TODO Auto-generated method stub
  103.     
  104. }
  105. }


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