Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Divide Class Into 2 or more Classes

    Hi

    I have an assignment that requires dividing the following java code into 2 or more classes. I have tried many times to divide the class but I couldn't.

    Java Code:
    import java.io.*;
    import java.util.Calendar;
    
    public class Test {
        public static void main(String[] args){
            try{
                FileInputStream firstNameStream = new FileInputStream("H:\\OpenCOBOL\\FIRSTNAME.dat");
                FileInputStream lastNameStream = new FileInputStream("H:\\OpenCOBOL\\LASTNAME.dat");
                FileInputStream creditStream = new FileInputStream("H:\\OpenCOBOL\\CREDITLIMIT.dat");
                FileInputStream streetStream = new FileInputStream("H:\\OpenCOBOL\\STREET.dat");
                FileInputStream cityStream = new FileInputStream("H:\\OpenCOBOL\\CITY.dat");
                FileInputStream provinceStream = new FileInputStream("H:\\OpenCOBOL\\PROVINCE.dat");
               
                FileWriter writeStream = new FileWriter("H:\\Other\\CustomerOutput.txt");
                FileWriter AwriteStream = new FileWriter("H:\\Other\\AddressOutput.txt");
                FileWriter BwriteStream = new FileWriter("H:\\Other\\CandAOutput.txt");
                          
                DataInputStream fnIN = new DataInputStream(firstNameStream);
                DataInputStream lnIN = new DataInputStream(lastNameStream);
                DataInputStream clIN = new DataInputStream(creditStream);
                DataInputStream stIN = new DataInputStream(streetStream);
                DataInputStream ciIN = new DataInputStream(cityStream);
                DataInputStream prIN = new DataInputStream(provinceStream);
    
                BufferedWriter cout = new BufferedWriter(writeStream);
                BufferedWriter aout = new BufferedWriter(AwriteStream);
                BufferedWriter caout = new BufferedWriter(BwriteStream);
               
                BufferedReader fnBR = new BufferedReader(new InputStreamReader(fnIN));
                BufferedReader lnBR = new BufferedReader(new InputStreamReader(lnIN));
                BufferedReader clBR = new BufferedReader(new InputStreamReader(clIN));
                BufferedReader stBR = new BufferedReader(new InputStreamReader(stIN));
                BufferedReader ciBR = new BufferedReader(new InputStreamReader(ciIN));
                BufferedReader prBR = new BufferedReader(new InputStreamReader(prIN));
               
                String fnstrLine;
                String fnstrLine1;
                String fnstrLine2;
                
                Calendar now = Calendar.getInstance();
                int num = now.get(Calendar.MILLISECOND);
                int num1 = num;
        		int current = 65535;
        		int number = num + current;
        		String stnumber = ""+number;
        		String copynumber1 = ""+number;
        		int copynumber = 0;
               
                while ((fnstrLine = fnBR.readLine()) != null) {
                	cout.write(stnumber);
                	num = now.get(Calendar.MILLISECOND);
                	current += 1670;
                	number = num + current;
                	stnumber = ""+number;
                	cout.write(" ");
                    cout.write(fnstrLine);
                    cout.write(" ");
                    cout.write(lnBR.readLine());
                    cout.write(" ");
                    cout.write(clBR.readLine());
                    cout.write("\r\n");
                }
                
                cout.close();
                current = 65535;
                while ((fnstrLine1 = stBR.readLine()) != null){
                	aout.write(copynumber1);
                	current += 1670;
                	copynumber = num1 + current;
                	copynumber1 = ""+copynumber;
                	aout.write(" ");
                    aout.write(fnstrLine1);
                    aout.write(" ");
                    aout.write(ciBR.readLine());
                    aout.write(" ");
                    aout.write(prBR.readLine());
                    aout.write(" ");
                    aout.write('c');
                    aout.write("\r\n");
                }
    
                aout.close();
                       
                FileInputStream customerSream = new FileInputStream("H:\\Other\\CustomerOutput.txt");
                FileInputStream AddressStream = new FileInputStream("H:\\Other\\AddressOutput.txt");
                
                DataInputStream CustomerIN = new DataInputStream(customerSream);
                DataInputStream AddressIN = new DataInputStream(AddressStream);
                
                BufferedReader CustomerBR = new BufferedReader(new InputStreamReader(CustomerIN));
                BufferedReader AddressBR = new BufferedReader(new InputStreamReader(AddressIN));
                
    
                while ((fnstrLine2 = CustomerBR.readLine()) != null){
                    caout.write(fnstrLine2);
                    caout.write(" ");
                    caout.write(AddressBR.readLine());
                    caout.write("\r\n");
                }
                
    
                caout.close();
                fnIN.close();
                lnIN.close();
                clIN.close();
                stIN.close();
                ciIN.close();
                prIN.close();
                CustomerIN.close();
                AddressIN.close();
                
            }catch(Exception e){
                System.err.println("Error: " + e.getMessage());
            }
        }
    }
    Thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried? While this is a class, it's not a very good use of classes, everything is done inside of main. See if you can make use of turning it into real classes with instance variables and methods besides main. Perhaps one class can be used to open and read information from a file. Perhaps even a class to encapsulate all the information from the streams.

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Do you mean I split the class into:

    First:
    Java Code:
    FileInputStream firstNameStream = new FileInputStream("H:\\OpenCOBOL\\FIRSTNAME.dat");
                FileInputStream lastNameStream = new FileInputStream("H:\\OpenCOBOL\\LASTNAME.dat");
                FileInputStream creditStream = new FileInputStream("H:\\OpenCOBOL\\CREDITLIMIT.dat");
                FileInputStream streetStream = new FileInputStream("H:\\OpenCOBOL\\STREET.dat");
                FileInputStream cityStream = new FileInputStream("H:\\OpenCOBOL\\CITY.dat");
                FileInputStream provinceStream = new FileInputStream("H:\\OpenCOBOL\\PROVINCE.dat");
    
    DataInputStream fnIN = new DataInputStream(firstNameStream);
                DataInputStream lnIN = new DataInputStream(lastNameStream);
                DataInputStream clIN = new DataInputStream(creditStream);
                DataInputStream stIN = new DataInputStream(streetStream);
                DataInputStream ciIN = new DataInputStream(cityStream);
                DataInputStream prIN = new DataInputStream(provinceStream);
    
     BufferedReader fnBR = new BufferedReader(new InputStreamReader(fnIN));
                BufferedReader lnBR = new BufferedReader(new InputStreamReader(lnIN));
                BufferedReader clBR = new BufferedReader(new InputStreamReader(clIN));
                BufferedReader stBR = new BufferedReader(new InputStreamReader(stIN));
                BufferedReader ciBR = new BufferedReader(new InputStreamReader(ciIN));
                BufferedReader prBR = new BufferedReader(new InputStreamReader(prIN));
    Second:
    Java Code:
    FileWriter writeStream = new FileWriter("H:\\Other\\CustomerOutput.txt");
                FileWriter AwriteStream = new FileWriter("H:\\Other\\AddressOutput.txt");
                FileWriter BwriteStream = new FileWriter("H:\\Other\\CandAOutput.txt");
    
    BufferedWriter cout = new BufferedWriter(writeStream);
                BufferedWriter aout = new BufferedWriter(AwriteStream);
                BufferedWriter caout = new BufferedWriter(BwriteStream);
    
    String fnstrLine;
                String fnstrLine1;
                String fnstrLine2;
                
                Calendar now = Calendar.getInstance();
                int num = now.get(Calendar.MILLISECOND);
                int num1 = num;
        		int current = 65535;
        		int number = num + current;
        		String stnumber = ""+number;
        		String copynumber1 = ""+number;
        		int copynumber = 0;
               
                while ((fnstrLine = fnBR.readLine()) != null) {
                	cout.write(stnumber);
                	num = now.get(Calendar.MILLISECOND);
                	current += 1670;
                	number = num + current;
                	stnumber = ""+number;
                	cout.write(" ");
                    cout.write(fnstrLine);
                    cout.write(" ");
                    cout.write(lnBR.readLine());
                    cout.write(" ");
                    cout.write(clBR.readLine());
                    cout.write("\r\n");
                }
                
                cout.close();
                current = 65535;
                while ((fnstrLine1 = stBR.readLine()) != null){
                	aout.write(copynumber1);
                	current += 1670;
                	copynumber = num1 + current;
                	copynumber1 = ""+copynumber;
                	aout.write(" ");
                    aout.write(fnstrLine1);
                    aout.write(" ");
                    aout.write(ciBR.readLine());
                    aout.write(" ");
                    aout.write(prBR.readLine());
                    aout.write(" ");
                    aout.write('c');
                    aout.write("\r\n");
                }
    
                aout.close();
                       
                FileInputStream customerSream = new FileInputStream("H:\\Other\\CustomerOutput.txt");
                FileInputStream AddressStream = new FileInputStream("H:\\Other\\AddressOutput.txt");
                
                DataInputStream CustomerIN = new DataInputStream(customerSream);
                DataInputStream AddressIN = new DataInputStream(AddressStream);
                
                BufferedReader CustomerBR = new BufferedReader(new InputStreamReader(CustomerIN));
                BufferedReader AddressBR = new BufferedReader(new InputStreamReader(AddressIN));
                
    
                while ((fnstrLine2 = CustomerBR.readLine()) != null){
                    caout.write(fnstrLine2);
                    caout.write(" ");
                    caout.write(AddressBR.readLine());
                    caout.write("\r\n");
                }
                
    
                caout.close();
                fnIN.close();
                lnIN.close();
                clIN.close();
                stIN.close();
                ciIN.close();
                prIN.close();
                CustomerIN.close();
                AddressIN.close();

  4. #4
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    any answer guys !!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This question will require some thought.

    You can create a class called person, which has a field for each type of data being read from the files. Then have another class which handles reading all the files and building person objects and storing them in an array list. Then have one class for printing out all the objects to the screen.

Similar Threads

  1. Divide bigdecimal again
    By ellhar in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 10:19 AM
  2. All possible way to divide a number?
    By bobocheez in forum New To Java
    Replies: 4
    Last Post: 09-24-2010, 02:24 AM
  3. Divide and Conquer@Array...im becoming crazy!
    By wyldstyle in forum New To Java
    Replies: 0
    Last Post: 08-23-2009, 09:20 PM
  4. How to divide code in classes?
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 05:36 PM
  5. Replies: 0
    Last Post: 05-13-2008, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •