Good Morning,
Having a problem with my program not calculating. I have wrote a program CashRegisterA Class that should be used with the RetailItem Class. The CashRegisterA Class should stimulate the sale of the retail item. It should have a constructor that accepts a RetailItem object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased.
CashRegisterA
import java.util.Scanner;
public class CashRegisterA
{
//Attributes
public double retailCost;
public int quantity;
final double TAX_RATE = 0.06;
double subtotal;
double Tax;
double total;
// File salesReceipt;
RetailItem myRetail = new RetailItem();
//Constructors
public CashRegisterA()
{
}
//default constructor
public CashRegisterA(int q)
{
quantity = q;
}
//Methods
public double getSubTotal()
{
subtotal = retailCost * quantity;
return subtotal;
}
public double getTax()
{
Tax = retailCost * TAX_RATE;
return Tax;
}
public double getTotal()
{
total = subtotal + Tax;
return total;
}
}
RetailItem
import java.util.Scanner;
public class RetailItem {
private String description;
private int unitsOnHand;
private double price;
public RetailItem(RetailItem i) {
this.description = i.description;
this.unitsOnHand = i.unitsOnHand;
this.price = i.price;
}
public RetailItem() {
description = null;
unitsOnHand = 0;
price = 0.00;
}
public RetailItem(String d, int u, double p) {
this.description = d;
this.unitsOnHand = u;
this.price = p;
}
public RetailItem(String d) {
this.description = d;
this.unitsOnHand = 0;
this.price = 0.00;
}
public RetailItem(int u) {
this.description = null;
this.unitsOnHand = u;
this.price = 0.00;
}
public RetailItem(double p) {
this.description = null;
this.unitsOnHand = 0;
this.price = p;
}
public void setDescription(String d) {
this.description = d;
}
public void setUnitsOnHand(int u) {
this.unitsOnHand = u;
}
public void setPrice(double p) {
this.price = p;
}
public String getDescription() {
return description;
}
public int getUnitsOnHand() {
return unitsOnHand;
}
public double getPrice() {
return price;
}
public String toString() {
return "Description: " + description + "\nUnits on hand: " + unitsOnHand + "\nPrice: $" + price;
}
}
Transaction
import java.util.Scanner;
import java.text.DecimalFormat;
public class Transaction
{
//Methods
public static void main(String[] args)
{
String itemName = " ";
double itemCost = 0.00;
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#0.00");
RetailItem myRetailItem = new RetailItem();
CashRegisterA myCashRegisterA = new CashRegisterA();
System.out.print("What is the name of the item being purchased? : " );
itemName = sc.nextLine();
System.out.print("How many " + itemName + "(s) are you buying? ");
myCashRegisterA.quantity = sc.nextInt();
System.out.println();
System.out.print("Subtotal: " + df.format(myCashRegisterA.getSubTotal()));
System.out.println();
System.out.print("Salestax: " + df.format(myCashRegisterA.getTax()));
System.out.println();
System.out.print("Total: " + df.format(myCashRegisterA.getTotal()));
}
}