• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please, I need help to compile  RSS feed

 
ruth angel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will send the original file and then what I have done so far.

we need to create a small Java program that loops through an array of toys ordered as a start to the inventory automation project. We can use a sample list of possible items. The items are for the inventory list are ball, bat, bear, car, doll, game, glove, playstation, and train.

In the sample program, keep a tally of the different items. If the item has already been mentioned, just increment that count as opposed to creating a new count for that item. If we have orders for more than 4 items can sometimes cause stock outages. To alert the shipping department of situations where outages might occur, we decide to put an asterisk (*) before any item for which the customer has requested a quantity of 5 or more.

At the end, print to the screen a summary of the items and their total count.

We can use this as the structure

public class ToysInventory {
// declare counters to keep track of the number of each type of toys
private �
// Toys in our inventory
// declare an array to hold our inventory of toys
private String [] toysInventory = �
// count the number of each type of toys in our inventory.
// This method uses the "length" method on the array to get the number
// of toys in the inventory.
public void countToys () {

}
// Print the name of each toy, followed by its count.
// identify toys whose count exceeds five with a "*".
public void printItems() {

}
// This is the main method of the class. It calls the methods of the class to
// perform its job.
public static void main (String [] args) {
// instantiate a class of "toysInventory", then call its methods:
// countToys and printItems.
ToysInventory collection = new ToysInventory ();

}
(Hint: Use myArray.length to determine how long the order input array is. Use System.out.print() to print a portion of an output line, and use System.out.println() to print a line and the carriage return to the next line. Make sure you handle the case where the inventory item description is not among those listed above � such as could happen with a typo or error.)
Here is your order input array: "bear," "train," "car," "ball," "doll," "ball," "train," "doll," "game," "train," "bear," "doll," "train," "car," "ball," "bat," "glove," "bat," "b," "doll," "bear," "ball," "doll," "bat," "car," "glove," "train," "doll," "bear"


This is what I came up with, but I dont know what to do with the counter and variable.

Can you help me?
Thank you
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Can you help me?

next time don't use ctrl-A to copy the contents of your friend's email
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Please don't post the same question to more than one forum; see our explanation here. I've deleted the other copy of this thread.

Now: what have you come up with? I see only the problem, but no start at a solution.
 
ruth angel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I am sorry. I forgot to put what I have done.


public class ToysInventory {
private String[] toysOrdered = { "bear", "train", "car", "ball", "doll",
"ball", "train", "doll", "game", "train", "bear", "doll", "train","car", "ball", "bat", "glove", "bat", "b", "doll", "bear", "ball", "doll", "bat", "car", "glove", "train", "doll", "bear" };

private String[] ToysInventory = { "ball", "bat", "bear", "car",
"doll","game", "glove", "playstation", "train" };

private int InventoryCounter[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//


;public static void main(String[] args) {//BEGIN main()

ToysInventory toys = new ToysInventory();

toys.countToys();


//EXIT PROGRAM
System.exit(0);
}//END main()

private void countToys() {

for (int orderedIndex = 0; orderedIndex < toysOrdered.length;

orderedIndex++) {
int found = 0; //set found to false
for (int inventoryIndex = 0; inventoryIndex <

ToysInventory.length; inventoryIndex++) {
if (toysOrdered[orderedIndex] ==

ToysInventory[inventoryIndex]) {


}//END if
}//END for
}
}
}//END ToysInventory



Somebody can help me to fix this mess? thank you very much
Ruth
 
ruth angel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The objective is to get loops which find the toys in the toysOrdered class and print out the quantities in the end. Also if there is an order for more than 4 items there needs to be an * with an error message.

The only thing is that I get confuse with the counter and the variable. I would like to be able to do this thing Thank you
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ruth angel:
...Somebody can help me to fix this mess? ...

Tip: Use Code Tags. (You can edit your own posts by cliking on the paper/pencil icon.)
 
Jay Suttiruttana
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the solution. You can cut and paste it in any text editor and javac it. I hope this is not one of your college or high school homework.


[ August 30, 2006: Message edited by: Ernest Friedman-Hill ]
 
Jay Suttiruttana
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an output (from the code previously posted):

% java ToysInventory
[order status]
bear = 4
train = 3
car = 4
ball = 3
*doll = 6
train = 2
*game = 5
[ August 31, 2006: Message edited by: Jay Suttiruttana ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really isn't such a mess. The approach you're taking is logical and workable. But ask yourself this: When you're counting a particular toy type...
  • Do you want to take an element of toysOrdered and compare it to every element in toysInventory (incrementing "found" whenever you find a match)? OR...
  • Do you want to take an element of toysInventory and compre it to every element in toysOrdered (incrementing "found" whenever you find a match)?
  • The answer to this will determine which loop should be on the outside, and which loop should be on the inside.

    Then you just need to figure out 2 things:
  • What should you put inside the body of the inner loop? That is, what exactly should happen when you find a match?
  • What should you put at the end of each outer loop iteration? That is, what exactly should happen when you've finished counting a particular type of toy?
  •  
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jay Suttiruttana:
    Here's the solution. You can cut and paste it in any text editor and javac it...

    Jay, did you miss this at the top of the forum?
    We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

    :roll:
     
    Jay Suttiruttana
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My bad. Please don't beat me like a redheaded step child. I'm still a rookie at this Java Ranch. Next time, I'll just provide hints.
     
    David O'Meara
    Rancher
    Posts: 13459
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please let us beat you? It wouldn't be any trouble, promise
    (thanks for your understanding)
     
    ruth angel
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi everybody, thank you very much for the tips. I will put my mind together tonight lets see if I can do it.

    This is not a school work. It is something that I found out on a website some time ago. Thank you

    That is my first time in this forum. I feel I can really learn something here with you guys.

    Ruth
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by ruth angel:
    ...It is something that I found out on a website some time ago...

    I've seen this in a few different places on the web, but I can't tell where it originally came from. Anyway, you're really close. Take a look at my suggestions above, and you should be able to fill in the pieces.
    Originally posted by ruth angel:
    ...The only thing is that I get confuse with the counter and the variable...

  • orderedIndex holds an int value.
  • toysOrdered references an array of String references.
  • toysOrdered[orderedIndex] references the String at index orderedIndex in the array.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!