'Assign customer types and show only preferred customer

I am stuck with this program. I need it to create jobs, store distinct customer data, and display the preferred customers. What I have so far is below, when I run the code and enter a job it always stores the customer type as regular and name as null null, preferred customers dont print at all.

Sample I/O

  • Summary
  • Name: Joe Smith Customer Type: Preferred Customer Since: 2009
  • Size of lawn: 726 square meters
  • Cost for grass cutting: $10.45
  • Vehicles to wash: 1
  • Cost for washing: $18.00
  • Total cost: $28.45
  • Summary
  • Name: Barb MacDonald Customer Type: Senior
  • Size of lawn: 726 square meters
  • Cost for grass cutting: $9.87
  • Vehicles to wash: 0
  • Cost for washing: $0.00
  • Total cost: $9.87
public class Job {

    public static final String BUSINESS_NAME = "Will's Summer Services";
    public static final double COST_PER_SQUARE_METER = 0.016;
    public static final double COST_WASH_PER_VEHICLE = 20.0;

    public static final int LENGTH_MIN = 5;
    public static final int LENGTH_MAX = 100;
    public static final int LENGTH_DEFAULT = 100;
    public static final int WIDTH_MIN = 5;
    public static final int WIDTH_MAX = 50;
    public static final int WIDTH_DEFAULT = 50;
    public static final int VEHICLES_MAX = 3;

    private Customer customer;
    private int length;
    private int width;
    private int numberOfVehicles;
    private int area;
    private double costGrassCutting;
    private double costWashing;
    private double costTotal;

    //Constructors
    public Job() {
        customer = new Customer();
    }

    public Job(String name) {
        customer = new Customer(name);
    }

    public Job(String name, int length, int width, int numberOfVehicles) {
        customer = new Customer(name);
        this.length = length;
        this.width = width;
        this.numberOfVehicles = numberOfVehicles;
    }

    public void getInformation() {

        //Obtain the required information from the user
        Scanner input = new Scanner(System.in);

        System.out.println("\nPlease enter job information\n");
        int customerType = CisUtility.getInputInt("Customer type (0=regular 1=preferred 2=senior)?");
        Customer customer = new Customer();
        PreferredCustomer preferredCustomer = new PreferredCustomer();
        SeniorCustomer seniorCustomer = new SeniorCustomer();

        while (customerType != 0 && customerType != 1 && customerType != 2) {
            customerType = CisUtility.getInputInt("Error - Please try again.\nCustomer type (0=regular 1=preferred 2=senior)?");

            switch (customer.getCustomerTypeDesctipion()) {
                case "0":
                    customer.getCustomerTypeDesctipion();
                    break;

                case "1":
                    preferredCustomer.getCustomerTypeDesctipion();
                    preferredCustomer.getDiscountRate();
                    preferredCustomer.getInformation();
                    break;

                case "2":
                    seniorCustomer.getCustomerTypeDescription();
                    seniorCustomer.getDiscountRate();
                    break;

            }
        }

        //Get the customer information.
        customer.getInformation();

        System.out.println("What is the length of the lawn(meters)?");
        length = input.nextInt();
        input.nextLine();  //burn

        while (length < LENGTH_MIN || length > LENGTH_MAX) {
            System.out.println("Invalid length entered, please try again");
            System.out.println("What is the length of the lawn(meters)?");
            length = input.nextInt();
            input.nextLine();  //burn
        }

        System.out.println("What is the width of the lawn(meters)?");
        width = input.nextInt();
        input.nextLine();  //burn

        while (width < WIDTH_MIN || width > WIDTH_MAX) {
            System.out.println("Invalid width entered, please try again");
            System.out.println("What is the width of the lawn(meters)?");
            width = input.nextInt();
            input.nextLine();  //burn
        }

        System.out.println("How many vehicles to wash?");
        numberOfVehicles = input.nextInt();
        input.nextLine();  //burn

        while (numberOfVehicles > VEHICLES_MAX) {
            System.out.println("Invalid number of vehicles entered, please try again");
            System.out.println("How many vehicles to wash?");
            numberOfVehicles = input.nextInt();
            input.nextLine();  //burn
        }
    }

    /**
     * Calculate the overall cost. Also set the related cost attributes.
     
     */
    public double calculateCost() {

        double discount = customer.getDisountRate();

        //Calculations 
        area = length * width;
        costGrassCutting = COST_PER_SQUARE_METER * area * (1 - discount);
        costWashing = numberOfVehicles * COST_WASH_PER_VEHICLE * (1 - discount);
        costTotal = costGrassCutting + costWashing;
        return costTotal;

    }

