Extra Credit Modifying Book Class
import java.util.Date;
public class Food {
public String name;
public int id;
String entryTime;
private static List<Food> directory = new ArrayList<Food>();
public Food() {
this.name = null;
this.id = 0;
}
// Part 1.1 --> 1 argument constructor for name
public Food(String name) {
this.name = name;
// Part 1.3 --> Unique ID for each object by adding Food to arraylist
Food.directory.add(this);
this.id = this.getFoodCount();
// Part 2.4 --> Add time when Food enters directory
Date date = new Date();
this.entryTime = date.toString();
}
// Part 1.2 --> toString method to display Food name and id
public String toString(){
return ("Food name: '" + this.name + "'. Food ID: " + this.id + ". Entry time: " + this.entryTime);
}
// Part 1.4 --> Public getter for Food count that returns the size of the array with the Food names
public static int getFoodCount() {
return Food.directory.size();
}
// Part 1.5 --> Tester Method that initializes 2 Foods...
public static void main(String args[]) {
Food lebron = new Food("Pizza");
Food messi = new Food("Lionel Messi");
System.out.println(lebron);
System.out.println(messi);
System.out.println(messi.getFoodCount());
}
}
Food.main(null);
// Part 2.1 --> Basketball runs the Constructor from Athlete
class Basketball extends Athlete {
private String draft;
// Part 2.2 --> Instance variable Draft which is unique to Basketball
public Basketball(String name, String draft) {
super(name);
this.draft = draft;
}
// Part 2.3 --> Make Getters and Setters for Draft
public String getDraft() {
return this.draft;
}
public void setDraft(String draft) {
this.draft = draft;
}
@Override
public String toString() {
return ("Athlete name: '" + this.name + "'. Athlete ID: " + this.id + ". draft: " + this.draft + ". Entry time: " + this.entryTime);
}
// Part 2.5 for Basketball --> Define tester method to test all items, adds 2 basketball players,shows athlete count before and after, prints items in directory
public static void main(String [] args) {
// Part 2.6 --> Tester Method to test all items
System.out.println("Before Directory Athlete Count: " + Athlete.getAthleteCount()); // directory count is truly static across inheritance
Basketball[] bballPlayers = {
new Basketball("Kevin Durant", "Round 1, Pick 2"),
new Basketball("Nikola Jokic", "Round 2, Pick 41")
};
for (int i = 0; i < bballPlayers.length; i++) { // Use of conventional loop for variation
System.out.println(bballPlayers[i]); // still works with toString()
}
System.out.println("After Directory Athlete Count: " + Athlete.getAthleteCount());
}
}
Basketball.main(null);
// Part 2.1 --> Soccer runs the Constructor from Athlete
class Soccer extends Athlete {
public String academy;
// Part 2.2 --> Instance variable Academy which is unique to Soccer
public Soccer(String name, String academy) {
super(name);
this.academy = academy;
}
// Part 2.3 --> Make Getters and Setters for Academy
public String getAcademy() {
return this.academy;
}
public void setAcademy(String academy) {
this.academy = academy;
}
@Override
public String toString() {
return ("Athlete name: '" + this.name + "'. Athlete ID: " + this.id + ". academy: " + this.academy + ". Entry time: " + this.entryTime);
}
// Part 2.5 for Soccer --> Define tester method to test all items, adds 2 soccer players,shows athlete count before and after, prints items in directory
public static void main(String [] args) {
// Part 2.6 --> Tester Method to test all items
System.out.println("Before Directory Athlete Count: " + Athlete.getAthleteCount());
Soccer[] soccerPlayers = { // Same technique as in Basketball tester
new Soccer("Antoine Griezmann", "Real Sociedad"),
new Soccer("Rafael Leão", "Sporting CP")
};
for (int i = 0; i < soccerPlayers.length; i++) {
System.out.println(soccerPlayers[i]);
}
System.out.println("After Directory Athlete Count: " + Athlete.getAthleteCount());
}
}
Soccer.main(null);