You are on page 1of 20

Book & Customer -File Handling:(L1-1)

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

class Source{
public static void main(String args[])
{

}}
class Address implements Serializable{

String city;
String State;
int zip;
String Country;
public Address(String city, String state, int zip, String country) {

this.city = city;
State = state;
this.zip = zip;
Country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return State;
}
public void setState(String state) {
this.State = state;
}

public int getZip() {


return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
this.Country = country;
}
@Override
public String toString() {
return "Address [city=" + city + ", State=" + State + ", zip=" + zip +
", Country=" + Country + "]";
}
}
class Book implements Serializable{

int bookId;
String title;
String description;
String author;
int totalQuantity;
int availableQuantity;
double price;
double rentPerDay;

public Book(int bookId, String title, String description, String author, int
totalQuantity, int availableQuantity,
double price, double rentPerDay) {
this.bookId = bookId;
this.title = title;
this.description = description;
this.author = author;
this.totalQuantity = totalQuantity;
this.availableQuantity = availableQuantity;
this.price = price;
this.rentPerDay = rentPerDay;
}
public Book() {}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getTotalQuantity() {
return totalQuantity;
}

public void setTotalQuantity(int totalQuantity) {


this.totalQuantity = totalQuantity;
}
public int getAvailableQuantity() {
return availableQuantity;
}
public void setAvailableQuantity(int availableQuantity) {
this.availableQuantity = availableQuantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getRentPerDay() {
return rentPerDay;
}
public void setRentPerDay(double rentPerDay) {
this.rentPerDay = rentPerDay;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", title=" + title + ", description="
+ description + ", author=" + author
+ ", totalQuantity=" + totalQuantity + ",
availableQuantity=" + availableQuantity + ", price=" + price
+ ", rentPerDay=" + rentPerDay + "]";
}
}
interface AdminService{
Book[] readBooks() ;
void writeBooks();
void insertBooks(Book book);
}
class AdminServiceImpl implements AdminService {
public static Book booksArray[] = new Book[5];
public static int count = 0;
File f1 = new File("books.txt");
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;

public void insertBooks(Book book) {


booksArray[count] = book;
count++;
}

public void writeBooks() {


try {
fos = new FileOutputStream(f1);
} catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException");
}
try {
oos = new ObjectOutputStream(fos);
for (int i = 0; i < booksArray.length; i++) {
if (booksArray[i] != null) {
oos.writeObject(booksArray[i]);
} else {
break;
}
}
oos.close();
} catch (IOException ioe) {
System.out.println("IOException");
}
}

public Book[] readBooks() {


try {
fis = new FileInputStream(f1);
} catch (FileNotFoundException fnfe2) {
System.out.println("FileNotFoundException2");
}
try {
ois = new ObjectInputStream(fis);
for (int i = 0; i < booksArray.length; i++) {
if (ois.available() > 0) {
try {
booksArray[i] = (Book) ois.readObject();
} catch (ClassNotFoundException cnfe) {
System.out.println("ClassNotFoundException");
}

} else {
break;
}
}
ois.close();
} catch (IOException ioe2) {
System.out.println("IOException2");
}

return booksArray;
}
}
class Customer implements Serializable{

int userId;
String emailId;
String password;
String firstName;
String lastName;
String city;
String gender;
long phoneNumber;
Address address;
public Customer(int userId, String emailId, String password, String
firstName, String lastName, String city,
String gender, long phoneNumber, Address address) {
this.userId = userId;
this.emailId = emailId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.gender = gender;
this.phoneNumber = phoneNumber;
this.address = address;
}
@Override
public String toString() {
return "Customer [userId=" + userId + ", emailId=" + emailId + ",
password=" + password + ", firstName="
+ firstName + ", lastName=" + lastName + ", city=" + city +
", gender=" + gender + ", phoneNumber="
+ phoneNumber + ", address=" + address + "]";
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}

}
interface CustomerService{
void writeCustomer();
Customer[] readCustomer();
void insertCustomer(Customer customer);
}
class CustomerServiceImpl implements CustomerService {
public static Customer[] customerArray = new Customer[5];
public static int count = 0;
File f2 = new File("customer.txt");
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;

public void insertCustomer(Customer customer) {


customerArray[count] = customer;
count++;
}

public void writeCustomer() {


try {
fos = new FileOutputStream(f2);
} catch (FileNotFoundException fnfe3) {
System.out.println("FileNotFoundException3");
}
try {
oos = new ObjectOutputStream(fos);
for (int i = 0; i < customerArray.length; i++) {
if (customerArray[i] != null) {
oos.writeObject(customerArray[i]);
} else {
break;
}
}
oos.close();
} catch (IOException ioe3) {
System.out.println("IOException3");
}

public Customer[] readCustomer() {


try {
fis = new FileInputStream(f2);
} catch (FileNotFoundException fnfe4) {
System.out.println("FileNotFoundException4");
}
try {
ois = new ObjectInputStream(fis);
for (int i = 0; i < customerArray.length; i++) {
if (ois.available() > 0) {
try {
customerArray[i] = (Customer) ois.readObject();
} catch (ClassNotFoundException cnfe2) {
System.out.println("ClassNotFoundException2");
}

} else {
break;
}
}
ois.close();
} catch (IOException ioe4) {
System.out.println("IOException4");
}

return customerArray;
}
}
-----------------------------------------------------------------------------------
-
Sore BooksDetails to Collection:(L2-1)

import java.util.*;
import java.util.Map.Entry;

interface AdminService{
void addBooks(Book book);
void displayBooks();
}
class Source{
public static void main(String []args){

}
}
class Customer implements Comparable<Customer>{

int userId;
String emailId;
String password;
String firstName;
String lastName;
String city;
String gender;
long phoneNumber;
Address address;
public Customer(int userId, String emailId, String password, String
firstName, String lastName, String city,
String gender, long phoneNumber, Address address) {
this.userId = userId;
this.emailId = emailId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.gender = gender;
this.phoneNumber = phoneNumber;
this.address = address;
}
@Override
public String toString() {
return "Customer [userId=" + userId + ", emailId=" + emailId + ",
password=" + password + ", firstName="
+ firstName + ", lastName=" + lastName + ", city=" + city +
", gender=" + gender + ", phoneNumber="
+ phoneNumber + ", address=" + address + "]";
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public int compareTo(Customer c){
return this.userId-c.getUserId();
}
}
class Address{
String city;
String State;
int zip;
String Country;
public Address(String city, String state, int zip, String country) {
this.city = city;
State = state;
this.zip = zip;
Country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return State;
}
public void setState(String state) {
this.State = state;
}

public int getZip() {


return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
this.Country = country;
}
@Override
public String toString() {
return "Address [city=" + city + ", State=" + State + ", zip=" + zip +
", Country=" + Country + "]";
}

}
class Book {

int bookId;
String title;
String description;
String author;
int totalQuantity;
int availableQuantity;
double price;
double rentPerDay;

public Book(int bookId, String title, String description, String author, int
totalQuantity, int availableQuantity,
double price, double rentPerDay) {
super();
this.bookId = bookId;
this.title = title;
this.description = description;
this.author = author;
this.totalQuantity = totalQuantity;
this.availableQuantity = availableQuantity;
this.price = price;
this.rentPerDay = rentPerDay;
}
public Book() {}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getTotalQuantity() {
return totalQuantity;
}

public void setTotalQuantity(int totalQuantity) {


this.totalQuantity = totalQuantity;
}
public int getAvailableQuantity() {
return availableQuantity;
}
public void setAvailableQuantity(int availableQuantity) {
this.availableQuantity = availableQuantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getRentPerDay() {
return rentPerDay;
}
public void setRentPerDay(double rentPerDay) {
this.rentPerDay = rentPerDay;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", title=" + title + ", description="
+ description + ", author=" + author
+ ", totalQuantity=" + totalQuantity + ",
availableQuantity=" + availableQuantity + ", price=" + price
+ ", rentPerDay=" + rentPerDay + "]";
}
}
class TestCustomer {
public static TreeMap<Customer,Book> customerMap=new
TreeMap<Customer,Book>();

public void addCustomer(Customer cust,Book book) {


customerMap.put(cust,book);
}
public void displayCustomer() {
for(Entry<Customer, Book> m:customerMap.entrySet())
System.out.println(m.getKey()+" "+m.getValue());
}
}
class AdminServiceImpl implements AdminService {
static HashSet<Book> bookSet=new HashSet<Book>();
public void addBooks(Book book) {
Iterator<Book> I=bookSet.iterator();
while(I.hasNext()) {
if(I.next()==book)
return;
}bookSet.add(book);
}
public void displayBooks() {
Iterator<Book> I=bookSet.iterator();
while(I.hasNext())
System.out.println(I.next());
}

}
-----------------------------------------------------------------------------------
-
Customer and Book File-Collection:(L2-2)

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

class Source{
public static void main(String args[])
{

}}
class Book implements Serializable{

int bookId;
String title;
String description;
String author;
int totalQuantity;
int availableQuantity;
double price;
double rentPerDay;
public Book(int bookId, String title, String description, String author, int
totalQuantity, int availableQuantity,
double price, double rentPerDay) {
this.bookId = bookId;
this.title = title;
this.description = description;
this.author = author;
this.totalQuantity = totalQuantity;
this.availableQuantity = availableQuantity;
this.price = price;
this.rentPerDay = rentPerDay;
}
public Book() {}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getTotalQuantity() {
return totalQuantity;
}

public void setTotalQuantity(int totalQuantity) {


this.totalQuantity = totalQuantity;
}
public int getAvailableQuantity() {
return availableQuantity;
}
public void setAvailableQuantity(int availableQuantity) {
this.availableQuantity = availableQuantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getRentPerDay() {
return rentPerDay;
}
public void setRentPerDay(double rentPerDay) {
this.rentPerDay = rentPerDay;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", title=" + title + ", description=" +
description + ", author=" + author
+ ", totalQuantity=" + totalQuantity + ", availableQuantity=" +
availableQuantity + ", price=" + price
+ ", rentPerDay=" + rentPerDay + "]";
}
}
interface AdminService{
void insertBooks(Book book);
void writeBooks();
List<Book> readBooks();
}
class AdminServiceImpl implements AdminService {

public static ArrayList<Book> booksList=new ArrayList<Book>();

@Override
public void insertBooks(Book book) {
if(book!=null){
booksList.add(book);}
}

@Override
public void writeBooks() {
if(booksList.isEmpty()==false){
try {
ObjectOutputStream oos1 = new ObjectOutputStream(new
FileOutputStream("books.txt"));
for (Book t : booksList) {
oos1.writeObject(t);
}
oos1.close();
} catch (FileNotFoundException fnfe2) {
System.out.println("FileNotFoundException2");
} catch (IOException ioe2) {
System.out.println("IOException2");
}
catch (Exception e) {
System.out.println("hiii");
}
}
}

@Override
public List<Book> readBooks() {
try {
ObjectInputStream ois1 = new ObjectInputStream(new
FileInputStream("books.txt"));
while (ois1.available() > 0) {
Book t = (Book) ois1.readObject();
booksList.add(t);
ois1.close();
}
} catch (FileNotFoundException fnfe4) {
System.out.println("FileNotFoundException4");
} catch (IOException ioe4) {
System.out.println("IOException4");
} catch (ClassNotFoundException cnfe2) {
System.out.println("ClassNotFoundException2");
}

return booksList;
}
}
class Customer implements Serializable{

int userId;
String emailId;
String password;
String firstName;
String lastName;
String city;
String gender;
long phoneNumber;
Address address;
public Customer(int userId, String emailId, String password, String firstName,
String lastName, String city,
String gender, long phoneNumber, Address address) {
this.userId = userId;
this.emailId = emailId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.gender = gender;
this.phoneNumber = phoneNumber;
this.address = address;
}
@Override
public String toString() {
return "Customer [userId=" + userId + ", emailId=" + emailId + ", password=" +
password + ", firstName="
+ firstName + ", lastName=" + lastName + ", city=" + city + ", gender=" +
gender + ", phoneNumber="
+ phoneNumber + ", address=" + address + "]";
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}

}
interface CustomerService{
void insertCustomer(Customer customer);
void writeCustomer();
List<Customer> readCustomer();

}
class CustomerServiceImpl implements CustomerService {
public static ArrayList<Customer> customerList=new ArrayList<Customer>();

public void insertCustomer(Customer customer) {


customerList.add(customer);
}

public void writeCustomer() {


try {
ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("customer.txt"));
for (Customer c : customerList) {
oos.writeObject(c);
}
oos.close();
} catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException");
} catch (IOException ioe) {
System.out.println("IOException");
}
}

public List<Customer> readCustomer() {


try {
ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("customer.txt"));
while (ois.available() > 0) {
Customer c = (Customer) ois.readObject();
customerList.add(c);
ois.close();
}
} catch (FileNotFoundException fnfe3) {
System.out.println("FileNotFoundException3");
} catch (IOException ioe3) {
System.out.println("IOException3");
} catch (ClassNotFoundException cnfe) {
System.out.println("ClassNotFoundException");
}

return customerList;
}
}
class Address implements Serializable{

String city;
String State;
int zip;
String Country;
public Address(String city, String state, int zip, String country) {

this.city = city;
State = state;
this.zip = zip;
Country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return State;
}
public void setState(String state) {
this.State = state;
}

public int getZip() {


return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
this.Country = country;
}
@Override
public String toString() {
return "Address [city=" + city + ", State=" + State + ", zip=" + zip + ",
Country=" + Country + "]";
}
}
-----------------------------------------------------------------------------------
----
Admin and Customer Login:(L2-3)

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.InvalidNameException;

class Address {

private String city;


private String state;
private int zip;
private String country;

public Address(String city, String state, int zip, String country) {


super();
this.city = city;
this.state = state;
this.zip = zip;
this.country = country;
}
}
class Customer implements Serializable{

private int userId;


String emailId;
String password;
private String firstName;
private String lastName;
private String city;
private String gender;
private long phoneNumber;
private Address address;
public Customer(int userId, String emailId, String password, String
firstName, String lastName, String city,
String gender, long phoneNumber, Address address) {
super();
this.userId = userId;
this.emailId = emailId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.gender = gender;
this.phoneNumber = phoneNumber;
this.address = address;
}
public Customer()
{

}
@Override
public String toString() {
return "Customer [userId=" + userId + ", emailId=" + emailId + ",
password=" + password + ", firstName="
+ firstName + ", lastName=" + lastName + ", city=" + city +
", gender=" + gender + ", phoneNumber="
+ phoneNumber + ", address=" + address + "]";
}
}
class Admin
{
private String name;
String email;
String password;
public Admin()
{

}
public Admin(String name, String email, String password) {
super();
this.name = name;
this.email = email;
this.password = password;
}

}
interface AdminService{
public boolean validateAdmin(String email,String password);
}
class AdminServiceImpl implements AdminService
{
public static Admin[] adminArray=new Admin[5];

AdminServiceImpl()
{
adminArray[0]=new Admin("Krithick","krithick@gmail.com","krithi");
adminArray[1]=new Admin("Raja","rajan@gmail.com","rajan#345");
adminArray[2]=new Admin("Chandrav","chand@gmail.com","wel$234");
adminArray[3]=new Admin("Ankit","ankit@gmail.com","kit@56");
adminArray[4]=new Admin("Akilan","akilan@gmail.com","ak*76");
}

@Override
public boolean validateAdmin(String email, String password) {
// TODO Auto-generated method stub
for(int i=0;i<5;i++){
if(adminArray[i].email.equals(email) &&
adminArray[i].password.equals(password)){
return true;
}
}
return true;
}
}
interface CustomerService {

public boolean validateCustomer(String email,String password);

class CustomerServiceImpl implements CustomerService{

public static Customer[] customerArray=new Customer[5];


public CustomerServiceImpl()
{
customerArray[0]=
new
Customer(101,"raj@gmail.com","xxxxxx","Raj","Kunar","Chennai","Male",
9001018761l,new
Address("Chennai","TamilNadu",600075,"India"));
customerArray[1]=
new
Customer(102,"krithick@gmail.com","xxxxxx","Krithick","Rajan","Chennai","Male",
9001018761l,new
Address("Chennai","TamilNadu",600075,"India"));
customerArray[2]=
new
Customer(103,"chandrav@gmail.com","xxxxxx","Chandrav","Rajan","Bangalore","Male",
9001018761l,new
Address("Chennai","TamilNadu",600075,"India"));
customerArray[3]=
new
Customer(104,"shan@mail.com","an#2","Rajan","Arun","Bangalore","Male",
9001018761l,new
Address("Chennai","TamilNadu",600075,"India"));
customerArray[4]=
new
Customer(105,"akshay@gmail.com","xxxxxx","Akshay","Kumar","Mumbai","Male",
9001018761l,new
Address("Chennai","TamilNadu",600075,"India"));

}
@Override
public boolean validateCustomer(String email, String password) {
// TODO Auto-generated method stub
for(int i=0;i<5;i++){
if(customerArray[i].emailId.equals(email) &&
customerArray[i].password.equals(password)){
return true;
}
}
return true;
}
}

class Source{

public static void main(String args[])


{

}
}

You might also like