讲解 program编程、辅导 java程序设计
Assignment 2
Technology Store - worth 25% of module grade
1. Overview
Your are tasked with developing a Technology Store system. It is a menu-driven,
console app that can store technology devices of the following type:
• Tablet
• Smart Band
• Smart Watch
A technology device can only be added for an existing Manufacturer.
The information entered via the app will be persisted in XML files.
Coding the Assignment
You will be provided with full UML for the classes in the Technology Store system.
From the coding perspective, you will be given a lot of starter code, with some
classes fully completed and some partially completed. You will need to fully code the
inheritance hierarchy, the management of the collection of Technology Devices, and
the menu options for it. More details on this later in the spec.
Suggested Menu(s)
We have provided a suggested main menu, along with some sub menus. Please feel
free to deviate from it in any way you wish (but document if you change significantly,
especially if you have the change as an extra)
Similar Project - SocialNetworkV9.0
Download the solution for Social Network V9.0.
• Inheritance Hierarchy with Abstraction and Polymorphism
• Use of ISerializer Interface
• Basic Utilities class
• JUnit testing of Inheritance Hierarchy and NewsFeed (the API class)
Similar Project - ShopV8.0
Download the solution for Shop V8.0. Among other functionalities, you will see
examples of:
• Sorting
• Searching
• Basic Utilities class
• JUnit testing of Store (the API class). that will help you in the design of your
solution.
2. Classes Overview
The System has the follwing src classes:
Starting the New Project
You will see the starting code. Note that it is not complete, not all the classes
etc. are included. You will need to add these yourself.
When you have the code added:
• run the Driver menu and familiarise yourself with the menu.
• familiarise yourself with the code given to you.
• familiarise yourself with the TODO comments in the code. All the information
required to complete the TODO comments are in this spec. You should read
through the spec now, so that you have a good handle on what you need to
do.
Classes that are completed:
(all available in Starter Code)
Class Name Responsibility
Manufacturer
The responsibility for this class is to store
a Manufacturer i.e. a manufacturer has a
name and a number of employees.
ManufacturerAPI
The responsibility for this class is to store
and manage a list of Manufacturers.
Vehicles can only be added for
Manufacturers that exist in this list.
ScannerInput
This is the same class from Assignment 1
and should be used for all user input.
Utilities
This class contains utility methods used
throughout the system in multiple classes.
OperatingSystemUtility
This class contains utility methods used to
validate Operating Systems
DisplayTypeUtility
This class contains utility methods used to
validate Display types
Classes that are partially completed:
Class Name Responsibility
Driver
The responsibility for this class is to
manage the User Interface (UI) i.e.
the menu and user input/output. This
class should be the only class that
has System.out.println() or Scanner
reads in it. This class contains an
object of TechnologyDeviceAPI and
an object of ManufacturerAPI.
TechnologyDeviceAPI
The responsibility for this class is to
store and manage a list of
Technology. Note that Technology is
the super class in the hierarchy
pictured below, so any subclass
objects can be added to this list of
Technology e.g. an object of Tablet
can be added to it.
An Interface that you need to include is:
Interface
Name Responsibility
ISerializer
This interface is the same as that used in lectures and
labs. It should be implemented by both the
TechnologyDeviceAPI and ManufacturerAPI for XML
persistence in these classes.
Classes that you need to write from scratch (i.e. the Inheritance
Hierarchy):
Class Name Type
Abstract or
Concrete Responsibility
Technology Super Class Abstract
Manages the common
information relating to an
Technology i.e. price, id,
manufacturer and model
WearableDevice
Sub Class of
Technology Abstract
Manages the information
relating to a Wearable i.e.
material and size
SmartBand
Sub Class of
WearableDevice Concrete
Manages the specific
information relating to an
SmartBand i.e.
heartRateMonitor(y/n)
SmartWatch
Sub Class of
WearableDevice Concrete
Manages the specific
information relating to a
SmartWatch e.g.
displayType
ComputingDevice
Sub Class of
Technology Abstract
Manages the information
relating to a computing
device i.e. storage and
processor
Tablet
Sub Class of
ComputingDevice Concrete
Manages the specific
information relating to a
Tablet i.e.operatingSystem.
Test Classes
Class Name Status
TabletTest Completed
ComputingDeviceTest Completed
TechnologyTest Completed
ManufacturerTest Completed
TechnologyDeviceAPITest started, needs to be completed
WearableDeviceTest To be Done
SmartWatchTest To be Done
SmartBandTest To be Done
3. Hierarchy Overview
The following UML diagram shows the Inheritance structure, with fields, constructors
and methods:
While developing this hierarchy, remember the following:
As soon as you have entered the fields for a class in IntelliJ, generate boilerplate
methods i.e.
• constructors: should adhere to any validation rules outlined for the instance
fields. Note: for all data types apart from Strings - if the value passed as a
parameter is invalid, the default value (set at field declaration time) should
remain in place. In the case of strings the connstructor should truncate the
field (if the validation includes a maximum length).
• getters - each instance field should have a getter method.
• setters - each instance field should have a setter method. Setters should
adhere to the validation rules outlined for the instance fields. However, a
setter should not apply a default value; it should only update the field if the
value passed as a parameter was valid.
• equals - no changes from the generated code here. (You should delete
hashCode method generated with equals if you wish - it won’t cause any
problems to leave it in but it is not used.)
• toString - methods in the subclasses (Tablet, SmartWatch, SmartBand)
should return a String version of the current object state as well as the object
state of the super class, WearableDevice or ComputingDevice (and, when
applicable, Technology).
When coding any validation rules in the constructor and setters, you should use
methods from the utils classes to perform the validation (see utils tab for more
information).
We have included some helpful graphics below to illustrate this for you.
Pay attention to the format and the icons in the UML code above…they will tell you a
huge amount about writing the code.
The legend for the UML diagrams is :
Also note what information is where in the diagram, see the UML diagram for the
Technology class below :
4. Technology class
This is the super class in the hierarchy. The responsibility for this abstract class is to
manage a Technology. The UML is here:
NOTES:
• You may add additional instance fields of your choice (for extra credit!). If you
do so, the method list and parameters for existing methods will change/grow.
• The Hierarchy Overview tab has generic information on coding constructors,
getters, setters and toString. The information below is just the specifics
related to this class.
Fields
The following are private fields in the Technology class:
• manufacturer : the manufacturer has to be registered in the system in order
to add them as a manufacturer for a technology (this validation should be
handled in the Driver)
• id : must be unique in the system (regardless of case - this validation should
be handled in Driver). Default value “unknown”. There should be max 10
characters in id.
• modelName : No default value. There should be max 30 characters
in modelName
• price : Default value 20. Must be >= 20, with no upper limit.
Constructor Signature
public Technology(String modelName, double price, Manufacturer
manufacturer, String id)
Abstract method
There is two abstract methods in this class (the three concrete classes will provide
the implementation for it):
• getInsurancePremium()
• connectToInternet()
The return type for these methods can be read from the UML structure above.
toString method
This method should build a one line string containing the following information and
return it (note: no \n should be included in the String):
• id
• model name
• price
• manufacturer (name and number of employees)
JUnit Test Class
The Test Class for Technology (TechnologyTest.java) is included in the starter code
and fully written. Because TECHNOLOGY is an abstract class, the test Class creates
a Tablet instance and tests the technology fields only of it. So to run this ‘Technology’
test you also need to have Tablets started (even dummy methods as per UML will do
to get started)
5. WearableDevice class
The responsibility for this abstract class is to extend Technology by adding two fields.
The UML is here:
NOTES:
• You may add additional instance fields of your choice (for extra credit!). If you
do so, the method list and parameters for existing methods will change/grow.
• The Hierarchy Overview tab has generic information on coding constructors,
getters, setters and toString. The information below is just the specifics
related to this class.
Fields
There are two private field in this class:
• material : This is the material of the wearable. It should be less than 20 chars,
there is no default.
• size : This is the size of the wearable. It should be less than 10 chars, there is
no default.
Constructor
There is one constructor for this class. The parameter list for this constructor should
be the the parameter list for the Technology with the additional two fields above . The
constructor should call the superclass constructor.
Abstract methods
• getInsurancePremium() • connectToInternet() this can be left as abstract in this class
JUnit Test Class
You are asked to write the Test Class for WearableDevice. You should use the test
class for ComputingDevice as inspiration.
6a. SmartBand class
The responsibility for this concrete class is to extend Wearable and implement the
class for a SmartBand. The UML is here:
NOTES:
• You may add additional instance fields of your choice (for extra credit!). If you
do so, the method list and parameters for existing methods will change/grow.
• The Hierarchy Overview tab has generic information on coding constructors,
getters, setters and toString. The information below is just the specifics
related to this class.
Fields
There is one private field in this class:
• heartRateMonitor : This is a booleann field, no validation required
Constructor
There is one constructor for this class. The parameter list for this constructor should
be the same as the parameter list for the Wearable class but with the additional 1
field (above). The constructor should call the superclass constructor and also
instantiate the new field (with validations).
Abstract methods
getInsurancePremium - this method returns a (double) value for the insurance
premium of a Smart Band. The algorithm for calculating this is:
// Algorithm -
// price of Smart Band * .07
// e.g. 230 *.07 = 16.1
connectToInternet - this method returns a String for the internet connection details
of a Smart Band.
// return the String "Connects to the internet via Companion
App"
toString method
This method should build a one line string containing the following information and
return it (note: no \n should be included in the String):
• details from the WearableDevice toString() as well as
• heart rate monitor (should state “Includes Heart Rate Monitor” if true or “No
Heart Rate Monitor included” if false )
• insurance premium
• internet connection
JUnit Test Class
You will need to write the Test Case for SmartBand, use Tablet as a guide
6b. SmartWatch class
The responsibility for this concrete class is to extend Wearable and implement the
class for a SmartWatch. The UML is here:
NOTES:
• You may add additional instance fields of your choice (for extra credit!). If you
do so, the method list and parameters for existing methods will change/grow. • The Hierarchy Overview tab has generic information on coding constructors,
getters, setters and toString. The information below is just the specifics
related to this class.
Fields
There is one private field in this class:
• displayType : This is the display type of the smart watch. The display type
must “AMOLED” or “LCD” or “LED” or “TFT”. You can use the given utilities
class for this. The default value is “LCD”
Constructor
There is one constructor for this class. The parameter list for this constructor should
be the same as the parameter list for the Wearable class but with the additional 1
field (above). The constructor should call the superclass constructor and also
instantiate the new field (with validations).
Abstract methods
getInsurancePremium - this method returns a (double) value for the insurance
premium of a Smart Watch. The algorithm for calculating this is:
// Algorithm -
// price of Smart Watch * .06
// e.g. 230 *.06 = 13.8
connectToInternet - this method returns a String for the internet connection details
of a Smart Watch.
// return the String "Connects to the internet via
bluetooth"
toString method
This method should build a one line string containing the following information and
return it (note: no \n should be included in the String):
• details from the WearableDevice toString() as well as
• display type
• insurance premium
• internet connection
JUnit Test Class
You will need to write the Test Case for SmartWatch, use Tablet as a guide.
7. ComputingDevice class
The responsibility for this abstract class is to extend Technology by adding two fields.
The UML is here:
NOTES:
• You may add additional instance fields of your choice (for extra credit!). If you
do so, the method list and parameters for existing methods will change/grow.
• The Hierarchy Overview tab has generic information on coding constructors,
getters, setters and toString. The information below is just the specifics
related to this class.
Fields
There are two private field in this class:
• storage : This is the storage of the Computing device, stored in GB. It must be
between 8 and 128, and must be divisible by 8
• processor : This is the processor of the computing device. It should be 20
chars or less, there is no default.
Constructor
There is one constructor for this class. The parameter list for this constructor should
be the the parameter list for the Technology with the additional two fields above . The
constructor should call the superclass constructor.
public ComputingDevice(String modelName, double price,
Manufacturer manufacturer, String id, String processor, int storage)
Abstract methods
• getInsurancePremium()
• connectToInternet() this can be left as abstract in this class
JUnit Test Class
The Test Class for ComputingDevice (ComputingDeviceTest.java) is included in the
starter code and fully written. Because ComputingDevice is an abstract class, the
test Class creates a Tablet instance and tests the ComputingDevice fields only of it.
So to run this ‘ComputingDevice’ test you also need to have Tablets started (even
dummy methods as per UML will do to get started)
8. Tablet class
The responsibility for this concrete class is to extend Computing Device (which has
extended Technology) and implement the class for an Table. The UML is here:
NOTES:
• You may add additional instance fields of your choice (for extra credit!). If you
do so, the method list and parameters for existing methods will change/grow.
• The Hierarchy Overview tab has generic information on coding constructors,
getters, setters and toString. The information below is just the specifics
related to this class.
Fields
There is one private field in this class:
• operatingSystem : This is the tablets operating system, The default is
“Windows” and the allowable values as per OperatingSystemUtility
Constructor
There is one constructor for this class. The parameter list for this constructor should
be the same as the parameter list for the ComputingDevice class but with the
additional field above . The constructor should call the superclass constructor and
also instantiate the field.
public Tablet(String modelName, double price, Manufacturer
manufacturer, String id, String processor, int storage, String
operatingSystem)
Abstract method
getInsurancePremium - this method returns a (double) value for the insurance
premium of a Smart Watch. The algorithm for calculating this is:
// Algorithm -
// price of tablet * .01
// e.g. 230 *.01 = 2.3
connectToInternet - this method returns a String for the internet connection details
of a tablet.
// return the String "Connects to the internet via Wi-Fi"
JUnit Test Class
`
The Test Class for Tablet (Tablet.java) is included in the starter code and fully
written.
9. TechnologyDeviceAPI class
This API class is responsible for storing and managing ALL the Technology Devices
in the system.
This UML here is starter UML - depending on the menu items you include in your
Driver class, the methods here will grow / change:
There is starter code for TechnologyDeviceAPI in the Starter Code you are given.
Fields
There are two private fields,
• technologyList, which is an ArrayList of Technology;
• file , the file that technology devices will be saved to /loaded from.
Basic CRUD on technologyList ArrayList
addTechnologyDevice (with the new Technology as a parameter)
This method will add a Technology object (passed as a parameter) to the
ArrayList technologyList. There is no validation in this method. Returns true if the
technology was added and false if not.
deleteTechnologyByIndex (with index - position of technology in array list as
parameter)
This method removes an technology object at the location index, which is passed as
a parameter. There is some validation in this method.
• Check that the passed index exists in the ArrayList:
• if it does exist, remove it from the ArrayList and return the object that
was just deleted.
• if the passed index is not valid, return null.
deleteTechnologyById (with id of technology as parameter)
This method removes an technology object with the id which is passed as a
parameter. There is some validation in this method.
• Check that the passed id exists in the ArrayList:
• if it does exist, remove the corresponding technology from the ArrayList
and return the object that was just deleted.
• if the passed index is not valid, return null.
getTechnologyByIndex (with index - position of technology in array list as
parameter)
This method returns an Technology object at the location index, which is passed as a
parameter. There is some validation in this method.
• Check that the passed index exists in the ArrayList:
• if it does exist, return the object at that position.
• if the passed index is not valid, return null.
getTechnologyDeviceById (with id of technology as parameter)
This method returns a Technology object with that exact id (ignoring case), which is
passed as a parameter. There is some validation in this method.
• Check that the passed id exists in the ArrayList:
• if it does exist, return the object with that id.
• if the passed id is not found, return null.
Reporting Methods
listAllTechnologyDevices()
This method should return a String containing the details of all the technology
in technologyList along with the index number associated with each technology
device. If no technology exist yet, “No Technology Devices” should be returned.
listAllSmartBands()
This method should return a String containing the details of all the Smart Bands
in technologyList along with the index number associated with each smart band
device. If no smart band exist yet, “No Smart Bands” should be returned.
listAllSmartWatches()
This method should return a String containing the details of all the Smart Watchs
in technologyList along with the index number associated with each smart watch
device. If no smart watches exist yet, “No Smart Watches” should be returned.
listAllTablets()
This method should return a String containing the details of all the Tablets
in technologyList along with the index number associated with each tablet device. If
no tablets exist yet, “No Tablets” should be returned.
listAllTechnologyAbovePrice(with price(double) as a parameter)
This method should return the list of technology equal or above the entered price .
• If no such technology exist, “No technology more expensive
than ??” (include price).
listAllTechnologyBelowPrice(with price(double) as a parameter)
This method should return the list of technology equal or below the entered price.
• If no such technology exist, “No technology cheaper than ??” (include
price).
listAllTechDevicesByChosenManufacturer( with a Manufacturer as a
parameter)
This method should return a String containing the details of all the technology
in technologyList whose manufacturer is equal to that passed in as parameter. If no
such technology exist, " “No technology manufactured by " +
manufacturer” should be returned.
listAllTabletsByOperatingSystem( with a operating system as a parameter)
This should return a String containing all the tablets that have the operating system
as the one passed in as a parameter. Validation should be done to ensure its a valid
operating system. If invalid system return "Invalid Operating System If no such
tablet exist, **"No tablet with the operating system "+ os ** should be returned.
numberOfTechnologyDevices()
This method returns the number of technology devices in the system
(in technologyList).
numberOfTablets()
This method returns the number of tablets in the system (in technologyList).
numberOfSmartBands()
This method returns the number of smart bands in the system (in technologyList).
numberOfSmartWatch()
This method returns the number of smart watches in the system
(in technologyList).
numberOfTechnologyByChosenManufacturer(with a manufacturer passed in
as a parameter)
This method returns the number of technology in the system (in technologyList)
whose manufacturer is that passed in.
Update Methods
updateTablet(String id, Tablet updatedDetails)
This method takes in a id and replaces the corresponding object with the Tablet as
input (updatedDetails).
updateSmartWatch(String id, SmartWatch updatedDetails)
This method takes in a id and replaces the corresponding object with the
SmartWatch as input (updatedDetails).
updateSmartBand(String id, SmartBand updatedDetails) This method takes in a id and replaces the corresponding object with the Smart
Band as input (updatedDetails).
Validation Methods
isValidId(String id)
This method is written for you. It returns
• true if that id does not exist in the technologyList collection
• false if that id does exist in the technologyList collection
Sorting Methods
sortByPriceDescending()
This method should change the technologyList object so that it is sorted by price in
descending order.
sortByPriceAscending()
This method should change the technologyList object so that it is sorted by price in
ascending order.
swapTechnology (List technologyList, int i, int j)
This should be a private method that swaps the objects at positions i and j in the
collection technologyList. This method should be used in your sorting method.
Other Methods
topFiveMostExpensiveTechnology()
• returns a List of the top 5 most expensive technology, sorted by price
topFiveMostExpensiveSmartWatch()
• returns a List of the top 5 most expensive Smart Watch, sorted by price
topFiveMostExpensiveTablet()
• returns a List of the top 5 most expensive tablet, sorted by price
Persistence
All of the persistence methods are supplied in the starter code.
TODO: make sure you implement the ISerializer interface in TechnologyDeviceAPI
(and also ManufacturerAPI).
Save
This method saves all TECHNOLOGY objects from the ArrayList to an XML
file technologyDevices.xml.
Load
This method loads all saved Vehicle objects back into the program (i.e into the
ArrayList vehicles) from the XML file technologyDevices.xml.
fileName()
This method should return the file that the vehicles collection is saved to/load from.
JUnit Test Class
A partial test class for TechnologyDeviceAPI given in the Starter Code
You are asked to complete the given test class.
You can check how complete your updated test class is by using the Coverage Tool
in Intellij.
10. Driver class
The responsibility of the Driver class is to run the app and perform I/O with the user.
There is starter code for Driver in the Starter Code.
Menu
The code supplied uses a “layered” menu approach i.e. a suggested main menu that
calls suggested sub menus e.g.:
We have given complete code for the Manufacturer Management Menu, and some
skeleton code for the remaining options.
It is completely up to you how you would like to design the Technology Store Menu
and the Reports Menu. As you design these, you should try use all the methods that
are written in TechnologyDeviceAPI. For example:
• when reporting on the Technology Store, you have different types of
Technology to report on e.g. Tablets, etc.
• when reporting on manufacturers, you can report on all manufacturers or one
specifically.
When writing this class, the above menus are just a suggestion. You may deviate
from them as you wish. If you provide more elaborate menu items/reports, you may
need to add more methods to the ManufacturerAPI and/or TechnologyDeviceAPI
class (extra credit!).
If you do deviate from the suggested menus, ensure that you still include the
following basic elements:
• CRUD on the technology list
• Reporting for both Technology and Manufacturers and
• Persistence
A note on I/O
Aside from the ScannerInput class, this class should be the only class that has:
• System.out.print statements
• ScannerInput objects defined/used.
11. Utils Package
The utils package contains collections of utilities, grouped by function.
These are included in the Starter Code. Ensure that you understand any you use.
Name Responsibility
ISerializer
This interface is the same as that used in lectures and labs. It should be
implemented by both the TechnologyAPI and ManufacturerAPI for XML
persistence in these classes.
OperatingSystemUtility This class contains utility methods used to validate Operating Systems
DisplayTypeUtility This class contains utility methods used to validate Display types
ScannerInput This is the same class from Assignment 1 and should be used for all user input.
Utilities
This class contains utility methods used throughout the system in multiple
classes. This class stores the reusable Utility (general) methods in one area.
TODO for Utilities
• You can choose to validate without the use of these packages but it will be
more time consuming and you will lose marks.
• Make sure that you implement the ISerializer interface in ManufacturerAPI
and TechnologyDeviceAPI.
12. Junit
• TechnologyTest - The Technology (abstract) class is fully tested (in Starter
Code)
• ComputerDeviceTest - The ComputerDevice (abstract) class is fully tested
(in Starter Code)
• TabletTest - The Tablet class is fully tested (in Starter Code)
• ManufacturerTest - this is fully tested (in Starter Code)
• TechnologyDeviceAPITest - this is partially tested, you need to complete
this to 90-100%. (partal tests in Starter Code)
What you need to develop from scratch:
• WearableDeviceTest - model this on the ComputerDeviceTest code above.
Aim for 90-100% coverage.
• SmartWatchTest - model this on the TabletTest code above. Aim for 90-100%
coverage
• SmartBandTest - model this on the TabletTest code above. Aim for 90-100%
coverage
• TechnologyDeviceAPITest - achieve a code coverage of at least 90%
(starter test code provided above). Note: code coverage percentage is based
on all the functionality completed.
13. Running the Tests
Test Folder is Excluded
• The test folder is coloured orange in your skeleton code (it will look more
orange in your project). This means that it is excluded and cannot be “seen”
by the compiler or the JUnit test runners.
Including the Test Folder
• To run your tests, you need to include the test folder by following these steps:
1. Right click on the orange test folder and “cancel exclusion”:
2. Your test folder will now be grey:
3. Right click on the grey test folder and “Mark Directory As”, “Test
Sources”:
4. The test folder will now be green and you can now run your tests as normal:
Delete the out folder
The orange “out” folder is generated by the compiler.
You can delete this by right clicking on it and selecting “delete”:
Confirm that you want to delete the folder (it will be regenerated when you compile
again)
Folder Structure
Your folder structure should now look like this:
14. Plan of Action/Attack
When approaching this assignment, we suggest that you use this sequence.
1. You should start at the models part of the assignment.
2. Then