public void addProduct (Vector <Stock> temp){
Scanner sc = new Scanner (System.in);
sc.useDelimiter("\n");
String name = "";
String qty = "";
String cost = "";
String qt = "";
String cos = "";
int qy = 0;
int cs = 0;
int q = 0;
int c = 0;
boolean v;
boolean match = false;
boolean added = false;
Stock st = null;
do{
System.out.println("Enter Product Name: ");
name = sc.next();
if((name.matches(valn)) && (temp.size() == 0)){
v = true;
do{
try{
System.out.println("Enter Quantity: ");
q = sc.nextInt();
System.out.println("Enter Cost: ");
c = sc.nextInt();
v = false;
}catch(InputMismatchException ie){
sc.next();
System.out.println ("Invalid Entry!");
}
}while(v);
qty = Integer.toString(q);
cost = Integer.toString(c);
Stock s1 = new Stock(name, qty, cost);
temp.add(s1);
added = true;
System.out.println ("Product was Added Succesfully!");
System.out.println ();
}else if((name.matches(valn)) && (temp.size() > 0)){
int l = 0;
for(int i = 0; i < temp.size(); i ++){
st = temp.elementAt(i);
if(name.equalsIgnoreCase(st.getName())){
l = i;
match = true;
}
}
if(match == true){
System.out.println ("Product Already Exists.");
System.out.println ("Note: Product will be Overwritten!");
System.out.println ();
v = true;
do{
try{
System.out.println("Enter Quantity: ");
qy = sc.nextInt();
System.out.println("Enter Cost: ");
cs = sc.nextInt();
v = false;
}catch(InputMismatchException ie){
sc.next();
System.out.println ("Invalid Entry! Please Try Again.");
}
}while(v);
if((qty.matches(valq)) && (cost.matches(valc))){
qt = Integer.toString(qy);
cos = Integer.toString(cs);
}
temp.elementAt(l).setQuantity(qt);
temp.elementAt(l).setCost(cos);
added = true;
System.out.println ("Product Overwritten Successfully!");
}else if (match != true){
v = true;
do{
try{
System.out.println("Enter Quantity: ");
q = sc.nextInt();
System.out.println("Enter Cost: ");
c = sc.nextInt();
v = false;
}catch(InputMismatchException ie){
sc.next();
System.out.println ("Invalid Entry! Please Try Again.");
}
}while(v);
qty = Integer.toString(q);
cost = Integer.toString(c);
Stock s1 = new Stock(name, qty, cost);
temp.add(s1);
added = true;
System.out.println ("Product was Added Succesfully!");
System.out.println ();
}
}else
System.out.println ("Invalid Name Input!");
}while(added != true);
}
When I am testing the program regardin entering a product name which is already stored, i.e match == true
if statement, everything works perfectly fine, but the quantity
and cost
are not saved. What could be the issue please?
Aucun commentaire:
Enregistrer un commentaire