AP CSA的一道编程题答案

1. 题目

A clothing store sells shoes, pants, and tops. The store also allows a customer to buy an “outfit,” which consists of three items: one pair of shoes, one pair of pants, and one top.
Each clothing item has a description and a price. The four types of clothing items are represented by the four classes Shoes, Pants, Top, and Outfit. All four classes are subclasses of a ClothingItem class, shown

public class ClothingItem
    private String description;
    private double price;
    public ClothingItem()
        description = "";
        price = 0;
    public ClothingItem(String descr, double aPrice)
        description = descr;
        price = aPrice;
    public String getDescription()
    { return description; }
    public double getPrice()
    { return price; }

The following diagram shows the relationship between the ClothingItem class and the Shoes, Pants, Top, and Outfit classes.

The store allows customers to create Outfit clothing items each of which includes a pair of shoes, pants, and a top. The description of the outfit consists of the description of the shoes, pants, and top, in that order, separated by "/" and followed by a space and "outfit". The price of an outfit is calculated as follows. If the sum of the prices of any two items equals or exceeds $100, there is a 25% discount on the sum of the prices of all three items. Otherwise there is a 10% discount.

For example, an outfit consisting of sneakers ($40), blue jeans ($50), and a T-shirt ($10) would have the name "sneakers/blue jeans/T- shirt outfit" and a price of 0.90(40 + 50 + 10) = $90.00. An outfit consisting of loafers ($50), cutoffs ($20), and dress-shirt ($60) would have the description "loafers/cutoffs/dress-shirt outfit" and price 0.75(50+20+60) = $97.50.
Write the Outfit subclass of ClothingItem. Your implementation must have just one constructor that takes three parameters representing a pair of shoes, pants, and a top, in that order.

A client class that uses the Outfit class should be able to create an outfit, get its description, and get its price. Your implementation should be such that the client code has the following behavior:

Shoes shoes;
Pants pants;
Top top;
/* Code to initialize shoes, pants, and top */
ClothingItem outfit = new Outfit (shoes, pants, top); //Compiles without error
ClothingItem outfit = new Outfit (pants, shoes, top); //Compile-time error
ClothingItem outfit = new Outfit (shoes, top, pants); //Compile-time error

Write your solution below.

2. 答案


Last modification:March 6th, 2022 at 08:23 pm
If you think my article is useful to you, please feel free to appreciate