Explore BrainMass

A Java program showing object oriented programming

Please do not use exception handling, not covered in this course. See note below.

Project 4 brings together all of the major concepts covered in the course in addition to providing some practice with string processing, file operations, and a little bit of object-oriented programming. In this project you will first study an existing, complete, working object-oriented program. After you have developed a good understanding of the existing program you will develop some modifications to it that make some important improvements.

The program in question is called which uses objects created from the SalesTeam and Person classes. You are already familiar with the Person class. The SalesTeam class uses Person objects to build up a model of a sales team consisting of any number of sales persons and their annual sales data for some number of years. The SalesAward main class creates an array of SalesTeam objects each of which represents one of the company's sales regions. It has the user load these objects with data for a selected time period and then it analyzes this data to determine which team had the highest total sales and which individual sales person had the highest total sales. This information is used to make awards to the highest performers.

Attached to this topic you will find the source code for these three classes and a Word document that contains detailed documentation of these classes. What you need to do in the first phase of this project is study the code provided and the documentation. Develop a good understanding of how this code works. You will need it in phase two of the project in which you will work on some modifications to make some improvements to it. Ask about anything you do not understand. You cannot move forward to make improvements if you do understand where you are beginning from.

Shortly I will post the instructions for the second phase which will include a specification for some updates to the SalesTeam class along with two complete, working main classes that use the upgraded SalesTeam class: A revised SalesAward class and a ListTeams class that generates a list of the teams and their team members. But you need to develop an understanding of the existing code first.
For those who are implementing the loadData() method for Project 4 as I described it note that you have to implement a default constructor in SalesTeam to make that work. Adding the default constructor is OK, of course. Actually you can add any additional methods you find useful as long as you implement the loadAllTeams() method as specified.

By now you should have studied the original version of the SalesAward and related classes. You should have a good understanding of the SalesTeam and SalesAward classes. You are already familiar with the Person class. Now what you need to do is update the SalesTeam class. Attached you will find the following three files:

This is a version of the SalesAwards class that is designed to use a new, upgraded version of the SalesTeam class. It will not compile with the old SalesTeam because the required method loadAllTeams() is not available in the old version. Your job is to create the loadAllTeams() method and add it to Because you are adding a new method but not deleting or changing the existing methods the new version will work with the old version of SalesAward and any other programs that use the SalesTeam class. In other words, it is backwards compatible.

This is a second program that uses the new version of SalesTeam. It will display all of the teams and their members that are in the data file. It is provided for additional testing and to illustrate object re-use. The SalesTeam class can re-used in multiple programs. The work that went into creating it does not have to be duplicated. As long as each class that needs the SalesTeam functionality respects the standardized interface that SalesTeam implements they will all be able to use the same SalesTeam class.


This is a data file that contains the data for a set of sales teams. When loadAllData() is called it is given a String as its only argument. The content of this string will be the file name of the file that contains this data. The structure of the file is described below.

The loadAllTeams( ) Method
Your primary job in this project, along with studying the existing classes to understand how they work, is to program the loadAllTeams( ) method. Its header will be as follows:

public static SalesTeam[] loadAllTeams( String fileName ) throws Exception

This method declares a sting variable fileName as its only parameter. This variable will be used to pass the name of the data file to the method. The job of the method is to open the data file and read its contents. Using the data it finds in the file it will create an array of SalesTeam objects and will load them with all of the data required. It will load the team names, the team members, and the sales data for each member. When it is finished it will return a reference to the SalesTeam[] that it created. It will implement code that "understands" the file structure of the sales team data files described below.

Notice that this method is declared as static. That means that it will be called using the class name instead of a SalesTeam object reference as would be the case with an instance method. See how the method is called in SalesAwards2. No SalesTeam object has to already exist for this method to be called. This is what is known as a factory method. It is essentially a factory that manufactures an array of SalesTeam objects using the input file to obtain the raw resources needed to populate the SalesTeam objects with data.

Although it is not an absolute requirement, for maximum credit on the project you will also implement the following method:

public boolean loadData( Scanner input )

This is an instance method that declares as its only parameter a Scanner reference. It will use this Scanner to load all of the data that is required to populate one SalesTeam object assuming that the Scanner is positioned to read the team name of the next team to be loaded in the standard sales team data file format. Because this is an instance method it will require that a SalesTeam object have already been created. The creation of the SalesTeam objects to go into the array being created and filled by loadAllTeams() can supply the objects to be loaded with loadData(). Because this method is declared public it could also be used by other programs that need to load SalesTeam objects with data but that, for some reason, do not use the loadAllTeams() method.

Although it is not included as part of this project, consider that if saveAllTeams() and saveData() methods were also added then the SalesTeam class would be able to implement its own file-based database system that can load/save either full sets of teams or individual teams. This begins to become a very capable class.

The Standard Sales Team Data File Format (SSTDFF)
A key design objective of the new SalesTeam class was to implement support for a standardized file format for saving and reading sales team data. This allows us to have programs dedicated to maintaining the data and other programs dedicated to processing and displaying the data. Having to type in all the data for a group of sales teams every time we need to process that data is a major limitation to the old version. For that reason the SSTDFF was created.

