-2

I am trying to make an Android application where you can buy and sell items. I've run into a wall: I am stuck at how to represent categories and sub-categories. Any category can have some number of sub-categories, and these sub-categories can also have sub categories with fields in them, etc.

For instance, let's say you want to sell a pair of your watches, the category and sub categories might be:

-Jewelry
--Men
---Watches
----Rolex

or it might even be

-Jewelry
--Men
---Watches
----Rolex
-----Models
------SubMarine

How would you represent this with a class hierarchy? I can't seem to find a solution that doesn't end out with a ton of classes and something that is difficult to update.

I was thinking of using Jackson/Gson, Mysql/MSSQL for storage and most likely rest services for saving it; bonus points for an answer that can integrate with those technologies.

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
matn
  • 37
  • 7
  • Hi there! I tried to reword your question to be less focused on opinions and more on object-oriented design. For more information about the sorts of questions that are well-suited to Stack Overflow, check out http://stackoverflow.com/help/on-topic – Haldean Brown Dec 06 '16 at 21:58

2 Answers2

0

Inheritance would be a naive approach. A sub-class per Category and Subcategory cannot scale.

What about sub-sub-categories? Oh, my.

Maybe a better approach is to stick with a Category class and make it a Composite, or just create a tree of Category instances. That way you can assemble trees of categories to your heart's content by adding instances and data rather than new classes.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0
import java.util.Scanner;

class Runner{
    
    public void Startedrunning(){
        System.out.println(" Game started ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
    }
    
}


class fellDown extends Runner{
    
    @Override
    public void Startedrunning(){
        System.out.println("person started running");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        System.out.println("person fell down");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        System.out.println("Select an option");
        System.out.println("press 1 to Sleep there / press 2 to Wake up and start running");
        Scanner sc = new Scanner(System.in);
        int option = sc.nextInt();
        if(option == 1){
            System.out.println("Person is sleeping");
            System.out.println("game over");
        }else if(option == 2){
            System.out.println("Person wokeup an started running again");
        }else{
            System.out.println("perssed a wrong key");
        }
    }

}
class facedTree extends Runner{
    @Override
    public void Startedrunning(){
        System.out.println("there is tree on the way");
        try {
             Thread.sleep(1000); 
        } catch (InterruptedException e) {
  
        }
        System.out.println("Select an option");
        System.out.println("press 1 to jump/ press 2 to duck");
        Scanner sc = new Scanner(System.in);
        int option = sc.nextInt();
        if(option == 1){
            System.out.println("Person dodged the tree");
            System.out.println("game over");
        }else if(option == 2){
            System.out.println("Person hit the tree and is dead");
            System.out.println("game over");
        }else{
            System.out.println("perssed a wrong key");
        }
    }
}


public class Rungame{
    public static void main(String[] args){
            Runner person3 = new Runner();
            Runner person1= new fellDown();
            Runner person2 = new facedTree();
            person3.Startedrunning();
            ((fellDown) person1).Startedrunning();
            ((facedTree) person2).Startedrunning();
        
    }
}