简体   繁体   中英

JAVA Showing all elements in a index of a ArrayList

I am creating a menu-based program that generates invoices, this invoice is for monthly rent.
When the user selects a certain option, they are prompted for several user inputs which will be stored in variables(All doubles & 1 string). these user inputs are being stored in an ArrayList.

import java.util.Scanner;
public class BudgetInvoice {

    protected double monthlyRent;
    protected double waterBill;
    protected double energyBill;
    protected double carRent; 
    protected double internetRent;
    protected String invoiceID;
     
    Scanner myScan = new Scanner(System.in);
    public double getMonthlyRent() {
        return monthlyRent;
    }

    public void setMonthlyRent(double monthlyRent) {
        this.monthlyRent = monthlyRent;
    }

    public double getWaterBill() {
        return waterBill;
    }

    public void setWaterBill(double waterBill) {
        this.waterBill = waterBill;
    }

    public double getEnergyBill() {
        return energyBill;
    }

    public void setEnergyBill(double energyBill) {
        this.energyBill = energyBill;
    }

    public double getCarRent() {
        return carRent;
    }

    public void setCarRent(double carRent) {
        this.carRent = carRent;
    }

    public double getInternetRent() {
        return internetRent;
    }

    public void setInternetRent(double internetRent) {
        this.internetRent = internetRent;
    }

    public String getInvoiceID() {
        return invoiceID;
    }

    public void setInvoiceID(String invoiceID) {
        this.invoiceID = invoiceID;
    }
    
    public BudgetInvoice (double monthlyRent, double waterBill, double energyBill , double carRent, double internetRent, String invoiceID) 
    {
        this.monthlyRent = monthlyRent; 
        this.waterBill = waterBill;
        this.energyBill = energyBill;
        this.carRent = carRent;
        this.internetRent = internetRent;
        this.invoiceID = invoiceID;
    }

    public BudgetInvoice() {
        // TODO Auto-generated constructor stub
    }
    
    public String toString() {
        return "BudgetInvoice [monthlyRent=" + monthlyRent + ", waterBill=" + waterBill + ", energyBill=" + energyBill
                + ", carRent=" + carRent + ", internetRent=" + internetRent + ", invoiceID=" + invoiceID + ", myScan="
                + myScan + "]";
    }
}

you are not printing the result of the showInvoice() method. Be careful that you are returning a String and not printing inside the method itself.

"

Your method of showInvoice<\/code> should do just that (ie show the invoice). The print statement should be in that method and the method should have a return type of void<\/code> . Otherwise, you might want to rename the method to something more meaningful like getFormattedInvoice()<\/code>

Then you could just print the invoice by simply printing the object.

This will ensure your method signature matches the signature of the overridden method. Doing so can often help prevent debugging issues by catching improper signatures at compile time.

import java.util.ArrayList;
public class Tester {
    public static void main(String[] args) {
        
        InvoicerHub myInvoice = new InvoicerHub();
        
        myInvoice.generateInvoice(myInvoice);
        System.out.println(myInvoice.showInvoice());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM