Monday, July 27, 2009

Plz post you queries here

Hi Guys,
Please post your queries here. The "introduction" thread has grown too big so please avoid posting in there. Happy to help the beginners. Post your simple program requirements here. Expect queries/programs req. in c,cpp,c# also.

Monday, June 8, 2009

Sample computer merchandise program for blue


import java.util.*;

public class ComputerMerchandise {

public static void main(String[] args) {
java.util.Hashtable<Integer, Product> htProducts = new java.util.Hashtable<Integer, Product>();
for(Product product:Products.GetAllAvailableProduct())
{
htProducts.put(product.getProductId(),product);
System.out.println(product.toString());
}
Hashtable<Integer,Integer> orders = new Hashtable<Integer,Integer>();
java.util.Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.print("To Order new item. Press Y to leave press any other key..");
String input = scanner.next();
if(!input.equalsIgnoreCase("Y")) break;
System.out.print("Enter the product id:");
int productId = scanner.nextInt();
Product selectedProduct = htProducts.get(new Integer(productId));
if(selectedProduct==null)
System.out.println("InValid Product.");
else
{
System.out.print("Enter nos:");
int quantity = scanner.nextInt();
Integer intOrdered = orders.get(new Integer(productId));
int alreadyOrdered = intOrdered==null?0:intOrdered.intValue();
if(alreadyOrdered<=0)
{
orders.put(new Integer(productId), new Integer(quantity));
}else
{
orders.put(new Integer(productId), new Integer(quantity+alreadyOrdered));
}
}
}
if(!orders.isEmpty())
{
double total = 0.0d;
Enumeration<Integer> enu = orders.keys();
while(enu.hasMoreElements())
{
Integer key = enu.nextElement();
int nos = orders.get(key).intValue();
Product soldProduct = htProducts.get(key);
double price = soldProduct.getCost()*nos;
total+=price;
String display = "Product Id:"+soldProduct.getProductId()+",Item:"+soldProduct.getName()+",Quantity:"+nos+",Cost="+price;
System.out.println(display);
}
if(total>0)
System.out.println("Total="+total);
}
}
}

class Products
{
/*
* Returns a collection of avilable products
* */
static Product[] GetAllAvailableProduct()
{
Product[] products = {
new Product("iBallMouse",600,1234),
new Product("Samsung CRT Monitor",5000,3457),
new Product("LG Keyboard",1500,200)
};
return products;
}
}

class Product
{
private double cost;
private String name;
private Integer productId;

public Integer getProductId() {
return productId;
}

public void setProductId(Integer productId) {
this.productId = productId;
}

Product(String name,double cost,int productId)
{
this.name = name;
this.cost = cost;
this.productId = new Integer(productId);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getCost() {
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}

public String toString()
{
return String.format("ProductId:%d,ProductName:%s,Price:%s", this.productId,this.name,this.cost);
}
}