1

So i am making a little program and having trouble setting the length of the String array to the size a user specifies, how are you mean to assign the value that a user enters to the array length? I have the main class for user input which passes onto a demonstration class to store and run a few different calculations etc. The input of mac_attendee_limit will be the size of the array

Main Class

public class ProgMgmtSys {

private static Scanner sc = new Scanner(System.in);

public static void displayMenu() {
    System.out.println("\nMoolort Heritage Railway Demonstration Booking System\n");
    System.out.println("Menu");
    System.out.println("A: Add Demonstration");
    System.out.println("B: Add Attendee");
    System.out.println("C: Print Demonstration");
    System.out.println("D: Print Attendee");
    System.out.println("E: Select & Print Cost");
    System.out.println("Q: quit");
    System.out.print("Please enter your selection: ");
}

public static void addNewDemonstration(Demonstration demonstration) {

    String  identifier, title;
    double base_fee;
    int max_attendee_limit, start_time, duration;

    System.out.println("New Demonstration Creater");
    System.out.println("Enter an Identifier");
    identifier = sc.nextLine();
    System.out.println("Enter a Title");
    title = sc.nextLine();
    System.out.println("Enter the Base Fee");
    base_fee = sc.nextDouble();
    System.out.println("Enter the Macimum Attendee Limit");
    max_attendee_limit = sc.nextInt();
    System.out.println("Enter the Start Time");
    start_time = sc.nextInt();
    System.out.println("Enter the Duration");
    duration = sc.nextInt();

    demonstration.newDemonstration(identifier, title, base_fee, max_attendee_limit, start_time, duration);  

}

public static void newAttendeeBooking(Demonstration demonstration) {

    String attendeeName, attendeePhoneNumber, membershipType;

    System.out.println("New Attendee Booking");
    System.out.println("Enter Attendee Name: ");
    attendeeName  = sc.nextLine();
    System.out.println("Enter Attendee Phone Number: ");
    attendeePhoneNumber = sc.nextLine();
    System.out.println("Please enter discount type if applicable [concession, fsrs, arhs, mhr]: ");
    membershipType = sc.nextLine().toLowerCase();

    demonstration.newAttendee(attendeeName, attendeePhoneNumber, membershipType);

}

public static void selectDemonstrationsCost(Demonstration demonstration) {

    String discountType;

    System.out.println("Please Select the type of discount to see the cost for all demonstrations:");
    System.out.print("concession, fsrs, arhs, mhr");
    discountType = sc.nextLine().toLowerCase();

    demonstration.printDemonstrationsCost(discountType);

}


public static void main(String[] args) {
    String choice;
    Demonstration demonstration = new Demonstration();
    do {
        displayMenu();
        choice = sc.nextLine().toUpperCase();
        switch(choice) {
        case "A":
            addNewDemonstration(demonstration);
            break;
        case "B":
            newAttendeeBooking(demonstration);
            break;
        case "C":
            demonstration.printDemonstration();
            break;
        case "D":
            demonstration.printAttendee();
            break;
        case "E":
            selectDemonstrationsCost(demonstration);
            break;

        case "Q":
            System.out.println("Goodbye");
            break;
        default:
            System.out.println("Invalid selection. Please try again.");
        }
    } while (!choice.equals("Q"));      
}

}

Demonstration Class

public class Demonstration {

    private String  identifier;
    private String  title;
    private double base_fee;
    private int max_attendee_limit = 0;
    private int start_time;
    private int duration;
    private int newAttendee = 5;

    private String attendeeName[] = new String[max_attendee_limit];
    private String attendeePhoneNumber[] = new String[max_attendee_limit];
    private String membershipType[] = new String[max_attendee_limit];

    //Discount charges for all different societies & concession
    static final double concession_disc = .10; //10% Disc
    static final double fsrs_disc = .20; // 20% Discount
    static final double arhs_disc = .25; // 25% Discount
    static final double mhr_disc = 1.00; // 100% Discount - FREE

    private int i = 0;

    public Demonstration() {

    }

    public void newDemonstration(String identifier, String title, double base_fee, int max_attendee_limit, int start_time, int duration) {

        this.identifier = identifier;
        this.title = title;
        this.base_fee = base_fee;
        this.max_attendee_limit = max_attendee_limit;
        this.start_time = start_time;
        this.duration = duration;   

    }

    public void newAttendee(String attendeeName, String attendeePhoneNumber, String membershipType) {

        if (i < newAttendee) {

            this.attendeeName[i] = attendeeName;
            this.attendeePhoneNumber[i] = attendeePhoneNumber;
            this.membershipType[i] = membershipType;

            i++;
        }       
    }

    public void printDemonstration() {
        System.out.println("Identifier: " + identifier);
        System.out.println("Title: " + title);
        System.out.println("Base Fee: $" + base_fee);
        System.out.println("Maximum Attendee Limit: " + max_attendee_limit);
        System.out.println("Start Time: " + start_time);
        System.out.println("Duration: " + duration);

    }

    public void printAttendee() {

        for (int j=0;j< i;j++){

            System.out.println("Attendee Name: " + attendeeName[j]);
            System.out.println("Attendee Phone number: " + attendeePhoneNumber[j]);
            System.out.println("Membership Type: $" + membershipType[j]);

        }

    }
}

1 Answer 1

3

I think u need to initialize the array inside the function. It is because if u put it in global. When u create the class Demonstration demonstration = new Demonstration(); , the global array is created in this time. Then, although u change the max_attendee_limit, it will not change the size of the array.

u can do this:

public void newDemonstration(String identifier, String title, double base_fee, int max_attendee_limit, int start_time, int duration) {

        this.identifier = identifier;
        this.title = title;
        this.base_fee = base_fee;
        this.max_attendee_limit = max_attendee_limit;
        this.start_time = start_time;
        this.duration = duration;   

        attendeeName = new String[max_attendee_limit];
        attendeePhoneNumber = new String[max_attendee_limit];
        membershipType = new String[max_attendee_limit];
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.