Objective Write a program that will sort a basket of clothes
Objective:
Write a program that will sort a basket of clothes into their proper drawers. If you were not aware you are sort clothes by their type in this order:
Top Drawer – Undergarments
Next Drawer – Socks or Stockings
The Following Drawer – Tops
The Subsequent Drawer – Bottoms
The Cape Drawer – Capes
Write a class called Clothing
Instance Variables
Type – This can only be Undergarment, Socks, Stockings, Top, Bottom, and Cape
Color – This can only be Brown, Red, Pink, Orange, Green, Blue, Purple, and Grey
Constructors
Default
Parameterized
Accessors and Mutators for the instance variables
Make sure to check for valid values in the mutator
Methods
toString: Takes in no parameters and returns a string with the Type and Color of the garment
equals: Takes an instance of Clothing as a parameters and returns true only if the parameters are equal
Next write a class called Dresser
Instance Variables
Clothes – a 2D array where there are only 5 drawers, and each drawer can hold 10 items of clothing
Constructors
Just default that creates the 2D array
No Accessors or Mutators
Methods
add: Takes in an instance of Clothing as a parameter and returns nothing. The parameter is then sorted in their proper drawers by its type as mentioned above. If a drawer is full make sure to tell the user.
remove: Takes in an instance of Clothing as a parameter and returns nothing. This method searches for a piece of clothing, and if it exists it is removed (by setting that value to null).
print: This prints out every piece of clothing in the dresser
Finally write a class called DresserFrontEnd
Contains the main method
Prompts the user to add clothing, remove clothing, check what is in the dresser, or quit
Adding an item should prompt the user to enter the type and color
Removing should prompt the user to again enter the type and color they wish to remove
Printing shows the user what is in the dresser
Quit immediately halts the program
Example Output:
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
top
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
red
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
cape
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
purple
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
socks
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
grey
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
cape
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
blue
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
undergarment
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
pink
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
3
Drawer 0
undergarment pink
Drawer 1
socks grey
Drawer 2
top red
Drawer 3
Drawer 4
cape purple
cape blue
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
2
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
socks
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
grey
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
3
Drawer 0
undergarment pink
Drawer 1
Drawer 2
top red
Drawer 3
Drawer 4
cape purple
cape blue
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
9
Goodbye
Solution
//Describes the type of the clothing
enum Clothing_Type {
Undergarment, Socks, Stockings, Top, Bottom, Cape
}
//Describes the color of the clothing
enum Clothing_Color {
Brown, Red, Pink, Orange, Green, Blue, Purple, Grey
}
public class Clothing {
// Instance variables
private Clothing_Type type;
private Clothing_Color color;
/**
* Default constructor
*/
public Clothing() {
this.type = null;
this.color = null;
}
/**
* Parameterized constructor
* @param type
* @param color
*/
public Clothing(String type, String color) {
setType(type);
setColor(color);
}
/**
* @return the type
*/
public Clothing_Type getType() {
return type;
}
/**
* Sets the clothing type
* If not a valid type throws an exception
*/
public void setType(String type) throws IllegalArgumentException {
try {
this.type = Clothing_Type.valueOf(type);
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException(\"Invalid clothing type: \" + type);
}
}
/**
* @return the color
*/
public Clothing_Color getColor() {
return color;
}
/**
* Sets the clothing color
* If not a valid color throws an exception
*/
public void setColor(String color) throws IllegalArgumentException {
try {
this.color = Clothing_Color.valueOf(color);
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException(\"Invalid clothing color: \" + color);
}
}
@Override
public String toString() {
return getType() + \" \" + getColor();
}
@Override
public boolean equals(Object obj) {
if (obj != null) {
if (this == obj)
return true;
Clothing other = (Clothing) obj;
if ((color == other.color) && (type == other.type))
return true;
}
return false;
}
}
public class Dresser {
//Instance variables
private Clothing[][] clothes;
/**
* Default constructor
*/
public Dresser() {
this.clothes = new Clothing[5][10];
}
/**
* Gets the first vacant spot in the drawer
* @param drawer
* @return
*/
private int getVacantSpot(int drawer) {
for (int i = 0 ; i < 10 ; i++) {
if(this.clothes[drawer][i] == null)
return i;
}
return -1;
}
/**
* Takes in an instance of Clothing as a parameter and returns nothing.
* The parameter is then sorted in their proper drawers by its type as mentioned above.
* If a drawer is full displays a message to the user.
* @param garment
*/
public void add(Clothing garment) {
int i = 0;
for (Clothing_Type type : Clothing_Type.values()) {
if (type == garment.getType()) {
if (i > 1) // Because Socks and Stockings fall under one category and in enum we have them separate
i -= 1;
int spot = getVacantSpot(i);
if(spot != -1)
this.clothes[i][spot] = garment;
else
System.out.println(\"Drawer is full.\");
break;
}
i += 1;
}
}
/**
* Takes in an instance of Clothing as a parameter and returns nothing.
* This method searches for a piece of clothing, and if it exists it is removed (by setting that value to null).
* @param garment
*/
public void remove(Clothing garment) {
for (int i = 0 ; i < 5 ; i++) {
for (int j = 0 ; j < 5 ; j++) {
if((this.clothes[i][j] != null) && (clothes[i][j].equals(garment))) {
this.clothes[i][j] = null;
break;
}
}
}
}
/**
* This prints out every piece of clothing in the dresser
*/
public void print() {
int i = 0;
for (Clothing[] clothings : clothes) {
System.out.println(\"Drawer \" + i);
for (Clothing cloth : clothings) {
if(cloth != null) {
System.out.println(cloth);
}
}
i += 1;
System.out.println();
}
}
}
import java.util.Scanner;
public class DresserFrontEnd {
/**
* Displays the menu
*/
public static void menu() {
System.out.println(\"Welcome to the dresser!\");
System.out.println(\"Enter 1: to add an item\");
System.out.println(\"Enter 2: to remove an item\");
System.out.println(\"Enter 3: to print out the dresser contents\");
System.out.println(\"Enter 9: to quit\");
}
public static void main(String[] args) {
// Scanner to get user input
Scanner scanner = new Scanner(System.in);
try {
// Create Dresser object
Dresser dresser = new Dresser();
int choice = 0;
while (choice != 9) {
// Display menu
menu();
choice = scanner.nextInt();
// Clear keyboard buffer
scanner.nextLine();
switch (choice) {
case 1: // Add clothing
System.out.println(\"Enter the type\");
System.out.println(\"It may be undergarment, socks, stockings, top, bottom, or cape\");
StringBuffer type = new StringBuffer(scanner.nextLine().trim());
// Transform the input to camel case so that it can be used
// to match the enum type
type.replace(0, type.length(), type.toString().toLowerCase());
type.setCharAt(0, Character.toUpperCase(type.charAt(0)));
System.out.println(\"Enter a color\");
System.out.println(\"It may be brown, pink, orange, green, blue, purple, or grey\");
StringBuffer color = new StringBuffer(scanner.nextLine());
// Transform the input to camel case so that it can be used
// to match the enum color
color.replace(0, color.length(), color.toString().toLowerCase().trim());
color.setCharAt(0, Character.toUpperCase(color.charAt(0)));
// Add clothing to the drawer
dresser.add(new Clothing(type.toString(), color.toString()));
break;
case 2: // Remove clothing
System.out.println(\"Enter the type\");
System.out.println(\"It may be undergarment, socks, stockings, top, bottom, or cape\");
type = new StringBuffer(scanner.nextLine());
// Transform the input to camel case so that it can be used
// to match the enum type
type.replace(0, type.length(), type.toString().toLowerCase().trim());
type.setCharAt(0, Character.toUpperCase(type.charAt(0)));
System.out.println(\"Enter a color\");
System.out.println(\"It may be brown, pink, orange, green, blue, purple, or grey\");
color = new StringBuffer(scanner.nextLine());
// Transform the input to camel case so that it can be used
// to match the enum color
color.replace(0, color.length(), color.toString().toLowerCase().trim());
color.setCharAt(0, Character.toUpperCase(color.charAt(0)));
// Remove clothing to the drawer
dresser.remove(new Clothing(type.toString(), color.toString()));
break;
case 3: // Display dresser contents
dresser.print();
break;
case 9: // Quit
System.out.println(\"Goodbye\");
break;
default: // Invalid input
System.out.println(\"Invalid choice. Please try again.\");
}
System.out.println();
}
} catch (IllegalArgumentException e) {
// Close scanner
scanner.close();
System.out.println(e.getMessage());
}
}
}
SAMPLE OUTPUT:
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
top
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
red
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
cape
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
purple
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
socks
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
grey
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
cape
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
blue
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
1
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
undergarment
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
pink
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
3
Drawer 0
Undergarment Pink
Drawer 1
Socks Grey
Drawer 2
Top Red
Drawer 3
Drawer 4
Cape Purple
Cape Blue
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
2
Enter the type
It may be undergarment, socks, stockings, top, bottom, or cape
socks
Enter a color
It may be brown, pink, orange, green, blue, purple, or grey
grey
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
3
Drawer 0
Undergarment Pink
Drawer 1
Drawer 2
Top Red
Drawer 3
Drawer 4
Cape Purple
Cape Blue
Welcome to the dresser!
Enter 1: to add an item
Enter 2: to remove an item
Enter 3: to print out the dresser contents
Enter 9: to quit
9
Goodbye










