Java

OOP GUIDE / WORK

 

SHOESTORE & SHOE CLASSES SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solutions required.

 

 

TASK – PART 2 – IMPLEMENT COMPARABLE

 

Below are my new versions of the Shoe and ShoeStore classes.

 

public class Shoe implements Comparable

{

     private String brand;

     private int size;

     private String colour;

    

     //Creates a random shoe

     public Shoe()

     {

           String[] brands = {"Nike", "Adidas", "Puma", "Reebok", "New Balance"};

           String[] colours = {"Blue", "Pink", "Yellow", "Teal", "White", "Black"};

          

           brand = brands[(int)(Math.random() * brands.length)];

           colour = colours[(int)(Math.random() * colours.length)];

           size = (int)(Math.random()*8) + 6;  //6-13

     }

    

     public String toString()

     {

           return brand + ", " + colour + ", Size: " + size;

     }

    

     public String getBrand()

     {

           return brand;

     }

    

     public int getSize()

     {

           return size;

     }

          

     public String getColour()

     {

           return colour;

     }

 

     @Override

     public int compareTo(Object o)

     {

           Shoe other = (Shoe)o;

           if (!this.brand.equals(other.brand))

                return this.brand.compareTo(other.brand);

           else //same brands, so sort by shoe size

           {

                return ((Integer)this.size).compareTo(other.size);  //clever

           }

     }

}

 

import java.util.Arrays;

 

public class ShoeStore

{

     private Shoe[] inv;

    

     public ShoeStore(int amount)

     {

           inv = new Shoe[amount];

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

           {

                inv[i] = new Shoe();

           }

           Arrays.sort(inv);  //sort using compareTo

     }

    

     public void displayInventory()

     {

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

           {

                System.out.println(inv[i]);

           }

     }

}

 

 

TASK – PART 3 – TESTING

 

No solutions required.