Part-I

Imagine a Car Parts and Accessories shop, which requires a software system to keep track of stock items and prices. The shop will sell different kinds of stock items. However, to start with, you have been tasked with designing and implementing a class called StockItem with the following properties.

  • Imagine a Car Parts and Accessories shop, which requires a software system to keep track of stock items and prices. The shop will sell different kinds of stock items. However, to start with, you have been tasked with designing and implementing a class called StockItem with the following properties.
  • A constructor that creates a Stock Item with the specified quantity, price, and the fixed stock code.
  • All the appropriate 'setters'and 'getters methods, including a getStockName() method which returns the string "Unknown Stock Name" and a getStockDescription() method which returns the string "Unknown Stock Description".
  • An addStock() method that increases the stock level by the given amount. If the value is less than one or the stock exceeds 100, a suitable error message should be printed.
  • A sellStock() method that attempts to reduce the stock level by the given amount. If it is less than one, a suitable error message should be printed. If the amount is otherwise less than or equal to the stock level, then the reduction is successful and true is returned. Else there is no effect, but false is returned.
  • A getVAT() method that returns the standard percentage VAT rate, e.g., you can use 17.5
  • Appropriate 'setters method for price (without VAT) and 'getters methods for price with and without VAT
  • A method named toString() that returns a string giving the stock code, the stock name, the description, the quantity in stock, the price before VAT and the price after VAT. It must use the appropriate methods above to obtain the stock name, description, quantity and prices.

Task 1.1. Design and draw the corresponding UML class diagram of the StockItem class described above (no UML modelling tool is required, you can just draw using a simple editor, e.g., MS Word).

Task 1.2. Code and Test it! (This is recommended and purely for your benefit! This will help you in producing some working code before proceeding to the next step in Part-II. However, you can directly proceed to Part-II.)

Implement the above class and test it with a program called TestStockItem. That is, your TestStockItem class will contain the main() method, which you have known and perhaps practiced many similar programs in the practical lab exercises.

You should create some instances of StockItem class, add stock, sell some stock and change the price, whilst printing out the items in between.

Testing is typically a part of the program development - you should use a test strategy to test your program thoroughly. You may look at practical exercises week 3 (Step 4), identify suitable test cases for the StockItem class, write and document them in the form of a table along with the UML class design file.

Test Case Purpose Expected result

An example run might be as follows.

Creating a stock with 10 units Unknown item, price 99.99 each, and item code W101
Printing item stock information:
Stock Type: Unknown Stock Name
Description: Unknown Stock Description
StockCode: W101
Price WithoutVAT: 99.99
PriceWith VAT: 117.48825
Total unit in stock: 10
Increasing 10 more units
Printing item stock information:
Stock Type: Unknown Stock Name
Description: Unknown Stock Description
StockCode: W101
PriceWithout VAT: 99.99
PriceWith VAT: 117.48825
Total unit in stock: 20
Sold 2 units
Printing item stock information:
Stock Type: Unknown Stock Name
Description: Unknown Stock Description
StockCode: W101
PriceWithoutVAT: 99.99
PriceWith VAT: 117.48825
Total unit in stock: 18
Set new price 100.99 per unit
Printing item stock information:
Stock Type: Unknown Stock Name

Part-II

The Car Parts and Accessories shop has got plenty of GeoVision Sat Nav navigation system at very competitive prices, which are going to be the first item on sale. You need to design and implement a class NavSys which is a sub-class of StockItem. A parameterised constructor of the NavSys class must call the StockItem's constructor using super to initialise the instance variables. The NavSys class will override the instance methods getStockName() and getStockDescription() with ones that return "Navigation system" and " GeoVision Sat Nav" respectively. NavSys class will also override the toString() method using the concept of super in Java.

Task 2.1. Revise your UML diagram in Task 1.1, to incorporate the NavSys class and show their relationship using appropriate UML notations.

Task 2.2. Implement the NavSys class and test this with a program called TestNavSys by creating an instance of NavSys, adding and then selling some navigation system stock and changing the price, whilst printing out the item in between.

Testing is typically a part of the program development - you should use a test strategy to test your program thoroughly. You may look at practical exercises week 3 (Step 4), identify suitable test cases for the StockItem class, write and document them in the form of a table along with the UML class design file.

Test Case Purpose Expected result

An example run might be as follows.

Creating a stock with 10 units Navigation system, price 99.99, and item code NS101
Printing item stock information: Stock Type: Navigation system
Description: GeoVision Sat Nav
StockCode: NS101
PriceWithoutVAT: 99.99
PriceWith VAT: 117.48825
Total unit in stock: 10 Increasing 10 more units
Printing item stock information:
Stock Type: Navigation system
Description: GeoVision Sat Nav
StockCode: NS101
Price Without VAT: 99.99
PriceWith VAT: 117.48825
Total unit in stock: 20
Sold 2 units
Printing item stock information:
Stock Type: Navigation system

Part-III

You now invent three more subclasses of the StockItem class, and explore polymorphism and dynamic method binding. Like NavSys class in Part-II, for each of these invented classes, design the appropriate constructors, get and set methods. You also need the toString method to print the information to the console.

Task 3.1. Revise your UML diagram in Task 2.1, to incorporate your newly invented classes and show their relationship using appropriate UML notations.

Task 3.2. Implement all these classes and write a program called TestPolymorphism which has a class method itemInstance() to test just one instance of a StockItem given to it as a method parameter. This will increase the stock, sell some stock and change the price, printing out the item in between. The class will also have a main() method which builds an array containing one instance of each of the three subclasses of StockItem you have written so far, and then, in a loop, calls the class method to test each one.

Hint. Fragment of code given below.

.
.
.
class Test Polymorphism {
public static void itemInstance (StockItems) {
.
.
.
System.out.println("Printing item stock information:");
System.out.println(s);
.
.
.
}

public static void main(String[] args) {
StockItem [] s = new StockItem [4];
.
.
.
}
}

Testing is typically a part of the program development - you should use a test strategy to test your program thoroughly. You may look at practical exercises week 3 (Step 4), identify suitable test cases for the StockItem class, write and document them in the form of a table along with the UML class design file.

Test Case Purpose Expected result

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.