Wednesday, January 31, 2007

Program:1 for noel

Hi friends here i added the first program for noel.
check noels requirements in here:
http://www.blogger.com/comment.g?blogID=5133484865049964130&postID=7557509313208084077



/*
Warning:
Handling year data also as string- not as an integer
when adding a new car.. we are not checking whether
same car already exists so there may be duplicates...
*/

import java.io.*;
import java.util.*;

public class fornoel{
ArrayList alcars;
fornoel(){
alcars=new ArrayList();
String[] soptions={ "1) Add a car", "2) View Car Details ",
"3) delete a car", "4) quit"
};
while(true){
for(int i=0;i<soptions.length;i++)
System.out.println(soptions[i]);
System.out.println("Enter your choice: ");
int choice=0;
Scanner scan=new Scanner(System.in);
choice=scan.nextInt();
switch(choice){
case 4:
return;
case 3:
if(alcars.isEmpty()){
System.out.println("There is nothing to delete..");
break;
}
System.out.println("Enter the make of the car :");
String make=scan.next();
if(!alcars.contains(make)){
System.out.println("No car exists in this make..");
break;
}
System.out.println("Enter the model of the car :");
String model=scan.next();
if(!alcars.contains(model)){
System.out.println("No car exists in this model..");
break;
}
System.out.println("Enter the year :");
String year=scan.next();
if(!alcars.contains(year)){
System.out.println("No record found at this year..");
break;
}
delete(make,model,year);
break;
case 2:
if(alcars.isEmpty()){
System.out.println("No record found ..");
break;
}
System.out.println("Enter the make of the car :");
make=scan.next();
showcars(make);
break;
case 1:
System.out.println("Enter the make of the car :");
make=scan.next();
System.out.println("Enter the model of the car :");
model=scan.next();
System.out.println("Enter the year :");
year=scan.next();
if(!alcars.isEmpty())alcars.trimToSize();
alcars.add(make);alcars.add(model);alcars.add(year);
break;
default:
System.out.println("Please enter a valid choice...");

}

}
}

void delete(String make,String model,String year){
boolean match=false;
if(alcars.isEmpty()) {
System.out.println("No records found..");
return;
}
Object[] objcars=alcars.toArray();
for(int i=0;i<objcars.length;objcars=alcars.toArray()){
boolean del=false;
if(objcars[i].equals(make)){
if(objcars[i+1].equals(model)){
if(objcars[i+2].equals(year)){
alcars.remove(i+2);alcars.remove(i+1);alcars.remove(i);
del=true;
match=true;
}
}
}
if(del==false) i+=3;
else{
if(alcars.isEmpty()) return;
if(!alcars.contains(make)) return;
i=0;
}
}
if(!match) System.out.println("No match found...");
}

void showcars(String make){
Object[] objcars=alcars.toArray();
for(int i=0;i<objcars.length;i+=3){
if(objcars[i].equals(make)){
System.out.println("make="+objcars[i]);
System.out.println("model="+objcars[i+1]);
System.out.println("year="+objcars[i+2]);
System.out.println("-------------------");
}
}
}

public static void main(String args[]){
new fornoel();
}
}

8 comments:

Justin said...

what I am expecting to happen here is the Number of invalid items to equal 2. Not stay zero, so if I add another invalid item say ,"kd" it will show 3 invalid items the first two invalid items being, "b","f" ( they are not in order in the item listing)
Thanks a lot i appreciate you taking your time .
Justin




