Create a new project called petstore_domain

create 2 packages one com.anblicks.domain second com.anblicks.domain.test

move all domain model in to package one and keep all the tests in package 2

Make the Catalog Class Singleton Class

Create a testCatalog Class in the second package create atleast 10 tescases for testing the Search Catalog method by passing different sets of Items

Make all the model class functionality as interfaces and extend from that interface
Code all other classes like test classes on the interfaces unike depending on the class file.

Create a Catlog factory which will give different catalogs according to the Time if it is mornig the Catalog will be created from file if in the evening it will be from Database

while writing the search,buy funtionality decomanstate the use of equals method

All domain models need to override the to string method so when printing the objects we see the data.

All model classes need to be intantated by constructors and in the model interface there should not be exposing any set methods.

Create a new class called Cart which can hold the Items and also the total pricing of the cart use the respective collection class to manage Items
Cart should buy the Items from the Store when ever an Item is bought the store should adjust its Items accordingly
Cart should be able to be persist to the secondary device and should be able to retrieve
Create a class called user to manage the users using the system the user should have the userid,password and address and also have methods for buying the items from the store. User should hold the cart to manage its shopping expierence
The User should have the method where we can call for persisting the Cart and also should have the method to recreate from the secondary device
allow the system to start 5 users in different threads and do shopping where the user can buy items available and put it to cart persist and also retieve the stored cart and print it.
The store is been used by multiple users in different threads so it should be able to handle the synchronizing feature
This is what i did so far...
Catalog.java

public class Catalog implements CatalogInterface {



 private int Id=0;
 private String name=null;
 private ProductInterface[] products=new Product[10];

 ProductInterface product1=new Product(0,null);

 public static Catalog  instance = null;

 public Catalog(int Id, String name)
 {
     this.Id=Id;
     this.name=name;
     for(int i=0;i<products.length;i++)
         products[i]=product1;


 }
   public static Catalog getInstance(int Id, String name) {
      if(instance == null) {

         instance = new Catalog(Id, name);
      }
      return instance;



}
    @Override
    public int getId() {
        return 0;
    }
    @Override
    public void setName(String name) {



}
@Override
public String getName() {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void printAvailableProducts() {
    // TODO Auto-generated method stub

}
@Override
public void printAvailableItems() {
    // TODO Auto-generated method stub

}
@Override
public void printAvailableItemsinProduct(ProductInterface p) {
    // TODO Auto-generated method stub

}
@Override
public void searchIteminProduct(ProductInterface p, int id) {
    // TODO Auto-generated method stub

}
@Override
public void searchIteminProduct(ProductInterface p, ItemInterface item1) {
    // TODO Auto-generated method stub

}
@Override
public void deleteIteminProduct(ProductInterface p, ItemInterface item1) {
    // TODO Auto-generated method stub

}
@Override
public void deleteIteminProduct(ProductInterface p, int id) {
    // TODO Auto-generated method stub

}
@Override
public void buyIteminProduct(ProductInterface p, int id) {
    // TODO Auto-generated method stub

}
@Override
public void buyIteminProduct(ProductInterface p, ItemInterface item1) {
    // TODO Auto-generated method stub

}
@Override
public void addProduct(ProductInterface product) {
    // TODO Auto-generated method stub

}
@Override
public void DelProduct(ProductInterface product) {
    // TODO Auto-generated method stub

}
@Override
public void DelProduct(int id) {
    // TODO Auto-generated method stub

}
@Override
public boolean searchProduct(ProductInterface p1) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public void searchProduct(int id) {
    // TODO Auto-generated method stub

}
@Override
public void addItemtoProduct(ProductInterface P, ItemInterface item) {
    // TODO Auto-generated method stub

}
@Override
public void delItemfromProduct(ProductInterface product, int id) {
    // TODO Auto-generated method stub

}
@Override
public void serchItem(ItemInterface item1) {
    // TODO Auto-generated method stub

}


}

CatalogInterface.java
public interface CatalogInterface {




 public int getId();

 public void setName(String name);

 public String getName();

 public void printAvailableProducts(); 

 public void printAvailableItems(); 

 public void printAvailableItemsinProduct(ProductInterface  p); 

 public void searchIteminProduct(ProductInterface p, int id); 

 public void searchIteminProduct(ProductInterface p, ItemInterface item1); 

 public void deleteIteminProduct(ProductInterface p, ItemInterface item1); 

 public void deleteIteminProduct(ProductInterface p, int id); 

 public void buyIteminProduct(ProductInterface p, int id); 

 public void buyIteminProduct(ProductInterface p, ItemInterface item1); 

 public void addProduct(ProductInterface product) ; 

 public void DelProduct(ProductInterface product); 

 public void DelProduct(int id); 

 public boolean searchProduct(ProductInterface p1); 

 public void searchProduct(int id); 

 public void addItemtoProduct(ProductInterface P, ItemInterface item); 

 public void delItemfromProduct(ProductInterface product, int id); 

 public void serchItem(ItemInterface item1);

 public String toString();




}

Item.java
public class Item implements ItemInterface {



 private int Id;
 private String name;
 private int price;


public Item(int Id, String name,int price)
 {
     this.Id=Id;
     this.name=name;
     this.price=price;

 }

@Override
public void setId(int Id) {
    // TODO Auto-generated method stub
    this.Id=Id;

}

@Override
public int getId() {
    // TODO Auto-generated method stub
    return this.Id;
}

@Override
public void setName(String name) {
     this.name=name;        

}

@Override
public String getName() {
    // TODO Auto-generated method stub
    return  this.name;
}

@Override
public void setPrice(int price) {
    // TODO Auto-generated method stub
    this.price=price;
}

@Override
public int getPrice() {
    // TODO Auto-generated method stub
    return this.price;
}

@Override
public boolean equals(ItemInterface item1)
{
    if(this.getId()==item1.getId() && this.getName()==item1.getName() && this.getPrice()==item1.getPrice())
        return true;
    else
        return false;
}

@Override
public String toString()
{
     String text="Item";

     return text; 
}   



}

ItemInterface.java
public interface ItemInterface {



     public void setId(int Id);

     public int getId();

     public void setName(String name);

     public String getName();

     public void setPrice(int price);

     public int getPrice();

     public boolean equals(ItemInterface item1);

     public String toString();


}


Product.java
package com.anblicks.domain;





public class Product implements ProductInterface {



 private int Id;
 private String name;
 ItemInterface[]  Items=new Item[10];
 ItemInterface item1=new Item(0,null,0);

 public Product(int Id, String name)
 {
     this.Id=Id;
     this.name=name;


     for(int i=0;i<Items.length;i++)
         Items[i]=item1;


 }

@Override
public void printAvailableItems() {

     System.out.println("List of avilable items in product:"+ this.getName());  
        for(int i = 0; i < Items.length; i++)
        {  
            if( Items[i].getName()!=null)
            System.out.println("ID: "+Items[i].getId() +" Name: "+Items[i].getName()+" Price: "+Items[i].getPrice());  
        } 

}

@Override
public void addItem(ItemInterface item) {

     boolean b=this.searchItem(item);
     if(b==true)
     System.out.println("Item with id:" + item.getId() + " already there in same product");
     else
     {       
           int i=0;
           while(Items[i].getId()!=0) 
           {
             i++;
            if(i==Items.length)
               {
                 System.out.println("Array out of bound");
                 break;
               }

          }

          if(i<Items.length)
          {
           Items[i]=item;
           //System.out.println("Item has bean added");

          }
     }

}

@Override
public void delItem(int id) {

      int i=0;

        while(id!=Items[i].getId())
        {  
            i++;
            if(i==Items.length)
            {
                System.out.println("Item not found");
                break;
            }


        }   
         if(i<Items.length)
         {
             Items[i]=item1;
             System.out.println("Item with id " +id+ " has been deleted");
         }



}

@Override
public void delItem(ItemInterface item) {

     int id=item.getId();
       int i=0;

        while(!item.equals(Items[i]))
        {  

            if(i==Items.length)
            {
                System.out.println("item not found");
                break;
            }

            i++;
        }   
         if(i<Items.length)
         {
             Items[i]=item1;
             System.out.println("Item with " +id+ " was deleted");
         }



}

@Override
public void buyItem(ItemInterface item) {


       int id=item.getId();
       int i=0;

        while(!item.equals(Items[i]))
        {  

            if(i==Items.length)
            {
                System.out.println("item not found");
                break;
            }

            i++;
        }   
         if(i<Items.length)
         {
             Items[i]=item1;
             System.out.println("Item with " +id+ " has been bought by customer ");
         }




}

@Override
public void buyItem(int id) {

    int i=0;

    while(id!=Items[i].getId())
    {  
        i++;
        if(i==Items.length)
        {
            System.out.println("Item not found");
            break;
        }


    }   
     if(i<Items.length)
     {
         Items[i]=item1;
         System.out.println("Item with id " +id+ " has been bought by customer");
     }



}

@Override
public boolean searchItem(int id) {

      int i=0;
       boolean r=false;
        while(id!=Items[i].getId())
        {  
            i++;
            if(i==Items.length)
            {
                System.out.println("Item not found");
                r=false;
                break;
            }


        }   
         if(i<Items.length)
         {
             System.out.println("Result of your search for given item id: "+ id);
             System.out.println("ID: "+Items[i].getId() +
                                " Name: "+Items[i].getName()+
                                " Price: "+Items[i].getPrice());
             r=true;
         }

        return r;   

}

@Override
public boolean searchItem(ItemInterface item) {

       int i=0;
       boolean r=false;
        while(!item.equals(Items[i]))
        {   
            i++;
            if(i==Items.length)
            {
                //System.out.println("Item is not there");
                r=false;
                break;
            }


        }   
         if(i<Items.length)
         {

             System.out.println("ID: "+Items[i].getId() +" Name: "+Items[i].getName()+" Price: "+Items[i].getPrice());
             r=true;
         }


        return r;
}

@Override
public void setId(int Id) {

    this.Id=Id;

}

@Override
public int getId() {

    return this.Id;
}

@Override
public void setName(String name) {
    this.name=name;

}

@Override
public String getName() {

    return this.name;
}

@Override
public boolean equals(ProductInterface p1)
{
    if(this.getId()==p1.getId() && this.getName()==p1.getName() )
        return true;
    else
        return false;
}
@Override
public String toString()
{
     String text="Product";

     return text; 
}   



}

ProductInterface.java
package com.anblicks.domain;


public interface ProductInterface

{