    public Customer getCustomer() {
        return customer;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getNumberOfVehicles() {
        return numberOfVehicles;
    }

    public void setNumberOfVehicles(int numberOfVehicles) {
        this.numberOfVehicles = numberOfVehicles;
    }

    public int getArea() {
        return area;
    }

    public void setArea(int area) {
        this.area = area;
    }

    public double getCostGrassCutting() {
        return costGrassCutting;
    }

    public void setCostGrassCutting(double costGrassCutting) {
        this.costGrassCutting = costGrassCutting;
    }

    public double getCostWashing() {
        return costWashing;
    }

    public void setCostWashing(double costWashing) {
        this.costWashing = costWashing;
    }

    public double getCostTotal() {
        return costTotal;
    }

    public void setCostTotal(double costTotal) {
        this.costTotal = costTotal;
    }

    public void display() {
        //Show summary
        System.out.println(this.toString());
    }

    public String toString() {
        String output = "---------------------------------" + System.lineSeparator()
                + "- Summary " + System.lineSeparator()
                + "- " + customer.toString() + System.lineSeparator()
                + "- Size of lawn: " + area + " square meters" + System.lineSeparator()
                + "- Cost for grass cutting: " + CisUtility.getCurrency(costGrassCutting) + System.lineSeparator()
                + "- Vehicles to wash: " + numberOfVehicles + System.lineSeparator()
                + "- Cost for washing: " + CisUtility.getCurrency(costWashing) + System.lineSeparator()
                + "- Total cost: " + CisUtility.getCurrency(costTotal) + System.lineSeparator()
                + "---------------------------------" + System.lineSeparator() + System.lineSeparator();
        return output;
    }

}
public class PreferredCustomer extends Customer {

    public static final double DISCOUNT_PREFERRED = 0.10;
    private int customerSince;

    @Override
    public String getCustomerTypeDesctipion() {

        return "Preferred";
    }

    public double getDiscountRate() {
        return DISCOUNT_PREFERRED;

    }

    @Override
    public void getInformation() {
       this.customerSince = CisUtility.getInputInt("When did this person become a customer?");
       super.getInformation();

    }

    @Override
    public String toString() {
        return super.toString() + "Since: " + customerSince;
    }

    public int getCustomerSince() {
        return customerSince;
    }

    public void setCustomerSince(int customerSince) {
        this.customerSince = customerSince;
    }
    

}
public class SeniorCustomer extends Customer {
    
    public static final double DISCOUNT_SENIOR = 0.15;
    
    public String getCustomerTypeDescription() {
       
        return "Senior";
       
        
    
}
    public double getDiscountRate(){
        
    
        return DISCOUNT_SENIOR;
        
    
}
    
    
}
public class Customer {

    private String firstName;
    private String lastName;

    public static final int TYPE_PREFERRED = 1;
    public static final int TYPE_SENIOR = 2;

    public Customer() {
    }

    /**
     * Constructor that accepts first and last name as one string
   
     */
    public Customer(String name) {
        setFirstAndLastName(name);
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public double getDisountRate() {
        double discount = 0;

        return discount;
    }

    /**
     * Get the description based on the customer type
     *
     * @since 20210608
     * @author BJM
     */
    public String getCustomerTypeDesctipion() {

        return "regular";
    }

    /**
     * Get the first and last name out of the one string.Note that it is
     * expected to have exactly two parts. Note also that it is called from the
     * constructor which is why it is set to final (We'll get to this in
     * inheritance. It will work without final but give a warning.
     *
     * @param name Full name (first and last separated by space)
     * @since 20210608
     * @author BJM
     */
    public final void setFirstAndLastName(String name) {
        String[] parts = name.split(" ");
        if (parts.length >= 2) {
            firstName = parts[0];
            lastName = parts[1];
        }
    }

    public void getInformation() {

        this.firstName = CisUtility.getInputString("First Name?");
        this.lastName = CisUtility.getInputString("Last Name?");

    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void display() {
        CisUtility.display(this.toString());
    }

    public String toString() {
        String output = "Name: " + firstName + " " + lastName + " Customer Type: " + getCustomerTypeDesctipion();
        return output;
    }
}


Solution 1:[1]

You have a lot of problems with your code. Firstly, your Job class does more than one thing. Try separate the parsing of inputs to an own class. Secondly you fetch a lot of data without using it e.g.:

            case "0":
                customer.getCustomerTypeDesctipion();
                break;

            case "1":
                preferredCustomer.getCustomerTypeDesctipion();
                preferredCustomer.getDiscountRate();
                preferredCustomer.getInformation();
                break;

            case "2":
                seniorCustomer.getCustomerTypeDescription();
                seniorCustomer.getDiscountRate();
                break;

Every call on customer, preferredCustomer and seniorCustomer is not being used or printed, so why do you need it?

You are using a lot of while loops. Do you understand when to use them? Maybe you want to use if-statements instead?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valerij Dobler