Results 1 to 17 of 17
- 11-24-2011, 11:12 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Compare Abstract Class Values to User input?
I have created a abstract class called Pipe and then i have 5 different types of pipe. Each with values which differentiate it from the other types.
Java Code:public abstract class Pipe { protected boolean InnerInsulation, OuterReinforcement; protected int Colour; protected String[] grades; }
Java Code:public class Type1 extends Pipe { public Type1() { this.InnerInsulation = false; this.OuterReinforcement = false; this.Colour = 0; this.grades = new String[]{"1", "2", "3"}; } }
Java Code:public class Type2 extends Pipe { public Type2() { this.InnerInsulation = false; this.OuterReinforcement = false; this.Colour = 1; this.grades = new String[]{"2", "3", "4"}; } }
I now need to return the correct pipe type dependant on the user input. How would i go about making a 'Pipe Validator'. If the user values do not add up to qualify for a type of pipe, a message saying the order can not be completed.
Thanks.
If needed, here is the Order Class where all user inputs are collected:
Java Code:public class PipeOrder { boolean InnerInsulation, NoInsulation, OuterReinforcement, NoReinforcement, ChemicalResistance, NoResistance; int Colour, Grade, Quantity; double length, radius; String insulation, reinforcement, resistance; Scanner in = new Scanner(System.in); public PipeOrder() { System.out.println("What would you like the length of the pipe to be? (metres)"); while (true) { String UserLengthString = in.next(); try { length = Double.parseDouble(UserLengthString); if (length >= 0.1 && length <= 6.0) { break; } else if (length > 6.0 || length < 0.1) { System.out.println("We do not cater for the required pipe Length."); System.out.println("Please enter another value between 1 and 6:"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 6"); } } System.out.println("What would you like the radius of the pipe to be? (inches)"); while (true) { String UserRadiusString = in.next(); try { radius = Double.parseDouble(UserRadiusString); if (radius >= 1.0 && radius <= 20.0) { break; } else if (radius > 20.0 || radius < 1.0) { System.out.println("We do not cater for the required pipe Radius."); System.out.println("Please enter another value between 1 and 20:"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 20"); } } System.out.println("How many would you like?"); while (true) { String UserQuantityString = in.next(); try { Quantity = Integer.parseInt(UserQuantityString); if (Quantity >= 1 && Quantity <= 99) { break; } else if (Quantity >= 100 || Quantity <= 0) { System.out.println("We do not cater for the required pipe diameter."); System.out.println("Please enter another value between 5 and 50"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 99"); } } System.out.println("What grade do you require??"); while (true) { String UserGradeString = in.next(); try { Grade = Integer.parseInt(UserGradeString); if (Grade >= 1 && Grade <= 5) { break; } else if (Grade >= 6 || Grade <= 0) { System.out.println("You can only have a grade of 1 to 5"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 5"); } } System.out.println("How many colours do you require?"); while (true) { String UserColourString = in.next(); try { Colour = Integer.parseInt(UserColourString); if (Colour >= 0 && Colour <= 2) { break; } else if (Colour >= 3 || Colour < 0) { System.out.println("You can only have a maximum of 2 colours"); System.out.println("Please enter another value between 0 and 2"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 0 to 2"); } } } public boolean InnerInsulation() { System.out.println("Would you like inner insulation?"); while (true) { insulation = in.next(); InnerInsulation = (insulation.equalsIgnoreCase("Y") || insulation.equalsIgnoreCase("YES")); NoInsulation = (insulation.equalsIgnoreCase("N") || insulation.equalsIgnoreCase("NO")); if (InnerInsulation) { System.out.println("Insulation required"); return true; } else if (NoInsulation) { System.out.println("No insulation"); return false; } else { System.out.println("Please enter yes(y) or no(n)"); insulation = in.next(); } } } public boolean OuterReinforcement() { System.out.println("Would you like Outer Reinforcement?"); while (true) { reinforcement = in.next(); OuterReinforcement = (reinforcement.equalsIgnoreCase("Y") || reinforcement.equalsIgnoreCase("YES")); NoReinforcement = (reinforcement.equalsIgnoreCase("N") || reinforcement.equalsIgnoreCase("NO")); if (OuterReinforcement) { System.out.println("Reinforcement required"); return true; } else if (NoInsulation) { System.out.println("No Reinforcement"); return false; } else { System.out.println("Please enter yes(y) or no(n)"); reinforcement = in.next(); } } } public boolean ChemicalResistance() { System.out.println("Would you like Chemical Resistance?"); while (true) { resistance = in.next(); ChemicalResistance = (resistance.equalsIgnoreCase("Y") || resistance.equalsIgnoreCase("YES")); NoResistance = (resistance.equalsIgnoreCase("N") || resistance.equalsIgnoreCase("NO")); if (ChemicalResistance) { System.out.println("Reinforcement required"); return true; } else if (NoResistance) { System.out.println("No Resistance"); return false; } else { System.out.println("Please enter yes(y) or no(n)"); resistance = in.next(); } } } public double getUserLength() { return length; } public double getUserRadius() { return radius; } public int getUserQuantity() { return Quantity; } public int getUserGrade() { return Grade; } public int getUserColour() { return Colour; } }
- 11-25-2011, 12:53 AM #2
Re: Compare Abstract Class Values to User input?
If the user values do not add up to qualify for a type of pipe, a message saying the order can not be completed.
Can you show what data the user is entering and describe what tests should be made against each piece of data that the user entered.
- 11-25-2011, 01:08 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
That shows the different types of pipe and how each pipe is determined. Above i have shown how i am getting the user input and example of my abstract classes.
User data that effects pipe choice:
Grade - 1 to 5
Colour - 0 to 2 colours (actual colour does not need to be determined)
Outer Reinforcement - yes or no (boolean)
Inner Insulation - yes or no (boolean)
Chemical Resistance can be yes or no for any pipe and there for not used to help deterime the type of pipe.
User can not choose pipe type, they enter there own values and then i need to validate if what they require exists as a type of pipe. If not, return a error message.
Each type also refers to different pricing hence the need for different types.
- 11-25-2011, 01:26 AM #4
Re: Compare Abstract Class Values to User input?
I was asking what does the user enter: Strings or ints or ???
When you get all the user's input then you must validate each item.
Does the user enter something for each of the columns in your image?
It looks like you would need different tests for each type after you validate the type.
Start with the first type, see what code is needed for that and perhaps you can generalize those tests to be used for the other types and maybe not.
- 11-25-2011, 01:30 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
Int for colour and grade, String (boolean) for resistance, insulation and reinforcement.
User has to enter a value for each column apart from the type column.
Code in OP shows how i get input
- 11-25-2011, 01:45 AM #6
Re: Compare Abstract Class Values to User input?
Have you tried coding the data validation using condition tests in if statements?
- 11-25-2011, 01:52 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
I have considered it but was woundering isn't that to much code?
E.g.
if colour = 0 thn mor if statements to check all other values are correct. then same for colour 1 and 2.
Would there be a easier or more effient way to do this?
- 11-25-2011, 01:55 AM #8
Re: Compare Abstract Class Values to User input?
It depends on the data, if your valid numbers are contiguous, say 1 to 10, you could test if the user input is >= 1 AND <= 10.
In general you will have to test each value to be valid. That could be several if statements, one per user input value.
- 11-25-2011, 02:31 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
I could implement using if statements but then it would render my abstract class useless as i am just using if statements to determine the pipe, not the abstract classes.
How would i go about using the actual abstract type1, type2 etc..... classes to compare to user input?
- 11-25-2011, 02:37 PM #10
Re: Compare Abstract Class Values to User input?
Each type has its own set of valid data. Each type needs to validate that what the user entered is correct for that type.
What methods and data would be common to all the classes? They would be put in the base Pipe class.
The specifics for each type need to be in the Type class.
- 11-25-2011, 02:44 PM #11
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
Well there is nothing that is the same for any of the pipes. There is something different for each type.
Here is my main abstract class:
Java Code:public abstract class Pipe { protected boolean InnerInsulation, OuterReinforcement; protected int Colour; protected int[] grades; }
Java Code:public class Type1 extends Pipe { public Type1() { this.InnerInsulation = false; this.OuterReinforcement = false; this.Colour = 0; this.grades = new int[]{1, 2, 3}; } }
Java Code:public class Type2 extends Pipe { public Type2() { this.InnerInsulation = false; this.OuterReinforcement = false; this.Colour = 1; this.grades = new int[]{2, 3, 4}; } }
Java Code:public class Type3 extends Pipe { public Type3() { this.InnerInsulation = false; this.OuterReinforcement = false; this.Colour = 2; this.grades = new int[]{2, 3, 4, 5}; } }
Java Code:public class Type4 extends Pipe { public Type4() { this.InnerInsulation = true; this.OuterReinforcement = false; this.Colour = 2; this.grades = new int[]{2, 3, 4, 5}; } }
Java Code:public class Type5 extends Pipe { public Type5() { this.InnerInsulation = true; this.OuterReinforcement = true; this.Colour = 2; this.grades = new int[]{3, 4, 5}; } }
And since I can instantiate the abstract classes, im confused.
- 11-25-2011, 03:18 PM #12
Re: Compare Abstract Class Values to User input?
there is nothing that is the same for any of the pipes
All pipes have a type.
All pipes have a color.
And so on.
It's the values of these properties that are different. But all pipes have a set of properties in common.
The variables that hold those values are what goes into the base class.
Each type has different valid values for these common data items.
The code for each type needs to validate the values for its data items.
- 11-25-2011, 05:41 PM #13
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
I have solved the problem however i have occuered another 1 :/
I have the following code for the user to enter there data:
Java Code:public class PipeOrder { boolean InnerInsulation, NoInsulation, OuterReinforcement, NoReinforcement, ChemicalResistance, NoResistance; int Colour, Grade, Quantity; double length, radius; String insulation, reinforcement, resistance; Scanner in = new Scanner(System.in); public PipeOrder() { System.out.println("What would you like the length of the pipe to be? (metres)"); while (true) { String UserLengthString = in.next(); try { length = Double.parseDouble(UserLengthString); if (length >= 0.1 && length <= 6.0) { break; } else if (length > 6.0 || length < 0.1) { System.out.println("We do not cater for the required pipe Length."); System.out.println("Please enter another value between 1 and 6:"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 6"); } } System.out.println("What would you like the radius of the pipe to be? (inches)"); while (true) { String UserRadiusString = in.next(); try { radius = Double.parseDouble(UserRadiusString); if (radius >= 1.0 && radius <= 20.0) { break; } else if (radius > 20.0 || radius < 1.0) { System.out.println("We do not cater for the required pipe Radius."); System.out.println("Please enter another value between 1 and 20:"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 20"); } } System.out.println("How many would you like?"); while (true) { String UserQuantityString = in.next(); try { Quantity = Integer.parseInt(UserQuantityString); if (Quantity >= 1 && Quantity <= 99) { break; } else if (Quantity >= 100 || Quantity <= 0) { System.out.println("We do not cater for the required pipe diameter."); System.out.println("Please enter another value between 5 and 50"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 99"); } } System.out.println("What grade do you require??"); while (true) { String UserGradeString = in.next(); try { Grade = Integer.parseInt(UserGradeString); if (Grade >= 1 && Grade <= 5) { break; } else if (Grade >= 6 || Grade <= 0) { System.out.println("You can only have a grade of 1 to 5"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 1 to 5"); } } System.out.println("How many colours do you require?"); while (true) { String UserColourString = in.next(); try { Colour = Integer.parseInt(UserColourString); if (Colour >= 0 && Colour <= 2) { break; } else if (Colour >= 3 || Colour < 0) { System.out.println("You can only have a maximum of 2 colours"); System.out.println("Please enter another value between 0 and 2"); } } catch (NumberFormatException ex) { System.out.println("Please enter a number between 0 to 2"); } } } public boolean InnerInsulation() { System.out.println("Would you like inner insulation?"); while (true) { insulation = in.next(); InnerInsulation = (insulation.equalsIgnoreCase("Y") || insulation.equalsIgnoreCase("YES")); NoInsulation = (insulation.equalsIgnoreCase("N") || insulation.equalsIgnoreCase("NO")); if (InnerInsulation) { System.out.println("Insulation required"); return true; } else if (NoInsulation) { System.out.println("No insulation"); return false; } else { System.out.println("Please enter yes(y) or no(n)"); insulation = in.next(); } } } public boolean OuterReinforcement() { System.out.println("Would you like Outer Reinforcement?"); while (true) { reinforcement = in.next(); OuterReinforcement = (reinforcement.equalsIgnoreCase("Y") || reinforcement.equalsIgnoreCase("YES")); NoReinforcement = (reinforcement.equalsIgnoreCase("N") || reinforcement.equalsIgnoreCase("NO")); if (OuterReinforcement) { System.out.println("Reinforcement required"); return true; } else if (NoReinforcement) { System.out.println("No Reinforcement"); return false; } else { System.out.println("Please enter yes(y) or no(n)"); reinforcement = in.next(); } } } public boolean ChemicalResistance() { System.out.println("Would you like Chemical Resistance?"); while (true) { resistance = in.next(); ChemicalResistance = (resistance.equalsIgnoreCase("Y") || resistance.equalsIgnoreCase("YES")); NoResistance = (resistance.equalsIgnoreCase("N") || resistance.equalsIgnoreCase("NO")); if (ChemicalResistance) { System.out.println("Reinforcement required"); return true; } else if (NoResistance) { System.out.println("No Resistance"); return false; } else { System.out.println("Please enter yes(y) or no(n)"); resistance = in.next(); } } } public double getUserLength() { return length; } public double getUserRadius() { return radius; } public int getUserQuantity() { return Quantity; } public int getUserGrade() { return Grade; } public int getUserColour() { return Colour; } }
- 11-25-2011, 05:43 PM #14
Re: Compare Abstract Class Values to User input?
What are the names of the methods that are not called and where are they called from?
Giving the same name to variable and to a method is a poor technique.
The names of methods should indicate some action and are usually like a verb.
Also their names should begin with a lower case letter.
Class names should begin with an upper case letter.Last edited by Norm; 11-25-2011 at 05:45 PM.
- 11-25-2011, 05:45 PM #15
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Compare Abstract Class Values to User input?
In my main class if i call th order class:
PipeOrder po = new PipeOrder();
The following will not execute from the order class:
public boolean InnerInsulation()
public boolean OuterReinforcement()
public boolean ChemicalResistance()
It ends after the user enters a colour input.
- 11-25-2011, 05:48 PM #16
Re: Compare Abstract Class Values to User input?
In most cases, if you do not call a method, it will not be executed.
It ends after the user enters a colour input.
- 11-25-2011, 05:49 PM #17
Re: Compare Abstract Class Values to User input?
Giving the same name to variable and to a method is a poor technique.
The names of methods should indicate some action and are usually like a verb.
Also their names should begin with a lower case letter.
Class names should begin with an upper case letter.
Similar Threads
-
How can I check/ compare the user input to the values in my array?
By mindofastudent in forum New To JavaReplies: 3Last Post: 09-14-2011, 09:07 PM -
Class is not abstract and does not override abstract method run(com.
By rgeurts in forum New To JavaReplies: 4Last Post: 04-14-2011, 12:42 PM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 03:36 PM -
How to read user input in hh:mm:ss without time class
By lbayne in forum New To JavaReplies: 1Last Post: 09-30-2010, 03:36 AM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 11:51 AM
Bookmarks