Quiz... But Ice Cream Store
public class IceCream {
private static ArrayList<IceCream> iceCreams = new ArrayList<IceCream>();
private String flavor;
private int id;
public static final long MINUTE = 1000; // minute
protected long created;
protected long life;
public void startTime(){
created = System.currentTimeMillis(); //sets startTime to current time
}
// constructor for ice cream objects
public IceCream(String flavor) {
this.flavor = flavor; //sets flavor to the flavor passed in
this.id = IceCream.getIceCreamCount(); //sets id to the current num of ice creams
iceCreams.add(this); // adds ice cream to the list of ice creams
startTime(); // set time when ice cream is taken out
life = created; // set offset for ice creams life
this.setIceCreamLife(1); // provide a default for ice creams life
}
// gets the number of ice creams
public static int getIceCreamCount() {
return iceCreams.size();
}
// id getter
public int getId() {
return id;
}
public static ArrayList<IceCream> getIceCreams() {
return iceCreams;
}
// toString method for ice cream objects
public String toString(){
return id + ". " + flavor + " Life: " + getIceCreamLife() + " minutes";
}
// Sets the life of the ice cream
public void setIceCreamLife(int minutes) {
life += minutes * IceCream.MINUTE; // add minutes to life
}
// increments the life of the ice cream
public void removeIceCreamLife(){
life -= IceCream.MINUTE;
}
// gets the amount of time remaining for the ice cream to melt
public double getIceCreamLife() {
return (life - created) / IceCream.MINUTE;
}
// Checks if the ice cream has not melted yet
public boolean hasIceCreamLife() {
return life > created;
}
// prints non melted ice creams
public static void printIceCreams() {
System.out.println("Non-melted Ice Creams: ");
for (IceCream iceCream: IceCream.iceCreams) {
if (iceCream.hasIceCreamLife()) // Non melted ice cream check
System.out.println(iceCream);
}
}
public static void main(String[] args){
System.out.println("Number of Ice Creams: " + IceCream.getIceCreamCount());
IceCream[] iceCreams = {
new IceCream("Vanilla"),
new IceCream("Chocolate")
};
/*
for(IceCream iceCream: iceCreams){
System.out.println(iceCream);
}
*/
IceCream.printIceCreams();
System.out.println("Number of Ice Creams: " + IceCream.getIceCreamCount());
}
}
IceCream.main(null);
public class Gelato extends IceCream {
private int scoops = 0;
private int calories;
public Gelato(String flavor, int calories) {
super(flavor); // calls the constructor of the ice cream class
this.calories = calories;
super.setIceCreamLife(5); // adds 5 minute to default ice cream life
}
@Override
public void removeIceCreamLife(){
life -= 0.5 * IceCream.MINUTE;
}
@Override
public String toString() {
return(super.toString() + ", " + this.calories);
}
public static void main(String[] args){
System.out.println("Number of Ice Creams: " + IceCream.getIceCreamCount());
Gelato[] gelatos = {
new Gelato("Vanilla Gelato", 100),
new Gelato("Chocolate Gelato", 200),
new Gelato("Strawberry Gelato", 450)
};
for(Gelato gelato: gelatos){
System.out.println(gelato);
}
System.out.println("Number of Ice Creams: " + IceCream.getIceCreamCount());
}
}
Gelato.main(null);
class CheckOut extends TimerTask {
public void run() {
System.out.println("Hello World!");
}
}
// Library Simulation
public class IceCreamSim {
public static void main(String[] args) throws InterruptedException {
int minutes = 5; // Simulation minutes
// Sleep in loop creates delay for simulation
for (int i = 0; i < minutes; i++) {
System.out.println("Minute: " + i);
IceCream.printIceCreams();
System.out.println();
Thread.sleep(IceCream.MINUTE); // Part 3.4, Use a sleep in Java to assist with simulation
// Loop through books in Library
for (IceCream iceCream : IceCream.getIceCreams()) {
iceCream.removeIceCreamLife(); // apply aging formula
}
}
}
}
IceCreamSim.main(null);