Create Manufacturer FileHomepage  « Case Study « Create Manufacturer File

In this lesson we write the code to create a test Manufacturer file to be used with the Case Study. We want the file to have a single header record with a Manufacturer file number, followed by data records that contain the record information repeated until the end of the file.

Manufacturer File Formatgo to top of page Top

Stocking Goods Limited have provided us with the format of the Manufacturer file they want us to use although there is no test file provided.

Field Length Type Description Comments
Header Information
4 byteintManufacturer File numberIn the range -2,147,483,648 to 2,147,483,647
Record Information (repeated to EOF)
1 byteStringManufacturer record deleted field'0' - Current record, '1' - Deleted record.
30 byteStringManufacturer name fieldThe name of the manufacturer.
30 byteStringManufacturer location fieldThe town where the manufacturer is located.
40 byteStringManufacturer product description fieldThe name of the product.
8 byteStringManufacturer product price fieldThe price of the product. Format nnnnn.nn
3 byteStringManufacturer product stock level fieldValid ranges 0-999.
3 byteStringManufacturer product stock ordered fieldValid ranges 1-999. Must not be > stock level.

Manufacturer File Creationgo to top of page Top

Using the information in the table above we can create a simple Manufacturer file that should be fit for purpose when coding the rest of the case study.

NOTE: The pathnames used in all the examples for the case study are using a Windows operating system, so if you are using Unix or another operating system adjust them accordingly.

This also applies to code such as:


static String pathname = "C:\\_Case_Study\\src\\manufacturerTest.txt";

Which is used in the example below and you may have to adjust for your operating system.

Cut and paste the following code into your text editor and save it in the   c:\_Case_Study\src\testing directory. We have covered all the code below in the other sections of the site apart from the RandomAccessFile file which allows reading, writing and byte location via a pointer and the self documenting HTML tags we have added for the Javadoc tool, which will be run on project completion to produce system documentation.


package testing;

import java.io.IOException;
import java.io.RandomAccessFile;

public class CreateManufacturerFile {
    /**
     * The pathname for the Manufacturer file we are going to create.
     */
    static String pathname = "C:\\_Case_Study\\src\\manufacturertestfile.txt";
    /**
     * The Manufacturer file.
     */
    static RandomAccessFile manufacturerFile;
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            // Create Manufacturer file and header
            manufacturerFile = new RandomAccessFile(pathname, "rw");
            int fileNumber = 1234;
            manufacturerFile.writeInt(fileNumber);
            // Create Manufacturer records
            String[] createArray = {" ", "Smart Clothes Incorporated", "Tooting", 
                    "Blue Trousers", "12.99", "308", "52"};
            passManufacturerFields(createArray);
            String[] createArray1 = {" ", "Smart Clothes Incorporated", "Swindon", 
                    "Red Checked Cotton Shirt", "21.99", "627", " "};
            passManufacturerFields(createArray1);
            String[] createArray2 = {" ", "Fine Fancy Foods", "Birmingham", 
                    "Chrismas Pudding", "7.99", "627", "12"};
            passManufacturerFields(createArray2);
            String[] createArray3 = {" ", "Car Parts Ltd", "Swindon", 
                    "Front Axle", "222.49", "5", " "};
            passManufacturerFields(createArray3);
            String[] createArray4 = {" ", "Fine Fancy Foods", "Swindon", 
                    "Crispy Duck", "5.99", "72", " "};
            passManufacturerFields(createArray4);
            String[] createArray5 = {" ", "Smart Clothes Incorporated", "Birmingham", 
                    "Blue Jeans", "29.99", "123", "7"};
            passManufacturerFields(createArray5);
            String[] createArray6 = {" ", "Smart Clothes Incorporated", "Chester", 
                    "Pink Scarf", "2.99", "722", " "};
            passManufacturerFields(createArray6);
            String[] createArray7 = {" ", "Fine Fancy Foods", "Chester", 
                    "Smoked Salmon", "9.99", "422", " "};
            passManufacturerFields(createArray7);
            String[] createArray8 = {" ", "Smart Clothes Incorporated", "Braintree", 
                    "Top Hat", "79.99", "29", "2"};
            passManufacturerFields(createArray8);
            String[] createArray9 = {" ", "Car Parts Ltd", "Birmingham", 
                    "Back Axle", "252.99", "8", " "};
            passManufacturerFields(createArray9);
            String[] createArray10 = {" ", "Fine Fancy Foods", "Braintree", 
                    "Strawberry Cheesecake", "2.99", "999", " "};
            passManufacturerFields(createArray10);
            String[] createArray11 = {" ", "Car Parts Ltd", "Tooting", 
                    "Windscreen Wipers", "12.99", "278", " "};
            passManufacturerFields(createArray11);
            String[] createArray12 = {" ", "Fine Fancy Foods", "Tooting", 
                    "Quiche Lorraine", "4.99", "812", "127"};
            passManufacturerFields(createArray12);
            manufacturerFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * This method passes a Manufacturer data field for writing.
     *
     * @param A String array holding Manufacturer information.
     */
    private static void passManufacturerFields(String[] manufacturerArray) 
            throws IOException {
        String s = "";
        // Pass string array to write out
        for (int i=0; i<7; i++) {
            s = manufacturerArray[i];
            writeBytesToFile(i, s);
        }
    }

