Home > front end >  Calling Method To Add Parameters Into An Array From A Different Class
Calling Method To Add Parameters Into An Array From A Different Class

Time:12-01

So I'm writing this program it is meant to fill an array of bills with 3 types of information for 5 people. The main problem is that when I run the program it breaks after the information of the first person is entered. (keep in mind addBill is a method in a different class).

The code is down below. How could I fix it? Thank You.

//Code -- Driver Class

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    
    // Taking Doctor's Name And Capacity.
    System.out.println("Enter the doctor's name: ");
    String doctorName = input.nextLine();
    
    System.out.println("Enter the capacity: ");
    int capacity = input.nextInt();
    
    // Creating A billBook Object.
    // billBook information = new billBook(doctorName, capacity);
    billBook[] bills = new billBook[capacity];
    
    // Asking User To Enter Patient's Information.
    for(int i = 0; i < capacity; i  ){
        System.out.println();
        System.out.println("Bill "   (i 1));
        System.out.println("Enter the invoice number, patient's name, and visit date: ");
        String invoiceNumber = input.nextLine();
        String name = input.nextLine();
        input.nextLine();
        String visitDate = input.nextLine();
        input.nextLine();
        // bills[i].addBill(input.nextLine(), input.nextLine(), input.nextLine());
        bills[i].addBill(invoiceNumber, name, visitDate);
    }
}

//Code -- billBook Class

// Declaring Instances.
bill[] bills;
String Doctor;

// The Constructor Methods.
billBook(){}
billBook(String Doctor, int capacity){
    this.Doctor = Doctor;
    bill[] bills = new bill[capacity];
}

// The GET Methods.
public String getDoctor(){
    return Doctor;
}
public bill[] getBills(){
    return bills;
}

// The SET Methods.
public void setDoctor(String Doctor){
    this.Doctor = Doctor;
}

// The Add Bill Method.
public void addBill(String invoiceNumber, String patientName, String visitDate){
    for(int i = 0; i < bills.length; i  ){
        bills[i] = new bill(invoiceNumber, patientName, visitDate);
    } 
}

// The Pay Bill Method.
public int payBill(String invoiceNumber){
    for (int i = 0; i < bills.length; i  ){
        if(invoiceNumber.equals(bills[i].getInvoiceNumber())){
            bills[i].setPaid(true);
            System.out.println("Bill Is Paid.");
            return 1;
        }
        else{
            System.out.println("The Bill Is Not Found.");
        } 
    }
    return -1;
}

//Code -- bill Class

// Declaring instances.
private String invoiceNumber;
private String visitDate;
private String patientName;
private boolean paid;

// The Constructor Methods.
bill(){}
bill(String invoiceNumber, String visitDate, String patientName){
    this.invoiceNumber = invoiceNumber;
    this.visitDate = visitDate;
    this.patientName = patientName;
    paid = false;
}

// The GET Methods.
public String getInvoiceNumber(){
    return invoiceNumber;
}
public String getVisitDate(){
    return visitDate;
}
public String getPatientName() {
    return patientName;
}
public boolean isPaid() {
    return paid;
}

// The SET Methods.
public void setInvoiceNumber(String invoiceNumber) {
    this.invoiceNumber = invoiceNumber;
}
public void setPatientName(String patientName) {
    this.patientName = patientName;
}
public void setVisitDate(String visitDate) {
    this.visitDate = visitDate;
}
public void setPaid(boolean paid) {
    this.paid = paid;
}

// The Printing Method.
public void printInvoice(){
    if(isPaid()){
        System.out.println("Invoice Number: "   getInvoiceNumber()   
                "\nVisit Date: "   getVisitDate()   
                "\nPatient's Name: "   getPatientName()   
                "\nThe bill is paid.");
    }
    else{
        System.out.println("Invoice Number: "   getInvoiceNumber()   
                "\nVisit Date: "   getVisitDate()   
                "\nPatient's Name: "   getPatientName()   
                "\nThe bill is not paid.");
    }
} 

CodePudding user response:

You never initialize the members of billBook[] bills array, as well as the bills array in billBook class.

  • Related