Share on Google+Share on Google+

arvind
restrictedorder
0 Answer(s)      6 years and a month ago
Posted in : Java Beginners
<p>//Three classes: OrderController, OrderUI, Order</p>

<p>import java.util.regex.*; //Use of ArrayList
import java.util.*; //Use of ArrayList
import java.io.*; //Use of InputStreamReader and BufferedReader</p>

<p>//Controller class:
//Methods: initiateOrderDetailsAddition():void ; initiateOrderDetailsView():void ; initiateViewBills():void</p>

<p>class OrderController
{</p>

<p>   //Contract: initiateOrderDetailsAddition():void
   //Controls the addition of order details
   public void initiateOrderDetailsAddition() //UC2(a): Add order details
   {
    OrderUI orderUI=new OrderUI();
    double totalPrice;
    Order order;
    boolean purchase=false;
    String temp="";
    ArrayList&lt;Object> orderDetails=new ArrayList&lt;Object>();
    orderDetails=orderUI.getOrderDetails();
    if(orderDetails!=null) //If no system error occurs
      {
         temp=CDAManufacturers.validateOrderDetails(orderDetails); //Checks if the customer, product id are in their respective lists
         if(temp.equals("")) //If the inputs are valid
         {           </p>

<pre class="prettyprint">          order=CDAManufacturers.addOrderDetails(orderDetails); //Add order details to the order list
          if(order!=null) //If no system error occurs
            orderUI.notifyOrderAdditionSuccess(order); //Prompt user successful addition of order details
        else
        {
          orderUI.displayErrorMessage("Order addition aborted!!!"); //Prompt user that his order process has been aborted
          orderUI.displayMenu(); //Display clerk's menu
        }
</code></pre>

<p> }
         else
         {
           orderUI.displayErrorMessage(temp); //Prompt user the invalid ids
           orderUI.displayMenu(); //Display clerk's menu
         }
      }
      else
       {
  orderUI.notifyOrderFailure(); //Prompt user failure of order details addition due to system error
 } <br />
     }</p>

<p>  //Contract: initiateOrderDetailsView():void
  //Controls the viewing of order details
  public void initiateOrderDetailsView()  //UC2(b): View order details
  {</p>

<pre class="prettyprint">BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OrderUI orderUI=new OrderUI();
Order orderOb=null;
ArrayList&lt;Object&gt; orderDetails=new ArrayList&lt;Object&gt;();
int orderId=orderUI.getOrderId();
if(orderId!=-1) //If system error occurs
{
  orderOb=CDAManufacturers.searchOrderList(orderId); //Check order id is in the order list
  if(orderOb!=null) //If order id is in the order list
  {
    orderDetails=CDAManufacturers.returnOrderDetails(orderOb); //Fetch order details from the order list
    orderUI.displayOrderDetails(orderDetails); //Display order details
    orderUI.displayMenu(); //clerk's menu is displayed
  }
  else
  {
    orderUI.displayErrorMessage("Invalid order ID!!!!");
    orderUI.displayMenu(); //clerk's menu is displayed
  }
}
else
{
  orderUI.displayErrorMessage("Sorry!!! unable to display the order details");
  orderUI.displayMenu(); //clerk's menu is displayed
}
</code></pre>

<p>  }
 //Contract: initiateViewBills():void
  //Controls the viewing of bill details
 public void initiateViewBills()
 {</p>

<pre class="prettyprint">BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OrderUI orderUI=new OrderUI();
Order orderOb=null;
double bill=0;
ArrayList&lt;Object&gt; orderDetails=new ArrayList&lt;Object&gt;();
int orderId=orderUI.getOrderId();
if(orderId!=-1) //If system error occurs
{
  orderOb=CDAManufacturers.searchOrderList(orderId); //Check order id is in the order list
  if(orderOb!=null) //If order id is in the order list
  {
    orderDetails=CDAManufacturers.returnOrderDetailsForBill(orderOb); //Fetch order details from the order list
    bill=CDAManufacturers.calculateBill(orderDetails);
    bill=bill*1.4;
    System.out.println("Rs."+bill);
    orderUI.displayMenu(); //clerk's menu is displayed
  }
  else
  {
    orderUI.displayErrorMessage("Invalid order ID!!!!");
    orderUI.displayMenu(); //clerk's menu is displayed
  }
}
else
{
  orderUI.displayErrorMessage("Sorry!!! unable to display the order details");
  orderUI.displayMenu(); //clerk's menu is displayed
}
</code></pre>

<p>  }
}</p>

<p>//---------------------------------------------------------------------------------------------------------------</p>

<p>//Class OrderUI:
//Methods: getOrderDetails():List ; showOrderDetailsAdditionScreen():list ; diplayProductList(list):void
//         requestOrderAdditionConfirmation():boolean ; displayErrorMessage(String):void ;notifyAddProduct():String
//         displayErrorMessage(String):void ; notifyOrderAdditionSuccess(Object):void ; displayOrderId(int):void
//         notifyOrderFailure():void ; displayMenu():void ; getOrderId():int ; showOrderIdScreen():int; displayOrderDetails(List):void ; </p>

<p>class OrderUI
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));</p>

<p>  //Contract: getOrderDetails():List
  //It reads the order details and returns the order details list
  public ArrayList&lt;Object> getOrderDetails()
  {
    ArrayList&lt;Object> orderDetails=showOrderDetailsAdditionScreen(); //Read order details from user
    return(orderDetails);
  }</p>

<p>  //Contract: showOrderDetailsAdditionScreen():list
  //Takes order details and validates the inputs
  private ArrayList&lt;Object> showOrderDetailsAdditionScreen()
  {
    String addProductFlag="";
    ArrayList&lt;Object> orderDetails=new ArrayList&lt;Object>();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    boolean flag=false;
    String temp;
    try
    {
      System.out.println();
      while(flag==false) //Repeatedly asks for customer id until a feasible id is entered
      {
        System.out.println("");
        System.out.println("Enter customer id: ");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderDetails.add(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user if an incorrect input is entered
      }
      diplayProductList(CDAManufacturers.returnProductList());
      addProductFlag=notifyAddProduct();
      while(addProductFlag.equalsIgnoreCase("y")) //Checks if the user wants to buy an product
      {
      flag=false;
      while(flag==false) //Repeatedly asks for product id until a feasible id is entered
      {
        System.out.println("");
        System.out.println("Enter the Product Id ");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderDetails.add(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user if an incorrect input is entered
     }
        flag=false;
      while(flag==false) //Repeatedly asks for product id until a feasible id is entered
      {
        System.out.println("");
        System.out.println("Enter the Product quantity(in numbers): ");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderDetails.add(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user if an incorrect input is entered
          } 
        addProductFlag=notifyAddProduct();
        }
      return orderDetails;
    }
    catch(Exception e)
    {
      return null;
    }
  }</p>

<p> //Contract: diplayProductlList(list):void
  //Displays product list as catalogue
  private void diplayProductList(ArrayList&lt;Product> productList)
  {
    System.out.println("The details of the product available:");
    System.out.println("");
    System.out.println("Product Id \t\tProduct Name"); 
    System.out.println("");
    for(Product productOb:productList)
      System.out.println(productOb.getProductId()+"\t\t"+productOb.getProductName()); //Display product id and name
  }</p>

<p>  //Contract: notifyAddProduct():String
  //Ask user if he wants to buy an additional product
  public String notifyAddProduct()
  {
   String temp="";
   boolean flag=false;
    try
    {
      while(flag==false) //Repeatedly ask for input until a valid value is entered
      {
        System.out.println("Want to add Product to order? (Y/N)");
        temp=br.readLine();
        if(temp.equalsIgnoreCase("y")||temp.equalsIgnoreCase("n")) //Input can only be y or n
          flag=true;
        else
          System.out.println("Please enter Y or N..."); //Prompt user of an invalid input
      }
      return(temp);
    }
    catch(Exception e)
    {
      //System.err.println("Exception: "+e);
      return("n");
    } 
  } </p>

<p>  //Contract: requestOrderAdditionConfirmation():boolean
  //Request user for order confirmation
  private boolean requestOrderAdditionConfirmation()
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    boolean flag=false;
    String cont="";
    try
    {
      while(flag==false) //Repeatedly ask for input until a valid value is entered
      {
        System.out.println("Are you sure you want to purchase? (Y/N)");
        cont=br.readLine();
        if(cont.equalsIgnoreCase("y")||cont.equalsIgnoreCase("n")) //Input can only be y or n
          flag=true;
        else
          System.out.println("Please enter Y or N..."); //Prompt user of an invalid input
      }
      if(cont.equalsIgnoreCase("n"))
        return(false);
      else
        return(true);
    }
    catch(Exception e)
    {
      return false;
    }
  }</p>

<p>  //Contract: displayErrorMessage(String):void
  //Displays the error message passed
  public void displayErrorMessage(String error)
  {
    System.out.println();
    System.err.println(error); //Diplay the error encountered
  }</p>

<p>  //Contract: notifyOrderAdditionSuccess(Object):void
  //Notify user of successful order and display order id
  public void notifyOrderAdditionSuccess(Order order)
  {
      displayOrderId(order.getOrderId()); //Display order id generated
      displayMenu(); //Display clerk's menu
  }</p>

<p>  //Contract: displayOrderId(int):void
  //Displays the order id
  private void displayOrderId(int orderId)
  {
   System.out.println();
   System.out.println("The customer with order Id: "+orderId+" has ordered the products successfully");
   displayMenu(); //Display clerk's menu
  }</p>

<p>  //Contract: notifyOrderFailure():void
  //Notify user of the order failure
  public void notifyOrderFailure()
  {
    System.out.println();
    System.out.println("The order process failed");
    displayMenu(); //Display clerk's menu
  }</p>

<p>  //Contract: displayMenu():void
  //Display clerk's menu
  public void displayMenu()
  {
    try
    {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.println();
      System.out.println("Press any key to continue");
      br.readLine();
    }
    catch(Exception e)
    {
      System.err.println("Exception: "+e);
    }
    MainClerkSystem mainclerkSystemOb=new MainClerkSystem();
    mainclerkSystemOb.initiateClerkView();
  }</p>

<p>  //Contract: getOrderId():int
  //Takes order id and returns it
  public int getOrderId()
  {
      int orderId;
      orderId=showOrderIdScreen(); //Read order id from user
      return(orderId);
  }</p>

<p>  //Contract: showOrderIdScreen():int
  //Take a valid order id from user
  private int showOrderIdScreen()
  {
    String temp;
    boolean flag=false;
    int orderId=-1;
    try
    {
      while(flag==false) //Repeatedly ask user to enter a value until a feasible input is entered
      {
        System.out.println("");
        System.out.println("Enter the order Id:");
        temp=br.readLine();
        Pattern p=Pattern.compile("[0-9][0-9]*"); //Input must be a positive integer
        Matcher m=p.matcher(temp);
        if(m.matches())
        {
          orderId=Integer.parseInt(temp);
          flag=true;
        }
        else
          System.out.println("A positive integer expected..."); //Prompt user of an invalid input
      }
      return(orderId);
    }
    catch(Exception e)
    {
      System.err.println("Exception: "+e);
      return(-1);
    }
  }</p>

<p>  //Contract:displayOrderDetails(List):void
  //Displays the order details which is passed as a list
  public void displayOrderDetails(ArrayList&lt;Object> orderDetails)
  {
    System.out.println("");
    System.out.println("Order id :\t\t"+orderDetails.get(0));
    System.out.println("Customer name :\t"+orderDetails.get(1));
    System.out.println("Order status :\t"+orderDetails.get(2));
    System.out.println("Date Of Order :\t"+orderDetails.get(3));
    System.out.println("Date Of Delivery :\t"+orderDetails.get(4));
    int x=((orderDetails.size()-4)/2);
    System.out.println("");
    System.out.println("Quantity ordered \t Products ordered");
    for(int i=5;i<orderDetails.size()-x;i++)
     {
 System.out.println("\t"+orderDetails.get(i+x)+"\t"+orderDetails.get(i));
     }
  }                                </p>

<p>  }</p>

<p>//----------------------------------------------------------------------------------------------------------</p>

<p>//Class Order:
//Attributes: int id ; String status ; String dateOfOrder ; String dateOfDelivery
//            int customerId ; Integer[] productIdList ; Integer[] productQuantityList
//Methods:    setOrderId(int):void ; getOrderId():int ; setCustomerId(int):void ; getCustomerId():int
//            setProductId(List):void ; getProductId():list ; setProductQuantity(List):void ; getProductQuantity():list 
//            setStatus(String):void ; getStatus():String ; setDateOfOrder(String):void ; getDateOfOrder():String
//            setDateOfDelivery(String):void ; getDateOfDelivery():String ; </p>

<p>class Order
{
  private int id;
  private String status;
  private String dateOfOrder;
  private String dateOfDelivery;
  private int customerId;
  private ArrayList&lt;Integer> productIdList=new ArrayList&lt;Integer>();
  private ArrayList&lt;Integer> productQuantityList=new ArrayList&lt;Integer>();</p>

<p>  //Contract: setOrderId(int):void
  //It modifies order id
  public void setOrderId(int id)
  {
    this.id=id;
  }</p>

<p>  //Contract: getOrderId():int
  //It returns order id
  public int getOrderId()
  {
    return id;
  }</p>

<p>  //Contract: setCustomerId(int):void
  //It modifies customer id
  public void setCustomerId(int customerId)
  {
    this.customerId=customerId;
  }</p>

<p>  //Contract: getCustomerId():int
  //It returns customer id
  public int getCustomerId()
  {
    return customerId;
  }</p>

<p>  //Contract: setProductId(List):void
  //It modifies Product id list
  public void setProductId(ArrayList&lt;Integer> tempProductId)
  {
    productIdList.clear();
    for(Integer productId:tempProductId)
      productIdList.add(productId);
  }</p>

<p>  //Contract: getProductId():list
  //It returns Product id list
  public ArrayList&lt;Integer> getProductId()
  {
    return productIdList;
  }</p>

<p>//Contract: setProductQuantity(List):void
  //It modifies Product Quantity list
  public void setProductQuantity(ArrayList&lt;Integer> tempProductQuantity)
  {
    productQuantityList.clear();
    for(Integer productQuantity:tempProductQuantity)
      productQuantityList.add(productQuantity);
  }</p>

<p>  //Contract: getProductQuantity():list
  //It returns Product Quantity list
  public ArrayList&lt;Integer> getProductQuantity()
  {
    return productQuantityList;
  }</p>

<p>  //Contract: setStatus(String):void
  // It modifies order status
   public void setStatus(String status)
  {
    this.status=status;
  }</p>

<p>  //Contract: getStatus():String
  //It returns order status
  public String getStatus()
  {
    return status;
  }</p>

<p>  //Contract:setDateOfOrder(String):void
  // It modifies date of order
   public void setDateOfOrder(String dateOfOrder)
  {
    this.dateOfOrder=dateOfOrder;
  }</p>

<p>  //Contract: getDateOfOrder():String
  //It returns date of order
  public String getDateOfOrder()
  {
    return dateOfOrder;
  }</p>

<p>  //Contract:setDateOfDelivery(String):void
  // It modifies date of delivery
  public void setDateOfDelivery(String dateOfDelivery)
  {
    this.dateOfDelivery=dateOfDelivery;
  }</p>

<p>  //Contract: getDateOfDelivery():String
  //It returns date of delivery
  public String getDateOfDelivery()
  {
    return dateOfDelivery;
  }</p>

<p>}</p>

Ads

View Answers









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads