Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My question is how do you turn the following output code i have into a proper on

ID: 3640800 • Letter: M

Question

My question is how do you turn the following output code i have into a proper one. It needs to look like a store receipt. Right now it kinda works the way it is but not always if the numbers were to be above 10000 then i would have a problem. I know with PrintF I can do something like this.

Here is the last line of code:

System.out.println("---------------FreshGreens's Produce----------------------");
System.out.println(date);
System.out.println("Cashier: Smith");
System.out.println("-----------------------------------------------------");
System.out.println("| Items |Quantity |Price |Total |" );

System.out.println("|---------------------------------------------------|");
for (Shop s : list)
{
System.out.println("|" + s.getItem()+" | " +s.getQuantity()+
" | "+s.getPrice()+" | " +s.getTotal() + " |");
sum+=s.getTotal();
}

System.out.println("|---------------------------------------------------|");
System.out.println("| Total= "+sum +" |");
System.out.println("-----------------------------------------------------");
}

Explanation / Answer

PS: Please rate the answer You can use Formatter class for that in Java- Add import.util.Formatter to your imports For example, first create a formatter object Formatter fmt=new Formatter(); Now say what you want to do Formatter format = fmt.format("%20s%12s%12s%12s", "State", "Obama","McCain","Other"); //Here I'm saying I want State to be having a width of 20 characters, Obama 12 characters, McCain 12 characters and other 12 characters. Now I print this System.out.println(fmt); In your case you can do Formatter fmt=new Formatter(); System.out.println("%10s%10s%10s%15s","Items","Quantity","Price","Total" );

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote