javaconstructormultiple-constructors

Java using default constructor


I have hit a little problem with constructors at my program. It is a simple bank database which stores customer data. I have to implement methods for depositing, withdrawing and transferring cash between two accounts. I have implemented that kind of constructor to add new bank account:

public Customer() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter id of customer:");
        this.id = scan.nextLine();
        File folder = new File("CustomerDataBase" + File.separator + this.id + ".txt");
        if(folder.exists()) {
            System.out.println("Customer ID already exists!");
            System.exit(1);
        }  
        try {      
            System.out.println("Enter name of customer:");
            this.name = scan.nextLine();
            System.out.println("Enter surname of customer:");
            this.surname = scan.nextLine();
            System.out.println("Enter PESEL number of customer:");
            this.pesel = scan.nextLine();           
            System.out.println("Enter address of customer:");
            System.out.println("    Street:");
            this.adressStreet = scan.nextLine();
            System.out.println("    City:");
            this.adressCity = scan.nextLine();
            System.out.println("    Zip Code: ");
            this.zipCode = scan.nextLine();
            System.out.println("Enter funds of Customer:");
            this.funds = Double.parseDouble(scan.nextLine());
            this.saveCustomer();
            scan.close();
        }catch(NumberFormatException e) {
            System.out.println("Error : " + e);
            System.exit(1);
        }   
    }

And then I have a method withdraw :

static void withdraw (int amount, int id) {

    File f = new File("CustomerDataBase" + File.separator + String.valueOf(id) + ".txt");
    Scanner fRead;

    Customer tempCustomer = new Customer();

    try{
        fRead = new Scanner(f);
        tempCustomer.id = fRead.nextLine();
        tempCustomer.name = fRead.nextLine();
        tempCustomer.surname = fRead.nextLine();
        tempCustomer.pesel = fRead.nextLine();
        tempCustomer.adressStreet = fRead.nextLine();
        tempCustomer.adressCity = fRead.nextLine();
        tempCustomer.zipCode = fRead.nextLine();
        tempCustomer.funds = Double.parseDouble(fRead.nextLine()) - id;
        fRead.close();
        tempCustomer.saveCustomer();
    }catch(FileNotFoundException e) {
        System.out.println("File not found!");
        System.exit(1);
    }   
}

The withdraw method reads data from file and have to stores it in class. So I'm creating object as customer type. But I want to use just "plain" (default) constructor which Java provides when you do not declare your own. How to do that? I read about super(): statement, but if I understood it right, it works only when you inherit from another class.


Solution

  • Every class comes with a default constructor that is not visible in the class itself. However, please note that if you specify a constructor other than the default constructor, the default constructor can't be used, per @Rustam comment below. For example, let's say your Customer class looks like this:

    public class Customer {
    
        private String name;
    
        private String lastName;
    
        private age int;
    
        private String ssn;
    
        //default constructor that is NOT visible
        Customer()
        {
    
        }
    
        //other constructor given name and lastName
        Customer(name, lastName)
        {
            this.name = name;
    
            this.lastName = lastName;
        }
    
        //getters and setters
    
    }
    

    The constructor Customer() is created by default and it is not necessary for you to include in your class.

    You can then create a customer instance using the default constructor and then you would need to use setters to set the properties, like follows:

    public class Test {
    
        public static void main(String [] args){
    
            Customer c = new Customer();
    
            //setting parameters
            c.setName("Jose");
    
            c.setLastName("Mejia");
    
        }
    
    }