Results 1 to 17 of 17
  1. #1
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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;
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.
    There any many ways to validate user input. You could use an if test, you could do a lookup in a hash table or database.
    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.

  3. #3
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.

  5. #5
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Compare Abstract Class Values to User input?

    Have you tried coding the data validation using condition tests in if statements?

  7. #7
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.

  9. #9
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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. #11
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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;
    }
    These are the different types:

    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};
        }
    }
    So then using these extended classes and there values, I need to compare the 4 user inputs but I have no idea how to do so :/

    And since I can instantiate the abstract classes, im confused.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Compare Abstract Class Values to User input?

    there is nothing that is the same for any of the pipes
    That can not be true.
    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.

  13. #13
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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;
        }
    }
    However the last 3 boolean methods will not execute, After colour input is entered, program exits.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.

  15. #15
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default 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.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.
    If you want the program to do something then you will have to add code for the steps you want it to take.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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

  1. Replies: 3
    Last Post: 09-14-2011, 09:07 PM
  2. Replies: 4
    Last Post: 04-14-2011, 12:42 PM
  3. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  4. Replies: 1
    Last Post: 09-30-2010, 03:36 AM
  5. Replies: 6
    Last Post: 02-11-2009, 11:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •