public class Dress {
private int code;
private String color;
private String material;
private String brand;
public Dress(int code, String color, String material, String brand) {
this.code = code;
this.color = color;
this.material = material;
this.brand = brand;
}
public int getCode() {
return code;
}
}
public class DressCollection {
private Dress[] formal;
private Dress[] casual;
private int formalTail;
private int casualTail;
public DressCollection(int size) {
formal = new Dress[size];
casual = new Dress[size];
formalTail = 0;
casualTail = 0;
}
public boolean addCasual(Dress dress) {
if (casualTail == casual.length) {
return false;
}
casual[casualTail++] = dress;
return true;
}
public boolean addFormal(Dress dress) {
if (formalTail == formal.length) {
return false;
}
formal[formalTail++] = dress;
return true;
}
public boolean removeCasual(int code) {
for (int i = 0; i < casualTail; i++) {
if (casual[i].getCode() == code) {
for (int j = i + 1; j < casualTail; j++) {
casual[j - 1] = casual[j];
}
casual[--casualTail] = null;
return true;
}
}
return false;
}
public boolean removeFormal(int code) {
for (int i = 0; i < formalTail; i++) {
if (formal[i].getCode() == code) {
for (int j = i + 1; j < formalTail; j++) {
formal[j - 1] = formal[j];
}
formal[--formalTail] = null;
return true;
}
}
return false;
}
}
import java.util.Scanner;
public class Wardrobe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
DressCollection dressCollection = new DressCollection(3);
String choice;
int code;
String color;
String material;
String brand;
while (true) {
System.out.println("1. Add casual\n2. Remove casual\n" +
"3. Add formal\n4. Remove formal\n0. Exit");
choice = in.nextLine();
switch (choice) {
case "1":
case "3":
System.out.println("Code:");
code = Integer.parseInt(in.nextLine());
System.out.println("Color:");
color = in.nextLine();
System.out.println("Material:");
material = in.nextLine();
System.out.println("Brand:");
brand = in.nextLine();
if (choice.equals("1")) {
System.out.println(dressCollection.addCasual(new Dress(code, color, material, brand)));
} else {
System.out.println(dressCollection.addFormal(new Dress(code, color, material, brand)));
}
break;
case "2":
case "4":
System.out.println("Code:");
code = Integer.parseInt(in.nextLine());
if (choice.equals("2")) {
System.out.println(dressCollection.removeCasual(code));
} else {
System.out.println(dressCollection.removeFormal(code));
}
break;
case "0":
System.exit(0);
}
}
}
}
Comments
Leave a comment