Question

This project will allow you to write a program to get more practice with the stack...

This project will allow you to write a program to get more practice with the stack and queue data structures, as well as more practice with object-oriented ideas that we explored in the previous projects.

In this assignment you will be writing a simulation of an order-fulfillment system for a company like Amazon.com. These companies take orders for products and ship them to customers based on what they have in inventory. For this assignment you will be performing a scaled-back version of this task – you will write a program that will read an order from a text file, turn that order into a list of products, determine which products can be shipped now and which must be delayed until later, and then produce two reports – one showing what will be shipped now, and one showing what portions of the order remain “on hold” for a later date.

In the order-fulfillment system we are simulating, orders are fulfilled using either First-In, First-Out processing (for orders that can be fulfilled immediately). However, orders that must be delayed are prioritized in the reverse order they were made – more recent orders should be fulfilled first – and so follow Last-In, First-Out processing. Products that can be filled immediately should be reported in the order they appear in the file. Products that must be delayed should appear in the report in the reverse order that they appear in the input file. To perform this processing and receive full credit you must use queues and stacks to perform this processing as discussed below.

For this program you will be writing a Product class to contain the data about each individual product in the database. You may write this class from scratch, or you may use the Product class you built in Lab 2 as a base for this lab. However, the Product class for this lab has some new requirements, so read below carefully and make sure you modify your class to implement these changes.

Before beginning to code, think carefully about what you need to do to implement the requirements discussed below. Also consider how you will break the portions of this assignment that are not class-based down into smaller methods. Remember that monolithic solutions to this problem will lose points regardless of whether they produce the correct output or not.

Objectives

  • Practice with programming fundamentals
    • Review of various Java fundamentals (branching, loops, variables, methods, etc.)
    • Review of Java File I/O concepts
    • Practice with Java ArrayList concepts
    • Practice with object-oriented programming and design
    • Practice with Java interfaces
    • Practice with stack and queue data structures

Instructions

Create a new Project folder named StackQueue, then look at the instructions below.

Part I - The Product class

For this assignment you must design a class named SimpleProduct.  This class must implement the Product interface given below. You must download this file and import it into your StackQueue source code folder before you can start to implement your own SimpleProduct class.

  • Product.java (Links to an external site.)

It is up to you how to represent the member variables of your SimpleProduct class, but it must implement all of the methods given in the interface. Note that your SimpleProduct class is similar to - but not exactly the same as - the Product class you created in a previous project. Feel free to copy that class and adjust it so that it implements the Product interface above for your submission for this assignment.

In addition, your SimpleProduct class must implement the following methods not provided in the interface, but are inherited from the Object class:

  • equals(Object obj) – should return a true value if the name and type of the Product are the same.
  • toString() – should return a string that contains the name of the product, the type of the product, the price and the quantity of the product as a comma-separated list enclosed in parentheses.

As you write this class, write a test program to go along with it. Test each method as you write it, as we have discussed in class. You will need to start with a skeleton of your SimpleProduct code that has each method in the interface written as a "stub" as seen in the Closed Lab code you have modified (i.e. code that does nothing but return a default value to allow the class to compile), and then you should modify your stub code one method at a time, testing each change you make. You will not need to submit this test program, but using it will make your code development much, much easier.

Part II - The Order Reporting Program

Write a well-structured program named OrderReporting.java that performs the following actions:

  1. Ask the user to enter the name of the file that contains the order database.
  2. Input the customer and product information from the file. See below for the file format information for the order fulfillment system - it contains more information than what you set up above. Customer information may be stored in any format you choose (though see below about creating a Customer class). Each individual product should be stored in its own Product object.  
  3. Create two Collection objects – a FIFO queue of orders to be fulfilled and a stack of orders to be delayed. Use the inStock attribute of each Product to determine which Collection they below to. Items that are in stock should be placed in the FIFO queue, while items that are out of stock should be pushed onto the stack.
  4. For the orders that can be fulfilled now, calculate the shipping cost and the sales tax. Sales tax information is in the customer block of the input data file (see below for the file format). Shipping cost is based on how much is ordered. Basic shipping is 15% of the total cost of the order. If there is at least $10 worth of product being shipped at a time, then the shipping cost is reduced to 5%. If there is at least $25 worth of product being shipped at a time, then the shipping cost is reduced to 0% (i.e. free shipping). Sales tax should be computed before adding in the cost of shipping, and shipping costs and sales tax should ONLY be calculated for orders actually being fulfilled, not for everything in the file.
  5. Output two well-formatted reports as outlined below. The first report is the report of orders to be fulfilled and should list the products that are able to be fulfilled now in FIFO order (i.e. in the order they appear in the file). The second report is the report of orders to be delayed, and should list the products that remain to be fulfilled in LIFO order (i.e. in the reverse of the order they appear in the file). Both reports should include a total dollar amount for the items in the order, and the fulfilled order report should include lines for shipping as well as sales tax.

The input file format for this order fulfillment system is:

customer last name
customer first name
customer street address
customer city
customer state
customer zip
customer sales tax
product 1 name
product 1 type
product 1 price
product 1 quantity
product 1 instock?
…
product n name
product n type
product n price
product n quantity
product n instock?

Note the inclusion of a block of customer information at the top of the file. After the customer information is finished, the product blocks continue just as the file format described in Part I above. A sample input file might look like this:

Fakename
Bob
123 Fake Street
Fake City
FS
99999
0.07
The Shawshank Redemption
DVD
19.95
1
true
Dracula
Book
4.95
1
false
The Dark Knight
DVD
19.95
1
true
Lego Ultimate Building Set
Toy
29.95
3
false
The Girl With The Dragon Tattoo
Book
14.95
1
true
Iron Man
DVD
19.95
1
false
Under The Dome
Book
19.95



1
true

An example of the two reports that this file would generate is given below. Note that you will need to make use of Java's formatted output methods to get the report to output properly.

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

Note that you can read a boolean value using the Scanner's nextBoolean() method. This works like nextDouble() or nextInt(). You can also use nextLine() to read the value as a String and then use Boolean.parseBoolean() to turn it into a boolean value - just like you would with Integer.parseInt(). See the Java API documentation for Boolean (Links to an external site.) and Integer (Links to an external site.) for more information.

product.java

/**
 * Product
 * 
 *   A simple interface for a possible family of Product 
 *   classes.
 *   
 *   @author Jeremy Morris
 *   @version 20120928
 */
import java.util.Scanner;

public interface Product {

        /*
         * setName
         *  @param name - new name for the product
         */
        public void setName(String name);

        /*
         * getName
         *  @return the name of the product
         */
        public String getName();

        /*
         * setType
         *  @param type - the type of the product
         */
        public void setType(String type);

        /*
         * getType
         * @return - the product type
         */
        public String getType();

        /*
         * setPrice
         * @param price - the price of the product
         */
        public void setPrice(double price);

        /*
         * getPrice
         * @return the price of the product
         */
        public double getPrice();

        /*
         * setQuantity
         * @param quantity - the number of this product in inventory
         */
        public void setQuantity(int quantity);

        /*
         * getQuantity
         * @return the number of this product in inventory
         */
        public int getQuantity();
        
        /*
         * setInStock
         * @param inStock - true if this product is in stock
         */
        public void setInStock(boolean inStock);

        /*
         * getQuantity
         * @return true if this product is in stock
         */
        public boolean getInStock();
        
        /*
         * readNextProduct
         * @param inFile - a Scanner containing product entries
         * @return false if the product cannot be completely read,
         *                      true otherwise
         */
        public boolean readNextProduct(Scanner inFile);

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Files:

Product.java

import java.util.Scanner;

public interface Product {

   /*
   * setName
   * @param name - new name for the product
   */
   public void setName(String name);

   /*
   * getName
   * @return the name of the product
   */
   public String getName();

   /*
   * setType
   * @param type - the type of the product
   */
   public void setType(String type);

   /*
   * getType
   * @return - the product type
   */
   public String getType();

   /*
   * setPrice
   * @param price - the price of the product
   */
   public void setPrice(double price);

   /*
   * getPrice
   * @return the price of the product
   */
   public double getPrice();

   /*
   * setQuantity
   * @param quantity - the number of this product in inventory
   */
   public void setQuantity(int quantity);

   /*
   * getQuantity
   * @return the number of this product in inventory
   */
   public int getQuantity();
  
   /*
   * setInStock
   * @param inStock - true if this product is in stock
   */
   public void setInStock(boolean inStock);

   /*
   * getQuantity
   * @return true if this product is in stock
   */
   public boolean getInStock();
  
   /*
   * readNextProduct
   * @param inFile - a Scanner containing product entries
   * @return false if the product cannot be completely read,
   *            true otherwise
   */
   public boolean readNextProduct(Scanner inFile);
}

SimpleProduct.java

import java.util.Scanner;

public class SimpleProduct implements Product{
  
