4 Replies - 2152 Views - Last Post: 20 February 2011 - 06:53 PM Rate Topic: -----

#1 mido91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-February 11

Data Structures and Collection Framework

Posted 20 February 2011 - 12:45 PM

I am given four classes that I should make use of various data structures some to be used in the Java Collections Framework and some to construct my self such as: breadth first, I am also suppose to use an array, a linked list, a tree or a combination of these.

The program must be capable of modelling the hierarchical relationships between parts in an assembly. The application detail:
A factory is set up to assemble artefacts that are made up of parts. Those parts may themselves be constructed of sub-parts. For example, a shed might be made up of a door, two end walls, two side walls and a roof. The door consists of a door panel, 3 hinges and 6 size 8 screws. In general, artefacts are made up of parts which are themselves made of parts

I am asked to implement an application that supports a hierarchy in which parts are made up from other parts, which themselves may be made of other parts (to n levels).

The PartsInformationRepository provides a framework in which to develop the application. I am asked to implement the methods in the PartsRepository class and may need to make other changes/add additional classes to the code that is provided.

The application described above needs to ensure that neither direct nor indirect recursive components are specified. I.e. the application needs to check that parts are not specified such that A contains B which contains C which contains A. This functionality has to be implemented also.

The application should support the following functionality:
Add a part.
For a given part number, display its detail
Link a part as a direct sub-component of another part
For a given part number, list its direct components
For a given part number, list all direct assemblies in which it is used
For a given part number, list all assemblies up to the final product in which it is used
For a given part number, list all sub-components down to the lowest level
List n-levels of assemblies above a given part number
List n-levels of components below a given part number

Firstly I am suppose to start with adding a part in the PartsRepository class. How can I do this?

Thanks for your time.

Parts class:
package partsRepository;

public class Part {
    
   
 /** Creates a new instance of Part */
    public Part()
    {
        
    }

    public Part(String sPartNum, String partName) {
    }

    public Part(int partNum, String partName){
    }
}



partsRepository class:
package partsRepository;

public class PartsRepository {

    

    public PartsRepository() {
    }

    /**
     * 
     * @param aPerson Adds a Part to the family tree, with an empty set of
     * relationships.
     */
    public void addPart(Part aPart) {
    }
    public void makeLinkToComponent(Part aPart, Part subPart) {
    }

    
    public void listPartDetails(Part aPart) {
        
    }

    public void listDirectAssembly(Part aPart) {
        
    }


    public void listDirectComponents(Part aPart) {
       
    }

    public void listAllAssemblies(Part aPart) {
    }

    public void listAllComponents(Part aPart) {
    }

    public void listNAssemblies(Part aPart, int numberOfGenerations) {
    }

    public void listNComponents(Part aPart, int numberOfGenerations) {
    }

    
}



Supplier class:
package partsRepository;

public class Supplier {

}



TestPartsRepository class:
package partsRepository;

import java.util.Scanner;
import java.io.*;

public class TestPartsRepository {

    PartsRepository partsRep1;
    final String RESOURCE_LOCATION = "/partsRepository/resources/";

    /**
     * @param args the command line arguments
     */
    TestPartsRepository() {
        partsRep1 = new PartsRepository();
    }

    public Part getPartDetails() throws IOException {
        int partNo = getPartNum();
        String name = getPartName();
        Part newPart = new Part();     // You need to change this is match your
        // version of Part
        return newPart;
    }

    public void addSupplier() throws IOException {
        // Complete this
    }

    private String getPartName() throws IOException {
        String partName;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter - name : ");
        partName = scan.nextLine();
        return partName;
    }

    private int getPartNum() throws IOException {

        Scanner scan = new Scanner(System.in);
        String sNumber = "";
        int number = 0;

        while (sNumber.length() == 0) {
            System.out.print("Enter number: ");
            try {
                sNumber = scan.nextLine();
                number = Integer.parseInt(sNumber);
            } catch (NumberFormatException e) {
                System.out.println("Please enter digits only");
                sNumber = "";
            }
        }
        return number;
    }

    private void processInput() throws IOException {
        Scanner scan = new Scanner(System.in);
        Part aPart = null, subPart = null;
        String selection;
        char iChoice;

        selection = scan.nextLine().toUpperCase();
        iChoice = selection.charAt(0);
        while (iChoice != 'X') {
            switch (iChoice) {
                case 'A':
                    aPart = getPartDetails();
                    break;
                case 'B':
                    aPart = getPartDetails();
                    subPart = getPartDetails();
                    partsRep1.makeLinkToComponent(aPart, subPart);
                    break;
                default:
                    System.out.println("\nInvalid input choice. Try again\n");
                //do nothing
            }
            inputMenu();
            selection = scan.nextLine().toUpperCase();
            iChoice = selection.charAt(0);
        }
    }

    private void processQuery() throws IOException {
        Scanner scan = new Scanner(System.in);
        String selection;
        Part aPart;
        char qChoice;
        int numOfGens;

        selection = scan.nextLine().toUpperCase();
        qChoice = selection.charAt(0);
        while (qChoice != 'X') {
            switch (qChoice) {
                case 'K':
                    partsRep1.listPartDetails(this.getPartDetails());
                    break;
                case 'L':
                    partsRep1.listDirectAssembly(this.getPartDetails());
                    break;
                case 'M':
                    partsRep1.listDirectComponents(this.getPartDetails());
                    break;
                case 'N':
                    partsRep1.listAllAssemblies(this.getPartDetails());
                    break;
                case 'O':
                    partsRep1.listAllComponents(this.getPartDetails());
                    break;

                case 'T':
                    aPart = this.getPartDetails();
                    System.out.println("Now enter - number of levels required : ");
                    numOfGens = scan.nextInt();
                    scan.nextLine();
                    partsRep1.listNAssemblies(aPart, numOfGens);
                    break;
                case 'U':
                    aPart = this.getPartDetails();
                    System.out.println("Now enter - number of levels required : ");
                    numOfGens = scan.nextInt();
                    scan.nextLine();
                    partsRep1.listNComponents(aPart, numOfGens);
                    break;
            }
            queryMenu();
            selection = scan.nextLine().toUpperCase();
            qChoice = selection.charAt(0);
        }
    }

    private void menu() {
        System.out.println("\nPART REPOSITORY MENU\n");
        System.out.println("L\tLoad Data");
        System.out.println("I\tInput Details");
        System.out.println("Q\tMake a Query\n");

        System.out.println("X\tEXIT\n");

        System.out.println("Enter menu choice, X to exit: ");
    }

    private void inputMenu() {
        System.out.println("\nPART REPOSITORY INPUT MENU\n");
        System.out.println("A\tAdd a part to the repository");
        System.out.println("B\tMake a part a sub-component");

        System.out.println("X\tEXIT INPUT\n");

        System.out.println("Enter menu choice, X to exit: ");
    }

    private void queryMenu() {
        System.out.println("\nPART REPOSITORY QUERY MENU\n");
        System.out.println("K\tList part details");
        System.out.println("L\tList direct assemblies");
        System.out.println("M\tList direct components");
        System.out.println("N\tList all assemblies up to final product");
        System.out.println("O\tList all sub-components to the lowest level");
        System.out.println("T\tList n-levels of assembly");
        System.out.println("U\tList n-levels of sub-component\n");

        System.out.println("X\tEXIT QUERY\n");

        System.out.println("Enter menu choice, X to exit: ");
    }

    private void loadData() throws IOException {

        Scanner pFile, cFile, lineScan;
        String entry, partNum = null, partName = null;
        String subNum = null, subName = null;
        // read file part.txt and create new instance of part for each one
        InputStream instream = getClass().
                getResourceAsStream(RESOURCE_LOCATION + "parts.txt");
        pFile = new Scanner(instream);
        while (pFile.hasNext()) {
            entry = pFile.nextLine();
            lineScan = new Scanner(entry);
            while (lineScan.hasNext()) {
                partNum = lineScan.next();
                partName = lineScan.next();
            }
            if ((partName != null) && (partNum != null)) {
                Part aPerson = new Part(partNum, partName);
                partsRep1.addPart(aPerson);
                System.out.println(partNum + " " + partName + " added");

            }
        }
        // read file sub-componanet file and create the link
        instream = getClass().
                getResourceAsStream(RESOURCE_LOCATION + "components.txt");
        cFile = new Scanner(instream);
        while (cFile.hasNext()) {
            entry = cFile.nextLine();
            lineScan = new Scanner(entry);
            while (lineScan.hasNext()) {
                partNum = lineScan.next();
                partName = lineScan.next();
                subNum = lineScan.next();
                subName = lineScan.next();
            }
            if ((partNum != null) && (partName != null) &&
                    (subNum != null) && (subName != null)) {
                Part aPart = new Part(partNum, partName);
                Part subPart = new Part(subNum, subName);
                partsRep1.makeLinkToComponent(aPart, subPart);
                System.out.println(partNum + " " + partName + " " + subNum +
                        " " + subName + " sub-component added");
            }
        }


    }

    public static void main(String[] args) throws IOException {
        TestPartsRepository tFT = new TestPartsRepository();
        Scanner scan = new Scanner(System.in);
        char mChoice;
        String selection;

        tFT.menu();
        selection = scan.nextLine().toUpperCase();
        mChoice = selection.charAt(0);
        while (mChoice != 'X') {
            switch (mChoice) {
                case 'L': {
                    tFT.loadData();
                    break;
                }
                case 'I': {
                    tFT.inputMenu();
                    tFT.processInput();
                    break;
                }
                case 'Q': {
                    tFT.queryMenu();
                    tFT.processQuery();
                    break;
                }
                default: {
                    System.out.println("\nInvalid choice. Try again\n");
                }
            }
            tFT.menu();
            selection = scan.nextLine().toUpperCase();
            mChoice = selection.charAt(0);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Data Structures and Collection Framework

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Data Structures and Collection Framework

Posted 20 February 2011 - 03:10 PM

Based on the problem description, it might help you visualize (conceptualize) the answer to your question, (paraphrased) "How do I add a part?" if you think:

- the PartsRepository is a collection of parts
- parts can contain (or include) other parts

As an example, partA, partB, partC, and partD are all parts in your parts repository. They are all instantiations of class Part. ( Part partA = new Part();, Part partB = new Part();, etc. ). If partA contains partB and partC, then partB and partC would be 'added' to your parts repository by the method addPart( Part part ), as in:

    partA.addPart( partB );
    partA.addPart( partC );



and if partC includes partD, then you'd add partD to the parts repository using:

    partC.addPart( partD );



Just something to get you thinking about the design. Think about it, code some, and come back when you need more help.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Data Structures and Collection Framework

Posted 20 February 2011 - 04:43 PM

There may be, AND I SAID MAY BE before being prosecuted, a misconception here
The Collection Framework is not to organize data on disk they are usefull tools on how to process data when it is in memory
The way data is stored is irrelevant, when you have your data in memory then you can process it in multiple (many times redundant ways) offered by the Collection Framework
Was This Post Helpful? 1
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Data Structures and Collection Framework

Posted 20 February 2011 - 05:12 PM

Prosecuted? For what crime?

I'll have to learn more about Collection Frameworks. Thanks for pointing out what MIGHT BE the most important detail in the OP's question.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: Data Structures and Collection Framework

Posted 20 February 2011 - 06:53 PM

If you're dealing with data structures, you might want to check out a few of my resources:
JVM Blog Entry on An overview of the Java Collections Framework
Java Data Structures Resource Thread

Trees can get a little tricky, and I've got a few tutorials in the Data Structures thread on Graph Theory and Trees.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1