vendredi 18 septembre 2015

Wrong Grocery Items when I add Items to the cart

This is for my class assignment. I keep getting the wrong groceryitem in the output. How should I go about getting the right item when the code is ran?

Here is the output i am currently getting:

Shopper Name: Gary


ice cream:5 units at $4.25 per unit = $21.25

ice cream:5 units at $4.25 per unit = $21.25

----> Subtotal = $42.5

----> 7% tax = $2.975

---->Total = $45.475

Shopper Name: Sally


ice cream:2 units at $4.25 per unit = $8.5

ice cream:2 units at $4.25 per unit = $8.5

----> Subtotal = $17.0

----> 7% tax = $1.1900000000000002

---->Total = $18.19


Cart Class:

public class Cart
{
public String Name;
public int ItemNum;
public double tax;




public Cart(String ShopperName){
    Name = ShopperName;
}
public String getShopperName(){
    return Name;
}
public int getItemNumber(){
    return ItemNum;
}
public void addItem1(GroceryItem GroceryItem, int NumberItem){
    GroceryItem = GroceryItem;
    ItemNum = NumberItem;
}
public void addItem2(GroceryItem GroceryItem, int NumberItem){
    GroceryItem = GroceryItem;
    ItemNum = NumberItem;
}
public double getItemTotal(){
    double item_total = (double) (GroceryItem.getCost()*getItemNumber());
    return item_total;
}
public double getSubtotal(){
    double subtotal = (double) (getItemTotal() + getItemTotal());
    return subtotal;
}
public double getTaxTotal(){
    double tax = .07;
    double taxtotal = (double) (getSubtotal()*tax);
    return taxtotal;
}
public double getTotal(){
    double Total = (double) (getTaxTotal()+getSubtotal());
    return Total;
}
public void printReceipt(){
    System.out.println("Shopper Name: " + getShopperName());
    System.out.println("----------------------");
    System.out.println(GroceryItem.getName()+":"+ getItemNumber()+ " units at $" + GroceryItem.getCost()+" per unit"+ " = $"+ getItemTotal());
    System.out.println(GroceryItem.getName()+":"+ getItemNumber()+ " units at $" + GroceryItem.getCost()+" per unit"+ " = $"+ getItemTotal());
    System.out.println("----> Subtotal = $" + getSubtotal());
    System.out.println("----> 7% tax = $" + getTaxTotal() );
    System.out.println("---->Total = $" + getTotal());
    System.out.println("");
    System.out.println("");
    System.out.println("");
}
}

Driver Class:

/**
 * Driver for Cart and GroceryItem.
 */
public class Driver
{
public static void main(String[] args)
{
    // create grocery items
    GroceryItem item1 = new GroceryItem("milk", 3.39);
    GroceryItem item2 = new GroceryItem("eggs", 1.75);
    GroceryItem item3 = new GroceryItem("ice cream", 4.25);

    // create new carts
    Cart shopper1 = new Cart("Gary");
    Cart shopper2 = new Cart("Sally");


    // add items to first cart 
    shopper1.addItem1(item2, 1);        //1 "eggs" is being added
    shopper1.addItem2(item1, 5);        //5 "milk" are being added

    // add items to second cart
    shopper2.addItem1(item3, 2);        //2 "ice cream" are being added
    shopper2.addItem2(item2, 2);        //2 "eggs" are being added

    // print cart's receipt
    shopper1.printReceipt();
    shopper2.printReceipt(); 

}
}



from Newest questions tagged java - Stack Overflow http://ift.tt/1MuQcZa
via IFTTT

Aucun commentaire:

Enregistrer un commentaire