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);
}
}
}

New Topic/Question
Reply




MultiQuote






|