/*
 * $Author: bhevans $
 * $Date: 1996/06/05 21:32:43 $
 * $Source: /home/phoenix/cscstd/ijkl/jlarsen/box/RCS/ReportGenerator.java,v $
 * $Revision: 1.1 $
 * $Log: ReportGenerator.java,v $
# Revision 1.1  1996/06/05  21:32:43  bhevans
# Initial revision
#
# Revision 1.1  1996/05/26  22:46:42  bhevans
# Initial revision
#
 */

package com.nanosoft.pcm.machine;
import com.nanosoft.pcm.machine.*;
import java.util.*;

/**
 * The report generator creates a series of  logs  which
 * may be printed to any character device via the receiptPrinter.
 * Some logs are not used in the production model of the pcm machine
 * These include: 
 * 
    *
  1. InventoryLog *
  2. TransactionLogSet *
* Their definitions have remained, however, to ease the process of * improving the pcm software at a later date. * @author $Author: bhevans $ * @version $Revision: 1.1 $ * @see Printer * @see Log */ public class ReportGenerator extends Object { private Printer receiptPrinter; // output device private TransactionLog userLog; // user recpts /** * @param aPrinter The printer to which the report generator is to * send logs */ public ReportGenerator(Printer aPrinter) { super(); receiptPrinter = aPrinter; userLog = new TransactionLog(); } /** * This method is used whenever a tranaction with a customer * has finished * Implementation Notes: these currently use ints. ( not MoneyAmounts ) * @param change The change returned to the customer * @param received The cash received from the customer * @param print Whether or not the log should be sent to the printer */ public void printLog(int change, int received) { this.postTransaction(change,received,true); } public void clearLog() { this.postTransaction(0,0,false); } private void postTransaction(int change, int received, boolean print) // FINALIZES A TRANSACTION { userLog.post(change,received); if ( print ) { receiptPrinter.printLog(userLog); } userLog.clean(); // clean up for next run } /** * @param data records the sale of a particular item */ public void logTransaction(StorageItem data) // STORE COMMODITY INFO FOR RECEIPT / TRANS REPORT { userLog.add(data); } /** * This will be modified to handle MoneyAmounts once the * MoneyAmount API and use is nailed down. */ static public String intToCurr(int X) // STATIC FUNCTION TO CONVERT AN int X TO A CURRENCY STRING { String money = new String(); int front = X / 100; int hunds = X % 100; if (hunds == 0) { money = "$" + front + ".00"; } else { money = "$" + front +"."+ hunds; } return money; } }; // -------------------------------------------------- // // Transaction Log Class // // -------------------------------------------------- /** * The transaction log records an individual sale and * prepares the receipt for printing. */ class TransactionLog extends Log { private final int DLen = 26; // default len. for item description private int change; // $ change returned private int collected; // $ collected StringBuffer buf; // 'output' buffer private String Fill(String in) // PADS THE STRING TO DLEN SPACES ( Left Just ) { StringBuffer buf = new StringBuffer(); int index; buf.append(in); for ( index = in.length(); index < DLen; index++ ) { buf.append(" "); } return buf.toString(); } public TransactionLog() { super(); buf = new StringBuffer(); } /** * @param Change The change returned to the customer * @param Collection The money collected from the customer */ public void post(int Change, int Collection) { super.post(); change = Change; collected = Collection; } /** * @param obj The object to add to the log */ public void add(StorageItem obj) { super.add(obj); } /** * @return A header string for the printer */ public String headerString() { buf.append(super.addLine(super.stamp) ); buf.append(super.addLine("Number of purchases : " + new Integer(super.n))); return buf.toString(); } /** * Note: Destroys a portion of the log * @return A string for the body portion of the receipt to the printer */ public String dataString() { StorageItem S = (StorageItem)items.pop(); // S ==> (S)torageItem return super.addLine(" : " + this.Fill(S.getDescription()) + " : " + ReportGenerator.intToCurr(S.getPrice())); } /** * @return A footer string for the printer */ public String footerString() { return super.addLine("Collected : "+ReportGenerator.intToCurr(collected))+ super.addLine("Change : " + ReportGenerator.intToCurr(change)); } /** * Clears the contents of the log */ public void clean() { super.clean(); } }