Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sorting Laundry Objective: Write a program that will sort a basket of clothes in

ID: 3603010 • Letter: S

Question




Sorting Laundry Objective: Write a program that will sort a basket of clothes into their proper drawers. aware you are sort clothes by their type in this order: If you were not . 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 o 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 o Default o Parameterized Accessors and Mutators for the instance variables o Make sure to check for valid values in the mutator Methods o toString: Takes in no parameters and returns a string with the Type and Color of the garment o 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 o Clothes-a 2D array where there are only 5 drawers, and each drawer can hold 10 items of clothing . Constructors o Just default that creates the 2D array No Accessors or Mutators . Methods o 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 o 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. o print: This prints out every piece of clothing in the dresser Finally write a class called DresserFrontEnd e 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 o o o Quit immediately halts the program

Explanation / Answer

Clothing.java

enum ClothTypes{

undergarment,socks,stockings,top,bottom,cape;

}

enum ClothColors{

brown,red,pink,orange,green,blue,purple,grey;

}

public class Clothing {

private String clothType;

private String clothColor;

public Clothing() {

// TODO Auto-generated constructor stub

}

public Clothing(String clothType, String clothColor) throws Exception {

super();

if(Clothing.containsCloth(clothType)){

this.clothType=clothType;

}else{

throw new Exception("Invalid cloth type");

}

if(Clothing.containsColor(clothColor)){

this.clothColor=clothColor;

}else{

throw new Exception("Invalid cloth color");

}

}

public String getClothType() {

return clothType;

}

public void setClothType(String clothType) throws Exception{

if(Clothing.containsCloth(clothType)){

this.clothType=clothType;

}else{

throw new Exception("Invalid cloth type");

}

}

public String getClothColor() {

return clothColor;

}

public void setClothColor(String clothColor) throws Exception {

if(Clothing.containsColor(clothColor)){

this.clothColor=clothColor;

}else{

throw new Exception("Invalid cloth color");

}

}

public static boolean containsCloth(String clothType) {

for (ClothTypes c : ClothTypes.values()) {

if (c.name().equals(clothType)) {

return true;

}

}

return false;

}

public static boolean containsColor(String color) {

for (ClothColors c : ClothColors.values()) {

if (c.name().equals(color)) {

return true;

}

}

return false;

}

@Override

public String toString() {

return "Clothing [clothType=" + clothType + ", clothColor=" + clothColor + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((clothColor == null) ? 0 : clothColor.hashCode());

result = prime * result + ((clothType == null) ? 0 : clothType.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Clothing other = (Clothing) obj;

if (clothColor == null) {

if (other.clothColor != null)

return false;

} else if (!clothColor.equals(other.clothColor))

return false;

if (clothType == null) {

if (other.clothType != null)

return false;

} else if (!clothType.equals(other.clothType))

return false;

return true;

}

}

Dresser.java

public class Dresser {

Clothing[][] clArr;

public Dresser() {

clArr=new Clothing[5][10];;

}

public void add(Clothing clothing){

String clothType=clothing.getClothType();

switch (clothType) {

case "undergarment":

if(!isDrawerFull(clArr[0]))

for (int i = 0; i < clArr[0].length; i++) {

if(clArr[0][i]==null){

clArr[0][i]=clothing;

break;

}

}

break;

case "socks":

case "stockings":

if(!isDrawerFull(clArr[1]))

for (int i = 0; i < clArr[1].length; i++) {

if(clArr[1][i]==null){

clArr[1][i]=clothing;

break;

}

}

break;

case "top":

if(!isDrawerFull(clArr[2])){

for (int i = 0; i < clArr[2].length; i++) {

if(clArr[2][i]==null){

System.out.println("set");

clArr[2][i]=clothing;

break;

}

}

}

break;

case "bottom":

if(!isDrawerFull(clArr[3]))

for (int i = 0; i < clArr[3].length; i++) {

if(clArr[3][i]==null){

clArr[3][i]=clothing;

break;

}

}

break;

case "cape":

if(!isDrawerFull(clArr[4]))

for (int i = 0; i < clArr[4].length; i++) {

if(clArr[4][i]==null){

clArr[4][i]=clothing;

break;

}

}

break;

default:

break;

}

}

public void remove(Clothing clothing){

String clothType=clothing.getClothType();

switch (clothType) {

case "undergarment":

for (int i = 0; i < clArr[0].length; i++) {

if(clArr[0][i]!=null&&clArr[0][i].equals(clothing)){

clArr[0][i]=null;

}

}

break;

case "socks":

case "stockings":

for (int i = 0; i < clArr[1].length; i++) {

if(clArr[1][i]!=null&&clArr[1][i].equals(clothing)){

clArr[1][i]=null;

}

}

break;

case "top":

for (int i = 0; i < clArr[2].length; i++) {

if(clArr[2][i]!=null&&clArr[2][i].equals(clothing)){

clArr[2][i]=null;

}

}

break;

case "bottom":

for (int i = 0; i < clArr[3].length; i++) {

if(clArr[3][i]!=null&&clArr[3][i].equals(clothing)){

clArr[3][i]=null;

}

}

break;

case "cape":

for (int i = 0; i < clArr[4].length; i++) {

if(clArr[4][i]!=null&&clArr[4][i].equals(clothing)){

clArr[4][i]=null;

}

}

break;

default:

break;

}

}

public void print(){

for (int i = 0; i < clArr.length; i++) {

System.out.println("Drawer "+i);

for (int j = 0; j < clArr[i].length; j++) {

Clothing clothing = clArr[i][j];

if(clothing!=null)

System.out.println(clothing.getClothType()+" "+clothing.getClothColor());

}

}

}

public boolean isDrawerFull(Clothing[] clArr){

for (int i = 0; i < clArr.length; i++) {

if(clArr[i]==null){

return false;

}

}

return true;

}

}

DresserFrontEnd.java

import java.util.Scanner;

public class DresserFrontEnd {

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

Dresser d=new Dresser();

String clothType;

String clothColor;

Clothing cl;

while(true){

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: quit");

try{

switch(sc.nextInt()){

case 1:

System.out.println("Enter the type");

System.out.println("It may be undergarment, socks, stockings, top, bottom, or cape");

clothType=sc.next();

System.out.println("Enter a color");

System.out.println("It may be brown, pink, orange, green, blue, purple or grey");

clothColor=sc.next();

cl=new Clothing(clothType,clothColor);

d.add(cl);

break;

case 2:

System.out.println("Enter the type");

System.out.println("It may be undergarment, socks, stockings, top, bottom, or cape");

clothType=sc.next();

System.out.println("Enter a color");

System.out.println("It may be brown, pink, orange, green, blue, purple or grey");

clothColor=sc.next();

cl=new Clothing(clothType,clothColor);

d.remove(cl);

break;

case 3:

d.print();

break;

case 9:

System.exit(0);

break;

}

}catch(Exception ex){

System.out.println(ex.getMessage());

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

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: 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
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: quit
3
Drawer 0
Drawer 1
socks pink
Drawer 2
Drawer 3
Drawer 4
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: 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
green
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: quit
3
Drawer 0
Drawer 1
socks pink
socks green
Drawer 2
Drawer 3
Drawer 4
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: 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
green
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: quit
3
Drawer 0
Drawer 1
socks pink
Drawer 2
Drawer 3
Drawer 4
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: quit
9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote