my code is to read lines from a .txt and parse those inputs.
inputs are in this form <operation> <category> <name> <price> <quantity> <weight> <optional field1> <optional field2>
Electronics have 2 option fields is it fragile (F/NF) and the state it’s shipping to, groceries have 1 optional field perishable (P/NP). here’s a few example of line inputs
insert clothing shirt 20.50 1 1
insert electronics PS3 300 1 5.2 F NM
insert groceries cabbage 2.00 5 1 NP
Im thinking of writing a legalLine() method to process the errors in inputs
class sampleClass{
public static void isLegalLine(String lineInput){
Scanner s = new Scanner(lineInput);
String operation = null;
String category = null;
String name = null;
float price = 0;
int quantity = 0;
float weight = 0;
String opt1 = null;
String opt2 = null;
try{
operation = s.next();
category = s.next();
name = s.next();
price = Float.parseFloat(s.next());
quantity = Integer.parseInt(s.next());
weight = Float.parseFloat(s.next());
if (!operation.equalsIgnoreCase("insert")
&& !operation.equalsIgnoreCase("search")
&& !operation.equalsIgnoreCase("delete")
&& !operation.equalsIgnoreCase("update")
&& !operation.equalsIgnoreCase("print")) {
System.out.println("invalid operation");
}
// more validations for variables category, name, price, quantity, weight goes here
if(category.equalsIgnoreCase("electronics")){
try{
opt1 = s.next();
opt1 = s.next();
}catch(Exception e){
}
}else if(category.equalsIgnoreCase("groceries")){
opt1 = s.next();
}
}catch (Exception e){
//general catch for now
e.getMessage();
}
}
public static void main(String[] args)
{
FileReader freader = new FileReader(args[0]);
BufferedReader bfrReader = new BufferedReader(freader);
isLegalLine(bfrReader.readLine());
//program does calculation for total price of input
}
you see… I’ve gone through all the trouble setting each token to its respective variables in the isLegalLine(), how can I pass those information back to the main()? and is this a good design for processing input line errors?
Since you will need to process them as well, a good solution I see is this:
Create a Product class with three subclasses: clothing, electronics, and groceries
Then make your function
return the Product after parsing the line.