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