Browse Source

Initial commit

master
Robby Zambito 1 year ago
commit
3073323191
14 changed files with 516 additions and 0 deletions
  1. +9
    -0
      AllDrawersOfTypeFullException.java
  2. +37
    -0
      Clothes.java
  3. +9
    -0
      ClothesTypeMismatchException.java
  4. +42
    -0
      Color.java
  5. +118
    -0
      Drawer.java
  6. +98
    -0
      Dresser.java
  7. +40
    -0
      Main.java
  8. +19
    -0
      Pajamas.java
  9. +34
    -0
      Pants.java
  10. +1
    -0
      README.md
  11. +30
    -0
      Shirt.java
  12. +29
    -0
      Shorts.java
  13. +31
    -0
      Socks.java
  14. +19
    -0
      Underwear.java

+ 9
- 0
AllDrawersOfTypeFullException.java View File

@@ -0,0 +1,9 @@
public class AllDrawersOfTypeFullException extends Exception
{
public AllDrawersOfTypeFullException(){}
public AllDrawersOfTypeFullException(String message)
{
super(message);
}
}

+ 37
- 0
Clothes.java View File

@@ -0,0 +1,37 @@
public abstract class Clothes
{
public enum Type {
SHIRT, PANTS, SHORTS, SOCKS, UNDERWEAR, PAJAMAS
}
private int color;
public Clothes(int color)
{
this.color = color;
}
public String getColor()
{
switch(color)
{
case Color.BLACK: return "black";
case Color.RED: return "red";
case Color.GREEN: return "green";
case Color.YELLOW: return "yellow";
case Color.BLUE: return "blue";
case Color.MAGENTA: return "magenta";
case Color.CYAN: return "cyan";
case Color.WHITE: return "white";
}
return "DEFAULT";
}
public int getNumLines()
{
return toString().split("\n").length;
}
public abstract String toString();
}

+ 9
- 0
ClothesTypeMismatchException.java View File

@@ -0,0 +1,9 @@
public class ClothesTypeMismatchException extends Exception
{
public ClothesTypeMismatchException(){}
public ClothesTypeMismatchException(String message)
{
super(message);
}
}

+ 42
- 0
Color.java View File

@@ -0,0 +1,42 @@
public abstract class Color
{
private static final String _RESET = "\u001B[0m";
private static final String _BLACK = "\u001B[30m";
private static final String _RED = "\u001B[31m";
private static final String _GREEN = "\u001B[32m";
private static final String _YELLOW = "\u001B[33m";
private static final String _BLUE = "\u001B[34m";
private static final String _MAGENTA = "\u001B[35m";
private static final String _CYAN = "\u001B[36m";
private static final String _WHITE = "\u001B[37m";
public static final int RESET = 0;
public static final int BLACK = 1;
public static final int RED = 2;
public static final int GREEN = 3;
public static final int YELLOW = 4;
public static final int BLUE = 5;
public static final int MAGENTA = 6;
public static final int CYAN = 7;
public static final int WHITE = 8;
public static String toString(int color)
{
switch(color)
{
case RESET: return _RESET;
case BLACK: return _BLACK;
case RED: return _RED;
case GREEN: return _GREEN;
case YELLOW: return _YELLOW;
case BLUE: return _BLUE;
case MAGENTA: return _MAGENTA;
case CYAN: return _CYAN;
case WHITE: return _WHITE;
}
return _RESET;
}
}

+ 118
- 0
Drawer.java View File

