I am developing a simple POS program. Which I have a couple loops to add the part information to a list (invoiceList), then all the parts ordered under another list (invoiceListMain) and then all the invoices ordered into a main list (invoiceLogList). So essentially I have List within List within List.
This works when I output the whole list in a print statement. But I need to be able to unpack the list.
[[738, [113, 5.0, 43.45], [248, 2.0, 12.78]], [459, [248, 10.0, 63.9], [113, 1.0, 8.69]]]
I get this error
ArrayListDemo.java:136: error: incompatible types: String cannot be converted to ArrayList for(ArrayList line : invoiceLogList)
when I do:
for(int i=0; i < invoiceLogList.size(); i++)
{
for(ArrayList<String> line : invoiceLogList)
{
System.out.print(" \n" + line.get(0));
}
}
Before negative ratings, please leave a reason why.
From researching, I think it has to do with how I had to compile all my list within list within list.
invoiceLogList.add(invoiceListMain.toString());
I had to add .toString() in order to get this to work. So my guess is I can't unpack this as an Array because it is no longer an array? It's a list? I'm not 100% sure.
Does anyone have advice on a better way or how to unpack the data within?
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class ArrayListDemo {
public static void main(String[]args){
//Part 1
ArrayList<String> part1 = new ArrayList<String>();
part1.add("06011");
part1.add("Castrol EDGE 0W20 Motor Oil - 1 qt");
part1.add("9.49");
//Part 2
ArrayList<String> part2 = new ArrayList<String>();
part2.add("248");
part2.add("Castrol EDGE 5W30 Motor Oil - 1 qt");
part2.add("6.39");
//Part 3
ArrayList<String> part3 = new ArrayList<String>();
part3.add("03050");
part3.add("Castrol Hypuron 10W30 Motor Oil - 1 gal");
part3.add("6.39");
//Part 4
ArrayList<String> part4 = new ArrayList<String>();
part4.add("113");
part4.add("Castrol Pyroplex Protection ES Multi-Purpose Grease - 14 oz");
part4.add("8.69");
//Part 5
ArrayList<String> part5 = new ArrayList<String>();
part5.add("020");
part5.add("Castrol GTZ ULTRACLEAN 5W20 Motor Oil - 6 gal");
part5.add("69.99");
//adding parts to main Part List
ArrayList<ArrayList<String>> mainPartList = new ArrayList<ArrayList<String>>();
mainPartList.add(part1);
mainPartList.add(part2);
mainPartList.add(part3);
mainPartList.add(part4);
mainPartList.add(part5);
//-----INVOICE INPUT----------------------------------------------------------------------------
//create a new scanner
Scanner input = new Scanner(System.in);
//Invoice Input
String a = "";
String invoiceNumber = "";
String p = "";
double q = 0.0;
double partPrice = 0.0;
double partSum = 0.0;
String partNum = "";
String partDescrip = "";
String c = "";
//Invoice Array
ArrayList<String> invoiceLogList = new ArrayList<>();//Stores all invoices created
do{//loop to create invoice
ArrayList<String> invoiceListMain = new ArrayList<>();//stores individual invoices
Random generator = new Random();//creates random invoice number
int sequence = generator.nextInt(1000);
invoiceListMain.add(String.format("%03d", sequence));//invoice number
System.out.print("Invoice #: " + sequence);
do{//loop to add parts to invoice
ArrayList<String> invoiceList = new ArrayList<String>();//stores part#, qty, sumTotal
System.out.print("\n\nPart Number: ");
p = input.next();
//fetch part info from array mainPartList
for(ArrayList<String> part : mainPartList)
{
if(part.contains(p))
{
for (int i = 0; i < 1; i++)
partNum = part.get(0);
partDescrip = part.get(1);
invoiceList.add(partNum);//add part# to list
System.out.print("\t" + partNum + " - " + partDescrip);
partPrice = Double.parseDouble(part.get(2));//grabs price of part in list
}
}
System.out.print("\nQuantity: ");
q = input.nextDouble(); //asks quantity sold
invoiceList.add(Double.toString(q));//add quantity to list
partSum = partPrice * q;
invoiceList.add(Double.toString(partSum));//add part sum to list
System.out.printf("\t $%.2f",partSum);
System.out.print("\n\n---Add to cart?: ");
c = input.next();
invoiceListMain.add(invoiceList.toString());//adding part order to mainList
}while(c.equalsIgnoreCase("Y"));
//Create Invoice Loop
System.out.print("\n\nCreate a new Invoice? ");
a = input.next();
invoiceLogList.add(invoiceListMain.toString());
}while(a.equalsIgnoreCase("Y"));
//Print all invoices
for(int i=0; i < invoiceLogList.size(); i++)
{
for(ArrayList<String> line : invoiceLogList)
{
System.out.print(" \n" + line.get(0));
}
}
}
}
invoiceLogList? What isinvoiceListMain?List<List<List<String>>>to aList<String>?toString(). Why did you need to do that?List<SomeType>whereSomeTypeis the class name.