Design a Guitar Inventory System


  • 4

    Design Guitar Inventory System (Object Oriented Design).

    Solution: Let’s assume we want to design a basic Guitar Inventory System which provides the following functionality:

    • Searching the inventory for guitars on the basis of some specifications.
    • User can add / delete a guitar.

    The class Guitar represents a guitar as name suggests. It has a inner class GuitarSpec that stores all the information to specify a guitar which will be used to search a specific kind of guitars. Inventory class represents the inventory of guitars in which we add or delete from guitars.

    The classes :

    1. Guitar
    2. GuitarSpec
    3. Inventory

    Full code is given below:

    // This enum is used to specify the builder of a guitar
    public enum Builder { 
    
      FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY;
    
      public String toString() {
        switch(this) {
          case FENDER:   return "Fender";
          case MARTIN:   return "Martin";
          case GIBSON:   return "Gibson";
          case COLLINGS: return "Collings";
          case OLSON:    return "Olson";
          case RYAN:     return "Ryan";
          case PRS :     return "PRS";
          default:       return "Unspecified";
        }
      }
    }
    
    // This enum is used to specify the type of a guitar
    public enum Type { 
    
      ACOUSTIC, ELECTRIC;
    
      public String toString() {
        switch(this) {
          case ACOUSTIC: return "acoustic";
          case ELECTRIC: return "electric";
          default:       return "unspecified";
        }
      }
    }
    
    // This enum is used to specify the wood of a guitar
    public enum Wood { 
    
      INDIAN_ROSEWOOD, BRAZILIAN_ROSEWOOD, MAHOGANY, MAPLE,
      COCOBOLO, CEDAR, ADIRONDACK, ALDER, SITKA;
    
      public String toString() {
        switch(this) {
          case INDIAN_ROSEWOOD:    return "Indian Rosewood";
          case BRAZILIAN_ROSEWOOD: return "Brazilian Rosewood";
          case MAHOGANY:           return "Mahogany";
          case MAPLE:              return "Maple";
          case COCOBOLO:           return "Cocobolo";
          case CEDAR:              return "Cedar";
          case ADIRONDACK:         return "Adirondack";
          case ALDER:              return "Alder";
          case SITKA:              return "Sitka";
          default:  return "unspecified";
        }
      }
    }
    
    // This class represents a guitar
    public class Guitar {
    
      private String serialNumber;
      private double price;
      GuitarSpec spec;  // GuitarSpec is used to specify a guitar
    
      public Guitar(String serialNumber, double price, GuitarSpec spec) {
        this.serialNumber = serialNumber;
        this.price = price;
        this.spec = spec;
      }
    
      public String getSerialNumber() {
        return serialNumber;
      }
    
      public double getPrice() {
        return price;
      }
    
      public void setPrice(float newPrice) {
        this.price = newPrice;
      }
    
      public GuitarSpec getSpec() {
        return spec;
      }
    }
    
    // GuitarSpec is used to specify a guitar
    // We will use this class to search a guitar in the inventory
    public class GuitarSpec {
     
      private Builder builder; 
      private String model;
      private Type type;
      private int numStrings;
      private Wood backWood;
      private Wood topWood;
    
      public GuitarSpec(Builder builder, String model, Type type,
                        int numStrings, Wood backWood, Wood topWood) {
        this.builder = builder;
        this.model = model;
        this.type = type;
        this.numStrings = numStrings;
        this.backWood = backWood;
        this.topWood = topWood;
      }
    
      public Builder getBuilder() {
        return builder;
      }
    
      public String getModel() {
        return model;
      }
    
      public Type getType() {
        return type;
      }
    
      public int getNumStrings() {
        return numStrings;
      }
    
      public Wood getBackWood() {
        return backWood;
      }
    
      public Wood getTopWood() {
        return topWood;
      }
    
      public boolean matches(GuitarSpec otherSpec) {
        if (builder != otherSpec.builder)
          return false;
        if ((model != null) && (!model.equals("")) &&
            (!model.toLowerCase().equals(otherSpec.model.toLowerCase())))
          return false;
        if (type != otherSpec.type)
          return false;
        if (numStrings != otherSpec.numStrings)
          return false;
        if (backWood != otherSpec.backWood)
          return false;
        if (topWood != otherSpec.topWood)
          return false;
        return true;
      }
    }
    
    // The inventory class
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    
    public class Inventory {
      private List guitars;
    
      public Inventory() {
        guitars = new LinkedList();
      }
    
      public void addGuitar(String serialNumber, double price,
                            GuitarSpec spec) {
        Guitar guitar = new Guitar(serialNumber, price, spec);
        guitars.add(guitar);
      }
    
      public Guitar getGuitar(String serialNumber) {
        for (Iterator i = guitars.iterator(); i.hasNext(); ) {
          Guitar guitar = (Guitar)i.next();
          if (guitar.getSerialNumber().equals(serialNumber)) {
            return guitar;
          }
        }
        return null;
      }
    
    /* This method returns a list of guitars that matches with the specification 
    of searchSpec object */
      public List search(GuitarSpec searchSpec) {
        List matchingGuitars = new LinkedList();
        for (Iterator i = guitars.iterator(); i.hasNext(); ) {
          Guitar guitar = (Guitar)i.next();
          if (guitar.getSpec().matches(searchSpec))
            matchingGuitars.add(guitar);
        }
        return matchingGuitars;
      }
    }
    
    // This class is used for testing the Application
    
    import java.util.Iterator;
    import java.util.List;
    
    public class AppTester {
    
      public static void main(String[] args) {
        // Set up Rick's guitar inventory
        Inventory inventory = new Inventory();
        initializeInventory(inventory);
    
        GuitarSpec whatErinLikes = 
          new GuitarSpec(Builder.FENDER, "Stratocastor", 
                         Type.ELECTRIC, 6, Wood.ALDER, Wood.ALDER);
        List matchingGuitars = inventory.search(whatErinLikes);
        if (!matchingGuitars.isEmpty()) {
          System.out.println("Erin, you might like these guitars:");
          for (Iterator i = matchingGuitars.iterator(); i.hasNext(); ) {
            Guitar guitar = (Guitar)i.next();
            GuitarSpec spec = guitar.getSpec();
            System.out.println("  We have a " +
              spec.getBuilder() + " " + spec.getModel() + " " +
              spec.getType() + " guitar:\n     " +
              spec.getBackWood() + " back and sides,\n     " +
              spec.getTopWood() + " top.\n  You can have it for only $" +
              guitar.getPrice() + "!\n  ----");
          }
        } else {
          System.out.println("Sorry, Erin, we have nothing for you.");
        }
      }
    
      private static void initializeInventory(Inventory inventory) {
        inventory.addGuitar("11277", 3999.95, 
          new GuitarSpec(Builder.COLLINGS, "CJ", Type.ACOUSTIC, 6,
                         Wood.INDIAN_ROSEWOOD, Wood.SITKA));
        inventory.addGuitar("V95693", 1499.95, 
          new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, 6,
                         Wood.ALDER, Wood.ALDER));
        inventory.addGuitar("V9512", 1549.95, 
          new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, 6,
                         Wood.ALDER, Wood.ALDER));
        inventory.addGuitar("122784", 5495.95, 
          new GuitarSpec(Builder.MARTIN, "D-18", Type.ACOUSTIC, 6,
                         Wood.MAHOGANY, Wood.ADIRONDACK));
        inventory.addGuitar("76531", 6295.95, 
          new GuitarSpec(Builder.MARTIN, "OM-28", Type.ACOUSTIC, 6,
                         Wood.BRAZILIAN_ROSEWOOD, Wood.ADIRONDACK));
        inventory.addGuitar("70108276", 2295.95, 
          new GuitarSpec(Builder.GIBSON, "Les Paul", Type.ELECTRIC, 6,
                         Wood.MAHOGANY, Wood.MAHOGANY));
        inventory.addGuitar("82765501", 1890.95, 
          new GuitarSpec(Builder.GIBSON, "SG '61 Reissue", Type.ELECTRIC, 6,
                         Wood.MAHOGANY, Wood.MAHOGANY));
        inventory.addGuitar("77023", 6275.95, 
          new GuitarSpec(Builder.MARTIN, "D-28", Type.ACOUSTIC, 6,
                         Wood.BRAZILIAN_ROSEWOOD, Wood.ADIRONDACK));
        inventory.addGuitar("1092", 12995.95, 
          new GuitarSpec(Builder.OLSON, "SJ", Type.ACOUSTIC, 12,
                         Wood.INDIAN_ROSEWOOD, Wood.CEDAR));
        inventory.addGuitar("566-62", 8999.95, 
          new GuitarSpec(Builder.RYAN, "Cathedral", Type.ACOUSTIC, 12,
                         Wood.COCOBOLO, Wood.CEDAR));
        inventory.addGuitar("6 29584", 2100.95, 
          new GuitarSpec(Builder.PRS, "Dave Navarro Signature", Type.ELECTRIC, 
                         6, Wood.MAHOGANY, Wood.MAPLE));
      }
    }
    

    You can connect with me here: https://www.linkedin.com/in/shashi-bhushan-kumar-709a05b5/


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.