Jump to content

Item<--->stock

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
TAboy24

TAboy24

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi pals...i worked harder on my stock and items classes i asked before...but still unable making the work together...
I'm trying writing classes Item to include int Quantity,String name and a new&different&immutable serial number will be produced automaticly for every item...(then being able inherating the Item for sub classes for types of food)
Also i'm trying build a proper class Stock enabling to manage the Items...having array of maximal size 100(not linked list!)...enabling some basic actions as:public void addItem (Item newItem)
public void removeItem (Item item)
public String toString ()
public void order (int x)-if quantity of the item less then x,it must be ordered...
I wrote the Item classI attached here the classes as well as ZIP)
import java.util.ArrayList;
import java.io.*;
import java.text.NumberFormat;
import java.util.Scanner;

public class Item {
private String _name;
private int _quantity;
private int serial;

public Item(int quantity, String name) {
if (quantity>0){
_name = name;
_quantity = quantity;}
else
System.out.println("Quantity must be more than 0");}
public Item (int quantity) {
if (quantity>0) {
Scanner getInput = new Scanner(System.in);
_quantity=quantity;
System.out.println("type a name of Item please");
_name = getInput.nextLine();
} else
System.out.println("Quantity must be more than 0");}

public String getName() {
return _name;}
public int getQuantity() {
return _quantity;}

public String toString () {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (_name + "\t" +_quantity + "\t");}

public void printItem() {
System.out.println("Item: "+_name);
System.out.println("quantity: "+_quantity);

}
}

and wrote Stock Class as:
import java.util.ArrayList;
import java.io.*;
import java.text.NumberFormat;
import java.util.Scanner;

import java.util.Scanner;
public class Stock {

final int MAX_SIZE=100;
private Item[] itemList=new Item[MAX_SIZE];
String itemName;
int itemQuantity ;
int i=0;
public Stock() {
while (i<MAX_SIZE){
System.out.println("add a new item name and quantity or type empty or 0 value to quit");
Scanner getInput = new Scanner(System.in);
itemName= getInput.nextLine();
itemQuantity= getInput.nextInt();


if ((itemName.length()==0) || (itemQuantity==0)||(i+itemQuantity>MAX_SIZE)){
System.out.println("Illegal Input!-input must be a valid string and more than 0 quantity and not exceed the maximal limitation");
break;}


}}


// Add item to stock
public void addItem (Item newItem) {
i++;
while ((newItem.getName()==null)|| (i>MAX_SIZE)) {
System.out.print("Illegal Input!");
break;}
itemList[i]=Item(newItem.getQuantity(),newItem.getName());

}

public void removeItem (Item item) {

for (int k;k<MAX_SIZE;k++) {
if (item==itemList[k])
itemList[i]=Item(0,"");}

}

public String toString () {
for (int k;k<MAX_SIZE;k++) {
system.out.println(itemList[k].getName+"/n"+"/n");
}

}}
1) So far i compiled the item class ok...but in Item i experience problems with the constructor during running time:public Item(int quantity, String name)...the String working badly...
2)the Stock class i cant bring to proper compilation especialy when getting the lines calling the constructor of Item...and generally i need your help there because i'm totaly lost...how to write it to be able co-working with Item?(i know its still writeen messy,sorry)

Thank you very much...

Attached Files


Edited by WingedPanther, 05 January 2009 - 07:20 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
1) what do you mean by the string working badly, can you give an example?
2) what error messages are you getting?
3) Please us code tags (the # button) when posting code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
TAboy24

TAboy24

    Newbie

  • Members
  • PipPip
  • 13 posts
Thanks,
the problem starting in running time of Item that its writes after the input that "cannot find symbol-viriable.."...so its problems in the constructor probably...and then the Stock when calling the Item's constructor having problems in the compilation...i just can't finfd why:(

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'd have to refresh myself on Java a bit more...
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Hey,

I just looked at your code. Your Item class compiles, but for your Stock class, heres some hints:

1) When you initialise a new variable by calling its own constructor you need to use the keywod new. ie:

String str = new String();

2) When you call a method you have to end the call with '()', otherwise the java is going to look for some instance variable with the name of your method instead.

3) Java is case sensitive. All classes should begin with an upper case. (hint: Even the one you call to print to the console.)

4) You shouldnt be printing anything to the console in a toString() method.

5) Look up how to write and initialise a for loop again(although where its wrong it isnt needed.)

HTH