@@ -0,0 +1,118 @@
public class Drawer
{
private Clothes[] clothes;
private Clothes.Type type;
private int nextAddIndex = 1;
private boolean hasContents = false;
public Drawer(Clothes.Type type)
{
this.type = type;
switch(type)
{
case SHIRT:
clothes = new Shirt[41];
fill(new Shirt(Color.RESET));
break;
case PANTS:
clothes = new Pants[11];
fill(new Pants(Color.RESET));
break;
case SHORTS:
clothes = new Shorts[21];
fill(new Shorts(Color.RESET));
break;
case SOCKS:
clothes = new Socks[61];
fill(new Socks(Color.RESET));
break;
case UNDERWEAR:
clothes = new Underwear[41];
fill(new Underwear(Color.RESET));
break;
case PAJAMAS:
clothes = new Pajamas[41];
fill(new Pajamas(Color.RESET));
}
}
/**
* return true if the add was successful
* return false if the add was unsuccessful
*/
public boolean add(Clothes clothes)
{
if(clothes.getClass().equals(this.clothes[0].getClass()))
{
try {
this.clothes[nextAddIndex] = clothes;
nextAddIndex++;
} catch(ArrayIndexOutOfBoundsException e) {
return false;
}
hasContents = true;
}
else {
try{
throw new ClothesTypeMismatchException();
} catch(ClothesTypeMismatchException e) {
e.printStackTrace();
return false;
}
}
return true;
}
protected Class getType()
{
return clothes[0].getClass();
}
private void fill(Clothes clothes)
{
fill(clothes, 0, this.clothes.length - 1);
}
public void fill(Clothes clothes, int startingIndex, int endingIndex)
{
for(int i = startingIndex; i < endingIndex; i++)
{
this.clothes[i] = clothes;
}
}
public boolean isFull()
{
return clothes.length <= nextAddIndex;
}
public String toString()
{
StringBuilder result = new StringBuilder("Type: " + clothes[0].getClass().getSimpleName() + "\tCapacity: " + (clothes.length - 1) + "\tContains: " + (nextAddIndex - 1));
if(hasContents)
{
String[][] allClothes = new String[nextAddIndex][clothes[0].getNumLines()];
result.append("\n");
for(int i = 1; i < nextAddIndex; i++)
allClothes[i] = clothes[i].toString().split("\n");
for(int i = 1; i < nextAddIndex; i++)
if(i + 1 < nextAddIndex)
{
for(int j = 0; j < allClothes[i].length; j++)
result.append(allClothes[i][j] + "\t" + allClothes[i + 1][j] + "\n");
i++;
}
else
for(int j = 0; j < allClothes[i].length; j++)
result.append(allClothes[i][j] + "\n");
}
return result.toString();
}
}

+ 98
- 0
Dresser.java View File

@@ -0,0 +1,98 @@
public class Dresser
{
private Drawer[] drawers;
public Dresser(int numberOfDrawers)
{
drawers = new Drawer[numberOfDrawers];
Clothes.Type[] typeArr = new Clothes.Type[] {
Clothes.Type.SHIRT, Clothes.Type.PANTS, Clothes.Type.SHORTS, Clothes.Type.SOCKS, Clothes.Type.UNDERWEAR, Clothes.Type.PAJAMAS
};
for(int i = 0; i < numberOfDrawers; i++)
drawers[i] = new Drawer( typeArr[i % 6] );
}
public Dresser()
{
this(6);
}
public Drawer getDrawer(int drawer)
{
return drawers[drawer];
}
public Drawer getDrawer(String str) throws ClassNotFoundException
{
return getDrawer(Class.forName(str));
}
public Drawer getDrawer(Class drawer)
{
for(int i = 0; i < drawers.length; i++)
if(drawers[i].getType().equals(drawer)) return drawers[i];
try{
throw new ClothesTypeMismatchException();
} catch(ClothesTypeMismatchException e) {
e.printStackTrace();
System.exit(-1);
}
return null;
}
public void add(Clothes clothes)
{
if(clothes instanceof Shirt)
{
for(int i = 0; i <= drawers.length; i += 6)
if(drawers[i].add(clothes)) break;
return;
}
if(clothes instanceof Pants)
{
for(int i = 1; i <= drawers.length; i += 6)
if(drawers[i].add(clothes)) break;
return;
}
if(clothes instanceof Shorts)
{
for(int i = 2; i <= drawers.length; i += 6)
if(drawers[i].add(clothes)) break;
return;
}
if(clothes instanceof Socks)
{
for(int i = 3; i <= drawers.length; i += 6)
if(drawers[i].add(clothes)) break;
return;
}
if(clothes instanceof Underwear)
{
for(int i = 4; i <= drawers.length; i += 6)
if(drawers[i].add(clothes)) break;
return;
}
if(clothes instanceof Pajamas)
{
for(int i = 5; i <= drawers.length; i += 6)
if(drawers[i].add(clothes)) break;
return;
}
}
public String toString()
{
StringBuilder result = new StringBuilder();
for(Drawer drawer: drawers)
result.append(drawer.toString() + "\n");
return result.toString();
}
}

+ 40
- 0
Main.java View File

@@ -0,0 +1,40 @@
import java.util.*;

public class Main
{
public static void main(String[] args) throws ClassNotFoundException
{
long t = System.currentTimeMillis();
Dresser d = new Dresser();
//d.getDrawer(Drawer.SHIRT).add(new Pants(Color.BLUE)); // Produces a ClothesTypeMismatchException
// Add shirts
d.add(new Shirt());
d.add(new Shirt(Color.BLUE));
d.add(new Shirt(Color.GREEN));
d.add(new Shirt(Color.YELLOW));
d.add(new Shirt(Color.MAGENTA));
// Add pants
d.add(new Pants());
d.add(new Pants(Color.YELLOW));
d.add(new Pants(Color.MAGENTA));
// Add shorts
d.add(new Shorts());
d.add(new Shorts(Color.MAGENTA));
// Add socks
d.add(new Socks());
d.add(new Socks(Color.BLUE));
d.add(new Socks(Color.GREEN));
d.add(new Socks(Color.YELLOW));
d.add(new Socks(Color.RED));
System.out.print(d);
System.out.printf("Run time: %dms\n", (System.currentTimeMillis() - t));
}
}

+ 19
- 0
Pajamas.java View File

@@ -0,0 +1,19 @@
public class Pajamas extends Clothes
{
private int color;
public Pajamas(int color)
{
super(color);
this.color = color;
}
public Pajamas()
{
this(Color.BLUE);
}
public String toString()
{
return Color.toString(color) + getColor() + " pajamas" + Color.toString(Color.RESET);
}
}

+ 34
- 0
Pants.java View File

@@ -0,0 +1,34 @@
public class Pants extends Clothes
{
private int color;
public Pants(int color)
{
super(color);
this.color = color;
}
public Pants()
{
this(Color.BLUE);
}
public String toString()
{
return Color.toString(color) + " _____________\n" + Color.toString(Color.RESET) +
Color.toString(color) + " [_I_I[L]=_I_I_] \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / | : \\ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | /| \\ | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | '- | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " \\ | /^\\ | / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " \\ | | | | / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / -|-| |-|- \\ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " |__|_| |_|__| \n" + Color.toString(Color.RESET) +
Color.toString(color) + " [____] [____] \n" + Color.toString(Color.RESET);
}
}

+ 1
- 0
README.md View File

@@ -0,0 +1 @@
# Dresser

+ 30
- 0
Shirt.java View File

@@ -0,0 +1,30 @@
public class Shirt extends Clothes
{
private int color;
public Shirt(int color)
{
super(color);
this.color = color;
}
public Shirt()
{
this(Color.RED);
}
public String toString()
{
return Color.toString(color) + " __.-.__.-.__ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " .'\\ '-.__.-' /'. \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / | | \\ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / | | \\\n" + Color.toString(Color.RESET) +
Color.toString(color) + " '-._/ _.-' \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " '._ _.' \n" + Color.toString(Color.RESET) +
Color.toString(color) + " `\"\"\"\"` \n" + Color.toString(Color.RESET);
}
}

+ 29
- 0
Shorts.java View File

@@ -0,0 +1,29 @@
public class Shorts extends Clothes
{
private int color;
public Shorts(int color)
{
super(color);
this.color = color;
}
public Shorts()
{
this(Color.GREEN);
}
public String toString()
{
return Color.toString(color) + " _____________\n" + Color.toString(Color.RESET) +
Color.toString(color) + " [_I_I[L]=_I_I_] \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / | : \\ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | /| \\ | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | '- | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " \\ | /^\\ | / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | | | | | | \n" + Color.toString(Color.RESET) +
Color.toString(color) + " |__|_| |_|__| \n" + Color.toString(Color.RESET) +
Color.toString(color) + " [____] [____] \n" + Color.toString(Color.RESET);
}
}

+ 31
- 0
Socks.java View File

@@ -0,0 +1,31 @@
public class Socks extends Clothes
{
private int color;
public Socks(int color)
{
super(color);
this.color = color;
}
public Socks()
{
this(Color.WHITE);
}
public String toString()
{
return Color.toString(color) + " ________ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " /` `\\ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " |` _____|__ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " |` /` `\\\n" + Color.toString(Color.RESET) +
Color.toString(color) + " |`==|` `| \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / |` `| \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / |`=======`| \n" + Color.toString(Color.RESET) +
Color.toString(color) + " ______/ / / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " / / / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | ______/ / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " |___/ ______/ \n" + Color.toString(Color.RESET) +
Color.toString(color) + " | / / \n" + Color.toString(Color.RESET) +
Color.toString(color) + " |________|____/ \n" + Color.toString(Color.RESET);
}
}

+ 19
- 0
Underwear.java View File

@@ -0,0 +1,19 @@
public class Underwear extends Clothes
{
private int color;
public Underwear(int color)
{
super(color);
this.color = color;
}
public Underwear()
{
this(Color.WHITE);
}
public String toString()
{
return Color.toString(color) + getColor() + " underwear" + Color.toString(Color.RESET);
}
}