案例描述如下:
1.影片租赁店目前提供普通影片,新片,儿童影片三种类型影片供顾客租赁,不同影片类型拥有不同价格码
2.计费规则按照影片类型和租期有所不同
3.提供常客积分制度,积分对影片类型和租期有一定要求
要求:计算顾客的消费金额并打印租赁详单
Movie(影片)
/**
* 影片类,分为不同类型价格不同
*
* @author lune
* @create 2017-11-14 17:40
*/
public class Movie {
public static final int REGULAR = 0; //普通影片
public static final int NEW_RELEASE = 1; //新片
public static final int CHILDRENS = 2; //儿童影片
private String title; //影片名
private int priceCode; //价格码
public Movie(String title, int priceCode) {
this.title = title;
this.priceCode = priceCode;
}
public String getTitle() {
return title;
}
public int getPriceCode() {
return priceCode;
}
public void setPriceCode(int priceCode) {
this.priceCode = priceCode;
}
}
Rental(租赁)
/**
* 租赁类,用于绑定某个顾客租赁的影片,包含影片名和租期
*
* @author lune
* @create 2017-11-14 17:35
*/
public class Rental {
private Movie movie; //租赁的影片
private int daysRented; //租期
public Rental(Movie movie, int daysRented) {
this.movie = movie;
this.daysRented = daysRented;
}
public Movie getMovie() {
return movie;
}
public int getDaysRented() {
return daysRented;
}
}
Customer(顾客)
/**
* 顾客类,可进行多种租赁,需要打印详单
*
* @author lune
* @create 2017-11-14 17:35
*/
public class Customer {
private String name; //顾客名
private Vector rentals = new Vector(); //租赁列表
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
//添加租赁信息
public void addRental(Rental rental) {
rentals.addElement(rental);
}
//打印详单方法
public String statement() {
double totalAmount = 0; //总金额
int frequentRenterPoints = 0; //积分点
Enumeration items = rentals.elements(); //用户所有租赁列表
String result = getName() + " 的租赁详单如下 :" + "\n";
//循环遍历租赁影片,计算消费金额
while (items.hasMoreElements()) {
double thisAmount = 0; //当前单个租赁金额
Rental each = (Rental) items.nextElement();
//租赁计费规则
switch (each.getMovie().getPriceCode()) {
case Movie.REGULAR: //普通片,起步价为2元,租期超过2天的部分每天1.5元
thisAmount += 2;
if (each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE: //新片,每天3元
thisAmount += each.getDaysRented() * 3;
break;
case Movie.CHILDRENS: //儿童片,起步价1.5元,租期超过3天的部分每天1.5元
thisAmount += 1.5;
if (each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
frequentRenterPoints++; //每借一张加1个积分点
//积分累加条件:新版本的片子,借的时间大于1天
if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE)
&& each.getDaysRented() > 1) {
frequentRenterPoints++;
}
//添加详单
result += "\t" + each.getMovie().getTitle() + "\t" +
"\t"
+ String.valueOf(thisAmount) + "\n";
totalAmount += thisAmount;