0

I want to Define class name MutiItemSale that represents a sale of multiple items of type Sale. The class MutiItemSale that have instance variable whose type is Sale[], which will be used as a partially filled array. There will also anther instance variable of type int that keep track of how much of this array is currently used . the exact detail on method and other instance variable.Use this class in program that obtains information for items of type sale and of type discountSale. And compute the total bill for the list of items sold.
the problem is i dont know how to write multiemsale class that take sale array . so please help. thank
this part of mycod ..........................................................

public class Sale
{
    private String name; //A nonempty string
    private double price; //nonnegative

    public Sale( )
    {
        name = "No name yet";
        price = 0;
    }

    /**
     Precondition: theName is a nonempty string; thePrice is nonnegative.
    */
    public Sale(String theName, double thePrice)
    {
        setName(theName);
        setPrice(thePrice);
    }

    public Sale(Sale originalObject)
    {
        if (originalObject == null)
        {
            System.out.println("Error: null Sale object.");
            System.exit(0);
        }
        //else
        name = originalObject.name;
        price = originalObject.price;
    }

    public static void announcement( )
    {
        System.out.println("This is the Sale class.");
    }

    public double getPrice( )
    {
        return price;
    }

    public void setPrice(double newPrice)
    {
        if (newPrice >= 0)
            price = newPrice;
        else
        {
            System.out.println("Error: Negative price.");
            System.exit(0);
        }
    }

    public String getName( )
    {
        return name;
    }

    /**
     Precondition: newName is a nonempty string.
    */
    public void setName(String newName)
    {
        if (newName != null && newName != "")
            name = newName;
        else
        {
            System.out.println("Error: Improper name value.");
            System.exit(0);
        }
    }

    public String toString( )
    {
        return (name + " Price and total cost = $" + price);
    }

    public double bill( )
    {
        return price;
    }

    /*
     Returns true if the names are the same and the bill for the calling
     object is equal to the bill for otherSale; otherwise returns false.
     Also returns false if otherObject is null.
    */
    public boolean equalDeals(Sale otherSale)
    {
        if (otherSale == null)
            return false;
        else
            return (name.equals(otherSale.name)
                && bill( ) == otherSale.bill( ));
    }

    /*
     Returns true if the bill for the calling object is less
     than the bill for otherSale; otherwise returns false.
    */
    public boolean lessThan (Sale otherSale)
    {
        if (otherSale == null)
        {
            System.out.println("Error: null Sale object.");
            System.exit(0);
        }
        //else
        return (bill( ) < otherSale.bill( ));
    }

    public boolean equals(Object otherObject)
    {
        if (otherObject == null)
            return false;
        else if (getClass( ) != otherObject.getClass( ))
            return false;
        else
        {
            Sale otherSale = (Sale)otherObject;
            return (name.equals(otherSale.name)
               && (price == otherSale.price));
        }
    }

    public Sale clone( )
    {
        return new Sale(this );
    }
}
------------------ and discountclass

public class DiscountSale extends Sale
{
    private double discount; 

    public DiscountSale( )
    {
        super( );
        discount = 0;
    }

    /**
     Precondition: theName is a nonempty string; thePrice is nonnegative;
     theDiscount is expressed as a percent of the price and is nonnegative.
    */
    public DiscountSale(String theName,
                                  double thePrice, double theDiscount)
    {
        super(theName, thePrice);
        setDiscount(theDiscount);
    }

    public DiscountSale(DiscountSale originalObject)
    {
        super(originalObject);
        discount = originalObject.discount;
    }

    public static void announcement( )
    {
        System.out.println("This is the DiscountSale class.");
    }

    public double bill( )
    {
        double fraction = discount/100;
        return (1 - fraction)*getPrice( );
    }

    public double getDiscount( )
    {
        return discount;
    }

    /**
     Precondition: Discount is nonnegative.
    */
    public void setDiscount(double newDiscount)
    {
        if (newDiscount >= 0)
            discount = newDiscount;
        else
        {
            System.out.println("Error: Negative discount.");
            System.exit(0);
        }
    }

    public String toString( )
    {
        return (getName( ) + " Price = $" + getPrice( )
                + " Discount = " + discount + "%\n"
                + "   Total cost = $" + bill( ));
    }

    public boolean equals(Object otherObject)
    {
        if (otherObject == null)
            return false;
        else if (getClass( ) != otherObject.getClass( ))
            return false;
        else
        {
            DiscountSale otherDiscountSale =
                             (DiscountSale)otherObject;
            return (super.equals(otherDiscountSale)
                && discount == otherDiscountSale.discount);
        }
    }

    public DiscountSale clone( )
    {
        return new DiscountSale(this );
    }
}

Edited by Dani: Code formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by javaAddict
0

First of all equalDeals does what equals does. So delete equalDeals.

Inside the MutiItemSale class you will define an array of Sale objects:
Sale [] sales = new Sale[100]; //the 100 is for this example, you can use what you want.
You can have a count int variable and when you "add" a new Sale object you will increase it.
Now pay attention: The sales array contains 100 null Sale objects. The sales is an array and it is not null BUT the sales[0] is a Sale object and it is null. If you want to create it do:
sales[0] = new Sale(); So you will use the count to determine how many new Sale() objects you have instantiated in the array and use that count as a limit to your for-loop.
You will have methods that take as argument Sale object and insert them in the array. Use the count as index and after you add a new Sale increase it
You will have a method that takes an int argument and returns the Sale object that is at that specific index in the array.
And the method that returns the bill will loop through the array (be careful to use the count variable as the limit) and take the sum of all the Sale objects from the array

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.