SSTDFF stores the data for from one to any number of Sales teams. It is a simple ASCII text file format that can be read and edited in any text editor. The first line of the file has a number that indicates how many teams are stored in the file. Following that line are repetitions of the data for one team. If the first line specified 1 team there will be data for 1 team. If the first line specified 3 teams there will be data for 3 teams, etc. The following is a sample file which is also attached below that will be used to explain the file format. Line numbers are shown in red text and are not part of the file:

1: 3
2: Northwest Region
3: 3
4: Jones:George:M:25:146.8
5: 0:541239.57:1013570.05:153071.55:237905.35
6: Morton:Elizabeth:H:63:225.7
7: 347934.28:705603.21:875390.00:650451.75:863725.40
8: Butler:Alex:B:27:122.3
9: 0:0:109070.38:185236.56:247190.00
10: Southeast Region
11: 3
12: Morroson:James:H:37:175.3
13: 0:93756.23:158471.45:256834.86:307812.25
14: Kellog:William:L:54:187.4
15: 595327.64:648053.62:786345.89:921835.55:1125673.45
16: Morton:Sally:S:46:148.3
17: 476013.59:510783.23:589472.95:678926.85:596340.35
18: Southwest Region
19: 4
20: Jamison:Wayne:R:61:210.3
21: 372897.86:457132.49:397503.25:406864.43:532846.81
22: Green:Betty:E:36:126.4
23: 795924.86:851005.39:932433.72:1274912.34:983762.87
24: Sanchez:Jose:Z:36:165.7
25: 0:0:278395.55:286845.35:176953.07
26: Ward:George:K:45:237.6
27: 0:0:0:54967.00:236953.97

Line 1 contains just the value 3 in this case. That specifies that the file contains data for 3 teams. You would read that first line and use the number to create an array of the correct size.

Lines 2 thru 9 provide the data for first team as follows:

Line 2 contains the team name, Northwest Region in this case.

Line 3 specifies how many members are on the team, 3 in this case. Following the line specifying how many members there are there will be pairs of lines with data for each person as follows:

Line 4 supplies colon-separated data for the Person object for this team member, George M. Jones in this case. This should look familiar. The name data plus the age and weight are supplied.

Line 5 supplies colon-separated data for the annual sales data for this member. The first number is always for year 2005 because that is when the company was created. The next will be for year 2006. The next for 2007, etc. There can be as many years worth of data as might be needed. You will find out how many years of data there are by reading and splitting the first one encountered and checking the size of the array generated by split(). SSTDFF guarantees that all sales data lines will contain the same number of years of data so you only have to check this once. If a member did not work for the company in a particular year that year will have zero for annual sales.

There will be as many repetitions of person and sales lines as are needed to supply the number of members specified. In this case 3 members were specified so there will be three pairs of lines: 4 & 5, 6 & 7, and 8 & 9.

Line 9 is the last line for the Northwest Region team. One you have read the last line for a team your are either positioned to read the team name of the next team if you have reached the end of the file.

In this file the team Southeast Region is contained on lines 10 thru 17 and Southwest Region on lines 18 thru 27. Note that Southwest Region has 4 team members.

Study this file structure until it is very clear how it works. I will post some additional material about processing this kind of file.

Turn in your modified file as an attachment.

It looks like I did not really explain why loadData() returns a boolean and under what circumstances it should return what value. This is the scoop:

It returns a boolean to indicate if the load operation succeeded or failed. If every thing went OK then you return true. But if something went wrong in the logical processing of the file then you return false to inform the calling code that there was a problem.

I am not concerned about file operation errors here such as failure to read the file itself (disk drive hit a bad spot, etc.). Those things will cause exceptions to be thrown and we do not discuss exception processing in this course.

What you need to detect and use to force a false return value are the following:

1. The team size is less than 1. How could that possibly be OK? It can't be. We must have a corrupt file or maybe we are not reading a SSTDFF file at all. Who knows what that goofy user is going to throw our way?

2. A data line for a Person object data does not contain 5 data elements. All such data lines must contain 5 elements: first name. middle initial, last name, age, and weight. If it is anything but 5 then something is wrong that the program just cannot deal with. Time to bomb out with a "false" return.

3. The number of data elements in a sales data line is less than 1. This cannot be correct. We could require that all sales data lines contain the same number of years of data as the first one encountered but that would be more complicated so you do not have to do that. But, if you see a reasonably easy way to do it it would be positively valued.

So that is basically it. If any of the three conditions above occur you should abort the load process by returning false. If we were covering exception processing I would show you a much cleaner, more elegant way of dealing with such situations but that is beyond the scope of this course.

This program should be a "B" or "C" with errors included and cannot be perfect. And no exception handling.


Solution Preview

The main part of this solution was adding the methods to the SalesTeam class that could load the data from a file. There ...

Solution Summary

This solution provides a Java program that shows object oriented programming. It also shows file I/O.