Sprint-4 Stage 2
Sprint-4 Stage 2
import java.util.*;
class Customer {
private int userID;
private String emailid;
private String password;
private String firstName;
private String lastName;
private String city;
private String gender;
private long phoneNumber;
private Address address;
public Customer() {
}
@Override
public String toString() {
String result = userID + " ";
result += emailid + " ";
result += password + " ";
result += firstName + " ";
result += lastName + " ";
result += city + " ";
result += gender + " ";
result += phoneNumber + "/n";
result+=address;
return result;
}
class Address {
private String city;
private String state;
private int zip;
private String country;
public Address() {
}
@Override
public String toString() {
String result ="Address " + "[city=" + city + "," + " ";
result += "state=" + state + "," + " ";
result += "zip=" + zip + "," + " ";
result += "country=" + country + "]";
return result;
}
}
}
}
-----------------------------------------------------------------
Admin and Book Classes:(L1-2)
import java.util.*;
class Book {
int bookId;
String title;
String description;
String author;
double price;
int totalQuantity;
int availableQuantity;
double rentPerDay;
-------------------------------------------------------------------------
Customer Name Validator:(L2-1)
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.naming.InvalidNameException;
class Customer{
String firstName;
String lastName;
//setters
public void setFirstName(String fn) throws InvalidNameException{
int result;
result= Pattern.matches("^[A-Za-z]{6,}",fn) ? 1 : -1;
if(result==-1){throw new InvalidNameException();}
this.firstName=fn;
}
public void setLastName(String ln) throws InvalidNameException{
int result;
result= Pattern.matches("^[A-Za-z]{6,}",ln) ? 1 : -1;
if(result==-1){throw new InvalidNameException();}
this.lastName=ln;
}
//getters
public String getFirstName(){return firstName;}
public String getLastName(){return lastName;}
}
class Source{
public static void main(String[] args) throws InvalidNameException{
}
}
----------------------------------------------------------
Customer Mobile Number Exception:(L2-2)
import java.util.regex.Pattern;
class InvalidPhoneNumberExcception extends Exception {
public InvalidPhoneNumberExcception() {
}
class Customer {
private int userId;
private String emailId;
private String password;
private String firstName;
private String lastName;
private String city;
private String gender;
private long phoneNumber;
private int result;
Customer() {
@Override
public String toString() {
return String.format(
"Customer [userId=%s, emailId=%s, password=%s, firstName=%s, lastName=%s,
city=%s, gender=%s, phoneNumber=%s]",
userId, emailId, password, firstName, lastName, city, gender, phoneNumber);
}
}
-----------------------------------------------------------------
interface CustomerService {
public void createCustomer(Customer customer);
public Customer updateCustomer(Customer customer) throws
CustomerNotFoundException;
public void deleteCustomer(int id) throws CustomerNotFoundException;
public Customer searchCustomer(int id) throws CustomerNotFoundException;
public Customer[] getCustomers();
}
@Override
public void createCustomer(Customer customer) {
customerArray[index++] = customer;
}
@Override
public Customer updateCustomer(Customer customer) throws
CustomerNotFoundException {
int flag = 0;
for(int ctr=0; ctr<5; ctr++)
{
if(customer.equals(customerArray[ctr]))
{
flag = 1;
break;
}
}
if(flag == 0)
{
throw new CustomerNotFoundException();
}
return customer;
}
@Override
public void deleteCustomer(int id) throws CustomerNotFoundException {
int flag = 0;
for(int ctr=0; ctr<5; ctr++)
{
if(id == customerArray[ctr].getUserId())
{
flag = 1;
break;
}
}
if(flag == 0)
{
throw new CustomerNotFoundException();
}
}
@Override
public Customer searchCustomer(int id) throws CustomerNotFoundException {
int flag = 0, ctr;
for(ctr=0; ctr<5; ctr++)
{
if(id == customerArray[ctr].getUserId())
{
flag = 1;
break;
}
}
if(flag == 0)
{
throw new CustomerNotFoundException();
}
return customerArray[ctr];
}
@Override
public Customer[] getCustomers() {
return customerArray;
}
}
public CustomerNotFoundException() {
}
class Customer {
private long userId;
private String emailId;
private String password;
private String firstName;
private String lastName;
private String city;
private String gender;
private long phoneNumber;
private Address address;
Customer() {
@Override
public String toString() {
return String.format(
"Customer [userId=%s, emailId=%s, password=%s, firstName=
%s, lastName=%s, city=%s, gender=%s, phoneNumber=%s, address=%s]",
userId, emailId, password, firstName, lastName, city,
gender, phoneNumber, address);
}
class Address {
private String city;
private String state;
private int zip;
private String country;
Address() {
@Override
public String toString() {
return String.format("Address [city=%s, state=%s, zip=%s, country=%s]",
city, state, zip, country);
}
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;
}
@Override
public Book updateBook(Book book) throws BookNotFoundException{
for(int i=0;i<5;i++)
{
if(book.getBookId()==bookArray[i].bookId)
{
bookArray[i]=book;
return bookArray[i];
}
}
throw new BookNotFoundException();
}
@Override
public void deleteBook(int id) {
for(int i=0;i<5;i++)
{
if(id==bookArray[i].bookId)
{
bookArray[i]=null;
break;
}
}
throw new BookNotFoundException();
}
@Override
public Book searchBook(int id) {
for(int i=0;i<5;i++)
{
if(bookArray[i].getBookId()==id) {
return bookArray[i];
}
}
throw new BookNotFoundException();
}
@Override
public Book[] getBooks() {
return bookArray;
}
}
------------------------------------------------------------------
BookRental Service:(L3-3)
import java.util.Date;
import java.util.regex.Pattern;
interface BookRentalService {
void createBookRental(BookRental bookRental);
BookRental[] getBookRentals();
@Override
public BookRental updateBookRental(BookRental bookRental) {
int ctr;
for (ctr = 0; ctr < size; ctr++) {
if (bookRentalArray[ctr] == bookRental) {
bookRentalArray[ctr] = bookRental;
break;
}
}
return bookRentalArray[ctr];
}
@Override
public void deleteBookRental(int rentalId) {
for (int ctr = 0; ctr < size; ctr++) {
if (bookRentalArray[ctr].getRentalId() == rentalId) {
bookRentalArray[ctr] = null;
}
}
}
@Override
public BookRental[] getBookRentals() {
return bookRentalArray;
}
@Override
public BookRental[] searchByCustomerId(int customerId) {
BookRental[] result = new BookRental[2];
int rindex = 0;
for (int ctr = 0; ctr < 5; ctr++) {
if (customerId == bookRentalArray[ctr].getUserId()) {
result[rindex] = bookRentalArray[ctr];
rindex++;
}
}
return result;
}
@Override
public BookRental[] searchByBookId(int bookId) {
BookRental[] result = new BookRental[2];
int rindex = 0;
for (int ctr = 0; ctr < 5; ctr++) {
if (bookId == bookRentalArray[ctr].getBookId()) {
result[rindex] = bookRentalArray[ctr];
rindex++;
}
}
return result;
}
}
class BookRental {
private int rentalId;
private int bookId;
private int userId;
private int quantity;
private Date startDate;
private Date endDate;
private double totalAmount;
private Date returnedDate;
public BookRental(int rentalId, int bookId, int userId, int quantity, Date
startDate, Date endDate,
double totalAmount, Date returnedDate) {
this.rentalId = rentalId;
this.bookId = bookId;
this.userId = userId;
this.quantity = quantity;
this.startDate = startDate;
this.endDate = endDate;
this.totalAmount = totalAmount;
this.returnedDate = returnedDate;
}
interface CustomerService {
public void createCustomer(Customer customer);
@Override
public void createCustomer(Customer customer) {
customerArray[size] = customer;
size++;
}
@Override
public Customer updateCustomer(Customer customer) throws
CustomerNotFoundException {
int flag = 0;
for (int ctr = 0; ctr < 5; ctr++) {
if (customer.equals(customerArray[ctr])) {
flag = 1;
break;
}
}
if (flag == 0) {
throw new CustomerNotFoundException();
}
return customer;
}
@Override
public void deleteCustomer(int id) throws CustomerNotFoundException {
int flag = 0;
for (int ctr = 0; ctr < size; ctr++) {
if (id == customerArray[ctr].getUserId()) {
flag = 1;
break;
}
}
if (flag == 0) {
throw new CustomerNotFoundException();
}
}
@Override
public Customer searchCustomer(int id) throws CustomerNotFoundException {
int flag = 0, ctr;
for (ctr = 0; ctr < size; ctr++) {
if (id == customerArray[ctr].getUserId()) {
flag = 1;
break;
}
}
if (flag == 0) {
throw new CustomerNotFoundException();
}
return customerArray[ctr];
}
@Override
public Customer[] getCustomers() {
return customerArray;
}
@Override
public BookRental[] rentBook(BookRental bookRental) {
service.createBookRental(bookRental);
return service.getBookRentals();
}
@Override
public BookRental[] getRentalBookDetails(int customerId) {
return (service.searchByCustomerId(customerId));
}
}
class Customer {
private int userId;
private String emailId;
private String password;
private String firstName;
private String lastName;
private String city;
private String gender;
private long phoneNumber;
private Address address;
private int result;
Customer() {
@Override
public String toString() {
return String.format(
"Customer [userId=%s, emailId=%s, password=%s, firstName=
%s, lastName=%s, city=%s, gender=%s, phoneNumber=%s, address=%s]",
userId, emailId, password, firstName, lastName, city,
gender, phoneNumber, address);
}
class Address {
private String city;
private String state;
private int zip;
private String country;
Address() {
@Override
public String toString() {
return String.format("Address [city=%s, state=%s, zip=%s, country=%s]",
city, state, zip, country);
}
public InvalidNameException() {
}
public CustomerNotFoundException() {
}
}
}
--------------------------------------------------------------------------------