6 Replies - 252 Views - Last Post: 24 February 2014 - 12:01 PM Rate Topic: -----

#1 dwbrown7680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-February 14

Having problems with an Inventory Program

Posted 23 February 2014 - 11:31 PM

Hello,

I am new here and am hoping to get some help if possible. I am currently writing a program for a Java class that I am in. I have done just fine for the first two iterations of this program but I seem to have run into a brick wall. This is what is required of me:

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

Modify the output to display this additional feature you have chosen and the restocking fee.


There is only one error supposedly in the code (although that could change once it is fixed). The line that is currently listing as a problem is line 23 under the Inventory class. I am assuming I am defining my subclass wrong maybe? There may be other issues I'm not seeing in the extended software subclass but I have not been able to get it to compile and have been staring at this for the last 5 hours and just want to be able to compile so I can work out whatever else may be missing from there.I thought I was moving along nicely in the class but I've been stuck on this for two days now. Any assistance is greatly appreciated!

package inventoryprogram;

/**
 * This class uses an array to manage the Inventory of software.
 * @author Dustin Brown
 */
public class InventoryProgram {

    Software[] inventory = new Software[10];
    int current = 0;

    public InventoryProgram(int len) {
        inventory = new Software[len];
    }

    public void addSoftware(Software sw) throws Exception {
        if (current < 10) {
            inventory[current++]=sw;
        }else {
            throw new Exception("Inventory is full. No items can be added.");
        }
    }
    /**
     * sorts the array by name.
     */
    public void sort() {
        int len = current; //current length of the array
        for (int i=0; i<len;i++) {
            for(int j=i;j<len;j++) {
                if (inventory[i].getsoftwareTitle().compareToIgnoreCase(inventory[j].getsoftwareTitle())>0) {
                    Software temp = inventory[j];
                    inventory[j] = inventory[i];
                    inventory[i] = temp;
                }
            }
        }
    }
    /**
     * Get the total inventory
     * @return
     */
    public double totalInventory() {
        double totalValue = 0;
        for(int i=0;i<current;i++) {
            Software sw = inventory[i];
            totalValue = totalValue + sw.getsoftwareValue();
        }
        return totalValue;
    }
    /**
     * Gets the number of items in the inventory
     * @return
     */
    public int getNumberOfItems() {
        return current;
    }

    public void printInventory() {
        System.out.println("Current Inventory:");
        for(int i=0;i<current;i++) {
            System.out.println(inventory[i]);
        }
        System.out.println("The total value of the inventory is:" + totalInventory());
    }

}



package inventoryprogram;

/**
 * Author Dustin Brown
 * Week 6 Inventory Program part 2
 **/
public class Software
{
    //setting all of the necessary variables
    public String softwareTitle;
    public int softwareStock;
    public double softwarePrice;
    public int softwareItem;
    public double softwareValue;
          
    public Software(String softwareTitle, int softwareStock, double softwarePrice, int softwareItem)//constructor for the program 
    {
        this.softwareTitle = softwareTitle;
        this.softwareStock = softwareStock;
        this.softwarePrice = softwarePrice;
        this.softwareItem  = softwareItem;
    } 
 
    //everything below here are my setters and getters that I can make use of later
    public void setsoftwareTitle(String softwareTitle) 
    {
        this.softwareTitle = softwareTitle;
    } 
      
    public String getsoftwareTitle() 
    {
        return softwareTitle;
    } 
      
    public void setsoftwareStock(int softwareStock) 
    {
        this.softwareStock = softwareStock;
    } 
    
    public int getsoftwareStock() 
    {
        return softwareStock;
    } 
  
    public void setsoftwarePrice(double softwarePrice) 
    {
        this.softwarePrice = softwarePrice;
    } 
    
    public double getsoftwarePrice() 
    {
        return softwarePrice;
    } 
    
    public void setsoftwareItem(int softwareItem) 
    {
        this.softwareItem = softwareItem;
    } 
      
    public int getsoftwareItem() 
    {
        return softwareItem;
    } 
      
    public double getsoftwareValue() 
    {
        return softwarePrice * softwareStock;
    } 
 //making use of toString to convert the necessary variables into string format
 //so that I can use display them without erros when the program is ran.
 //also making use of String.format to try and make the output look clean
    @Override
   public String toString() 
   {
	String itemStr = "Item Number: " + String.format("%-3s" + "", softwareItem);
	String titleStr = "Name: " + String.format("%-17s" + "", softwareTitle);
	String stockStr = "Units In Stock: " + String.format("%3d" + "", softwareStock);
	String priceStr = "Unit Price: " + String.format("$ %8.2f" + "", softwarePrice);
	String valueStr = "Value: "+ String.format("$ %8.2f", getsoftwareValue());
	return titleStr + stockStr + priceStr + itemStr + valueStr; 
        //this line sets up what is displayed after this information is returned   
   }
}//closes the Software class



package inventoryprogram;

import java.util.Scanner;

/**
 * @author Dustin Brown
 */
public class Inventory
{
    public static void main(String[] args) 
    {


        boolean finish = false;
        String softwareTitle;
        int softwareItem;
        int softwareStock;
        double softwarePrice;
        String softwareType;
        InventoryProgram inv = new InventoryProgram(10);
        ExtendedSoftware sw = new ExtendedSoftware ();
        while (!finish) {
            Scanner input = new Scanner(System.in); // Initialize the scanner
            System.out.print("Enter software name (or type stop to exit): ");
            softwareTitle = input.nextLine(); //read name from user
            if (softwareTitle.equals("stop")) 
            {
                System.out.println("Exiting Program...");
                finish = true; //program ends
            } 
            else 
            {
                System.out.print("Enter item number: ");
                softwareItem = input.nextInt();
                System.out.print("Enter the type of software:");
                softwareType = input.next();
                System.out.print("Enter the amount of software on hand: ");
                softwareStock = input.nextInt(); // read quantity from user
                while (softwareStock < 0) // prompt until positive value is entered
                {
                    System.out.append("Quantity must be non-negative number. Try again:");
                    softwareStock = input.nextInt(); // read item number again
                }
                System.out.print("Enter the price per each software: ");
                softwarePrice = input.nextDouble();
                while (softwarePrice <= 0) // prompt until positive value is entered
                {
                    System.out.print("Price must be positive number. Please enter the correct price:");
                    softwarePrice = input.nextDouble(); //read price again
                }
                input = new Scanner(System.in);
                try 
                {
                  inv.addSoftware(sw);
                }
                catch(Exception e) 
                {
                    System.out.println("Inventory is full.");
                }
            }//end else
        }
        inv.sort();
        inv.printInventory();
    }
}



package inventoryprogram;

/**
 * Extends the Software class and adds the softwareType attribute
 * @author Dustin Brown
 */
public class ExtendedSoftware extends Software 
{

   public String softwareType;
   public ExtendedSoftware(String softwareTitle, int softwareStock, double softwarePrice, int softwareItem, String sofwareType)
   {
       super(softwareTitle, softwareStock, softwarePrice, softwareItem);
       this.softwareType = softwareType;
   }
   public void setsoftwareType(String softwareType)
   {
       this.softwareType = softwareType;
   }
   public String getsoftwareType()
   {
       return softwareType;
   }
    //define getters and setters for the new variable

    @Override
    public String toString() 
    {
       	String typeStr = "Type: " + String.format("%-17s" + "", softwareType);
        //Adds the variable declared above to the returned string so that the new variable is printed.
        return super.toString()+typeStr;
    }

    @Override
    public double getsoftwareValue() 
    {
        double softValue = super.getsoftwareValue();
        return (softValue * 1.05);
    }
}



sorry it is line 21 in the 3rd block of code, not line 23.

Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with an Inventory Program

#2 zlifanar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-January 13

Re: Having problems with an Inventory Program

Posted 24 February 2014 - 03:30 AM

