import java.io.*;
import java.text.DecimalFormat;
/**
* Driver to test the products, catalogue, invoice lines, and invoice
*
* @author Ron
* @version 1.0
*/
public class Driver
{
// class variables
private static ProductCatalogue pc ;
private static Invoice invoice ;
/**
* Create the product catalogue and an invoice.
* Ask the catalogue to display itself
* Ask the invoice how much it is worth
*
*/
public static void main(String[] args) throws IOException
{
// create the product catalogue
pc = new ProductCatalogue("Readme.txt");
// print the catalogue
pc.displayCatalogue();
// create an invoice
invoice = new Invoice();
// add lines to invoice
invoice.addLine(pc.getProduct("1"),10);
invoice.addLine(pc.getProduct("5"),5);
invoice.addLine(pc.getProduct("11"),15);
invoice.addLine(pc.getProduct("12"),2);
invoice.addLine(pc.getProduct("13"),15);
invoice.addLine(pc.getProduct("17"),2);
invoice.addLine(pc.getProduct("21"),5);
invoice.addLine(pc.getProduct("31"),11);
invoice.addLine(pc.getProduct("41"),8);
invoice.addLine(pc.getProduct("71"),2);
// report the invoice total cost
DecimalFormat formatter = new DecimalFormat("§##,##0.00");
double total = invoice.getTotal();
System.out.println("total for invoice= "+ formatter.format(total) );
}
}
import java.io.*;
/**
* The product catalogue is a collection of products the company has for sale.
*
* @author Ron
* @version 1.0
*/
public class ProductCatalogue
{
// instance variables
final int MAX_PRODUCTS = 100;
private int numberProducts;
private Product[] product = new Product[MAX_PRODUCTS];
private FileReader fr ;
private BufferedReader br ;
private String line;
/**
* Create the product catalogue. The catalogue is a collection
* of products read in from a source file.
*
* @param the name of the file containing product data
* in comma-separated format
*
*/
public ProductCatalogue(String sourceFile) throws IOException
{
fr = new FileReader(sourceFile);
br = new BufferedReader(fr);line = br.readLine();
int i=0;
while (line != null) // read lines until end-of-file
{
product[i] = getProductLine();
line = br.readLine();
i++; // assuming i doesn't exceed 99
}
br.close();
numberProducts = i;
}
/**
* Extract the fields from a line
*
* @return the product
*
*/
public Product getProductLine()
{
String productID = line.substring(0,line.indexOf(","));
line=line.substring(productID.length()+1,line.length());
//
String productName = line.substring(0,line.indexOf(","));
line=line.substring(productName.length()+1,line.length());
//
String supplierID = line.substring(0,line.indexOf(","));
line=line.substring(supplierID.length()+1,line.length());
//
String categoryID = line.substring(0,line.indexOf(","));
line=line.substring(categoryID.length()+1,line.length());
//
String productDescription = line.substring(0,line.indexOf(","));
line=line.substring(productDescription.length()+1,line.length());
//
String unitPrice = line.substring(0,line.indexOf(","));
line=line.substring(unitPrice.length()+1,line.length());
//
String unitsInStock = line.substring(0,line.indexOf(","));
line=line.substring(unitsInStock.length()+1,line.length());
//
String unitsOnOrder = line.substring(0,line.indexOf(","));
line=line.substring(unitsOnOrder.length()+1,line.length());
//
String reorderLevel = line.substring(0,line.indexOf(","));
line=line.substring(reorderLevel.length()+1,line.length());
//
String discontinued = line.substring(0,line.indexOf(","));
line=line.substring(discontinued.length()+1,line.length());
//
return new Product( productID, productName, supplierID,
categoryID, productDescription, unitPrice, unitsInStock,
unitsOnOrder, reorderLevel, discontinued );
}
/**
* Obtain the number of products in the catalogue
*
* @return the number of products
*
*/
public int getNumberProducts()
{
return numberProducts;
}
/**
* Get the product having a specific identifier. The array
* of products must be searched. The search ends when the
* specified product is found.
*
* @param the product id
* @return a product
*
*/
public Product getProduct(String productID)
{
boolean found= false;
int i= 0;
while(!found)
{
found = (product[i].getProductID().equals(productID));
i++;
}
return product[i-1]; // assumes the product will be found
// i.e. assumes a successful search
}
/**
* Display all products in the catalogue
*
*/
public void displayCatalogue()
{
for (int j=0; j<getNumberProducts(); j++)
{
System.out.println(product[j].getPrice()+" "+product[j].toString());
}
}
}
/**
* A product is something this company sells.
*
* @author Ron
* @version 1.0
*
*/
public class Product
{
// instance fields for a product
private String productID;
private String productName;
private String supplierID;
private String categoryID;
private String productDescription;
private String quantityPerUnit;
private String unitPrice;
private String unitsInStock;
private String unitsOnOrder;
private String reorderLevel;
private String discontinued;
/**
* Constructor for a Product
*/
public Product(String productID,String productName,String supplierID,
String categoryID, String productDescription, String unitPrice,
String unitsInStock,String unitsOnOrder,String reorderLevel,String discontinued )
{
this.productID=productID;
this.productName=productName;
this.supplierID=supplierID;
this.categoryID=categoryID;
this.productDescription=productDescription;
this.quantityPerUnit=quantityPerUnit;
this.unitPrice=unitPrice;
this.unitsInStock=unitsInStock;
this.unitsOnOrder=unitsOnOrder;
this.reorderLevel=reorderLevel;
this.discontinued=discontinued;
}
/**
* In this version, a product is represented by its
* identifier and its name
*
* @return product id + product name
*/
public String toString()
{
return productID+" "+productName;
}
/**
* Obtain the description for the product
*
* @return product description
*/
public String getProductDescription()
{
return productDescription;
}
/**
* Obtain the id for the product
*
* @return product id
*/
public String getProductID()
{
return productID;
}
/**
* Obtain the price for the product. Price must be converted to
* double from its string representation.
*
* @return product price
*/
public double getPrice()
{
return Double.parseDouble(unitPrice);
}
}
/**
* An invoice lists the products sold to a customer.
*
* @author Ron
* @version 1.0
*/
public class Invoice
{
final int MAX_LINES=10;
private InvoiceLine[] invoiceLines = new InvoiceLine[MAX_LINES];
private int numberLines;
/**
* Create the invoice
*
*/
public Invoice()
{
numberLines = 0; // number of lines on invoice
}
/**
* Get the total value of the invoice
*
* @return total value of invoice
*
*/
public double getTotal()
{
// total is the accumulation of prices over all lines
double total = 0.00;
for (int i=0; i<10; i++)
{
total += invoiceLines[i].getPrice();
}
return total;
}
/**
* Add a line to the invoice
*
* @param product - the product for the line
* quantity - the numnber of units for the line
*
*/
public void addLine(Product product, int quantity)
{
if (numberLines<MAX_LINES)
{
invoiceLines[numberLines] = new InvoiceLine(product, quantity);
System.out.println("product price = "+product.getPrice());
}
numberLines++;
}
}
/**
* An invoice line represents a product sold to a customer.
*
* @author Ron
* @version 1.0
*/
public class InvoiceLine
{
// instance variables
private Product product;
private int quantity;
/**
* Constructor for an invoice line
*
* @param product
* @param quantity
*/
public InvoiceLine(Product product, int quantity)
{
// initialise instance variables
this.quantity=quantity;
this.product = product;
}
/**
* Return the identifier for the product on this invoice line
*
* @return product id
*/
public String getProductID()
{
return product.getProductID();
}
/**
* Return the quantity for this invoice line
*
* @return quantity
*/
public int getQuantity()
{
return quantity;
}
/**
* Return the price for this invoice line
*
* @return price the price of the product
*/
public double getPrice()
{
return quantity*product.getPrice();
}
}
1