public class ToysInventory { //start of class
private String[] toysOrdered = {"bear", "train", "car", "ball", "doll",
"ball", "train", "doll", "game", "train",
"bear", "doll", "train","car", "ball",
"bat", "glove", "bat", "b", "doll", "bear",
"ball", "doll", "bat", "car", "glove",
"train", "doll","f", "bear" };

private String[] toysInventoryList = {"ball", "bat", "bear", "car", "doll",
"game", "glove", "playstation", "train" }; // private string show the inventory

private int InventoryCounter[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // used the zerops as indicated by the live chat session
// that will start all the counts at zero where every application should begin.
private int invalid;


public static void main(String[] args) {//BEGIN main()

ToysInventory toys = new ToysInventory();
toys.countToys();
System.exit(0);
}//END main()

private void countToys() {
int temp = 0;
int orderedIndex = 0;
int inventoryIndex = 0;

int invalid = 0;




for (inventoryIndex = 0; inventoryIndex < toysInventoryList.length; inventoryIndex++){ invalid = invalid =0;
for (orderedIndex = 0; orderedIndex < toysOrdered.length; orderedIndex++) { invalid = invalid = 0;

if (toysOrdered[orderedIndex] .equals (toysInventoryList[inventoryIndex])) {
temp = temp + 1;

}//END if
if (toysOrdered[orderedIndex] .equals (toysInventoryList[inventoryIndex])) {
invalid = invalid = 0;

}// end of second if statment
InventoryCounter[inventoryIndex] = invalid;
InventoryCounter[inventoryIndex] = temp;
}
temp = 0; //reset counter value
invalid = 0;
}

System.out.println("[order status]");
for (temp = 0; temp < InventoryCounter.length; temp++){
// (*) before any item for which the customer has requested
// a quantity of 5 or more.
if (InventoryCounter[temp] >= 5){
System.out.print("*");
}
else {
System.out.print(" ");
}
System.out.println(toysInventoryList[temp] + " = " + InventoryCounter[temp]);
}
System.out.print("Number of Invalid items" + "\t" + invalid + "\n" + "\n");
}//END countToys()

}//END ToysInventory



This is the output:
init:
deps-jar:
Compiling 1 source file to C:\java\Toysinventory\build\classes
compile:
run:
[order status]
ball = 4
bat = 3
bear = 4
car = 3
*doll = 6
game = 1
glove = 2
playstation = 0
*train = 5
Number of Invalid items 0

BUILD SUCCESSFUL (total time: 3 seconds)

PRAVINTH G said...

busy..please wait...i have more work...

Suleman Zia said...

Justin there you go sorry for the late reply. have fun.

*******************************

class ToysInvTester
{
public void getTotal(String[] A, String[] B, int C[])
{
int temp=0;

for(int i=0;i<B.length; i++){
for(int j=0; j<A.length; j++)
{
if(B[i].equals(A[j]))
temp = temp + 1;
C[i] = temp;
}
temp = 0;

}
}

public int getInvalidItems(String[] A, String[] B)
{
boolean found = false;
int invalid=0;
for(int i=0; i<B.length; i++)
{
for(int j=0; j<A.length; j++)
{
if(B[i].equals(A[j]))
found = true;
}
if(!found)
invalid++;
found = false;
}


return invalid;
}
}
class ToysInv
{
public static void main(String[] args)
{
String[] myArray1 = {"bear", "train", "car", "ball", "doll",
"ball", "train", "doll", "game", "train",
"bear", "doll", "train","car", "ball",
"bat", "glove", "bat", "b", "kb", "doll", "bear",
"ball", "doll", "bat", "car", "glove",
"train", "doll","f", "bear", };

String[] myArray = {"ball", "bat", "bear", "car", "doll",
"game", "glove", "playstation", "train" };

int InventoryCounter[] = {0,0,0,0,0,0,0,0,0
};
ToysInvTester test = new ToysInvTester();
test.getTotal(myArray1, myArray, InventoryCounter);
System.out.println("[order status]");
for (int temp = 0; temp < InventoryCounter.length; temp++){
// (*) before any item for which the customer has requested
// a quantity of 5 or more.
if (InventoryCounter[temp] >= 5){
System.out.print("*");
}
else {
System.out.print(" ");
}
System.out.println(myArray[temp] + " = " + InventoryCounter[temp]);
}
System.out.println( "Invalid Items: " + test.getInvalidItems(myArray, myArray1));
//System.out.println("Invalid Items " + num);

}
}

*******************************

output

[order status]
ball = 4
bat = 3
bear = 4
car = 3
*doll = 6
game = 1
glove = 2
playstation = 0
*train = 5
Invalid Items: 3

Next time try to break your programs in different methods.

Suleman Zia :)

Anonymous said...

I am a highschool student that has been assigned a project/presentation on Java as a programming Language. I have been to a lot of different sites that explain Java, but they are all a bit too complicated for me. Do you have any simple, broken down, definitions of java and how it works? Or can you refer me to someone, or a site, that can help me? I would appreciate your help so much. Thanks, Katie.

PRAVINTH G said...

Yes Its our great pleasure to help you. Java is one of the most powerfull language. Its a pure Object Oriented Programming language. Mainly developed with an idea of "Platform-Independent" (Compile-once run anywhere) Java files are compiled into bytecode -class files which you can run in any JVM's if you find any jre (most of the o/s comes with a jre) thats enough to run the compiled java-class files..
Please refer the below links to learn more about java..
Introduction to java ..from sun java.sun.com
What is the Java Platform
from OnJava
Learn more
Another Good place to learn more about java

Anonymous said...

good praveenth .... continue u r good job .....

kajri said...

CAN ANY 1 WRITE A PROG. FOR ME
ABOUT.....

HOTEL MANAGEMENT SYSTEM
IN
GW BASIC

veeru said...

Create a class named Package with data fields for weight in ounces, shipping method, and shipping cost. The shipping method is a character: A for air, T for truck, or M for mail. The Package class contains a constructor that requires arguments for weight and shipping method. The constructor calls a private calculateCost() method that determines the shipping cost based on the following:

Shipping Method ($)

Weight (ounce) Air Truck Mail

1 to 8 2.00 1.50 0.50

9 to 16 3.00 2.35 1.50

17 and over 4.50 3.25 2.15

The Package class also contains a display() method that displays the values using appropriate fields. Create a subclass named InsuredPackage that adds an insurance cost to the shipping cost based on the following:

Shipping Cost Before Insurance ($) Additional Cost ($)

0 to 1.00 2.45

1.01 to 3.00 3.95

3.01 and over 5.55

Write a program that instantiates at least three objects of each type (Package and InsuredPackage) using a variety of weights and shipping method codes. Display the result for each Package and InsuredPackage.