练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系。

任务

对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的List实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。

  1. 创建 Bank 类
  1. 为Bank类增加两个属性:customers(Customer对象的List)和numberOfCustomers(整数, 当前Customer对象的数量)
  1. 添加公有构造器,初始化customersList
  1. 添加addCustomer方法。该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customerList中。
  1. 添加getNumOfCustomers 访问方法,它返回numberofCustomers属性值。
  1. 添加getCustomer方法。它返回与给出的index参数相关的客户。
  1. 编译并运行TestBanking程序。可以看到下列输出结果:

Customer 1 is Simms,Jane

Customer 2 is Bryant,Owen

Customer 3 is Soley,Tim

Customer 4 is Soley,Maria

当前客户数量 = 4

package banking;

import java.util.ArrayList;
import java.util.List; public class Bank1
{
private List<Customer> customers ;
private int numberOfCustomers = ; public Bank1( )
{
customers =new ArrayList<>() ;
} public void addCustomer(String firstName ,String lastName)
{
customers.add(new Customer(firstName,lastName));
} public int getNumberOfCustomers() {
numberOfCustomers=customers.size();
return numberOfCustomers;
} public Customer getCustomer(int index)
{
Customer s=new Customer();
s=customers.get(index);
return s;
} }
package banking;

import java.util.List;

public class Customer extends Account
{
//成员属性
private String firstName ;
private String lastName ;
private double account ; //构造方法
public Customer()
{ }
//构造方法
public Customer(String f , String l)
{
this.firstName = f ;
this.lastName = l ;
} //get set
public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public double getAccount() {
return account;
} public void setAccount(double account) {
this.account = account;
} public String toString() {
return firstName + ", " + lastName ;
} }
package banking;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class TestBanking { public static void main(String[] args)
{
//实例化账户
Account at = new Account() ; System.out.println("Creating an account with a "+at.getBalance( ) +" balance") ;
System.out.println("Withdraw "+(at.getBalance( ) -at.withdraw() ));
double x=at.getBalance( );
System.out.println("Deposit "+(at.deposit(22.5)-x)) ;
System.out.println("Withdraw "+(at.getBalance( ) -at.withdraw(47.62) ));
System.out.println("The account has a balance of "+at.getBalance()) ; System.out.println("————————————————————————"); //实例化顾客
Customer cr = new Customer( ) ; cr.setFirstName("Jane");
cr.setLastName("Smith");
cr.setBalance();
System.out.println("Creating the customer "+cr.getFirstName() +" "+cr.getLastName());
System.out.println("Creating her account with a " +cr.getBalance()+" balance");
System.out.println("Withdraw "+(cr.getBalance()-cr.withdraw()) );
double x1 = cr.getBalance( ) ;
System.out.println("Deposit "+(cr.deposit(22.5)-x1));
System.out.println("Withdraw "+(cr.getBalance()-cr.withdraw(47.62)));
System.out.println("Customer "+cr.getLastName()+" "+cr.getFirstName()+
" has a balance of "+cr.getBalance()); System.out.println("————————————————————————"); Customer cr1 = new Customer( ) ; cr1.setFirstName("Jane");
cr1.setLastName("Smith");
cr1.setBalance();
System.out.println("Creating the customer "+cr1.getFirstName() +" "+cr1.getLastName());
System.out.println("Creating her account with a " +cr1.getBalance()+" balance");
System.out.println(cr1.withdraw1() );
System.out.println(cr1.deposit1(22.5));
System.out.println(cr1.withdraw1(47.62));
System.out.println(cr1.withdraw1());
System.out.println("Customer "+cr1.getLastName()+" "+cr1.getFirstName()+
" has a balance of "+cr1.getBalance()); System.out.println("————————————————————————"); Bank bk =new Bank( ) ; bk.addCustomer( "Simms" , "Jane" );
bk.addCustomer( "Bryant" , "Owen" );
bk.addCustomer( "Soley" , "Tim" );
bk.addCustomer( "Soley" , "Maria" ); System.out.println("————————————————————————"); // Bank1 bk1 = new Bank1(){}; bk1.addCustomer("Simms", "Jane");
bk1.addCustomer("Bryant", "Owen");
bk1.addCustomer("Soley","Tim");
bk1.addCustomer("Soley","Maria"); for(int i= ;i <bk1.getNumberOfCustomers();i++)
{
System.out.println("Customer "+(i+)+" is"+" "+bk1.getCustomer(i));
} System.out.println("当前客户数量 = "+bk1.getNumberOfCustomers()); System.out.println("————————————————————————");
} }

用List表示多重性的更多相关文章

  1. uml设计之多重性

    ---------------------------------------------------------------------------------------------------- ...

  2. UML课程复习重点

    第一章 一.UML图示建模工具 二.UML--统一建模语言,以图形符号为基础,描述软件模型既简洁又清晰.它不是开发方法,是独立于任何开发方法之外的语言.它用于描述软件系统分析.设计和实施中的各种模型. ...

  3. 【MSP是什么】MSP认证之成功的项目群管理

    同项目管理相比,项目群管理是为了实现项目群的战略目标与利益,而对一组项目进行的统一协调管理. 项目群管理 项目群管理是以项目管理为核心.单个项目上进行日常性的项目管理,项目群管理是对多个项目进行的总体 ...

  4. UML类图与面向对象设计原则

    1. 引言     从大一开始学习编程,到如今也已经有两年了.从最初学习的Html,Js,JaveSe,再到JavaEE,Android,自己也能写一些玩具.学习过程中也无意识的了解了一些所谓的设计模 ...

  5. Java三大框架之——Hibernate关联映射与级联操作

    什么是Hibernate中的关联映射? 简单来说Hibernate是ORM映射的持久层框架,全称是(Object Relational Mapping),即对象关系映射. 它将数据库中的表映射成对应的 ...

  6. Enterprise Achitect使用与类的关系的简单介绍

    本文作为Enterprise Achitect初步使用,另外也是类图基本介绍,是设计模式的基础.  类的关系有泛化(Generalization).实现(Realization).依赖(Depende ...

  7. 《UML大战需求分析》阅读随笔(一)

    UML:Unified Modeling Language(统一建模语言) 作为我专业学科里的一门语言,其目的就是交流,同客户交流,同自己交流. 用图像和文字,详细地讲解将要做的工程的 需求和功能细节 ...

  8. 《Entity Framework 6 Recipes》翻译系列 (1) -----第一章 开始使用实体框架之历史和框架简述

    微软的Entity Framework 受到越来越多人的关注和使用,Entity Framework7.0版本也即将发行.虽然已经开源,可遗憾的是,国内没有关于它的书籍,更不用说好书了,可能是因为EF ...

  9. 《Entity Framework 6 Recipes》中文翻译系列 (6) -----第二章 实体数据建模基础之使用Code First建模自引用关系

    2-5 使用Code First建模自引用关系 问题 你的数据库中一张自引用的表,你想使用Code First 将其建模成一个包含自关联的实体. 解决方案 我们假设你有如图2-14所示的数据库关系图的 ...

随机推荐

  1. java堆排序实现

    代码如下: public class HeapSort { public static void heapSort(DataWrap[] data) { System.out.println(&quo ...

  2. mysql登录出现1045错误

    这个问题是在window server 2012上安装mysql之后, 远程访问时出现的1045错误 我新建了一个相同的用户用于远程访问, 密码也相同, 但是还是访问不了 参照链接:https://b ...

  3. generating multiple ordered files in python

    Goal: To generate =35= files named 'capitalsquiz1.txt', 'capitalsquiz2.txt'...'capitalsquiz35.txt' * ...

  4. SCI 计算机 数学相关期刊

    数学,电子通信,计算机类 出版地 收录库 刊名 刊期 ISSN 影响因子 中国大陆 SCI CHINESE SCIENCE BULLETIN<科学通报>(英文版) 半月刊 1001-653 ...

  5. UVa - 12661 - Funny Car Racing

    先上题目: 12661 Funny Car RacingThere is a funny car racing in a city with n junctions and m directed ro ...

  6. PatentTips - Fair scalable reader-writer mutual exclusion

    BACKGROUND The present invention relates generally to multithreaded programming and, more specifical ...

  7. 优化实例- not use hash to avoid temp space issue

    在展开下面的original sql 和 execution plan之前,要知道这个SQL的问题就在于占用大量的TEMP space orignal SQL SELECT roster.IC_N A ...

  8. php生成随机password的几种方法

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/523 使用PHP开发应用程序,尤其是站点程序.经常须要生成随机password,如用户注冊 ...

  9. LA 4329(树状数组)

    算法竞赛入门经典 p197 题目大意: 一条大街上住着n个乒乓球爱好者.常常比赛切磋技术.每一个人都有一个不同的技能值a[i].每场比赛须要3个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在 ...

  10. 【Android】ListView 优化

    重用 ListView Item ListView创建时其会创建屏幕可容纳数量的 Item.ListView 滚动时,刚消失的 item 会被保存到回收池中.新出现的 item 从回收池中获取避免反复 ...