Skip to content
Snippets Groups Projects
Commit 6a771312 authored by Paul Rauser's avatar Paul Rauser
Browse files

chore: create clean task branch

parent b22486a8
No related branches found
No related tags found
No related merge requests found
package shoes;
public class App {
public static void main(String[] args) throws BoxException {
Sneakers sneakers = new Sneakers("Blue Sneakers", 42.5);
Sneakers sneakers2 = new Sneakers("White Sneakers", 44.0);
Boots boots = new Boots("Brown Boots", 39.0);
Boots boots2 = new Boots("Black Boots", 37.5);
ShoeBox<Boots> sb1 = new ShoeBox<>();
ShoeBox<Sneakers> sb2 = new ShoeBox<>();
ShoeBox<Boots> sb3 = new ShoeBox<>();
ShoeBox<Sneakers> sb4 = new ShoeBox<>();
sb1.putObjectInBox(boots);
sb2.putObjectInBox(sneakers);
sb3.putObjectInBox(boots2);
sb4.putObjectInBox(sneakers2);
System.out.println(sb1);
System.out.println(sb2);
System.out.println("\n_____________________________________________\n");
// task d)
System.out.println("Create shelf with x=3 y=5");
Shelf shelf = new Shelf(3,5);
System.out.println("Leeres Regal:\n"+shelf);
//Adding boxes to shelf
System.out.println("Add sb1 to 0/4");
shelf.addBox(0,4,sb1);
System.out.println("Add sb2 to 2/2");
shelf.addBox(2,2,sb2);
System.out.println("Add sb3 to 0/1");
shelf.addBox(0,1,sb3);
System.out.println("Add sb4 to 2/1");
shelf.addBox(2,1,sb4);
System.out.println("\nContents of shelf:");
System.out.println(shelf);
//shelf.addBox(42, 32, sb1); //IndexOutOfBoundsException
//System.out.println("Add sb2 to 2/2 to throw an Exception");
//shelf.addBox(2,2,sb2); //Exception
//Removing boxes from shelf
System.out.println("Remove Box from 2/2");
shelf.removeBox(2, 2);
//shelf.removeBox(3, 5); //IndexOutOfBoundsException
//System.out.println("Remove Box from 2/2 to throw an Exception");
//shelf.removeBox(2, 2); //Exception
System.out.println("\nContents of shelf after removing:");
System.out.println(shelf);
}
}
package shoes;
public class Boots extends Shoes {
public Boots(String name, double size) {
super(name, size);
}
}
package shoes;
public abstract class Box<T> {
private T content;
public Box() {
super();
}
public Box(T content) {
this.content = content;
}
public void putObjectInBox(T obj) throws BoxException {
if (content == null) {
content = obj;
} else {
throw new BoxException("Box is full!");
}
}
public T removeObjectInBox() {
T temp = content;
content = null;
return temp;
}
public T getObjectInBox() {
return content;
}
@Override
public String toString() {
return "[" + content + "]";
}
}
\ No newline at end of file
package shoes;
public class BoxException extends Exception {
public BoxException(String msg) {
super(msg);
}
}
package shoes;
import java.util.ArrayList;
import java.util.List;
public class Shelf {
private ArrayList<ArrayList<ShoeBox<? extends Shoes>>> shelf;
private int height;
private int width;
public Shelf(int height, int width) {
this.height = height;
this.width = width;
shelf = new ArrayList<>();
for (int i = 0; i < height; i++) {
shelf.add(new ArrayList<ShoeBox<? extends Shoes>>());
for (int j = 0; j < width; j++) {
shelf.get(i).add(null); // add empty slots
}
}
}
public void addBox(int x, int y, ShoeBox<? extends Shoes> box) throws BoxException {
if (x < 0 || y < 0 || x >= this.height || y >= this.width)
throw new IndexOutOfBoundsException("There is no such place in the shelf!");
if (shelf.get(x).get(y) != null)
throw new BoxException("There is already a different box in " + x + "/" + y + "!");
shelf.get(x).set(y, box);
}
public void removeBox(int x, int y) throws BoxException {
if (x < 0 || y < 0 || x >= this.height || y >= this.width)
throw new IndexOutOfBoundsException("There is no such place in the shelf!");
if (shelf.get(x).get(y) == null)
throw new BoxException("No item to remove in " + x + "/" + y + "!");
shelf.get(x).set(y, null);
}
public ShoeBox<? extends Shoes> get(int x, int y) {
return shelf.get(x).get(y);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (List<ShoeBox<? extends Shoes>> shelfY : shelf) {
for (ShoeBox<? extends Shoes> box : shelfY) {
sb.append("|" + (box == null ? "----free----" : box.toString()) + "|");
}
sb.append('\n');
}
return sb.toString();
}
}
package shoes;
public class ShoeBox<T extends Shoes> extends Box<T> {
public ShoeBox() {
super();
}
public ShoeBox(T content) {
super(content);
}
}
package shoes;
public abstract class Shoes {
private String name;
private double size;
public Shoes (String name, double size) {
this.name = name;
this.size = size;
}
@Override
public String toString() {
return "{" + name + ", " + size + "}";
}
}
package shoes;
public class Sneakers extends Shoes {
public Sneakers(String name, double size) {
super(name, size);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment