This discussion is archived
2 Replies Latest reply: Mar 9, 2013 4:54 PM by MackSix RSS

Need Help with this program

995573 Newbie
Currently Being Moderated
I am completely confused by this program. I am suppose to implement the three methods in bold but I have no clue where to begin. I have been doing research in the java book that came with my class but I cant seem to find the information I need from this book. The entire program is suppose to prompt the user for the cdName, itemNumber, numberOfItems, and the price of the cd. The program uses an array to store the information and then once the user enters stop the the informaton displays on the screen and calcuates the inventory value.
I am not looking for someone to answer this problem for me.  I am just completely confused and need an explanation.  This is an assignemnt for school.  I just need some advice on how to go about this._

import java.util.Scanner;

/**
*
* @author Harsh Marwaha
*/
public class InventoryProgramPart2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {


boolean finish = false;
String cdName;
int itemNum;
int quantity;
double price;
Inventory inv = new Inventory(10);
while (!finish) {
Scanner input = new Scanner(System.in); // Initialize the scanner
System.out.println("Enter CD name (or type stop to exit): ");
cdName = input.nextLine(); //read name from user
if (cdName.equals("stop")) {
System.out.println("Exiting Program...");
finish = true; //program ends
} else {
System.out.print("Enter item number: ");
itemNum = input.nextInt();
System.out.print("Enter quantity on hand: ");
quantity = input.nextInt(); // read quantity from user
while (quantity < 0) // prompt until positive value is entered
{
System.out.append("Quantity must be non-negative number. Try again:");
quantity = input.nextInt(); // read item number again
}
System.out.print("Enter price: ");
price = input.nextDouble();
while (price <= 0) // prompt until positive value is entered
{
System.out.print("Price must be positive number. Please enter the correct price:");
price = input.nextDouble(); //read price again
}

CDProduct cd = new CDProduct(cdName, itemNum, quantity, price);
try {
inv.addCDProduct(cd);
}catch(Exception e) {
System.out.println("Inventory is full.");
}
}//end else
}
inv.sort();
inv.printInventory();
}
}







/**
* This class uses an array to manage the Inventory of CDs.
* @author Harsh Marwaha
*/
public class Inventory {

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

public Inventory(int len) {
inventory = new CDProduct[len];
}

/**
* Add a CD to the inventory.
* @param cd
* @throws Exception
*/
*public void addCDProduct(CDProduct cd) throws Exception {*
*//********* Implement this method*
*}*
*/***
** sorts the array by cd name.*
**/*
*public void sort() {*
*//************ Implement this method*
*}*
*/***
** Get the total inventory*
** @return*
**/*
*public double calculateTotalInventory() {*
double totalValue = 0;
*//*********** implement this method*
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);
}
System.out.println("The total value of the inventory is:"+calculateTotalInventory());
}

}


/**
* A class that implements the product class for the CD product.
*
* @author Harsh Marwaha
*/
public class CDProduct {

private String name;
private int itemNum;
private int items;
private double price;

public int getItemNum() {
return itemNum;
}

public void setItemNum(int itemNum) {
this.itemNum = itemNum;
}

public int getItems() {
return items;
}

public void setItems(int items) {
this.items = items;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

/**
* Default constructor
*/
public CDProduct() {

}//end default constructor

/**
* Constructor with parameters
*
* @param name
* @param itemNum
* @param items
* @param price
*/
public CDProduct(String name, int itemNum, int items, double price) {
this.name = name;
this.itemNum = itemNum;
this.items = items;
this.price = price;
}//end constructor

public double getInventoryValue() {
return (items * price);
}

public String toString() {
return "Name:" + name + " Item:" + itemNum + " Items:" + items + " Price:" + price + " Value:" + getInventoryValue();
}
}

Edited by: 992570 on Mar 7, 2013 3:27 PM
  • 1. Re: Need Help with this program
    gimbal2 Guru
    Currently Being Moderated
    So what kind of help are you expecting? You're not asking much of a question, you're basically only stating "my book sucks, I don't understand this code, it needs to do some stuff". "guidance" is the job of a teacher, not a forum.

    If your book sucks, get a better one. Digitally if you can't spare the money. Amazon user reviews can point you to good ones. But remember that you also have the world wide web at your disposal, you can even learn large chunks of the Java language by just browsing Wikipedia. If you don't understand the code then you should be patient and spend more time on it. Perhaps you should let this particular code go for now and start from scratch, trying to create something that does what your homework assignment demands. I'm pretty sure you're having a case of information overload with all the code that is already there, starting from scratch will help you get your focus back.

    Btw: in the future post code using \
     tags to make it readable.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 2. Re: Need Help with this program
    MackSix Newbie
    Currently Being Moderated
    public void addCDProduct(CDProduct cd) throws Exception {
        inventory[what_goes_here?++] = what_goes_here?
    
    }
    
    public void sort() {
        CDProduct temp;
        for (int i = 0; i < what_goes_here?; i++) {
            for (int j = 1; j < what_goes_here? ; j++) {
                if (inventory[j] != what_goes_here?) {
                    int result = what_goes_here?.getName().compareToIgnoreCase(what_goes_here?);
                    if (result > 0) {
                        what_goes_here?
                    }
                }
            }
        }
    }
    
    public double calculateTotalInventory() {
        double totalValue = 0;
        for (int i = 0; i < what_goes_here?; i++) {
            if (inventory[i] != what_goes_here? && what_goes_here?.getItems() != what_goes_here?) {
                totalValue = totalValue + what_goes_here?;
            }
        }
        return what_goes_here?;
    }
    All you gotta do is figure out "what_goes_here?" and you have it. :)

    Edited by: MackSix on Mar 9, 2013 12:55 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points