Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(File Matching) Self-Review Exercise 15.2 asked you to write a series of single

ID: 3844910 • Letter: #

Question

(File Matching) Self-Review Exercise 15.2 asked you to write a series of single statements. Actually, these statements form the core of an important type of file-processing program—namely, a file-matching program. In commercial data processing, it’s common to have several files in each application system. In an accounts receivable system, for example, there’s generally a master file containing detailed information about each customer, such as the customer’s name, address, telephone number, outstanding balance, credit limit, discount terms, contract arrangements and possibly a
Create class FileMatch to perform the file-matching functionality. The class should contain methods that read oldmast.txt and trans.txt. When a match occurs (i.e., records with the same account number appear in both the master file and the transaction f
As transactions occur (i.e., sales are made and payments arrive in the mail), information about them is entered into a file. At the end of each business period (a month for some companies, a week for others, and a day in some cases), the file of transactions (called "trans.txt") is applied to the master file (called "oldmast.txt") to update each account’s purchase and payment record. During an update, the master file is rewritten as the file "newmast.txt", which is then used at the end of the next business period to begin the updating process again. File-matching programs must deal with certain problems that do not arise in single-file programs. For example, a match does not always occur. If a customer on the master file has not made any purchases or cash payments in the current business period, no record for this customer will appear on the transaction file. Similarly, a customer who did make some purchases or cash payments could have just moved to this community, and if so, the company may not have had a chance to create a master record for this customer. Write a complete file-matching accounts receivable program. Use the account number on each file as the record key for matching purposes. Assume that each file is a sequential text file with records stored in increasing account-number order. a) Define class TransactionRecord. Objects of this class contain an account number and amount for the transaction. Provide methods to modify and retrieve these values. b) Modify class Account in Fig. 15.9 to include method combine, which takes a TransactionRecord object and combines the balance of the Account object and the amount value of the TransactionRecord object. c) Write a program to create data for testing the program. Use the sample account data in Figs. 15.14 and 15.15. Run the program to create As transactions occur (i.e., sales are made and payments arrive in the mail), information about them is entered into a file. At the end of each business period (a month for some companies, a week for others, and a day in some cases), the file of transactions (called "trans.txt") is applied to the master file (called "oldmast.txt") to update each account’s purchase and payment record. During an update, the master file is rewritten as the file "newmast.txt", which is then used at the end of the next business period to begin the updating process again. File-matching programs must deal with certain problems that do not arise in single-file programs. For example, a match does not always occur. If a customer on the master file has not made any purchases or cash payments in the current business period, no record for this customer will appear on the transaction file. Similarly, a customer who did make some purchases or cash payments could have just moved to this community, and if so, the company may not have had a chance to create a master record for this customer. Write a complete file-matching accounts receivable program. Use the account number on each file as the record key for matching purposes. Assume that each file is a sequential text file with records stored in increasing account-number order. a) Define class TransactionRecord. Objects of this class contain an account number and amount for the transaction. Provide methods to modify and retrieve these values. b) Modify class Account in Fig. 15.9 to include method combine, which takes a TransactionRecord object and combines the balance of the Account object and the amount value of the TransactionRecord object. c) Write a program to create data for testing the program. Use the sample account data in Figs. 15.14 and 15.15. Run the program to createthe files trans.txt and oldmast.txt to be used by your file-matching program.
D) Create class FileMatch to perform the file-matching functionality. THe class should contain methods that read oldmast.tt and trans.txt. When a match occurs *i.e., records with the same account number appear in both the master file and the transatiocion file add the dollar amount in the transaction record to the current balance in the master record, and write the "newmast.txt" record. (Assume that purchases are indicated by positive amounts in the transaction file and payments by negative amounts.) When there’s a master record for a particular account, but no corresponding transaction record, merely write the master record to "newmast.txt". When there’s a transaction record, but no corresponding master record, print to a log file the message "Unmatched transaction record for account number…" (fill in the account number from the transaction record). The log file should be a text file named "log.txt".

Explanation / Answer

//AccountRecord.java

/**
* Created by Swapnil on 31-05-2017.
*/
public class AccountRecord {
    private int account;
    private String firstName;
    private String lastName;
    private double balance;

    public AccountRecord(){
        this(0,"","",0.0);
    }

    public AccountRecord(int acct,String first,String last,double bal){
        setAccount(acct);
        setFirstName(first);
        setLastName(last);
        setBalance(bal);

    }

    public void setAccount(int acct){
        account=acct;
    }

    public void setFirstName(String first){
        firstName=first;
    }

    public void setLastName(String last){
        lastName=last;

    }

    public void setBalance(double bal){
        balance=bal;

    }


    public double getBalance() {
        return balance;
    }

    public int getAccount() {
        return account;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    //the new method combine to increment balance on matching
    public void combine(AccountRecord Record){
        balance= (Record.balance) +balance;
    }

}


==============================================================================================

//CreateTextFile.java
/**
* Created by Swapnil on 31-05-2017.
*/
import java.io.File;
import java.util.FormatterClosedException;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Formatter;

// a class to use and create files to use
public class CreateTextFile {
    Scanner input=new Scanner(System.in) ;
    private Formatter output;
    public void OpenOperation(){
        //Asking user to enter variables to create TransFile
        int TransOrMaster;
        System.out.println("Create transaction File and OldMaster file " +
                " Transaction file=1 OldMasterFile=2");

        TransOrMaster=input.nextInt();
        switch (TransOrMaster){
            case 1:
                //input the various data for the file.
                //create the file
                try{
                    output=new Formatter("trans.txt");
                }
                catch(FileNotFoundException exception){
                    System.out.println("Sorry, can't open or create the desired file(trans.txt)");
                }
                catch(SecurityException exception){
                    System.out.println("Sorry, you don't have write access to this file");
                }

                System.out.println("Input the various data starting with the account No,the Name and the balance");
                while(input.hasNext()){
                    TransactionRecord newRecord=new TransactionRecord();

                    try{
                        newRecord.SetAccountNo(input.nextInt());
                        newRecord.SetName(input.next());
                        newRecord.SetTransactionAmount(input.nextDouble());

                        output.format("%d %s %d,",newRecord.getAccountNo(),newRecord.getName(),newRecord.getTransactionAmount());
                    }catch(Exception exception){
                        System.out.println("Can't output to File");
                    }


                }
                break;
            case 2:
                try{
                    output=new Formatter("oldmast.txt");
                }
                catch(FileNotFoundException exception){
                    System.out.println("Sorry, can't open or create the desired file(trans.txt)");
                }
                catch(SecurityException exception){
                    System.out.println("Sorry, you don't have write access to this file");
                }

                System.out.println("Input the various data starting with the account No,the Name and the balance");
                while(input.hasNext()){
                    AccountRecord newRecord=new AccountRecord();

                    try{
                        newRecord.setAccount(input.nextInt());
                        newRecord.setFirstName(input.next());
                        newRecord.setLastName(input.next());
                        newRecord.setBalance(input.nextDouble());

                        output.format("%d %s %s %d,",newRecord.getAccount(),newRecord.getFirstName(),newRecord.getLastName(),newRecord.getBalance());
                    }catch(Exception exception){
                        System.out.println("Can't output to File");
                    }
                }
                break;
            default:
                break;
        }


    }

}
=========================================================================================
//FileMatch.java
/**
* Created by Swapnil on 31-05-2017.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;

public class FileMatch {
    //this class compares files in the oldmast and newmast to find a record that matches
    //any that does not is put into a log file
    Scanner inOldMast;
    Scanner inNewMast;

    public void OpenFilesToCompare(){
        try{
            inOldMast=new Scanner(new File("oldmast.txt"));
            inNewMast=new Scanner(new File("newmast.txt"));


        }catch(FileNotFoundException e){
            System.out.println("Sorry, the files not be found");
        }

    }

    public void Compare(){
        //reading input from files
        AccountRecord RecordOldmast=new AccountRecord();
        RecordOldmast.setAccount(inOldMast.nextInt());
        RecordOldmast.setFirstName(inOldMast.next());
        RecordOldmast.setLastName(inOldMast.next());
        RecordOldmast.setBalance(inOldMast.nextDouble());

        //comparing and updating balance
        boolean IndicatorOldMast=true;
        boolean IndicatorNewMast=true;

        while(IndicatorOldMast){
            boolean primeIndicator=false;
            //checking the next file
            while( IndicatorNewMast){
                AccountRecord RecordNewMast=new AccountRecord();
                RecordNewMast.setAccount(inNewMast.nextInt());
                RecordOldmast.setFirstName(inNewMast.next());
                RecordNewMast.setBalance(inNewMast.nextDouble());

                //the comparisim
                if(RecordOldmast.getAccount()==RecordNewMast.getAccount()) {
                    RecordOldmast.combine(RecordNewMast);
                    IndicatorNewMast = false;
                    primeIndicator=true;
                }else{
                    IndicatorNewMast=inNewMast.hasNext();
                    try{
                        Formatter LogFileOutput=new Formatter(new File("LogFile.txt"));
                        LogFileOutput.format("%d", RecordNewMast.getAccount());
                    }catch(Exception e){
                        System.out.println("Sorry,, the file the file LogFile.txt can't be created");
                    }
                }

            }

            //if a match is found
            if(primeIndicator==true){
                IndicatorOldMast=inOldMast.hasNext();

            }
        }
    }
}

============================================================================================
//ProgramTest.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

    //main testing file
    public class ProgramTest {
        public static void main(String[]args){
            Scanner input=new Scanner(System.in);
            CreateTextFile Application=new CreateTextFile();
            // the int option variable is used to note when what a user selects

            int option=1;
            while(option==1){
                System.out.println("Create a file choose a type");
                Application.OpenOperation();
                System.out.println("Do you want to create another file. 1. yes 2.no");
                String yesOrno;

                yesOrno=input.next();
                if(yesOrno=="no"||yesOrno=="No"||yesOrno=="NO"){
                    option=2;
                }

            }

            //comparing the files
            FileMatch comparision=new FileMatch();
            comparision.OpenFilesToCompare();
            comparision.Compare();

            //outputting the files
            try{
                Scanner outOldmast=new Scanner(new File("oldmast.txt"));
                Scanner outNewMast=new Scanner(new File("newmast.txt"));
                Scanner outLog=new Scanner(new File("LogFile.txt"));

                //displaying result for oldmast.
                System.out.print("Oldmast.txt ");
                while(outOldmast.hasNext()){
                    System.out.printf("%d    %s    %s    %d ",outOldmast.nextInt(),outOldmast.next(),outOldmast.next(),
                            outOldmast.nextDouble());
                }

                System.out.println("Newmast.txt");
                while(outNewMast.hasNext()){
                    System.out.printf("%d   %s   %d ",outNewMast.nextInt(),outNewMast.next(),outNewMast.nextDouble());
                }

                System.out.println("LogFile.txt");
                System.out.print("The following account numbers don't match");
                while(outLog.hasNext()){
                    System.out.printf("%d ",outLog.nextInt());

                }

            }catch(FileNotFoundException e){
                System.out.println("Sorry on ore more files could not be accessed");

            }

        }

    }

================================================================================================
//TransactionRecord.java
/**
* Created by Swapnil on 31-05-2017.
*/
public class TransactionRecord {
    //Objects from this class have an account and amount for the transaction.
    //variables
    int AccountNo;
    String name;
    double TransactionAmount;

    //constructor
    public void SetAccountNo(int Account){
        AccountNo=Account;
    }

    public void SetName(String NameFor){
        name=NameFor;
    }

    public void SetTransactionAmount(double TransAmount){
        TransactionAmount=TransAmount;
    }

    public int getAccountNo(){
        return AccountNo;
    }

    public String getName(){
        return name;
    }

    public double getTransactionAmount(){
        return TransactionAmount;
    }

}
=====================================================
//ReadFromTextFile.java
/**
* Created by Swapnil on 31-05-2017.
*/
public class ReadFromTextFile {

}
==========================================================
//WriteToTextFile.java
/**
* Created by Swapnil on 31-05-2017.
*/
public class WriteToTextFile {
}