Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347229
  • 博文数量: 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:31:04

:12pt;color:rgb(0,0,0);font-style:norm实验题目 4 将用数组实现银行与客户间的多重关系。

实验目的 在类中使用数组作为模拟集合操作。

提示 对银行来说可添加 Bank 类。 Bank 对象跟踪自身与其客户间的关系。用

Customer 对象的数组实现这个集合化的关系。还要保持一个整数属性来跟踪 银 行当前有多少客户。

a. 创建 Bank

b. Bank 类 增 加 两 个 属 性 customers(Customer对象的数组 )

numberOfCustomers(整数跟踪下一个 customers 数组索引)

c. 添加公有构造器以合适的最大尺寸至少大于 5初始化 customers 数组。

d. 添加 addCustomer 方法。该方法必须依照参数构造一个新的

Customer 对象然后把它放到 customer 数组中。还必须把 numberofCustomers 属性的值加 1

e. 添加 getNumOfCustomers 访问方法它返回 numberofCustomers 性值。

f. 添加 getCustomer方法。它返回与给出的index参数相关的客户。

g. 编译并运行 TestBanking 程序。可以看到下列输出结果

Customer [1] is Simms,Jane

Customer [2] is Bryant,Owen
Customer [3] is Soley,Tim
Customer [4] is Soley,Maria


点击(此处)折叠或打开

  1. package com.troubleshooting.bank4;

  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 boolean deposit(double amt) {
  19.         balance += amt;
  20.         return true;
  21.     }

  22.     // 取钱
  23.     // amt取钱的额度
  24.     public boolean withdraw(double amt) {
  25.         if (balance >= amt) {
  26.             balance -= amt;
  27.             return true;
  28.         } else {
  29.             System.out.println("余额不足!");
  30.             return false;
  31.         }
  32.     }
  33. }
  34. package com.troubleshooting.bank4;

  35. //创建 Bank 类
  36. public class Bank {
  37.     // 为 Bank 类 增 加 两 个 属 性 : customers(Customer对象的数组 ) 和
  38.     // numberOfCustomers(整数,跟踪下一个 customers 数组索引)
  39.     // 用于存放客户
  40.     private Customer[] customers;
  41.     // 记录存放客户的数目
  42.     private int numberOfCustomers;

  43.     // 添加公有构造器,以合适的最大尺寸(至少大于 5)初始化 customers 数组。
  44.     public Bank() {
  45.         customers = new Customer[10];
  46.     }

  47.     // 添加一个Customer到数组中
  48.     // 添加 addCustomer 方法。该方法必须依照参数(姓,名)构造一个新的
  49.     // Customer
  50.     // 对象然后把它放到 customer 数组中。还必须把 numberofCustomers
  51.     // 属性的值加 1。
  52.     public void addCustomer(String f, String l) {
  53.         Customer cust = new Customer(f, l);
  54.         // customers[0]=cust;
  55.         // customers[1]=cust;
  56.         // customers[2]=cust;
  57.         // customers[3]=cust;
  58.         // customers[4]=cust;
  59.         // ................
  60.         customers[numberOfCustomers] = cust;
  61.         numberOfCustomers++;
  62.     }

  63.     // 添加 getNumOfCustomers 访问方法,它返回 numberofCustomers 属性值。
  64.     public int getNumOfCustomer() {
  65.         return numberOfCustomers;
  66.     }

  67.     // 添加 getCustomer方法。它返回与给出的index参数相关的客户。
  68.     public Customer getCustomer(int index) {
  69.         return customers[index];
  70.     }
  71. }

  72. package com.troubleshooting.bank4;

  73. public class Customer {

  74.     private String firstName;
  75.     private String lastName;
  76.     private Account account;

  77.     public Customer(String f, String l) {
  78.         firstName = f;
  79.         lastName = l;
  80.     }

  81.     public String getFirstName() {
  82.         return firstName;
  83.     }

  84.     public void setFirstName(String firstName) {
  85.         this.firstName = firstName;
  86.     }

  87.     public String getLastName() {
  88.         return lastName;
  89.     }

  90.     public void setLastName(String lastName) {
  91.         this.lastName = lastName;
  92.     }

  93.     public Account getAccount() {
  94.         return account;
  95.     }

  96.     public void setAccount(Account account) {
  97.         this.account = account;
  98.     }

  99. }

  100. package com.troubleshooting.testbank1;
  101. /*
  102.  * This class creates the program to test the banking classes.
  103.  * It creates a new Bank, sets the Customer (with an initial balance),
  104.  * and performs a series of transactions with the Account object.
  105.  */

  106. import com.troubleshooting.bank4.*;

  107. public class TestBanking4 {

  108.   public static void main(String[] args) {
  109.     Bank bank = new Bank();

  110.     // Add Customer Jane, Simms
  111.     //code
  112.     bank.addCustomer("Jane", "Simms");
  113.     //Add Customer Owen, Bryant
  114.     //code
  115.     bank.addCustomer("JOwen", "Bryant");
  116.     // Add Customer Tim, Soley
  117.     //code
  118.     bank.addCustomer("Tim", "Soley");
  119.     // Add Customer Maria, Soley
  120.     //code
  121.     bank.addCustomer("Maria", "Soley");
  122.     //遍历
  123.     for ( int i = 0; i < bank.getNumOfCustomer(); i++ ) {
  124.       Customer customer = bank.getCustomer(i);

  125.       System.out.println("Customer [" + (i+1) + "] is "
  126.              + customer.getLastName()
  127.              + ", " + customer.getFirstName());
  128.     }
  129.   }
  130. }
显示结果:
Customer [1] is Simms, Jane
Customer [2] is Bryant, JOwen
Customer [3] is Soley, Tim
Customer [4] is Soley, Maria

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