Goal: Modify the classes Transaction (from Project4), TransactionChecker (Observ
ID: 3726682 • Letter: G
Question
Goal: Modify the classes Transaction (from Project4), TransactionChecker (Observable), TransactionLogger (Observer),as specified in Details 2 and 3. Write a TransactionChecker class as specified in Detail 4
Create a project (named Proj4bSmith if Smith is your last name) that consists of these files:
TransactionChecker.java TransactionLogger.java TestTransactionChecker.java
Transaction.java (from Project 4).
Also download the transactions.txt file. Place it in the project folder.
Modify the TransactionChecker class:
Make this class a derived class that extends the base class Observable.
In the while loop, replace the printf statement with code that creates a Transaction object. For the transaction id, use an autogenerated id that starts with 1001 and increases by 1 for each new Transaction object created.
After the Transaction object is created in the while loop, write an if statement that notifies the observers if a transaction is found where amount is greater than $50,000 and the buyer is Eugene Eko. Notify all observers by sending them the Transactionobject. To do this, use the Observable methods setChanged and notifyObservers.
Modify the TransactionLogger class:
Have it implement the Observable interface, which means that an update method is required.
Add an update method with this header:
Write the info object (which is actually a Transaction object) out to the log file. After that, flush the output buffer, which forces the info be written to disk. Don't forget the @Override statement, which indicates that you are either overriding a method from a base class or implementing a required method from an interface.
Write a TestTransactionChecker class. In a main method:
Declare and instantiate a new TransactionChecker object.
Declare and instantiate a new TransactionLogger object.
Add the TransactionLogger object as an observer to the TransactionChecker object.
Call the checkTransactions method of the TransactionChecker object.
Transaction class from project 4:
public class Transaction {
private int _id;
private String _buyer;
private String _seller;
private double _amount;
private String _timestamp;
public Transaction() {};
public Transaction(int _id, String _buyer, String _seller, double _amount, String _timestamp) {
this._id = _id;
this._buyer = _buyer;
this._seller = _seller;
this._amount = _amount;
this._timestamp = _timestamp;
}
public int getId() {
return _id;
}
public String getBuyer() {
return _buyer;
}
public String getSeller() {
return _seller;
}
public double getPrice() {
return _amount;
}
public String toString() {
return "_id: " + _id + " _buyer:" + _buyer + " _seller: " + _seller;
}
}
Explanation / Answer
package Proj4bShubham;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Observable;
import java.util.Scanner;
public class TransactionChecker extends Observable{
Scanner _in;
public TransactionChecker(String fileName)
throws FileNotFoundException {
_in = new Scanner(new File(fileName));
}
public void checkTransactions( ) {
int i = 1001;
while(_in.hasNextLine( )) {
String line = _in.nextLine( );
String[ ] fields = line.split(",");
Transaction trans = new Transaction(i,fields[0],fields[1],Double.parseDouble(fields[2]),fields[3]);
i++;
if (trans.getBuyer().equalsIgnoreCase("Eugene Eko") && trans.getPrice() > 50000 ) {
setChanged();
notifyObservers(trans);
}
}
_in.close( );
}
}
package Proj4bShubham;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Observable;
import java.util.Observer;
public class TransactionLogger implements Observer{
PrintWriter _log;
public TransactionLogger(String fileName)
throws FileNotFoundException {
_log = new PrintWriter(fileName);
}
@Override
public void update(Observable obs, Object info){
_log.println(info.toString());
_log.flush();
}
}
package Proj4bShubham;
public class TestTransactionChecker {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
TransactionChecker checker = new TransactionChecker("transactions.txt");
TransactionLogger logger = new TransactionLogger("logger.txt");
checker.addObserver(logger);
checker.checkTransactions();
}
catch(Exception e){
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.