In your ExtendedSoftware class you are declaring the constructor as having 5 Arguments, but when you are attempting to instantiate it you aren't specifying any arguments. I would assume this is where the issue lies?

Cheers.
Was This Post Helpful? 0
  • +
  • -

#3 dwbrown7680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-February 14

Re: Having problems with an Inventory Program

Posted 24 February 2014 - 10:01 AM

View Postzlifanar, on 24 February 2014 - 03:30 AM, said:

In your ExtendedSoftware class you are declaring the constructor as having 5 Arguments, but when you are attempting to instantiate it you aren't specifying any arguments. I would assume this is where the issue lies?

Cheers.


What am I suppose to be specifying in those parenthesis then? Whenever I put anything inside of there the program runs but only outputs whatever is inside that set of parenthesis. I guess I am just a little confused. Thanks in advance for any possible help.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: Having problems with an Inventory Program

Posted 24 February 2014 - 10:08 AM

The constructor specifies very clearly what you're supposed to pass: public ExtendedSoftware(String softwareTitle, int softwareStock, double softwarePrice, int softwareItem, String sofwareType).

You need to pass (in the following order) values of the following types: String, int, double, int, String.
Was This Post Helpful? 0
  • +
  • -

#5 dwbrown7680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-February 14

Re: Having problems with an Inventory Program

Posted 24 February 2014 - 10:28 AM

View Postmacosxnerd101, on 24 February 2014 - 10:08 AM, said:

The constructor specifies very clearly what you're supposed to pass: public ExtendedSoftware(String softwareTitle, int softwareStock, double softwarePrice, int softwareItem, String sofwareType).

You need to pass (in the following order) values of the following types: String, int, double, int, String.


I am assuming I need to pass them prior to this point in the program? If I try to pass them in that parenthesis they never get overridden no matter what is input when the program prompts you. I get that I have to account for the 5 variables I have in my subclass but I guess I am just getting a little confused at this point. Sorry.

Thanks for any help,
Dustin
Was This Post Helpful? 0
  • +
  • -

#6 bcsmith8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-August 12

Re: Having problems with an Inventory Program

Posted 24 February 2014 - 11:07 AM

Here is what I believe you need to do.

In the Inventory class do the following.

-Cut line 21 and paste it in the try catch that starts at line 52.
-Add the user inputted parameters to the ExtendedSoftware object you are creating.

Here is what your try-catch block should look like after making the changes.

try{
     ExtendedSoftware sw = new ExtendedSoftware(softwareTitle,softwareItem,softwareType,softwareStock,softwarePrice);
     inv.addSoftware(sw);
}
catch(Exception e){
     System.out.println("Inventory is full");
}



This will pass the user values to the ExtendedSoftware object you create.

View Postbcsmith8, on 24 February 2014 - 01:03 PM, said:

Here is what I believe you need to do.

In the Inventory class do the following.

-Cut line 21 and paste it in the try catch that starts at line 52.
-Add the user inputted parameters to the ExtendedSoftware object you are creating.

Here is what your try-catch block should look like after making the changes.

try{
     ExtendedSoftware sw = new ExtendedSoftware(softwareTitle,softwareItem,softwareType,softwareStock,softwarePrice);
     inv.addSoftware(sw);
}
catch(Exception e){
     System.out.println("Inventory is full");
}



This will pass the user values to the ExtendedSoftware object you create.



After looking at your ExtendedSoftware Constructor, it looks like the order is not the same as what you read in.

The code should actually be:
try{
     ExtendedSoftware sw = new ExtendedSoftware(softwareTitle,softwareStock,softwarePrice,softwareItem,softwareType);
     inv.addSoftware(sw);
}
catch(Exception e){
     System.out.println("Inventory is full");
}


so it matches the constructor.

My apologies.
Was This Post Helpful? 0
  • +
  • -

#7 dwbrown7680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-February 14

Re: Having problems with an Inventory Program

Posted 24 February 2014 - 12:01 PM

Bcsmith 8 that did the trick! Program is working 100% properly now thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1