   //Create private variables
   private String name;
   private String type;
   private double price;
   private int quantity;
   private boolean inStock;
  
   //Assign initial values
   public SimpleProduct() {
       name = "";
       type = "";
       price = 0.0;
       quantity = 0;
       inStock = false;
   }
  
   //Set product name
   public void setName(String name) {
       this.name = name;
   }
  
   //Return product name
   public String getName() {
       return name;
   }
  
   //Set product type
   public void setType(String type) {
       this.type = type;
   }

   //Return product type
   public String getType() {
       return type;
   }

   //Set product price
   public void setPrice(double price) {
       this.price = price;
   }

   //Return product price
   public double getPrice() {
       return price;
   }

   //Set product quantity
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }

   //Return product quantity
   public int getQuantity() {
       return quantity;
   }
      
   //Set product in stock boolean
   public void setInStock(boolean inStock) {
       this.inStock = inStock;
   }
  
   //Return product in stock boolean
   public boolean getInStock() {
       return inStock;
   }
  
   /*
   * readNextProduct
   * @param inFile - a Scanner containing product entries
   * @return false if the product cannot be completely read,
   *            true otherwise
   */
   public boolean readNextProduct(Scanner inFile) {
       boolean returnValue = true;
       for(int i = 0;i<5;i++) {
           if(i == 0) {
               this.setName(inFile.nextLine());
           }
           else if(i == 1) {
               this.setType(inFile.nextLine());
           }
           else if(i == 2) {
               try {
                   this.setPrice(inFile.nextDouble());              
               }
               catch(Exception e) {
                   System.out.println("ERROR");
               }
           }

           else if(i == 3) {
               try {
                   this.setQuantity(inFile.nextInt());                  
               }
               catch(Exception e) {
                   System.out.println("ERROR");
               }
           }

           else if(i == 4) {
               try {
                   this.setInStock(inFile.nextBoolean());                  
               }
               catch(Exception e) {
                   System.out.println("ERROR");
               }
           }
           else {
               returnValue = false;
           }
           }
       return returnValue;
       }          
  
   //Return true if product name and type are the same, false otherwise
   public boolean equals(SimpleProduct i) {
       boolean result = false;
       if(i instanceof SimpleProduct) {
           SimpleProduct t1 = (SimpleProduct)i;
           if(this.name.equals(t1.getName())&&this.type.equals(t1.getType())) {
               result = true;
           }
       }
       return result;
   }
  
   //Return product information in string form
   public String toString() {
       String info = "(" + this.name + "," + this.type + "," + this.price + "," + this.quantity + "," + this.inStock +")";
       return info;
   }
}

OrderFulfillment.java

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class OrderFulfillment {

   public static void main(String[] args) {

       // Prompt user for file name
       Scanner in = new Scanner(System.in);
       System.out.print("Enter database filename: ");
       String fileName = in.nextLine();

       // Issue try statement
       try {
           File newFile = new File(fileName);
           Scanner fileScan = new Scanner(newFile);
          
           //Create array list for simple product objects
           ArrayList<SimpleProduct> summary = new ArrayList<>();
          
           //Collect customer and product information
           String Fname = "";
           String Lname = "";
           String address = "";
           String city = "";
           String state = "";
           String zip = "";
           double salesTax = 0.0;
           int n = 0;
           while (fileScan.hasNext()) {
               if (n == 0) {
                   Lname = fileScan.nextLine();
                   Fname = fileScan.nextLine();
                   address = fileScan.nextLine();
                   city = fileScan.nextLine();
                   state = fileScan.nextLine();
                   zip = fileScan.nextLine();
                   salesTax = fileScan.nextDouble();
                   n = 1;
               }
               SimpleProduct i = new SimpleProduct();
               fileScan.nextLine();
               i.setName(fileScan.nextLine());
               i.setType(fileScan.nextLine());
               i.setPrice(fileScan.nextDouble());
               i.setQuantity(fileScan.nextInt());
               i.setInStock(fileScan.nextBoolean());
               summary.add(i);
           }
          
           //Create queue and stack for outgoing and outstanding products
           Queue<SimpleProduct> FIFO = new LinkedList<>();
           Stack<SimpleProduct> delayed = new Stack<>();
          
           //Collect objects from the array list
           for (int i = 0; i < summary.size(); i++) {
               if (summary.get(i).getInStock()) {
                   FIFO.add(summary.get(i));
               } else {
                   delayed.push(summary.get(i));
               }
           }
          
           //Print out customer information
           System.out.println("Shipping to:");
           System.out.println(" " + Fname + " " + Lname);
           System.out.println(" " + address);
           System.out.println(" " + city + " " + state + " " + zip);
           System.out.println("-------------------------------------------------------------------------------");
          
           //Create order summary for outgoing products
           double subTotal = 0;
           double sum = 0;
           double shipping = 0;
           while(!FIFO.isEmpty()) {
               SimpleProduct out = FIFO.remove();
               System.out.printf("%3d%2s%-40s%-20s%10.2f%n", out.getQuantity(), " x ", out.getName(),
                       "(" + out.getType() + ")", out.getPrice()*out.getQuantity());
               subTotal = subTotal + out.getPrice()*out.getQuantity();
           }
           if (subTotal >= 10 && subTotal < 25) {
               shipping = subTotal * 0.05;
           } else if (subTotal < 10) {
               shipping = subTotal * 0.15;
           }

           double tax = subTotal * salesTax;
           sum = subTotal + tax + shipping;
          
           System.out.println("-------------------------------------------------------------------------------");
           System.out.printf("%-56s%20.2f%n", "Subtotal:", subTotal);
           System.out.printf("%-56s%20.2f%n", "Sales Tax: " + "(" + salesTax + ")", tax);
           System.out.printf("%-56s%20.2f%n", "Shipping:", shipping);
           System.out.println("-------------------------------------------------------------------------------");
           System.out.printf("%-56s%20.2f%n", "Total:", sum);
           System.out.println("-------------------------------------------------------------------------------");
          
           //Create order summary for outstanding products
           System.out.println("Orders Outstanding For:");
           System.out.println("Shipping to:");
           System.out.println(" " + Fname + " " + Lname);
           System.out.println(" " + address);
           System.out.println(" " + city + " " + state + " " + zip);
           System.out.println("-------------------------------------------------------------------------------");

           double outStanding = 0;
           while(!delayed.empty()) {
               SimpleProduct out = delayed.pop();
               System.out.printf("%3d%2s%-40s%-20s%10.2f%n", out.getQuantity(), " x ", out.getName(),
                       "(" + out.getType() + ")", out.getPrice()*out.getQuantity());
               outStanding = outStanding + out.getPrice()*out.getQuantity();
           }
           System.out.println("-------------------------------------------------------------------------------");
           System.out.printf("%-56s%20.2f%n", "Outstanding Balance:", outStanding);
           System.out.println("-------------------------------------------------------------------------------");
       }
      
       //Issue catch statement for any file error
       catch (IOException e) {
           System.out.println("ERROR");
       }
   }
}

input.txt

Fakename
Bob
123 Fake Street
Fake City
FS
99999
0.07
The Shawshank Redemption
DVD
19.95
1
true
Dracula
Book
4.95
1
false
The Dark Knight
DVD
19.95
1
true
Lego Ultimate Building Set
Toy
29.95
3
false
The Girl With The Dragon Tattoo
Book
14.95
1
true
Iron Man
DVD
19.95
1
false
Under The Dome
Book
19.95
1
true

Output:

Enter database filename: 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:
Shipping to:
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
-------------------------------------------------------------------------------

Hope this helps! Please let me know if any changes needed

Add a comment
Know the answer?
Add Answer to:
This project will allow you to write a program to get more practice with the stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • A set is a special kind of list, one that does not allow repeated, or duplicate,...

    A set is a special kind of list, one that does not allow repeated, or duplicate, entries. Whenever you must process an item in a data collection only once, you can use a set. For example, a compiler must find the identifiers in a program and ensure that each one has been defined only once. It could add each identifier encountered to a set. If this addition is unsuccessful, the compiler will have detected an identifier previously found. Remark: There...

  • For this project you will be writing up a simple Clock program to keep track of...

    For this project you will be writing up a simple Clock program to keep track of time. SimpleClock.java - contains your implementation of the SimpleClock class. You will need to provide the code for a constructor as well as the mutator methods set and tick and the accessor method toString. Look at the comments for each method to see how they should be implemented - the trickiest method is probably tick, which requires that you deal with the changing of...

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ListofQueuesPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ListofQueues in order to fulfill the needs of the interface. You will need to make sub-classes within...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ArrayListPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ArrayList in order to fulfill the needs of the interface. For each constructor and method in your...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT