Search code examples
javaoopuser-inputabstract

How to pass user input values to private variables in abstract classes (JAVA)


I'm trying a past paper question for Object Oriented Programming. I need to pass user input values to private variables in an abstract class. But since abstract classes cant be instantiated I cannot pass the user input for the variables in the abstract class "Room".So how do I pass the user input value to private variables in abstract class ? I do not know which room package type ( either standard or deluxe) user will select. so I cannot instantiate the child class either. Can anyone help ? NOTE : all variables should be accessed through set and get methods.

public abstract class Room{

  private  char roomType;
  private   int roomNumber;
  private  int numOfDays;
  private  char roomPackageType;
  
  public  void setRoomType(char roomType){
    this.roomType = roomType;
  }
  public char getRoomType(){
    return this.roomType;
  }
  
  public  void setRoomNumber(int roomNumber){
    this.roomNumber = roomNumber;
  }
  public  int getRoomNumber(){
    return this.roomNumber;
  }
  public  void setNumOfDays(int numOfDays){
    this.numOfdays = numOfDays;
  }
  public  int getNumOfDays(){
    return this.numOfdays;
  }
  public  void setRoomPackageType(char roomPackageType){
    this.roomPackageType = roomPackageType;
  }
  public  char getRoomPackageType(){
    return this.roomPackageType;
  }

  public abstract void callPayment(int numOfDays,String customerType,String roomPackageType);

}
public class Standard extends Room{
    
    public abstract void callPayment(int numOfDays,String customerType,String roomPackageType){
        //code
    }
}
public class Deluxe extends Room{
    
    public abstract void callPayment(int numOfDays,String customerType,String roomPackageType){
       //code
    }
}
import java.util.Scanner;
import java.util.*;

public class Q2{
  public static void main(String[] args){
    
    Customer c1 = new Customer();
    Scanner sc = new Scanner(System.in);
    
    System.out.println("Please specify whether you are a local customer(L) or a foreign customer(F)");
    char customerType = sc.next().charAt(0);
    if(customerType != 'L' && customerType != 'F')
      throw new InputMismatchException("Input must be either 'F' or 'L'");
    c1.setCustomerType(customerType);
    System.out.println("Enter your NIC number");
    String idNumber = sc.nextLine();
    c1.setIdNumber(idNumber);
    
    System.out.println("Enter the package you would like to purchase");
    char roomPackageType = sc.next().charAt(0);
    Room.setRoomPackageType(roomPackageType);
  }
}

Solution

  • You can try instantiating the child classes conditionally on the basis of roomPackageType being selected as abstract class methods can only be accessed through the classes that inherits it .

    System.out.println("Enter the package you would like to purchase('D' for Deluxe or 'S' for Standard)");
            char roomPackageType = sc.next().charAt(0);
            if(roomPackageType.equals ('D')) {
               Deluxe dlx = new Deluxe();
               dlx.setRoomPackageType(roomPackageType);
            }
            else if(roomPackageType.equals('S')) {
               Standard std = new Standard();
               std.setRoomPackageType(roomPackageType);
            }
           else{
                System.out.println("Invalid Room Package!");
             }