Home > Software design >  Singleton design pattern implementation with error "constructor courier in class courier cannot
Singleton design pattern implementation with error "constructor courier in class courier cannot

Time:01-26

Im trying to do a small implementation with the singleton design pattern for a courier service. The user would just be able to enter the information requested and also be able to view the temporary list. Im trying to implement it using the singleton design pattern but there seem to be more errors popping up.

I have two pages, the Main class which is digital vision and another class which is courier

Scomo

public class Scomo {

     

        
        
    public static void main(String[] args) {
        
             
                            
                        courier newcourier =  courier.getInstance();
            newcourier.method();
                        
            }
                        
                        
                        

    }

Courier

    import java.util.ArrayList;
import java.util.Scanner;


public class courier {
    
     public static boolean repeat = true;
    
    public static ArrayList<courier> courierlist = new ArrayList<courier>();
     private static courier Courier = new courier();
     
    private int customerID;
    private String customeraddress;
    private int customerno;
    private String productname;
    private String deliverystat;
      
   
    private courier() {
      
    }
public static courier getInstance(){

    return Courier;

}

    protected static void method(){
        
        while(repeat) {
            welcome();
            }
        
               
             }
        public static void welcome() {
            Scanner sc = new Scanner(System.in);
            System.out.println("Welcome To Digital vision Ltd");
            System.out.println("=========================");
            System.out.println("1.Enter new customer details\r\n2.View delivery status");
            System.out.println("=========================");
            System.out.println("Enter your choice :");
            String choice= sc.next();
            if(choice.equals("1")) {
            courierinstance();
            }else if(choice.equals("2")) {
            displaydelivery();
        
            }
            else {
            System.out.println("Please Enter a valid choice (1,2)");
            }
                }
                
                public static void courierinstance() { 
    
                 Scanner sc = new Scanner(System.in);
            System.out.println("Please provide the following data to add a new customer");
            System.out.println("Enter customer ID:");
            String customerID = sc.next();
                        System.out.println("Enter customer address:");
            String customeraddress = sc.next();
            System.out.println("Enter customer number:");
            String customerno = sc.next();
            System.out.println("Enter product name:");
            String productname = sc.next();
            System.out.println("Enter delivery status:");
            String deliverystat = sc.next();
                               
                        courier newcourier = new courier(customerID,customeraddress,customerno,productname,deliverystat);
            courierlist.add(newcourier);
            System.out.println("The customer has been registered Successfully"); 
    }
                
                public static void displaydelivery(){
                        
                            int couriercount = courierlist .size();
                if(couriercount  > 0) {
                System.out.println("Courier List");
                System.out.println("=============================");
                System.out.println("Product name   Delivery status ");
                System.out.println("===================================================");
                for(int i=0; i < couriercount ; i  ) {
                System.out.println(courierlist.get(i).productname   " "   courierlist.get(i).deliverystat);
                }
                System.out.println("=============================");
                }else {
                System.out.println("No customers are registered.");
                }
                        
                        
                        }
                        
                        
}

The errors I get are on the courier page and point directly to the code below.

courier newcourier = new courier(customerID,customeraddress,customerno,productname,deliverystat);

The error is "constructor courier in class courier cannot be applied to given types; required: no arguments found: String,String,String,String,String reason: actual and formal argument lists differ in length"

if i dont add the arguments i dont think the application will know which is for which since it shows as not used when i take them off

Im very confused as to how to fix this if anyone has any input on how i could correct this it would be very much appreciated.

Edit : Added with constructor

public static ArrayList<courier> courierlist = new ArrayList<courier>();
 private static courier Courier = new courier();
 
private int customerID;
private String customeraddress;
private int customerno;
private String productname;
private String deliverystat;
  
   
private courier(int customerID, String customeraddress, int customerno, String productname,String deliverystat) {
    
    this.customerID = customerID;
  this.customeraddress = customeraddress;
  this.customerno = customerno;
  this.productname = productname;
  this.deliverystat = deliverystat;
}

Error posed with line "private static courier Courier = new courier();" Error statement: "constructor courier in class courier cannot be applied to given types; required: int,String,int,String,String found: no arguments reason: actual and formal argument lists differ in length"

I tried adding arguments to it like so

 private static courier Courier = new courier(customeraddress, customerno, productname,deliverystat);

And got the error: non-static variable deliverystat cannot be referenced from a static context

CodePudding user response:

You've provided a private constructor that receives no parameters and are using that to create your "single instance".

You need to make another constructor that receives the five parameters you're trying to pass it:

public class courier {
        
    private int customerID;
    private String customeraddress;
    private int customerno;
    private String productname;
    private String deliverystat;      
   
    private courier() {
      
    }

    private courier(int customerID, String customeraddress, int customerno, String productname, String deliverystat) {
        this.customerID = customerID;
        this.customeraddress = customeraddress;
        this.customerno = customerno;
        this.productname = productname;
        this.deliverystat = deliverystat;
    }

}

The variable types that receive input from your scanner need to match the data types in your class.

You have:

System.out.println("Enter customer ID:");
String customerID = sc.next();
System.out.println("Enter customer address:");
String customeraddress = sc.next();
System.out.println("Enter customer number:");
String customerno = sc.next();
System.out.println("Enter product name:");
String productname = sc.next();
System.out.println("Enter delivery status:");
String deliverystat = sc.next();
                   
courier newcourier = new courier(customerID,customeraddress,customerno,productname,deliverystat);

So you're passing parameters that have this signature:

(String, String, String, String, String)

But the constructor has a signature of:

(int, String, int, String, String)

So you need to change your inputs to the same types.

Instead of:

String customerID = sc.next();

You'd use:

int customerID = sc.nextInt();
sc.nextLine(); // flush leftover return left in buffer

Note the type of "customerID" is now "int", AND "sc.next()" has been changed to "sc.nextInt()".

Same type of thing for customerno:

int customerno = sc.nextInt();
sc.nextLine();
  •  Tags:  
  • java
  • Related