Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * Write a description of class lab5 here.
  4.  *
  5.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  6.  * @version 18 OCTOBER 2012
  7.  *
  8.  *  Difference between the printf and format methods in Java
  9.  *  printf() and format()both methods are used to format String in Java and more or less similar. printf()is more close to C programming language because of identical name used in  C programming language, Anyone who has work in C previously can easily start with this printf() method also its look more as a replacement of System.out.println(). if you don't want to print just want a formatted string for any other purpose String format() method is a way to go. In summary you can say that printf()writes on stdout while format() return you a formatted string.
  10.  *  Read more: http://javarevisited.blogspot.com/2012/08/how-to-format-string-in-java-printf.html
  11.  *  
  12.  *  Assuming the 2 cities are in a signle String city
  13.  *  and the distance is in a int distance
  14.  *  String output = String.format("%40s Distance: %3d miles", city, distance);
  15.  *  Read more: http://www.dreamincode.net/forums/topic/275776-how-do-i-use-string-formats-to-arrange-output-neatly/
  16.  */
  17. import java.util.Scanner;
  18. public class lab5
  19. {
  20.     public static void main(String [] args)
  21.     {
  22.         /**ATOZ Telecommunications Sdn. Bhd. is a company that sells handphones and currently
  23.          * having promotions on 10 brand new sophisticated handphones. Assuming that all their
  24.          * handphones are stored in a queue, the first 8 customers purchase the handphone will be
  25.          * given a discount of 25% off. One customer is only limited to purchase one handphone.*/
  26.          
  27.         //a.CREATE A QUEUE OBJECT NAMED AS qHandphone
  28.         Queue qHandphone = new Queue();
  29.          //declaration of data
  30.         String sn;
  31.         String brand;
  32.         double price;
  33.         Scanner sc = new Scanner(System.in);
  34.         double totalPrice=0, discountPrice=0;
  35.        
  36.         //b.INPUT TEN(10) HANDPHONE OBJECTS AND STORE THEM INTO qHandphone
  37.         final int MAX = 10;
  38.         System.out.println("INPUT TEN(10) HANDPHONE OBJECTS AND STORE THEM INTO qHandphone");
  39.         for (int i = 0; i< MAX; i++)
  40.         {
  41.             System.out.println("Handphone no " + (i+1)+ ": ");
  42.             System.out.print("Serial No: "); sn = sc.next();
  43.             System.out.print("Brand: "); brand = sc.next().toUpperCase();
  44.             System.out.print("Price: "); price = sc.nextDouble();
  45.             Handphone hp = new Handphone (sn,brand,price);
  46.             qHandphone.enqueue(hp);
  47.             System.out.println();
  48.         }
  49.         //c.DISPLAY THE OUTPUT FOR ALL HANDPHONE OBJECTS USING THE FOLLOWING EXAMPLE:
  50.         System.out.println("SERIAL NUMBER   BRAND       NORMAL PRICE   DISCOUNTED PRICE");
  51.         int counter = 0;
  52.         while (! qHandphone.isEmpty())
  53.         {
  54.             Handphone hp = (Handphone)qHandphone.dequeue();
  55.             price = hp.getPrice();
  56.             if (counter<8)
  57.                 discountPrice = price*0.75;//alternative price - price*0.25
  58.             else
  59.                 discountPrice = price;
  60.             //System.out.println(hp.toString()+dc.format(discountPrice));
  61.             System.out.printf( "%-16s %-10s %-13.2f %-15.4f \n",hp.getSN(),hp.getBrand(),price,discountPrice );
  62.             //-16 for left align. s for string. f for float/double.
  63.             totalPrice += discountPrice;
  64.             counter++;
  65.         }
  66.         //d.CALCULATE AND DISPLAY THE TOTAL PRICES FOR THE HANDPHONES SOLD BY THE COMPANY.
  67.         System.out.printf("\nTOTAL PRICES FOR THE HANDPHONES SOLD BY THE COMPANY: RM %.2f",totalPrice);
  68.         sc.close();
  69.     }
  70. }