Hello friends,happy new year all..
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 class:(I 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...