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);
}
}

7 comments:

blue said...

plz check ur mail and reply back

blue said...

I need a bigger program.
And all the details are der in ur mail.So plz check and send me a bigger program as soon as possible.
It is urgent.

PRAVINTH G said...

Mr.Blue, First you please understand the purpose of this blog. we started this blog to help guys who are new to programming and who need help to learn it.
What do you mean by bigger program? do you think programs which have more number lines will do more? Still if you want big programs, we can do that for you. If you want the program to do some specific operation, then you can tell us instead of saying a bigger program. And i have not got any mail from you. Mail us, if i or chris available, we will look at it.

deenabella said...

hye pravinth.
im deena.and currently is taking programming somewhere here in Malaysia.this is my final year in university but still i am too too weak in java.
i hope u could help me,for at least,if i got stuck in the middle of nowhere in java.
thank you so much for creating this blog by the way.

deenabella said...

CAN U HELP ME FIGURE OUT THIS PROGRAM?

Given class of Date consists of the following attributes :-

public class Date {
public int day; //eg: 04
public int month; //eg: 12
public int year; //eg: 1976
}

The following are the super class and subclasses structures to show the inheritance concepts of students’ types in UiTM systems.

Super class : Student
public class Student {
String studID; //eg: 2005128754
String studName; //eg: Ali Bin Bakar
Date dateOfBirth = new Date(); //eg:04121976  nested from class of Date
String studProg; //eg:CS110
int studGender; //eg:1 for Male or 2 for Female
}

Subclass : LocalStudent
public class LocalStudent {
String stateFrom; //eg: Kedah
String hostelRoom; //eg: Jati0191
}

Subclass : InternationalStudent
public class InternationalStudent {
String passportNumber; //eg:123456789
String countryFrom; //eg: Yemen
String guardianPhoneNumber; //019-1234567
}

deenabella said...

and the questions are:
a)Write the normal constructor for super class and both subclasses.(DONE)
b)Write the mutator methods (setter) of super class.(DONE)
c)Write the accessor (getter) for each attribute from the subclass of InternationalStudent.
d)Determine number of local and international students.
e)Write a program to insert 100 records of students’ information into array of stud.(DONE)
f)Determine the date of birth of ‘Sheikh Thawaba Ahmed Rafsanjani’ from ‘Yemen’.

Anonymous said...

I am Ayesha I am STUDENT OF CLASS 10TH I WOULD LIKE TO HAVE SOME SIMPLE PROGRAMS THAT WOULD WORK FOR MY PROJECT