I have created a Java Shopping Cart App. I have used InputStreamReader class for it. But its showing strange behavoiur. I have already tried Scanner class and Data Input Stream class. But they they do not seem to be fit for this app.
Can anyone please point out whats wrong with this class?
Also as already stated, Scanner class and DIS class tend to skip the user input, same as here while using ISR class ( see Output : Rate ). I am tired of trying every Java user input utility class, and modifying my code again and again.
import java.util.ArrayList;
//import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.Throwable;
public class NewShop {
protected ArrayList<NewItem> ItemList;
ArrayList<NewItem> cartList ;
// public DataInputStream dis = new DataInputStream(System.in);
// private Scanner sc = new Scanner(System.in);
private InputStreamReader isr = new InputStreamReader(System.in);
public void addItem() {
long aCode = 0;
String aName ="";
double aRate = 0;
int aQuantity = 0;
NewItem foundItem;
System.out.println("Enter Item code:");
/* try{
String adddisString = dis.readLine();}
catch(IOException e){e.printStackTrace();} */
try{
aCode = isr.read();
System.out.println("code entered is : " + aCode);
}
catch(IOException e){e.printStackTrace();}
foundItem = search(aCode);
if (foundItem == null) {
System.out.println("Item name : ");
try{
aName = dis.readLine();
}
catch(IOException e){e.printStackTrace();}
System.out.println("Rate : ");
try{ aRate = isr.read(); }
catch(IOException e){e.printStackTrace();}
System.out.println("Quantity : ");
try{aQuantity = isr.read();}
catch(IOException e){e.printStackTrace();}
NewItem aItem = new NewItem(aName, aRate, aCode, aQuantity);
ItemList.add(aItem);
}
else {
System.out.println("Item exists");
}
}
}
Output :
shachindratripathi@saurabh-OptiPlex-3010:~/NewJava$ java NewShoppingCart
New Shop for Items created.
-----ITEM------
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Issue item
6. Exit
Choice:
3
Enter Item code:
1
code entered is : 49
Item name :
apple
Rate :
Quantity :
30
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Issue item
6. Exit
Choice:
1
code name rate quantity
49 apple 10.0 51
************
1. Display all items
2. Search items
3. Add items to list
4. Add items to cart
5. Issue item
6. Exit
Choice: