The object referenced from the invoice class adds a new entry to my ArrayList and is working fine, but now because it’s in a code block nothing else outside of it can be referenced to the object(invoice) or else I have to define the constructors again which I am unable to do without breaking my program. and I’m not sure how to fix this. I guess the question is how do I reference my class object outside that code block without disturbing what I already have. Or do I need to change anything else?
boolean isRunning = true;
List<Invoice> price = new ArrayList<>();
while(isRunning) {
try {
System.out.println();
System.out.println("Please enter the name of your item: (type quit to exit) ");
String item = input.next();
if(item.equalsIgnoreCase("quit")) {
break;
}
System.out.println("Please enter the price of the item to be added to the invoice. ");
double total = input.nextDouble();
price.add(new Invoice(item, total));
} catch (InputMismatchException e) {
System.out.println("Exception caught!");
}
}
double total = 0;
for(Invoice itemization : price) {
total += itemization.getTotal();
System.out.println();
System.out.println("Item: " + itemization.getItem() + "\nPrice: " + itemization.getTotal());
System.out.println("Total: " + Math.round(total * 100.00) / 100.00);
}
import java.util.*;
public class Invoice implements Balance {
Scanner input = new Scanner(System.in);
private double total;
public String item;
public Invoice(String item, double total) {
}
public String getItem() {
return item;
}
public double getTotal() {
return total;
}