HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Models an assembly line

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
modelslineassembly

Problem

The gist of the code is that there's an assembly line of products, in this case Toy and Book, that get inspected by workers who then add them to a box. The box is then sent to someone who knows what to do when they receive a box for a given product. I made the fields of Product, Toy, and Book public to shorten the code because it is already on the long side (just under 200 lines), though it is easy to read. A potential downside I noticed is that if I were to introduce a new product, then I would need to add a new Product class, a new AssemblyWorker class, new Receiver class, and another else if statement in the fetchWorker method. Is this normal or is my approach flawed?

Any feedback will be greatly appreciated.

```
package org.poly;

import java.util.*;

class Product {
int id;
double cost;
public String toString() {
return id+" "+cost;
}
}

class Toy extends Product {
String name;
public String toString() {
return super.toString()+" "+name;
}
}

class Book extends Product {
String title;
String author;
public String toString() {
return super.toString()+" "+title+" "+author;
}
}

class ProductBox {
private List products;

ProductBox() {
products = new ArrayList<>();
}

void addProduct(T product) {
products.add(product);
}

T getProduct(int idx) {
if (idx = products.size())
throw new ArrayIndexOutOfBoundsException();
return products.get(idx);
}

List getProducts() {
return products;
}
}

abstract class AssemblyWorker {

void startAssemblyLine(String text) {
Scanner scanner = new Scanner(text);
while (scanner.hasNext()) {
List tokens = new ArrayList<>(
Arrays.asList(scanner.nextLine().split(",")));
Iterator it = tokens.iterator();
inspectProduct(it);
}
}

abstract void inspectProduct(Iterator it);
abstract Receiver de

Solution

Spaces are not only cheap but free. These free spaces will add readability to your code and makes it easier to maintain.

class Book extends Product {
    String title;
    String author;
    public String toString() {
        return super.toString()+" "+title+" "+author;
    }
}


would be better like so

class Book extends Product {
    String title;
    String author;
    public String toString() {
        return super.toString() + " " + title + " " + author;
    }
}


or you can use String.format().

Code Snippets

class Book extends Product {
    String title;
    String author;
    public String toString() {
        return super.toString()+" "+title+" "+author;
    }
}
class Book extends Product {
    String title;
    String author;
    public String toString() {
        return super.toString() + " " + title + " " + author;
    }
}

Context

StackExchange Code Review Q#151628, answer score: 2

Revisions (0)

No revisions yet.