In: Computer Science
Writing cash register software is difficult. Beyond the required accuracy and being able to scan and look up prices, cash registers must calculate the sales taxes owed. But every locality sets its own sales tax rates and every item needs to be placed in a specific category for correct taxation. With both the rates and classifications needing to change, this makes it a perfect place for a Visitor.
the TaxVisitor class which creates the interface our visitors will be implementing.
the DelawareTaxVisitor class.
the the ErieCountyTaxVisitor class, and the the NJTaxVisitor class.
Delaware does not have any sales tax, but New Jersey and Erie County do. The latter two classes should increase the value of sales taxes when each method is called. The comments in each of those classes specify the sales tax to charge.
TaxVisitor class:
package edu.buffalo.cse116;
/**
* Interface implemented by Visitor classes. Each class defines a locality's tax regime. Client cash registers will use * the Vistor instance appropriate to their location and greatly simplify dealing with the sales tax complexities. * * @author Matthew Hertz */ public interface TaxVisitor { /** * Update sales tax information from the purchase of an item classified as groceries. This often receives favorable * tax status. * * @param amount Cost of the grocery item being purchased. */ public void salesTaxGrocery(double amount); /** * Update sales tax information from the purchase of an item classified as a prepared food. Since these are considered * luxuries, they often invoke the highest level of taxation. * * @param amount Cost of the prepared food item being purchased. */ public void salesTaxPreparedFood(double amount); /** * Update sales tax information from the purchase of an item classified as clothing. This often receives some tax * breaks, but this is even more highly dependent on the locality. * * @param amount Cost of the item of clothing being purchased. */ public void salesTaxClothing(double amount); }
the DelawareTaxVisitor class:
package edu.buffalo.cse116; /** * This class defines the sales tax rules for Delaware. Growing up in Maryland, the ads touted Delaware as "your home of * tax-free shopping." Because this is not really needed for reality, implement the class as if Delaware still has a 0% * tax rate. */ public class DelawareTaxVisitor implements TaxVisitor { }
the ErieCountyTaxVisitor class:
package edu.buffalo.cse116; /** * This class defines the sales tax rules for Erie County in New York State. New York State is even worse than many * places, as each county define their own sales tax rates that are included on top of the state sales tax. */ public class ErieCountyTaxVisitor implements TaxVisitor { /** Total of the sales taxes collected during this purchase. */ private double salesTaxes; /** The sales tax rate charged on all prepared foods and clothing. This does NOT get charged to groceries. */ private static final double ERIE_CO_TAX = 0.0475; /** The sales tax rate charged on all prepared food. This does NOT get charged to groceries or clothing. */ private static final double NY_STATE_TAX = 0.04; }
the NJTaxVisitor class:
package edu.buffalo.cse116; /** * This class defines the sales tax rules for New Jersey. The musical may say "[e]verything's legal in New Jersey", but * everything is also taxed at a consistent, relatively high rate. This does those basic calculations. */ public class NJTaxVisitor implements TaxVisitor { /** Total of the sales taxes collected during this purchase. */ private double salesTaxes; /** * The state sales tax rate charged on virtuall all purchases in the state. (There are a few, very rare exceptions * this homework should ignore.) */ private static final double STATE_TAX = 0.07; }
DelawareTaxVisitor.java
/**
* This class defines the sales tax rules for Delaware. Growing up
in Maryland, the ads touted Delaware as "your home of
* tax-free shopping." Because this is not really needed for
reality, implement the class as if Delaware still has a 0%
* tax rate.
*/
public class DelawareTaxVisitor implements TaxVisitor {
int saleTaxes;
@Override
public void salesTaxGrocery(double amount) {
// TODO Auto-generated method
stub
saleTaxes+=0;
}
@Override
public void salesTaxPreparedFood(double amount)
{
// TODO Auto-generated method
stub
saleTaxes+=0;
}
@Override
public void salesTaxClothing(double amount) {
// TODO Auto-generated method
stub
saleTaxes+=0;
}
}
ErieCountyTaxVisitor.java
/**
* This class defines the sales tax rules for Erie County in New
York State. New York State is even worse than many
* places, as each county define their own sales tax rates that are
included on top of the state sales tax.
*/
public class ErieCountyTaxVisitor implements TaxVisitor {
/** Total of the sales taxes collected during this purchase.
*/
private double salesTaxes;
/** The sales tax rate charged on all prepared foods and
clothing. This does NOT get charged to groceries. */
private static final double ERIE_CO_TAX = 0.0475;
/** The sales tax rate charged on all prepared food. This does
NOT get charged to groceries or clothing. */
private static final double NY_STATE_TAX = 0.04;
@Override
public void salesTaxGrocery(double amount) {
// TODO Auto-generated method stub
salesTaxes+=0;
}
@Override
public void salesTaxPreparedFood(double amount) {
// TODO Auto-generated method stub
salesTaxes+=(ERIE_CO_TAX+NY_STATE_TAX)*amount;
}
@Override
public void salesTaxClothing(double amount) {
// TODO Auto-generated method stub
salesTaxes+=ERIE_CO_TAX*amount;
}
public double getSalesTaxes() {
return salesTaxes;
}
}
NJTaxVisitor.java
/**
* This class defines the sales tax rules for New Jersey. The
musical may say "[e]verything's legal in New Jersey", but
* everything is also taxed at a consistent, relatively high rate.
This does those basic calculations.
*/
public class NJTaxVisitor implements TaxVisitor {
/** Total of the sales taxes collected during this purchase.
*/
private double salesTaxes;
/**
* The state sales tax rate charged on virtuall all purchases in the
state. (There are a few, very rare exceptions
* this homework should ignore.)
*/
private static final double STATE_TAX = 0.07;
@Override
public void salesTaxGrocery(double amount) {
// TODO Auto-generated method stub
salesTaxes+=STATE_TAX*amount;
}
@Override
public void salesTaxPreparedFood(double amount) {
// TODO Auto-generated method stub
salesTaxes+=STATE_TAX*amount;
}
@Override
public void salesTaxClothing(double amount) {
// TODO Auto-generated method stub
salesTaxes+=STATE_TAX*amount;
}
public double getSalesTaxes() {
return salesTaxes;
}
}