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.

Serializable Comparable Part PurchasedPart ManufacturedPart SubcontractedPart

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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: \n");

               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 \n");

               choice = (char) System.in.read();

               if (choice < '1' | choice > '7') {

                   System.out.println("Invalid selection, Please choose again\n");

               }

           } while (choice < '1' | choice > '7');

           System.out.println('\n');

           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

Add a comment
Know the answer?
Add Answer to:
The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life...

    fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...

  • I need help for part B and C 1) Create a new project in NetBeans called...

    I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...

  • Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....

    Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp. Also uses Super in the TextBook Class section. Problem 1 (10 points) Вook The left side diagram is a UML diagram for Book class. - title: String The class has two attributes, title and price, and get/set methods for the two attributes. - price: double Вook) Book(String, double) getTitle(): String setTitle(String): void + getPrice() double setPrice(double): void toString() String + The first constructor doesn't...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

    Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name?...

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT