public class Person Properties private int password privat

public class Person{

//========== Properties =============

private int password;

private String firstName;

private String lastName;

private String email;

//========= Constructor ============

public Person(){

password = 0;

firstName = \" \";

lastName = \" \";

email = \" \";

}

public Person(int p, String f,String l,String e){

password = p;

firstName = f;

lastName = l;

email = e;

}

//========== Behavior ===============

public void setPassword(int p){ password = p;}

public int getPassword(){

return password;}

public void setFirstName(String f){firstName = f;}

public String getFirstName(){

return firstName;}

public void setLastName(String l){lastName = l;}

public String getLastName(){

return lastName;}

public void setEmail(String e){email = e;}

public String getEmail(){

return email;}

public void display(){

System.out.println(\"Password = \"+getPassword());

System.out.println(\"First Name = \"+getFirstName());

System.out.println(\"Last Name = \"+getLastName());

System.out.println(\"Email = \"+getEmail());

}//end display()

public static void main(String args[]){

Person p2;

p2 = new Person(9999,\"Bill\",\"Smith\",\"bsmith@yahoo.com\");

//calls multi argument constructor

p2.display();

}//end main

}//end class

public class Dentist extends Person{

//=========== Properties ============

private int dentistId;

//=========== Constructors ==========

public Dentist(){

super();// sets 4 properties to blanks

dentistId = 0;

}

public Dentist(int p,String f,String l,String e, int d){

super( p,f,l,e);//pass 4 properties up to super class

dentistId = d;

}

//=========== Behavior ==============

public void setDentistId( int d) { dentistId = d;}

public int getDentistId() {

return dentistId; }

public void display(){

super.display();// calls display in super class to display 4 properties

System.out.println(\"Dentist Id = D\"+getDentistId());

}//end display()

public static void main(String args[]){

//Object 1

Dentist d1;

d1 = new Dentist();//calls no args constructor

d1.setDentistId(201);

d1.display();

System.out.println(\"\ \");

//Object 2

Dentist d2;

d2 = new Dentist(12345,\"Susan\",\"Martin\",\"sm@yahoo.com\",201);

//calls multi args constructor

d2.display();

}//end mian

}//end class

Let’s make it so that we can look up and find a Dentist in the “Dentists.txt” file. The Dentists are organized by Dentists Code. So we should be able to look in the File for Dentist “D201”, and it should give us back all the data about that Dentist, like,Dentist Name, etc. So we will need to read from the “Dentists.txt” file and select the Dentists code “D201”. The File is delimited by “:”(colons). Take a look at the file. Use FileInputStream and Buffered reader to read from the file.

Code for testing ‘Select’ that goes in main:

                Dentist d1 = new Dentist();

                D1.select(“D201”);

                d1.display();

Let’s also make it so that we can add a new Dentist to the “Dentists.txt” file. We should be able to append a new line to the “Dentists.txt” file with all the data for a new Dentists, like DentistCode, etc.

Use PrintStream and Buffered Write to write to the file.

Code for testing ‘Insert’ that goes in main:

                Dentist d1 = new Dentist();

                d1.insert(“D201”,”frank”,”Frank”,”Martin”,fm@yahoo.com”,”539”);

                //now go look in file to see if new line was added

Solution

You can use this code to select the record by code. You have to just pass filename and Argument ie. code to find the exact record. Here we use ArrayList and List to iterate and scaner to scan the file content.

To Compile : javac Main.java

To Run :- java Main D202

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) {
try {
   String searchCode = args[0];
File f = new File(\"test.txt\");
Scanner sc = new Scanner(f);

List<select> people = new ArrayList<select>();

while(sc.hasNextLine()){
String line = sc.nextLine();
String[] details = line.split(\":\");
       String code = details[0];
       String fName = details[2];
       String lName = details[3];
       String email = details[4];
       int number = Integer.parseInt(details[5]);  
       if(code.equals(searchCode)){
   select p = new select(code, fName, lName, email, number);
   people.add(p);}
       }

for(select p: people){
System.out.println(p.toString());
}
                      
} catch (FileNotFoundException e) {   
e.printStackTrace();
}
}
}

class select{

private String code;
private String fName;
private String lName;
private String email;
private int number;

public select(String code, String fName, String lName, String email, int number){
this.code = code;
this.setfName(fName);
this.setlName(lName);
this.email = email;
this.number = number;
}
/**
* @return the getcode
*/
public String getCode() {
return code;
}

/**
* @param gender the gender to set
*/
public void setCode(String code) {
this.code = code;
}

/**
* @param name the name to set
*/
public void setfName(String fName) {
this.fName = fName;
}

/**
* @return the name
*/
public String getfName() {
return fName;
}
/**
* @param name the name to set
*/
public void setlName(String lName) {
this.lName = lName;
}

/**
* @return the name
*/
public String getlName() {
return lName;
}
/**
* @return the number
*/
public int getNumber() {
return number;
}

/**
* @param age the age to set
*/
public void setNumber(int number) {
this.number = number;
}

public String toString(){
return this.code + \" \" + this.fName + \" \" + this.lName + \" \" + this.email + \" \" + this.number;
}

}

To Write string in file use follwing code pass the filename, string and append parameters:

class insert{
       private String code;
private String filename;
private String output;
private boolean append;
       public static void insert(String filename, String output, boolean append)
       {
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(filename, append));
out.write(output);
out.newLine();
out.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
       }
}

public class Person{ //========== Properties ============= private int password; private String firstName; private String lastName; private String email; //====
public class Person{ //========== Properties ============= private int password; private String firstName; private String lastName; private String email; //====
public class Person{ //========== Properties ============= private int password; private String firstName; private String lastName; private String email; //====
public class Person{ //========== Properties ============= private int password; private String firstName; private String lastName; private String email; //====
public class Person{ //========== Properties ============= private int password; private String firstName; private String lastName; private String email; //====
public class Person{ //========== Properties ============= private int password; private String firstName; private String lastName; private String email; //====

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site