    /**
     * This method populates the Manufacturer file and pads each field.
     *
     * @param fieldNumber A number denoting field to populate.
     * @param field The data to populate field with.
     */
    private static void writeBytesToFile(int fieldNumber, String field) 
            throws IOException {
        int spaceWrite = 0;
        String s = " ";
        // Populate Manufacturer data record
        switch (fieldNumber) {
            case 0:
                manufacturerFile.writeBytes(field); 
                break;
            case 1:
            case 2:
                manufacturerFile.writeBytes(field); 
                spaceWrite = 30 - field.length();
                for (int i=0; i<spaceWrite; i++) {
                    manufacturerFile.writeBytes(s); 
                }
                break;
            case 3:
                manufacturerFile.writeBytes(field); 
                spaceWrite = 40 - field.length();
                for (int i=0; i<spaceWrite; i++) {
                    manufacturerFile.writeBytes(s); 
                }
                break;
            case 4:
                manufacturerFile.writeBytes(field); 
                spaceWrite = 8 - field.length();
                for (int i=0; i<spaceWrite; i++) {
                    manufacturerFile.writeBytes(s); 
                }
                break;
            case 5:
            case 6:
                manufacturerFile.writeBytes(field); 
                spaceWrite = 3 - field.length();
                for (int i=0; i<spaceWrite; i++) {
                    manufacturerFile.writeBytes(s); 
                }
                break;
            default:
                System.out.println("Erroneous field!");
        }
    }
}

Compiling Our Source File With the -d Option

Open your command line editor:

Change to directory  cd c:\_Case_Study\src\testing

Compile CreateManufacturerFile.java using the java compiler with the -d option
  javac -d ..\..\classes CreateManufacturerFile.java

What we are saying with the -d option is that we want our compiled bytecode to go into the classes directory which is two directories above this directory. The following screenshot shows the javac command and the contents of the classes directory after the compile. Because we have used a package (package testing;) at the top of the code the compiler has created this directory within the classes directory. When we look in the testing directory we can see the compiled CreateManufacturerFile.class file.

compile create man file

Running The Packaged CreateManufacturerFile class

Run the CreateManufacturerFile class from the classes directory using our package as well
  java testing.CreateManufacturerFile

We should now have a test Manufacturer file ready to use within the src directory called manufacturertestfile.txt as shown in the screenshot below.

run create man file

That's it for the Scenario & Setup section.

Related Java6 Tutorials

Beginning Java6 - Primitive Variables
Beginning Java6 - Conditional Statements
Beginning Java6 - Loop Statements
Objects and Classes - Arrays
Objects & Classes - Class Structure and Syntax
Objects & Classes - Reference Variables
Objects & Classes - Methods
Objects & Classes - Instance Variables & Scope
Objects & Classes - Constructors
Objects & Classes - Methods
Objects & Classes - Static Members
Flow Control - Handling Exceptions
Flow Control - Declaring Exceptions
API Contents - Inheritance - Using the package keyword
API Contents - Inheritance - Using the import keyword


What's Next?

In the next section we set up the first part of the Model elements of the MVC pattern that can be derived from the project proposal.

<<  Project Setup                    Model Part 1  >>

go to home page Homepage go to home page Top

All the Scenario & Setup lessons are listed below. Click a link to go to that lesson.


Scenario & Setup

Project Proposal

Project Setup

Create Manufacturer File

Manufacturer File Format

Manufacturer File Creation