1

My program is complete however I have been trying to figure out how to format the output of each report when I print out the method printQueueInfo and printStackInfo. I have tried System.out.printf in each method mentioned above where I have the System.out.println at along with putting it in the reportOne and reportTwo methods. Below I have included what the output should look like vs what mine actually looks like along with my code. Any help is appreciated.

Below is what the code should look like (notice how the DVD/Book is lined up along with the price):

Enter database filename: 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 Tatto (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)                89.85
  1 x Dracula                        (Book)                4.95
  ---------------------------------------------------------------------------
  Outstanding Balance:                                   114.75 
-------------------------------------------------------------------------------

Below is my actual current code output (notice how my DVD/Book nor price are lined up):

Please 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
-------------------------------------------------

Below is my code:

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();

    }

}
Seumas Frew
  • 25
  • 2
  • 9
  • possible duplicate of [How can I pad a String in Java?](http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java) – Scary Wombat Mar 12 '15 at 01:18
  • @ScaryWombat That doesn't help me because I can't use Apache StringUtils. I don't even know what that is nor have been taught it meaning I can't use it for my project. – Seumas Frew Mar 12 '15 at 01:21
  • There are other solutions in this answer – Scary Wombat Mar 12 '15 at 01:24
  • 1
    @SeumasFrew the answer provided by Scary have another answer that can help you. Since you don't know what Apache StringUtils is I take that you are pretty new to java so it is a library that has a bunch of utilities classes to help you out with things that it is not implemented from java jang or it simplify what has. So the other answer is this one: http://stackoverflow.com/a/391978/460557 there you will see `String.format`, search about it. It will be pretty easy. – Jorge Campos Mar 12 '15 at 01:37

0 Answers0