/**
* Write a description of class lab5 here.
*
* @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
* @version 18 OCTOBER 2012
*
* Difference between the printf and format methods in Java
* 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.
* Read more: http://javarevisited.blogspot.com/2012/08/how-to-format-string-in-java-printf.html
*
* Assuming the 2 cities are in a signle String city
* and the distance is in a int distance
* String output = String.format("%40s Distance: %3d miles", city, distance);
* Read more: http://www.dreamincode.net/forums/topic/275776-how-do-i-use-string-formats-to-arrange-output-neatly/
*/
import java.util.Scanner;
public class lab5
{
public static void main(String [] args)
{
/**ATOZ Telecommunications Sdn. Bhd. is a company that sells handphones and currently
* having promotions on 10 brand new sophisticated handphones. Assuming that all their
* handphones are stored in a queue, the first 8 customers purchase the handphone will be
* given a discount of 25% off. One customer is only limited to purchase one handphone.*/
//a.CREATE A QUEUE OBJECT NAMED AS qHandphone
Queue qHandphone = new Queue();
//declaration of data
String sn;
String brand;
double price;
Scanner sc = new Scanner(System.in);
double totalPrice=0, discountPrice=0;
//b.INPUT TEN(10) HANDPHONE OBJECTS AND STORE THEM INTO qHandphone
final int MAX = 10;
System.out.println("INPUT TEN(10) HANDPHONE OBJECTS AND STORE THEM INTO qHandphone");
for (int i = 0; i< MAX; i++)
{
System.out.println("Handphone no " + (i+1)+ ": ");
System.out.print("Serial No: "); sn = sc.next();
System.out.print("Brand: "); brand = sc.next().toUpperCase();
System.out.print("Price: "); price = sc.nextDouble();
Handphone hp = new Handphone (sn,brand,price);
qHandphone.enqueue(hp);
System.out.println();
}
//c.DISPLAY THE OUTPUT FOR ALL HANDPHONE OBJECTS USING THE FOLLOWING EXAMPLE:
System.out.println("SERIAL NUMBER BRAND NORMAL PRICE DISCOUNTED PRICE");
int counter = 0;
while (! qHandphone.isEmpty())
{
Handphone hp = (Handphone)qHandphone.dequeue();
price = hp.getPrice();
if (counter<8)
discountPrice = price*0.75;//alternative price - price*0.25
else
discountPrice = price;
//System.out.println(hp.toString()+dc.format(discountPrice));
System.out.printf( "%-16s %-10s %-13.2f %-15.4f \n",hp.getSN(),hp.getBrand(),price,discountPrice );
//-16 for left align. s for string. f for float/double.
totalPrice += discountPrice;
counter++;
}
//d.CALCULATE AND DISPLAY THE TOTAL PRICES FOR THE HANDPHONES SOLD BY THE COMPANY.
System.out.printf("\nTOTAL PRICES FOR THE HANDPHONES SOLD BY THE COMPANY: RM %.2f",totalPrice);
sc.close();
}
}