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.
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
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
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 |