用List表示多重性
练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系。
任务
对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的List实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。

- 创建 Bank 类
- 为Bank类增加两个属性:customers(Customer对象的List)和numberOfCustomers(整数, 当前Customer对象的数量)
- 添加公有构造器,初始化customersList。
- 添加addCustomer方法。该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customerList中。
- 添加getNumOfCustomers 访问方法,它返回numberofCustomers属性值。
- 添加getCustomer方法。它返回与给出的index参数相关的客户。
- 编译并运行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表示多重性的更多相关文章
- uml设计之多重性
---------------------------------------------------------------------------------------------------- ...
- UML课程复习重点
第一章 一.UML图示建模工具 二.UML--统一建模语言,以图形符号为基础,描述软件模型既简洁又清晰.它不是开发方法,是独立于任何开发方法之外的语言.它用于描述软件系统分析.设计和实施中的各种模型. ...
- 【MSP是什么】MSP认证之成功的项目群管理
同项目管理相比,项目群管理是为了实现项目群的战略目标与利益,而对一组项目进行的统一协调管理. 项目群管理 项目群管理是以项目管理为核心.单个项目上进行日常性的项目管理,项目群管理是对多个项目进行的总体 ...
- UML类图与面向对象设计原则
1. 引言 从大一开始学习编程,到如今也已经有两年了.从最初学习的Html,Js,JaveSe,再到JavaEE,Android,自己也能写一些玩具.学习过程中也无意识的了解了一些所谓的设计模 ...
- Java三大框架之——Hibernate关联映射与级联操作
什么是Hibernate中的关联映射? 简单来说Hibernate是ORM映射的持久层框架,全称是(Object Relational Mapping),即对象关系映射. 它将数据库中的表映射成对应的 ...
- Enterprise Achitect使用与类的关系的简单介绍
本文作为Enterprise Achitect初步使用,另外也是类图基本介绍,是设计模式的基础. 类的关系有泛化(Generalization).实现(Realization).依赖(Depende ...
- 《UML大战需求分析》阅读随笔(一)
UML:Unified Modeling Language(统一建模语言) 作为我专业学科里的一门语言,其目的就是交流,同客户交流,同自己交流. 用图像和文字,详细地讲解将要做的工程的 需求和功能细节 ...
- 《Entity Framework 6 Recipes》翻译系列 (1) -----第一章 开始使用实体框架之历史和框架简述
微软的Entity Framework 受到越来越多人的关注和使用,Entity Framework7.0版本也即将发行.虽然已经开源,可遗憾的是,国内没有关于它的书籍,更不用说好书了,可能是因为EF ...
- 《Entity Framework 6 Recipes》中文翻译系列 (6) -----第二章 实体数据建模基础之使用Code First建模自引用关系
2-5 使用Code First建模自引用关系 问题 你的数据库中一张自引用的表,你想使用Code First 将其建模成一个包含自关联的实体. 解决方案 我们假设你有如图2-14所示的数据库关系图的 ...
随机推荐
- Linux之iptables(二、基本认识和组成)
iptables的基本认识 Netfilter组件 内核空间,集成在linux内核中 扩展各种网络服务的结构化底层框架 内核中选取五个位置放了五个hook(勾子) function(INPUT.OUT ...
- 8.mysql执行语句的顺序
mysql执行语句的顺序 一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...
- 53. Maximum Subarray(动态规划)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LMDB installation
Official Website: http://lmdb.readthedocs.io/en/release/ Install commands for Ubuntu: $ sudo apt-get ...
- Master Nginx(7) - Nginx for the Developer
Caching integration No application caching Caching in the database Caching in the filesystem Changin ...
- PatentTips - Indexes of graphics processing objects in GPU commands
BACKGROUND A graphics processing unit (GPU) is a specialized electronic device that is specifically ...
- Blue Jeans POJ 3080 寻找多个串的最长相同子串
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- Java排序算法之冒泡、选择、插入、快速
JavaSort Java经典排序算法代码 2018-1-26更新:冒泡排序,选择排序,插入排序,快速排序 1. 冒泡排序 特点:效率低,实现简单 思想(从小到大排): 第1个数和第2个数比较,如果第 ...
- oracle 内部机制-DTRACE
Oracle SQL Tuning and CBO Internals: Based Optimizer with CBO Internals and SQL Tuning Optimization ...
- linux 下ip命令对比ifconfig命令
原文:https://linux.cn/article-3144-1.html ------------------------------------------------------------ ...