I have four classes:
- SaltyFishMarket: Control begins here in main. All main does is declare a new SaltyFishMarket object and that activates the constructor. The constructor sets up the DecimalFormat object and the Scanner, then calls the buyFish method three times. The buyFish method returns a Fisherman object which gets used to access the amount of fish each fisherman caught. The buyFish method also updates a field that keeps track of Salty's total cost. After the constructor calculates the price that Salty paid per pound, it calls a method to set the price per pound that customers will have to pay. Finally, the constructor calls a method that sells fish to customers.
I decided to have five customers. I made a sellToOneCustomer method and had the sellFish method call it five times. I could change the number of customers by putting in more or fewer calls to sellToOneCustomer. After the five calls to sellToOneCustomer, the sellFish method calculates Salty's profit for the day and announces what kind of day Salty had. - Fisherman: The fields of my Fisherman class are a name, a fish count, a total weight of fish caught, a String for the weather, a probability that corresponds to the weather, and a random number generator. The constructor gets activated when a Fisherman object is declared (which happens in the SaltyFishMarket class). The constructor receives a name as an argument and sets the name field. After that the constructor declares a Weather object then queries that object to find out what the weather is. Based on the string that is returned by the Weather object, the constructor sets the probability.
One thing I did to avoid code duplication is make a method in the Fisherman class called goFishing that gets called by the SaltyFishMarket class after it makes a new Fisherman object. Since a fisherman casts his line five times, I made another method called FishOnce and I had the goFishing method call that one five times. The FishOnce method gets a random number between 0 and 1 and sees if that number is less than the probability. If so, a fish is caught. For example, suppose it's a windy day and the probability is 0.8. When you get a random double, if it's something like .4 or .6 or anything less than .8, you treat that as a signal that a fish was caught. About 80% of the numbers between 0 and 1 are less than .8, so that trick works well.
If a fish is caught, I declare a new Fish object. Then I query that object for the weight and length of the fish and that's how I decide whether to keep it or not.
I made the program more fun to run by printing out what happens as the fisherman fishes. For example, here is a partial run of my code. The specs didn't say we had to do this, but it's entertaining to see how the program is different every time it runs.
- Fish: This is a really simple class. The fields are a length, a weight, and a random number generator. The constructor uses the random number generator to set the length and weight of the fish within the bounds specified. Brandon showed how to do that in our webinar.
- Weather: This is a really simple class, too. Its only field is a random number generator. When a Weather object is created by the Fisherman class, the random number generator is used to get one of four possible values. I made a getWeather method that returns one of the four strings ("sunny", "cloudy", "windy", or "rainy").
