The purpose of this is to use inheritance, polymorphism, object comparison, sort
ID: 3702425 • Letter: T
Question
The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a new class named SubcontractedPart. These are parts that are manufactured but are sent to an outside business to have some additional process done. The class hierarchy is shown below.
The UML class diagram for the service classes is provided below.
Part-
partID : integer
- partDescription : String
- partSellPrice : double
+ DEFAULT_DESCRIPTION : String = “no part desc”
+ DEFAULT_SELL_PRICE : double = 0
+ Part (ID : int)
+ Part (ID : int, desc : String, sellPrice : double)
+ getTotalCost ( ) : double
+ compareTo (object: Object ) : int
+ getPartID ( ) : int
+ getPartDescription ( ) : String
+ getPartSellPrice ( ) : double
+ setPartID (newId : int) : void
+ setPartDescription (newDesc : String) : void
+ setPartSellPrice (newSellPrice : double) : void
+ toString ( ) : String
PurchasedPart -
handlingCost : double
- purchasePrice : double
- vendor : String
+ DEFAULT_VENDOR_NAME : String = “no vendor name”
+ DEFAULT_PURCHASE_PRICE : double = 0 + DEFAULT_HANDLING_COST : double = 0
+ PurchasedPart (ID : int)
+ PurchasedPart (ID : int, hCost : double, pPrice : double , vendor : String)
+ PurchasedPart (ID : int, desc : String, sellPrice : double, hCost : double, pPrice : double, vendor : String)
+ getTotalCost ( ) : double
+ getHandlingCost( ) : double
+ getPurchasePrice ( ) : double
+ getVendor( ) : String
+ setHandlingCost(newCost : double) : void
+ setPurchasePrice(newPrice : double) : void
+ setVendor(newVendor : String) : void
+ toString ( ) : String
ManufacturedPart -
laborCost : double -
materialCost : double
+ DEFAULT_LABOR_COST : double = 0
+ DEFAULT_MATERIAL_COST : double = 0
+ ManufacturedPart(id : int)
+ ManufacturedPart(id : int, lCost : double, mCost : double)
+ ManufacturedPart(id : int, desc : String, sellPrice : double, lCost : double, mCost : double)
+ getTotalCost ( ) : double
+ getLaborCost ( ) : double
+ getMaterialCost ( ) : double
+ setLaborCost (newLaborCost : double) : void
+ setMaterialCost (newMaterialCost : double) : void
+ toString ( ) : String
SubcontractedPart -
subcontractCost : double
+ DEFAULT_SUBCONTRACT_COST : double
+ SubcontractedPart (id: int)
+ SubcontractedPart (id: int, subcontractCost : double)
+ SubcontractedPart (id : int, desc: String, sellPrice: double, laborCost: double, materialCost : double, subcontractCost : double)
+ getTotalCost( ) : double
+ getSubcontractCost ( ) : double
+ setSubcontractCost (double) : void
+ toString ( ) : String
Make all the "part" classes serializable.
The following requirements are for the Part class.
Make the Part class abstract. Make the getTotalCost method abstract.
Replace the use of default values for handling errors with exceptions. This will affect the constructor and the set methods. Use the InvalidArgumentException :
package edu.seattlecomm.exceptions;
* This is a checked exception since it extends the Exception class.
*/
public class InvalidArgumentException extends Exception {
public InvalidArgumentException() {
super("Invalid values sent to method");
}
public InvalidArgumentException(String msg) {
super(msg);
}
}
Use the InvalidArgumentException provided when invalid parameter values are found. You will still need to use the default constants for missing data in some of the constructors.
Implement the Comparable interface.
Implement the Serializable interface.
Override the compareTo method. Use the PartID for the comparison.
The following requirements apply to the PurchasedPart class.
Replace the use of default values for handling errors with exceptions. This will affect the constructor and the set methods. Use the InvalidArgumentException when invalid parameter values are found. You will still need to use the default constants for missing data in some of the constructors.
Change the calculation for the total cost to be purchase price plus handling cost.
The following requirements apply to the ManufacturedPart class.
Replace the use of default values for handling errors with exceptions. This will affect the constructor and the set methods. Use the InvalidArgumentException when invalid parameter values are found. You will still need to use the default constants for missing data in some of the constructors.
Change the calculation for the total cost to be labor cost plus material cost.
The following requirements are for the SubcontractedPart class.
Make this class a subclass of the ManufacturedPart class.
Create a default value constant for the subcontract cost. Name this DEFAULT_SUBCONTRACT_COST and initialize it to zero.
Create the three constructors described in the UML class diagram. The first two constructors should use 'this' to call the third constructor (the constructor with six parameters). Use a combination of arguments and default values for the necessary arguments. The third constructor should use "super" to call the superclass constructor passing the appropriate arguments. Create edits to ensure the subcontract cost cannot be negative. Throw the InvalidArgumentException if the subcontract cost is invalid. Use the public default constants from the Part and ManufacturedPart classes as needed in the SubcontractedPart class constructors.
Create a toString method that returns a String object containing the value of each data member. Use the @Override annotation. Display the class of the object using the getClass method. Call the toString method in the superclass to display the superclass data.
Create a public method named getTotalCost that returns the total cost from the super class plus the subcontract cost. Use the @Override annotation to insure this method overrides the method of the same name in the parent class.
Do not create additional data members or methods.
The following requirements apply to the client application, ManageParts.
Make the ArrayList a variable that is local to main if it is not already. That is, the ArrayList must be created inside the main method.
Modify the menu as shown below.
Enter your choice:
1. Create Purchased Part
2. Create Manufactured Part
3. Create SubcontractedPart Part
4. List a part
5. Delete a part
6. List all parts
7. Exit
Create a SubcontractedPart object if the user enters menu option 3. Get the data for the object from the user using a Scanner object. Prompt the user to enter each piece of data (id, description, sell price, labor cost, material cost, and subcontract cost). Use this data to create the object. Store the object in the ArrayList.
Attempt to delete a Part from the ArrayList if the user enters menu option 5. Prompt the user to enter the PartID of the part to be deleted. Display an error message if the PartID doesn't exist in the ArrayList. Delete the Part object from the ArrayList if the user entered PartID is found in the ArrayList.
Display all the parts in order by the PartID when menu option 6 is selected. This will require you to sort the ArrayList. Display the PartID numbers from lowest to highest. You must use the sort method from the Collections class. See the material in the course notes for additional information.
When option 7 (exit) is selected, write each object in the ArrayList to a disk file. Name the file "parts.dat". Make this name a constant and use the constant in your code. Make the file name a relative file name (not absolute). Make sure you write each object contained in the ArrayList individually to the file. Do not write the ArrayList object itself to the file. The objects must be written to the file in PartID order. This means you will need to sort the ArrayList so it is in order from the smallest PartID to the largest PartID. You must use the sort method from the Collections class.
Put the code to write the data to a file in its own method separate from the main method. Pass the ArrayList to this method. If the ArrayList is empty then no file should be created or exist.
When the application starts, determine if the data file exists. If the file exists, read each object in the file and add it to the ArrayList. You don't need to display any data when the application starts. However if, immediately after the application starts, the user selects the option to display all the parts, then the parts added to the ArrayList from the file should be displayed.
Put the code to read the data from a file in its own method separate from the main method. Pass the ArrayList to this method. (The ArrayList will be empty at this point.) Remember to call this method only if the file exists.
Add try ... catch code as required. Display an error message and include the exception error message if any IO exception occurs when reading or writing the file. You can then stop the application using System.exit(-1). Do NOT throw any exceptions from the main method. Do NOT catch the or throw the CurrentModifictionException exception. This exception indicates a programming bug caused by trying to modify an array list accessed via a "for each" loop. Fix your code instead.
Use try with resources when writing and reading the data to and from the file.
Serializable Comparable Part PurchasedPart ManufacturedPart SubcontractedPartExplanation / Answer
package edu.seattlecomm.classes;
import java.io.Serializable;
abstract class Part implements Comparable, Serializable {
public abstract double getTotalCost();
private static final long serialVersionUID = -5709700166899658257L;
int partID;
String partDescription;
double partSellPrice;
final String DEFAULT_DESCRIPTION = "no part desc";
final double DEFAULT_SELL_PRICE = 0;
public Part(int partID) {
this.partID = partID;
this.partDescription = DEFAULT_DESCRIPTION;
this.partSellPrice = DEFAULT_SELL_PRICE;
}
public Part(int partID, String partDescription, double partSellPrice) {
this.partID = partID;
this.partDescription = partDescription;
this.partSellPrice = partSellPrice;
}
public int getPartID() {
return partID;
}
public void setPartID(int partID) {
this.partID = partID;
}
public String getPartDescription() {
return partDescription;
}
public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}
public double getPartSellPrice() {
return partSellPrice;
}
public void setPartSellPrice(double partSellPrice) {
this.partSellPrice = partSellPrice;
}
@Override
public int compareTo(Object o) {
// use typecasting
return this.partID - ((Part) o).getPartID();
}
@Override
public String toString() {
return "Part [partID=" + partID + ", partDescription=" + partDescription + ", partSellPrice=" + partSellPrice
+ "]";
}
}
package edu.seattlecomm.classes;
import edu.seattlecomm.exceptions.InvalidArgumentException;
public class ManufacturedPart extends Part{
private static final long serialVersionUID = 1L;
double laborCost ;
double materialCost ;
final double DEFAULT_LABOR_COST=0;
final double DEFAULT_MATERIAL_COST =0;
public ManufacturedPart(int partID) throws InvalidArgumentException {
super(partID);
if(partID<0)
{
throw new InvalidArgumentException(""+partID);
}
setLaborCost(DEFAULT_LABOR_COST);
setMaterialCost(DEFAULT_MATERIAL_COST);
}
public ManufacturedPart(int partID, double laborCost, double materialCost) throws InvalidArgumentException {
super(partID);
this.laborCost = laborCost;
this.materialCost = materialCost;
if(partID<0 ||laborCost<0 ||materialCost<0)
{
throw new InvalidArgumentException();
}
}
public ManufacturedPart(int partID, String partDescription, double partSellPrice, double laborCost, double materialCost) throws InvalidArgumentException {
super(partID, partDescription, partSellPrice);
this.laborCost = laborCost;
this.materialCost = materialCost;
if(partID<0 ||laborCost<0 ||materialCost<0)
{
throw new InvalidArgumentException();
}
}
public double getLaborCost() {
return laborCost;
}
public void setLaborCost(double laborCost) {
this.laborCost = laborCost;
}
public double getMaterialCost() {
return materialCost;
}
public void setMaterialCost(double materialCost) {
this.materialCost = materialCost;
}
@Override
public double getTotalCost() {
return laborCost+materialCost;
}
@Override
public String toString() {
return super.toString()+ " laborCost=" + laborCost + ", materialCost=" + materialCost + "]";
}
}
package edu.seattlecomm.classes;
import edu.seattlecomm.exceptions.InvalidArgumentException;
public class PurchasedPart extends Part{
private static final long serialVersionUID = 1L;
double handlingCost ;
double purchasePrice ;
String vendor ;
final String DEFAULT_VENDOR_NAME ="no vendor name";
final double DEFAULT_PURCHASE_PRICE =0;
final double DEFAULT_HANDLING_COST =0;
@Override
public double getTotalCost() {
return purchasePrice+handlingCost;
}
public PurchasedPart(int partID) throws InvalidArgumentException {
super(partID);
setVendor(DEFAULT_VENDOR_NAME);
setPurchasePrice(DEFAULT_PURCHASE_PRICE);
setHandlingCost(DEFAULT_HANDLING_COST);
if(partID<0 )
{
throw new InvalidArgumentException();
}
}
public PurchasedPart(int partID, double handlingCost, double purchasePrice, String vendor) throws InvalidArgumentException {
super(partID);
this.handlingCost = handlingCost;
this.purchasePrice = purchasePrice;
this.vendor = vendor;
if(partID<0 ||handlingCost<0 ||purchasePrice<0 ||vendor.isEmpty() )
{
throw new InvalidArgumentException();
}
}
public PurchasedPart(int partID, String partDescription, double partSellPrice, double handlingCost, double purchasePrice, String vendor) throws InvalidArgumentException {
super(partID, partDescription, partSellPrice);
this.handlingCost = handlingCost;
this.purchasePrice = purchasePrice;
this.vendor = vendor;
if(partID<0 ||handlingCost<0 ||purchasePrice<0 || partSellPrice<0||vendor.isEmpty() ||partDescription.isEmpty() )
{
throw new InvalidArgumentException();
}
}
public double getHandlingCost() {
return handlingCost;
}
public void setHandlingCost(double handlingCost) {
this.handlingCost = handlingCost;
}
public double getPurchasePrice() {
return purchasePrice;
}
public void setPurchasePrice(double purchasePrice) {
this.purchasePrice = purchasePrice;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
@Override
public String toString() {
return super.toString() +" handlingCost=" + handlingCost + ", purchasePrice=" + purchasePrice + ", vendor=" + vendor
+ "]";
}
}
package edu.seattlecomm.classes;
import edu.seattlecomm.exceptions.InvalidArgumentException;
public class SubcontractedPart extends ManufacturedPart {
/**
*
*/
private static final long serialVersionUID = 1L;
double subcontractCost;
final double DEFAULT_SUBCONTRACT_COST = 0;
/**
* @param partID
* @throws InvalidArgumentException
*/
public SubcontractedPart(int partID) throws InvalidArgumentException {
super(partID);
}
/**
* @param partID
* @param subcontractCost
* @throws InvalidArgumentException
*/
public SubcontractedPart(int partID, double subcontractCost) throws InvalidArgumentException {
super(partID);
this.subcontractCost = subcontractCost;
}
public SubcontractedPart(int partID, String partDescription, double partSellPrice, double laborCost,
double materialCost, double subcontractCost) throws InvalidArgumentException {
super(partID, partDescription, partSellPrice, laborCost, materialCost);
if (subcontractCost < 0) {
throw new InvalidArgumentException();
}
this.subcontractCost = subcontractCost;
}
@Override
public double getTotalCost() {
return super.getTotalCost()+subcontractCost ;
}
@Override
public String toString() {
return super.toString()+ " subcontractCost=" + subcontractCost + "]";
}
}
package edu.seattlecomm.classes;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ManageParts {
private static final String FILENAME = "./" + "parts.dat";
static ArrayList<Part> parts = new ArrayList<>();
// To load the data initially when application starts
static {
loadDataToArrayList();
}
public static void main(String[] args) throws IOException {
char choice;
for (;;) {
do {
System.out.println("Manage Parts");
System.out.println("Make a Selection on: ");
System.out.println(" 1. Create Purchased Part");
System.out.println(" 2. Create Manufactured Part");
System.out.println(" 3. Create SubcontractedPart Part");
System.out.println(" 4. List a part");
System.out.println(" 5. Delete a part");
System.out.println(" 6.List all parts");
System.out.println(" 7.Exit ");
choice = (char) System.in.read();
if (choice < '1' | choice > '7') {
System.out.println("Invalid selection, Please choose again ");
}
} while (choice < '1' | choice > '7');
System.out.println(' ');
switch (choice) {
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
break;
case '7':
// first sort the data
Collections.sort(parts);
// Write data to file
writePartsDataToFile();
System.exit(0);
break;
}
}
}
private static void loadDataToArrayList() {
File file = new File(FILENAME);
if (file.exists()) {
// Add data to arrayList
try {
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
// Add data from String to create object and add to arraylist
// add object to arraylist
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
private static void writePartsDataToFile() {
File file = new File(FILENAME);
FileWriter fr = null;
try {
fr = new FileWriter(file, true);
fr.write(parts.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
// close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Please Note : Due to shortage of time , All parts could not be completed , Please do the remaining parts , I made a skelton for you .
You can easily do rest of the things .
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.