import utils.*;
import java.io.*;
public class Orders {
static Order orders[];
public static void main(String [] args)throws Exception{
FileInputStream fstream= new FileInputStream("data2.txt");
DataInputStream in= new DataInputStream(fstream);
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String strLine;
String str[]= new String[5];
Input input= new Input();
String item;
int count=0;
while ((strLine= br.readLine())!= null)
{
count++;
}
orders= new Order[count];
for(int x=0; x<count; x++)
{
orders[x]= new Order();
}
fstream= new FileInputStream("data2.txt");
in= new DataInputStream(fstream);
br= new BufferedReader(new InputStreamReader(in));
for(int x=0; ((strLine= br.readLine())!= null); x++)
{
str= strLine.split(",");
orders[x].setRegion(str[0]);
orders[x].setRep(str[1]);
orders[x].setItem(str[2]);
orders[x].setUnit(Integer.parseInt(str[3]));
orders[x].setCost(Float.parseFloat(str[4]));
}
sortOrders();
System.out.println("Enter item to search ");
item= input.getString();
linearSearch(item);
}
public static void sortOrders()
{
int locOfSmallest;
Order temp;
for(int step=0; step<orders.length; step++)
{
//find the location of the smallest element from the array[step] to the end of the array
locOfSmallest= step;
for(int loc=step; loc<orders.length; loc++)
{
if(orders[loc].getRegion().compareTo(orders[locOfSmallest].getRegion())<0)
{
locOfSmallest= loc;
}
}
//exchange orders[step] with orders[locOfSmallest]
temp= orders[step];
orders[step]= orders[locOfSmallest];
orders[locOfSmallest]=temp;
}
}
public static void linearSearch(String item)
{
boolean found= false;
int loc=0;
int[] array= new int[50];
int x=0;
for(int i=0; i<orders.length; i++)
{
if(item.equalsIgnoreCase(orders[i].getRegion()))
{
found= true;
loc= i;
array[x]= i;
x++;
}
}
if(!found)
{
System.out.println("Item not found");
}
else
{
System.out.print("FOund at locations ");
for(int i=0; i<x; i++)
{
System.out.print(array[i]+ " ");
}
}
}
}
Imagery Nation
im·age·ry [n.] pl. im·age·ries 1. A set of mental pictures or images.
Miyerkules, Pebrero 15, 2012
Order Data Structure
public class Order {
private String region;
private String rep;
private String item;
private int unit;
private float cost;
public void setRegion(String newRegion)
{
this.region= newRegion;
}
public void setRep(String newRep)
{
this.rep= newRep;
}
public void setItem(String newItem)
{
this.rep= newItem;
}
public void setUnit(int newUnit)
{
this.unit= newUnit;
}
public void setCost(float newCost)
{
this.cost= newCost;
}
public String getRegion()
{
return this.region;
}
public String getRep()
{
return this.rep;
}
public String getItem()
{
return this.item;
}
public int getUnit()
{
return this.unit;
}
public float getCost()
{
return this.cost;
}
}
private String region;
private String rep;
private String item;
private int unit;
private float cost;
public void setRegion(String newRegion)
{
this.region= newRegion;
}
public void setRep(String newRep)
{
this.rep= newRep;
}
public void setItem(String newItem)
{
this.rep= newItem;
}
public void setUnit(int newUnit)
{
this.unit= newUnit;
}
public void setCost(float newCost)
{
this.cost= newCost;
}
public String getRegion()
{
return this.region;
}
public String getRep()
{
return this.rep;
}
public String getItem()
{
return this.item;
}
public int getUnit()
{
return this.unit;
}
public float getCost()
{
return this.cost;
}
}
Miyerkules, Pebrero 8, 2012
INPUT CLASS
import java.io.*;
public class Input {
static BufferedReader input= new BufferedReader(new InputStreamReader(System.in)) ;
public byte getByte(){
byte value=0;
try{
value= Byte.parseByte(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public String getString() throws Exception{
String str= "";
str= input.readLine();
return str;
}
public int getInt(){
int value=0;
try{
value= Integer.parseInt(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public float getFloat(){
float value=0;
try{
value= Float.parseFloat(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public short getShort(){
short value=0;
try{
value= Short.parseShort(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public long getLong(){
long value=0;
try{
value= Long.parseLong(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public double getDouble(){
double value=0;
try{
value= Double.parseDouble(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public boolean getBoolean(){
boolean value=false;
try{
value= Boolean.parseBoolean(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public char getCharacter() throws Exception {
char chr= '\u0000';
chr= input.readLine().charAt(0);
return chr;
}
}
public class Input {
static BufferedReader input= new BufferedReader(new InputStreamReader(System.in)) ;
public byte getByte(){
byte value=0;
try{
value= Byte.parseByte(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public String getString() throws Exception{
String str= "";
str= input.readLine();
return str;
}
public int getInt(){
int value=0;
try{
value= Integer.parseInt(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public float getFloat(){
float value=0;
try{
value= Float.parseFloat(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public short getShort(){
short value=0;
try{
value= Short.parseShort(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public long getLong(){
long value=0;
try{
value= Long.parseLong(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public double getDouble(){
double value=0;
try{
value= Double.parseDouble(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public boolean getBoolean(){
boolean value=false;
try{
value= Boolean.parseBoolean(input.readLine());
} catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
return value;
}
public char getCharacter() throws Exception {
char chr= '\u0000';
chr= input.readLine().charAt(0);
return chr;
}
}
STUDENT STRUCTURE
public class Student {
private String course, name, idNo, address;
private String year, age;
public void setName(String newName)
{
this.name= newName;
}
public void setCourse(String newCourse)
{
this.course=newCourse;
}
public void setYear(String newYear)
{
this.year=newYear;
}
public void setId(String newId)
{
this.idNo=newId;
}
public String getCourse()
{
return this.course;
}
public String getName()
{
return this.name;
}
public String getYear()
{
return this.year;
}
public String getId()
{
return this.idNo;
}
}
private String course, name, idNo, address;
private String year, age;
public void setName(String newName)
{
this.name= newName;
}
public void setCourse(String newCourse)
{
this.course=newCourse;
}
public void setYear(String newYear)
{
this.year=newYear;
}
public void setId(String newId)
{
this.idNo=newId;
}
public String getCourse()
{
return this.course;
}
public String getName()
{
return this.name;
}
public String getYear()
{
return this.year;
}
public String getId()
{
return this.idNo;
}
}
FILE WRITING AND SORTING (Main Program)
import java.io.*;
import utils.*;
public class WriteToFile {
static Student[] students;
public static void main(String[]args) throws Exception
{
FileWriter fw= new FileWriter("students.dat", true);
BufferedWriter out= new BufferedWriter(fw);
Input input= new Input();
int count;
//ask the number of students
System.out.println("How many students?");
count= input.getInt();
//initialize the students array size
students= new Student[count];
//instanstiate each array element
for(int i=0; i<students.length; i++)
{
students[i]= new Student();
}
//Student's details
for(int i=0; i<count; i++)
{
System.out.print("Enter Name: ");
students[i].setName(input.getString());
System.out.print("ID #: ");
students[i].setId(input.getString());
System.out.print("COurse: ");
students[i].setCourse(input.getString());
System.out.print("Year Level: ");
students[i].setYear(input.getString());
System.out.println("");
}
sortStudents();
//Writing data
for(int i=0; i<count; i++)
{
out.write(students[i].getName());
out.write(",");
out.write(students[i].getId());
out.write(",");
out.write(students[i].getCourse());
out.write(",");
out.write(students[i].getYear());
out.newLine();
}
out.close();
}
public static void sortStudents()
{
int locOfSmallest;
Student temp;
for(int step=0; step<students.length; step++)
{
//find the location of the smallest element from the array[step] to the end of the array
locOfSmallest= step;
for(int loc=step; loc<students.length; loc++)
{
if(students[loc].getName().compareTo(students[locOfSmallest].getName())<0)
{
locOfSmallest= loc;
}
}
//exchange students[step] with students[locOfSmallest]
temp= students[step];
students[step]= students[locOfSmallest];
students[locOfSmallest]=temp;
}
}
}
import utils.*;
public class WriteToFile {
static Student[] students;
public static void main(String[]args) throws Exception
{
FileWriter fw= new FileWriter("students.dat", true);
BufferedWriter out= new BufferedWriter(fw);
Input input= new Input();
int count;
//ask the number of students
System.out.println("How many students?");
count= input.getInt();
//initialize the students array size
students= new Student[count];
//instanstiate each array element
for(int i=0; i<students.length; i++)
{
students[i]= new Student();
}
//Student's details
for(int i=0; i<count; i++)
{
System.out.print("Enter Name: ");
students[i].setName(input.getString());
System.out.print("ID #: ");
students[i].setId(input.getString());
System.out.print("COurse: ");
students[i].setCourse(input.getString());
System.out.print("Year Level: ");
students[i].setYear(input.getString());
System.out.println("");
}
sortStudents();
//Writing data
for(int i=0; i<count; i++)
{
out.write(students[i].getName());
out.write(",");
out.write(students[i].getId());
out.write(",");
out.write(students[i].getCourse());
out.write(",");
out.write(students[i].getYear());
out.newLine();
}
out.close();
}
public static void sortStudents()
{
int locOfSmallest;
Student temp;
for(int step=0; step<students.length; step++)
{
//find the location of the smallest element from the array[step] to the end of the array
locOfSmallest= step;
for(int loc=step; loc<students.length; loc++)
{
if(students[loc].getName().compareTo(students[locOfSmallest].getName())<0)
{
locOfSmallest= loc;
}
}
//exchange students[step] with students[locOfSmallest]
temp= students[step];
students[step]= students[locOfSmallest];
students[locOfSmallest]=temp;
}
}
}
Lunes, Pebrero 6, 2012
Mag-subscribe sa:
Mga Post (Atom)