0% found this document useful (0 votes)
11 views4 pages

Coderbyte Test

The document outlines an enhancement of the Inventory class to include methods for adding, removing, and counting parts while ensuring parts are grouped by type and duplicates are not added. It also specifies improvements to the getDescription() methods of TableParts and ChairParts. The implementation includes a main method for testing the functionality of the Inventory class and its methods.

Uploaded by

sreeknine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Coderbyte Test

The document outlines an enhancement of the Inventory class to include methods for adding, removing, and counting parts while ensuring parts are grouped by type and duplicates are not added. It also specifies improvements to the getDescription() methods of TableParts and ChairParts. The implementation includes a main method for testing the functionality of the Inventory class and its methods.

Uploaded by

sreeknine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

version 1.01
Enhance the Inventory class to have a add/remove/size/countByType method and passes
the tests.
- Data Types and Modifiers must stay unchanged
- The inventory must group parts by type
- The inventory must not add parts with existing name (ex. if part "a" exists,
should not add any more parts with the same name)
- Enhance TableParts and ChairParts getDescription() method to pass their tests
*/

import [Link];
import [Link];
import [Link];
import [Link];

enum PartsType {
CHAIR_TYPE, TABLE_TYPE
}

class Inventory implements Storage {

Inventory() {}

static Map<String, PartsType> currentInventory = new HashMap<>();

@Override
public void add(Part part) {
if(![Link]([Link]))
[Link]([Link](), [Link]());
}

@Override
public void remove(PartsType type, String partName) {
String givenPartName = type == PartsType.TABLE_TYPE ? "(("+partName+"))" :
"("+partName+")";
boolean matchExists = false;
for([Link]<String, PartsType> e : [Link]()) {
if(type == [Link]() && [Link]([Link]())) {
matchExists = true;
}
}
if(matchExists) [Link](givenPartName, type);
}

@Override
public int size() {
return [Link]()/2;
}

@Override
public int countByType(PartsType type) {
int count = 0;
if([Link]() > 0) {
for ([Link]<String, PartsType> i : [Link]()) {
if (type == [Link]()) count++;
}
}
return count;
}

@Override
public Integer countByType(Class<? extends Part> part) {
PartsType type = [Link]().equals([Link]) ?
PartsType.TABLE_TYPE : PartsType.CHAIR_TYPE;
return countByType(type);
}

@Override
public void remove(Class<? extends Part> part, String partName) {
String givenPartName = [Link]().equals([Link]) ?
"(("+partName+"))" : "("+partName+")";
PartsType type = [Link]().equals([Link]) ?
PartsType.TABLE_TYPE : PartsType.CHAIR_TYPE;
boolean matchExists = false;
for([Link]<String, PartsType> e : [Link]()) {
if(type == [Link]() && [Link]([Link]())) {
matchExists = true;
}
}
if(matchExists) [Link](givenPartName, type);
}

@Override
public boolean containsName(List<Part> list, String name) {
return false;
}
}

interface Storage {
void add(Part part);

void remove(PartsType type, String partName);

int size();

int countByType(PartsType type);

Integer countByType(Class<? extends Part> part);

void remove(Class<? extends Part> part, String partName);

boolean containsName(final List<Part> list, final String name);


}

abstract class Part {


protected PartsType type;
protected String name;

Part(PartsType type, String name) {


[Link] = name;
[Link] = type;
}

public PartsType getType() {


return type;
}
public abstract String getDescription();
}

class ChairParts extends Part {

ChairParts(String name) {
super(PartsType.CHAIR_TYPE, name);
}

@Override
public String getDescription() {
return "(" +name+ ")";
}
}

class TableParts extends Part {

TableParts(String name) {
super(PartsType.TABLE_TYPE, name);
}

@Override
public String getDescription() {
return "((" +name+ "))";
}
}

class Factory {
Part createTable(String name) {
return new TableParts (name);
}

Part createChair(String name) {


return new ChairParts (name);
}
}

class Main {
public static void main(String[] args) {
run(new Inventory(), new Factory());
}

public static void run(Inventory storage, Factory factory) {


// Code below should not be changed
[Link]([Link]("a"));

[Link]([Link]("a"));
[Link]([Link]("b"));
[Link]([Link]("b"));
[Link]([Link]("c"));

assertEqual([Link](), 2);
assertEqual([Link](PartsType.TABLE_TYPE), 1);
assertEqual([Link](PartsType.CHAIR_TYPE), 3);

[Link](PartsType.TABLE_TYPE, "a");
[Link](PartsType.TABLE_TYPE, "a");
assertEqual([Link](PartsType.TABLE_TYPE), 0);
Part newChair = [Link]("newChair");
Part newTable = [Link]("newTable");

assertEqual([Link](), "(newChair)");
assertEqual([Link](), "((newTable))");

[Link](newChair);
assertEqual([Link](PartsType.CHAIR_TYPE), 4);
[Link]([Link], "newChair");
assertEqual([Link]([Link]), 3);

[Link]("done.");

static void assertEqual(Object o1, Object o2) {


if (o1 == o2 || [Link](o2)) {
return;
}
throw new RuntimeException("Assertion failed " + [Link]() + " is not
equal to " + [Link]());
}

You might also like