 public String toString();

 public void printAvailableItems(); 

 public void addItem(ItemInterface item); 

 public void delItem(int id); 

 public void delItem(ItemInterface item); 

 public void buyItem(ItemInterface item); 

 public void buyItem(int id);

 public boolean searchItem(int id); 

 public boolean searchItem(ItemInterface item); 

 public void setId(int Id);

 public int getId();

 public void setName(String name);

 public String getName();

 public boolean equals(ProductInterface p1);





}
haritha.devarakonda
Deleted Member

Do you have any specific questions?
If you are getting error messages you need help with, post them and your questions.

Thanks for the reply. Till this the project is working perfectly fine...but i need to write classes for store, cart and user
thanks

i need help to write those classes as per requirements

Can you explain what you are trying to do and how you need help doing it? Ask specific questions about the problems you are having. Try to itemize them into a list so they can be worked on one by one and not all at once.

Create a new class called Cart which can hold the Items and also the total pricing of the cart use the respective collection class to manage Items

package com.anblicks.domain;

public class Cart {
    private static final String[] Id = null;
    private double totalCost;
    private Item[] myItems;
    private int max=100;
    int numberOfItems;
    int price;
public Cart(int size){
    max = size;
    //set new cart to empty
    this.totalCost=0;
    myItems = new Item[max];
}
public void addItem(Item newItemObject){
    if(numberOfItems<max - 1){
        myItems[numberOfItems++] = newItemObject;
    }
}
public String getDisplayData(){
    System.out.println("List of avilable items in cart "+ this.getName());  
    for(int i = 0; i < myItems.length; i++)
    {  
        if(myItems[i].getName()!=null)
        System.out.println("productID:"+ Id[i]+ " ID: "+myItems[i].getId() +" Name: "+myItems[i].getName()+" Price: "+myItems[i].getPrice());  
    }
    return null; 

}
private String getName() {
    // TODO Auto-generated method stub
    return null;
}
public String getTotalCost(){
    String Total = "The total is $365";
    return Total;

}



}

I want to know how to write the getName() method and what is it supposed to return.
after that i want to calculate getTotalCost(), how to calculate total cost.

how to write the getName() method

Sounds like it should return a String that contains the name.

getTotalCost(), how to calculate total cost.

The total cost would be the sum of the costs for the individual parts.
Add up the costs and return the total.

Look at the tutorial for how to write a method:
return value
Define

Cart should buy the Items from the Store when ever an Item is bought the store should adjust its Items accordingly
i did it accordingly but i have no idea how to write a store class...can you please help me what my store class is supposed to have and how i should write it.

Make a list of what data the Store class needs to contain
and a list of the methods that users of the Store class can call to do business with a Store class object. These lists can change as the program's development continues.

You have posted over 600 lines of code that define several classes so you must know how to define a class.

This article has been dead for over six months. Start a new discussion instead.