如何格式化Java中具有多种类型变量的单行的控制台输出
我的程序已经完成,但是我一直在尝试在打印方法如何格式化Java中具有多种类型变量的单行的控制台输出,java,format,printf,Java,Format,Printf,我的程序已经完成,但是我一直在尝试在打印方法printQueueInfo和printStackInfo时,如何格式化每个报告的输出。我已经在上面提到的每个方法中尝试了System.out.printf,其中我有System.out.printlnat,并将它放在reportOne和reportTwo方法中。下面我已经包括了输出应该是什么样子,以及我的代码实际是什么样子。感谢您的帮助 下面是代码的样子(请注意DVD/书是如何与价格一起排列的): 下面是我当前的实际代码输出(请注意我的DVD/Boo
printQueueInfo
和printStackInfo
时,如何格式化每个报告的输出。我已经在上面提到的每个方法中尝试了System.out.printf
,其中我有System.out.println
at,并将它放在reportOne
和reportTwo
方法中。下面我已经包括了输出应该是什么样子,以及我的代码实际是什么样子。感谢您的帮助
下面是代码的样子(请注意DVD/书是如何与价格一起排列的):
下面是我当前的实际代码输出(请注意我的DVD/Book或价格是如何排列的):
下面是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Project04 {
private Customer customer;
private ArrayList<SimpleProduct> productList = new ArrayList<SimpleProduct>();
private SimpleProduct product;
private Queue<SimpleProduct> stockedQueue = new LinkedList<SimpleProduct>();
private Stack<SimpleProduct> unstockedStack = new Stack<SimpleProduct>();
private double subtotal;
private double preShipTotal;
private double shipTotal;
private double shippingPercent;
private boolean safeToAdd;
/**
* Customer constructor
* gets file information and distributes reports appropriately
*
*/
public Project04() {
//Get file from user
String fileName = getUserFile();
//place file contents in correct data structures
allocateFileContents(fileName);
//print reports
reportOne();
reportTwo();
}
/**
* @return String file name that contains order database.
*/
private String getUserFile(){
System.out.println("Please enter database file name: ");
Scanner inFile = new Scanner(System.in);
String fileName = inFile.nextLine();
return fileName;
}
/**
* @param fileName
* appropriately allocates contents of file to customer info, stocked queue, and unstocked stack
*/
private void allocateFileContents(String fileName){
Scanner input = null;
int i = 0;
try {
input = new Scanner(new File(fileName));
if((i == 0) && (! input.hasNext())){
System.out.println("Error: Empty text file. Exiting system.");
System.exit(0);
}
while (input.hasNextLine()) {
//if scanner has read past customer info
if (i >= 7) {
product = new SimpleProduct(input);
this.safeToAdd = true;
for (int p = 0; p < productList.size(); p++){
if (product.equals(productList.get(p).getName(), productList.get(p).getType())){
this.safeToAdd = false;
}
}
if (this.safeToAdd == true){
productList.add(product);
// in stock?
if (product.getInStock() == true) {
// add to FIFO queue
stockedQueue.add(product);
} else {
// add to stack
unstockedStack.push(product);
}
i = i + 5;
}
//if customer info
} else {
customer = new Customer(input);
i = i + 7;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Error: file not found. Exiting System. ");
System.exit(0);
}
}
/**
* prints report with in-stock items to be shipped
*/
private void reportOne(){
System.out.println("");
System.out.println("Shipping to:");
//print address
generateAddress();
System.out.println("-------------------------------------------------");
//print queue info
printQueueInfo();
System.out.println("-------------------------------------------------");
System.out.format("Subtotal: $%.2f%n", this.subtotal);
System.out.format("Sales tax: (%.2f) $%.2f%n", customer.getTax(),
(customer.getTax() * this.subtotal));
//get shipping
calculateShippingTotal();
System.out.format("Shipping: $%.2f%n", this.shipTotal);
System.out.println("-------------------------------------------------");
System.out.format("Total: $%.2f%n", (this.shipTotal + this.preShipTotal));
System.out.println("-------------------------------------------------");
}
/**
* Print queue product information and
* @return subtotal
*/
public double printQueueInfo(){
this.subtotal = 0;
int stockedQueueSize = stockedQueue.size();
SimpleProduct current;
for (int i = 0; i < stockedQueueSize; i++) {
//pop from queue, assign local variable, and get info
current = stockedQueue.remove();
double sub = current.getQuantity() * current.getPrice();
System.out.println(current.getQuantity() + " x "
+ current.getName() + " (" + current.getType() + ") "
+ current.getPrice());
this.subtotal = sub + this.subtotal;
}
return this.subtotal;
}
/**
* calculate total cost with shipping
*/
public void calculateShippingTotal(){
this.preShipTotal = (this.subtotal * customer.getTax()) + this.subtotal;
if (this.preShipTotal > 25) {
this.shippingPercent = 0;
} else if (this.preShipTotal > 10) {
this.shippingPercent = .05;
} else {
this.shippingPercent = .15;
}
this.shipTotal = this.shippingPercent * this.preShipTotal;
}
/**
* prints report with items that are on hold, along with outstanding balance
*/
private void reportTwo(){
System.out.println("");
System.out.println("Orders Outstanding For:");
//print address
generateAddress();
System.out.println("-------------------------------------------------");
//print stack info
printStackInfo();
System.out.println("-------------------------------------------------");
System.out.format("Outstanding balance: $%.2f%n", this.subtotal);
System.out.println("-------------------------------------------------");
}
/**
* prints on-hold items with product info
*/
public void printStackInfo(){
this.subtotal = 0;
int unstockedStackSize = unstockedStack.size();
SimpleProduct unstocked;
double sub;
for (int i = 0; i < unstockedStackSize; i++) {
unstocked = unstockedStack.pop();
sub = unstocked.getQuantity() * unstocked.getPrice();
System.out.println(unstocked.getQuantity() + " x "
+ unstocked.getName() + " (" + unstocked.getType() + ") "
+ unstocked.getPrice());
this.subtotal = sub + this.subtotal;
}
}
/**
* prints out customer address
*/
public void generateAddress(){
//System.out.println("");
System.out.println(" " + customer.getFirst() + " " + customer.getLast());
System.out.println(" " + customer.getStreet());
System.out.println(" " + customer.getCity() + ", " + customer.getState()
+ " " + customer.getZip());
//System.out.println("");
}
public static void main(String[] args) {
Project04 project04 = new Project04();
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.LinkedList;
导入java.util.Queue;
导入java.util.Scanner;
导入java.util.Stack;
公开课项目04{
私人客户;
private ArrayList productList=new ArrayList();
私有SimpleProduct产品;
private Queue stockedQueue=new LinkedList();
私有堆栈unstockedStack=新堆栈();
私人双倍小计;
私人双倍装运前总额;
私人双船道达尔;
私人双倍运费;
私有布尔安全添加;
/**
*客户建造师
*获取文件信息并适当分发报告
*
*/
公共工程04(){
//从用户获取文件
字符串文件名=getUserFile();
//将文件内容放在正确的数据结构中
AllocateFileContent(文件名);
//打印报告
reportOne();
报告二();
}
/**
*@返回包含订单数据库的字符串文件名。
*/
私有字符串getUserFile(){
System.out.println(“请输入数据库文件名:”);
扫描仪填充=新扫描仪(System.in);
字符串文件名=infle.nextLine();
返回文件名;
}
/**
*@param文件名
*适当地将文件内容分配给客户信息、库存队列和未库存堆栈
*/
私有void allocateFileContent(字符串文件名){
扫描仪输入=空;
int i=0;
试一试{
输入=新扫描仪(新文件(文件名));
如果((i==0)和(!input.hasNext()){
System.out.println(“错误:空文本文件。正在退出系统”);
系统出口(0);
}
while(input.hasNextLine()){
//如果扫描仪已读取过去的客户信息
如果(i>=7){
产品=新的SimpleProduct(输入);
this.safeToAdd=true;
对于(int p=0;pPlease enter database file name:
lab4_input.txt
Shipping to:
Bob Fakename
123 Fake Street
Fake City, FS 99999
-------------------------------------------------
1 x The Shawshank Redemption (DVD) 19.95
1 x The Dark Knight (DVD) 19.95
1 x The Girl With The Dragon Tattoo (Book) 14.95
1 x Under The Dome (Book) 19.95
-------------------------------------------------
Subtotal: $74.80
Sales tax: (0.07) $5.24
Shipping: $0.00
-------------------------------------------------
Total: $80.04
-------------------------------------------------
Orders Outstanding For:
Bob Fakename
123 Fake Street
Fake City, FS 99999
-------------------------------------------------
1 x Iron Man (DVD) 19.95
3 x Lego Ultimate Building Set (Toy) 29.95
1 x Dracula (Book) 4.95
-------------------------------------------------
Outstanding balance: $114.75
-------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Project04 {
private Customer customer;
private ArrayList<SimpleProduct> productList = new ArrayList<SimpleProduct>();
private SimpleProduct product;
private Queue<SimpleProduct> stockedQueue = new LinkedList<SimpleProduct>();
private Stack<SimpleProduct> unstockedStack = new Stack<SimpleProduct>();
private double subtotal;
private double preShipTotal;
private double shipTotal;
private double shippingPercent;
private boolean safeToAdd;
/**
* Customer constructor
* gets file information and distributes reports appropriately
*
*/
public Project04() {
//Get file from user
String fileName = getUserFile();
//place file contents in correct data structures
allocateFileContents(fileName);
//print reports
reportOne();
reportTwo();
}
/**
* @return String file name that contains order database.
*/
private String getUserFile(){
System.out.println("Please enter database file name: ");
Scanner inFile = new Scanner(System.in);
String fileName = inFile.nextLine();
return fileName;
}
/**
* @param fileName
* appropriately allocates contents of file to customer info, stocked queue, and unstocked stack
*/
private void allocateFileContents(String fileName){
Scanner input = null;
int i = 0;
try {
input = new Scanner(new File(fileName));
if((i == 0) && (! input.hasNext())){
System.out.println("Error: Empty text file. Exiting system.");
System.exit(0);
}
while (input.hasNextLine()) {
//if scanner has read past customer info
if (i >= 7) {
product = new SimpleProduct(input);
this.safeToAdd = true;
for (int p = 0; p < productList.size(); p++){
if (product.equals(productList.get(p).getName(), productList.get(p).getType())){
this.safeToAdd = false;
}
}
if (this.safeToAdd == true){
productList.add(product);
// in stock?
if (product.getInStock() == true) {
// add to FIFO queue
stockedQueue.add(product);
} else {
// add to stack
unstockedStack.push(product);
}
i = i + 5;
}
//if customer info
} else {
customer = new Customer(input);
i = i + 7;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Error: file not found. Exiting System. ");
System.exit(0);
}
}
/**
* prints report with in-stock items to be shipped
*/
private void reportOne(){
System.out.println("");
System.out.println("Shipping to:");
//print address
generateAddress();
System.out.println("-------------------------------------------------");
//print queue info
printQueueInfo();
System.out.println("-------------------------------------------------");
System.out.format("Subtotal: $%.2f%n", this.subtotal);
System.out.format("Sales tax: (%.2f) $%.2f%n", customer.getTax(),
(customer.getTax() * this.subtotal));
//get shipping
calculateShippingTotal();
System.out.format("Shipping: $%.2f%n", this.shipTotal);
System.out.println("-------------------------------------------------");
System.out.format("Total: $%.2f%n", (this.shipTotal + this.preShipTotal));
System.out.println("-------------------------------------------------");
}
/**
* Print queue product information and
* @return subtotal
*/
public double printQueueInfo(){
this.subtotal = 0;
int stockedQueueSize = stockedQueue.size();
SimpleProduct current;
for (int i = 0; i < stockedQueueSize; i++) {
//pop from queue, assign local variable, and get info
current = stockedQueue.remove();
double sub = current.getQuantity() * current.getPrice();
System.out.println(current.getQuantity() + " x "
+ current.getName() + " (" + current.getType() + ") "
+ current.getPrice());
this.subtotal = sub + this.subtotal;
}
return this.subtotal;
}
/**
* calculate total cost with shipping
*/
public void calculateShippingTotal(){
this.preShipTotal = (this.subtotal * customer.getTax()) + this.subtotal;
if (this.preShipTotal > 25) {
this.shippingPercent = 0;
} else if (this.preShipTotal > 10) {
this.shippingPercent = .05;
} else {
this.shippingPercent = .15;
}
this.shipTotal = this.shippingPercent * this.preShipTotal;
}
/**
* prints report with items that are on hold, along with outstanding balance
*/
private void reportTwo(){
System.out.println("");
System.out.println("Orders Outstanding For:");
//print address
generateAddress();
System.out.println("-------------------------------------------------");
//print stack info
printStackInfo();
System.out.println("-------------------------------------------------");
System.out.format("Outstanding balance: $%.2f%n", this.subtotal);
System.out.println("-------------------------------------------------");
}
/**
* prints on-hold items with product info
*/
public void printStackInfo(){
this.subtotal = 0;
int unstockedStackSize = unstockedStack.size();
SimpleProduct unstocked;
double sub;
for (int i = 0; i < unstockedStackSize; i++) {
unstocked = unstockedStack.pop();
sub = unstocked.getQuantity() * unstocked.getPrice();
System.out.println(unstocked.getQuantity() + " x "
+ unstocked.getName() + " (" + unstocked.getType() + ") "
+ unstocked.getPrice());
this.subtotal = sub + this.subtotal;
}
}
/**
* prints out customer address
*/
public void generateAddress(){
//System.out.println("");
System.out.println(" " + customer.getFirst() + " " + customer.getLast());
System.out.println(" " + customer.getStreet());
System.out.println(" " + customer.getCity() + ", " + customer.getState()
+ " " + customer.getZip());
//System.out.println("");
}
public static void main(String[] args) {
Project04 project04 = new Project04();
}
}