Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Last active April 25, 2016 14:22
Show Gist options
  • Save bhnascar/50c6171b8387c68607d6275064500a15 to your computer and use it in GitHub Desktop.
Save bhnascar/50c6171b8387c68607d6275064500a15 to your computer and use it in GitHub Desktop.
Free response #2
Imagine we're a bike shop. We sell bikes. Bikes have a manufacturer,
a model name, and a price. So we will represent bikes with the
following class:
public class Bike
{
private int price;
private String modelName;
private String manufacturer;
/* Returns the model name of this bike. */
public String getModelName() {
/* Implementation not shown. */
}
/* Returns the manufacturer of this bike. */
public String getManufacturer() {
/* Implementation not shown. */
}
/* Returns the price of this bike to the closest dollar. */
public int getPrice() {
/* Implementation not shown. */
}
}
So...we're a bike shop. Which means we have a large "inventory" of
bikes, which can be represented by an ArrayList. We should be able to
"sell" a bike (which means we will remove it from the ArrayList) and
earn some revenue. Furthermore, we should be able to "search" for
bikes - for example if a customer asks us if we have a particular bike
in stock. I want you to implement the "sell" and "search" methods in
the following bike shop class.
public class BikeShop
{
/* A list of all the bikes in our inventory. */
List<Bike> bikes;
private int revenue;
/* Constructor and some other methods not shown. */
/* Returns a Bike object that has the given manufacturer
* and the given modelName, if we have one in our inventory.
* If we have more than one bike that matches the given
* specifications, just return the first one that you find.
* Otherwise, returns null.
*
* The list of bikes is NOT sorted so you can just do a
* linear search.
*/
public Bike searchForBike(String manufacturer, String modelName) {
// TODO
}
/* Sells ALL the bikes with the given manufacturer and model name,
* and increases the stores revenue (by which I mean remove the
* bikes from our inventory and add the prices of the sold bikes
* to the revenue variable). Just to clarify, yes, I'm asking you
* to sell ALL the bikes that match, not just a single one.
*
* Precondition: the bike exists in our inventory. So you're
* guaranteed that you can find at least one such
* bike in our inventory.
* Post condition: (1) the revenue variable is updated to reflect
* the shop's new total revenue after selling
* the bikes.
* (2) no bikes with the given manufacturer and
* model name exist anymore in our inventory.
* */
public void sellBikes(String manufacturer, String modelName) {
// TODO
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment