Inventory (Guitar) App
To learn the object oriented design in java .
You must need to understand the basic concept of java.below example gives you
bit idea about how the java application are made and how hierarchy structure
are follows to build an application.
So Lets move to coding area where inventory
class has list of guitar,Based on user likes and specification it gives you
best preferred guitar as a out put.There are other classes and enums also which
help to bulid the application more stable and product oriented.Guitar
specification consist various type such as
1.Type of Guitar
2.Wood type of Guitar
3.Who are the builder of Guitar
4.Price of Guitar
So for deep understanding move on to the coding
side
1.Builder Enum Class
public enum Builder {
FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY;
public String toString() {
switch (this) {
case ANY:
return "ANY";
case COLLINGS:
return "COLLINGS";
case FENDER:
return "FENDER";
case GIBSON:
return "GIBSON";
case MARTIN:
return "MARTIN";
case OLSON:
return "OLSON";
case PRS:
return "PRS";
case RYAN:
return "RYAN";
}
return null;
}
}
2. Type
Enum Class :
public enum Type
{
ACOUSTIC, ELECTRIC;
public String
toString()
{
switch (this)
{
case ACOUSTIC:
return "ACOUSTIC";
case ELECTRIC:
return "ELECTRIC";
}
return null;
}
}
3.Wood Enum Class :
public enum Wood
{
INDIAN_ROSEWOOD, BRAZILIAN_ROSEWOOD, MAHOGANY, MAPLE, COCOBOLO;
public String toString()
{
switch (this)
{
case BRAZILIAN_ROSEWOOD:
return "BRAZILIAN_ROSEWOOD";
case COCOBOLO:
return "COCOBOLO";
case INDIAN_ROSEWOOD:
return "INDIAN_ROSEWOOD";
case MAHOGANY:
return "MAHOGANY";
case MAPLE:
return "MAPLE";
}
return null;
}
}
4. Guitar
Class
public class Guitar
{
String serialNo;
double price;
GuitarSpec spec;
public
Guitar(String serialNo, double price,
GuitarSpec spec)
{
this.serialNo = serialNo;
this.price = price;
this.spec = spec;
}
public String
getSerialNo()
{
return serialNo;
}
public double
getPrice()
{
return price;
}
public
GuitarSpec getSpec()
{
return spec;
}
public void
setPrice(float newPrice)
{
this.price = newPrice;
}
}
55. GuitarSpec Class
public class
GuitarSpec
{
Builder builder;
String model;
Type type;
Wood backWood;
Wood topWood;
private int numString;
public
GuitarSpec(Builder builder, String
model, Type type, Wood backWood, Wood topWood, int numString)
{
this.builder = builder;
this.model = model;
this.type = type;
this.backWood = backWood;
this.topWood = topWood;
this.numString = numString;
}
public int getNumString()
{
return numString;
}
public Builder
getBuilder()
{
return builder;
}
public String
getModel()
{
return model;
}
public Type
getType()
{
return type;
}
public Wood
getBackWood()
{
return backWood;
}
public Wood
getTopWood()
{
return topWood;
}
public boolean
matches(GuitarSpec searchSpec)
{
if (builder != searchSpec.builder)
return false;
if ((model != null)
&& (!model.equals(""))
&& (!model.equals(searchSpec.model)))
return false;
if (type != searchSpec.type)
return false;
if (backWood != searchSpec.backWood)
return false;
if (topWood != searchSpec.topWood)
return false;
if (numString != searchSpec.numString)
return false;
return true;
}
}
6. 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
addGuitars(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.getSerialNo().equals(serialNumber))
{
return guitar;
}
}
return null;
}
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;
}
}
7.Test Class (Guitar Test)
public class
FindGuitarTester
{
public static void
main(String[] args)
{
// TODO
Auto-generated method stub
Inventory inventory = new Inventory();
initializeInventory(inventory);
GuitarSpec whatSanketLike = new GuitarSpec(Builder.FENDER, "Store", Type.ELECTRICWood.BRAZILIAN_ROSEWOOD,Wood.BRAZILIAN_ROSEWOOD, 0);
List matchingGuitars = inventory.search(whatSanketLike);
if (!matchingGuitars.isEmpty())
{
System.out.println("sandy
you might like this 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 side \n" + spec.getBackWood()
+ "\n
you can have it for inly" + guitar.getPrice());
}
}
else {
System.out.println("Sorry
, Sanket we have find nothing for you");
}
}
private static void
initializeInventory(Inventory inventory) {
inventory.addGuitars("101", 200, new GuitarSpec(Builder.FENDER, "Store", Type.ELECTRIC, Wood.BRAZILIAN_ROSEWOOD,
Wood.BRAZILIAN_ROSEWOOD, 0));
}
}
8.OUTPUT :
sandy you might like this
guitars :
·
we have a FENDER Store
ELECTRICGuitar:
·
BRAZILIAN_ROSEWOOD Back and side BRAZILIAN_ROSEWOOD
·
you can have it for only for Rs 20000.0
No comments:
Post a Comment