Packing bakeries

Report a typo

You are working in a Pie company. The business is going well and bakeries are selling abroad. Sometimes due to customs rules and trade regulations, it is necessary to package bakeries in a box with a more basic name like Bakery or Food. The full class hierarchy follows:

class Food {}

class Bakery extends Food {}

class Cake extends Bakery {}

class Pie extends Bakery {}

class Tart extends Bakery {}

interface Box<T> {
    public void put(T item);
    public T get();
}

There is a Packer class available, but it is designed with business rule violations and lacks implementation. Correct the Packer code to ensure that:

  • Any kind of Bakery could be repacked in a Box with a more basic type (e.g. from a box with Pie to a box with Food)
  • Basic stuff like food can't be repacked into narrowly typed Boxes (e.g. with Cakes)
  • Arbitrary stuff like Strings or Objects can't be repacked without compile-time errors or warnings
  • Repacking actually happens

If this task takes too much time, try to skip it.

Write a program in Java 17
/**
This packer has too much freedom and could repackage stuff in wrong direction.
Fix method types in signature and add implementation.
*/
class Packer {
public void repackage(Box to, Box from) {
// Implement repackaging
}
}

// Don't change classes below
class Box<T> {
private T item;

public void put(T item) {
this.item = item;
}

public T get() {
return this.item;
}
}

class Goods {
}

class Food extends Goods {
}

class Bakery extends Food {
}

class Cake extends Bakery {
}

class Pie extends Bakery {
___

Create a free account to access the full topic