Assignment Objective

These assignments use either a simple application or a to learn and demonstrate your understanding of basic control structures, algorithms, inheritance, polymorphism, file manipulation and exception handling. If you choose the second assignment you will also need to demonstrate basic understanding of graphical user interface and even driven programming.

Assignment 2A

Section A: Part Class and Test Application

Write a class named Part to manage stock-level for the various parts. This class should provide the following methods to support the specified operations

int getStockLevel() // to return the current stock level
void replenish(int qty) // to add to the stock level the qty specified
double supply(int qty) // to work out and return the cost of supplying
// specified qty if stock available and -1.0 otherwise.

This class should have instance variables to store the id, name, stockLevel and unitPrice and a constructor taking values for all of the instance variables. This class should provide accessors for all the instance variables. Test your Part class with the TestPart class below.

TestPart class (Note: We may use different data during assessment)

public class TestPart {
public static void main(String args[]) {
// Part p122 has unit-price of 0.10 and current level = 5000
Part part = new Part("p122", "Chain", 5000, 0.10);

part.replenish(1000); // replenishing stock by 1000 parts
part.replenish(500); // replenishing stock by 500 parts
for (int i=0; i<2; i++) {
int qty = 6000; // attempting to supply 6000 parts
double amount = part.supply(qty);
if ( amount > 0 )
System.out.println(part.getID()+" "+ qty +" supplied. " + " cost = " + amount);
else System.out.println(part.getID() + " "+ qty +" items not available. ");
System.out.println("Available qty = " + part.getStockLevel());
}
}
}

Expected Output

p122 6000 supplied. cost = 600.0
Available qty = 500
p122 6000 items not available.
Available qty = 500

Test Application using Part class

(i) Declare an array that can store the references of up to 5 Part objects constructed with the data (ID, name, stock-level and unit-price) below.

p122, Chain, 48, 12.5
p123, Chain Guard, 73, 22.0
p124, Crank, 400, 11.5
p125, Pedal, 38, 6.5
p126, Handlebar, 123, 9.50

(ii) Now allow the users to either replenish or supply specified part. If the user wants to replenish, the letter R should be followed by ID and quantity. If the user wants to supply the letter S should be followed by ID and quantity. If a nonexistent ID is entered display an appropriate error message.

Please Enter either S (supply) or R (replenish) followed by ID and quantity.

R p122 10
New Stock-level for p122 (Chain) is 58

S p125 20
New Stock-level for p125 (Pedal) is 18

S p905 20
No part found with ID p905

R p122 30
New Stock-level for p122 (Chain) is 88

(iii) Display the final stock level for all the parts.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.