import java.util.*;
public class S10105810_Assignment
{
public static void main(String[] args)
{
int[] code = new int[20]; // array for codes
String[] title = new String[20]; // array for booktitles
double[] price = new double[20]; // array for price of books
int[] qty = new int[20]; // array for quantity of books
int count = 0;
int choice= 1;
int number =0;
int search=0; // search when there user enter 1
int idex=-1; // return if search cannot be found
int key; // To enter the book that user want to find
int serialnum=1; // To indicate the s/no of the book that is out of stock. Serial num always at 1
Scanner input = new Scanner(System.in);
while(choice != 0) // when choice is not 0 to exit.
{
System.out.println("MENU " + "\n" + "====");
System.out.println("1) Display All Books" + "\n2) Display books that are out of stock" + "\n3)Add new book"+ "\n4)Update Quantity of a book"+ "\n5)Display Lowest Quantity"+ "\n6)Search a book"+ "\n7)Remove a Book"+ "\n0)Exit");
System.out.print("Enter your option : " );
choice = input.nextInt();
if(choice==1)
{
System.out.println("Option 1. Display all books");
if(count<=6)
{
if(number>0)
{
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1;i<count+1;i++)
{
System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
if(idex!=-1)
{
RemovedBook(code,title,price,qty,count,number);
}
}
else
{
if(idex==-1)
{
count = initialization(code,title,price,qty);
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1;i<count+1;i++)
{
System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
}
if(idex!=-1)
{
RemovedBook(code,title,price,qty,count,number);
}
}
}
if(count>6)
{
if(number>0)
{
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1;i<count+1;i++)
{
System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
if(idex!=-1)
{
RemovedBook(code,title,price,qty,count,number);
}
}
else
{
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1;i<count+1;i++)
{
System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
if(idex!=-1)
{
RemovedBook(code,title,price,qty,count,number);
}
}
}
}
if(choice==2)
{
OutOfStock(code,title,price,qty,count,serialnum);
}
if(choice==3)
{
count=NewBook(code,title,price,qty,count);
}
if(choice==4)
{
number=updateQuantity(code,title,price,qty,count,number);
}
if(choice==5)
{
lowestQuantity(code,title,price,qty,count);
}
if(choice==6)
{
System.out.println("Option 6. Searching for a book");
System.out.print("Enter 1 if you wish to proceed with searching for a particular book(0 to skip): ");
search= input.nextInt();
while(search==1)
{
System.out.print("Enter the book you want to search for: ");
key= input.nextInt();
idex = searchingforBook(code,key,count);
if(idex == -1)
{
System.out.println("No such book Exist");
}
else
{
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
System.out.println(idex + "\t" + code[idex-1] + "\t" + price[idex-1] + "\t" + qty[idex-1] + "\t" +"\t"+ title[idex-1]);
}
System.out.print("Do you want to continue searching books? 1 to continue, 0 to end: ");
search=input.nextInt();
}
}
if(choice==7)
{
System.out.println("Option 7. Removing a book");
System.out.print("Enter 1 if you wish to proceed with removing a particular book(0 to skip): ");
search= input.nextInt();
while(search==1)
{
System.out.print("Enter the book you want to remove: ");
key= input.nextInt();
idex = removingBook(code,key,count);
if(idex == -1)
{
System.out.println("No such book");
}
else
{
code[idex-1]=0;
qty[idex-1]=0;
title[idex-1]="";
price[idex-1]=0;
System.out.println("Book of Serial no."+(idex)+" is Removed.");
}
System.out.print("If Continue removing books? Enter 1 to continue or 0 to End Removing book: ");
search=input.nextInt();
}
}
}
}
public static int initialization(int[] code, String[] title, double[] price, int[] qty)
{
code[0] = 9877;
title[0] = "Awaken The Giant Within";
price[0] = 19.75;
qty[0] = 6;
code[1] = 9965;
title[1] = "Being Happy!";
price[1] = 17.33;
qty[1] = 9;
code[2] = 9126;
title[2] = "Chicken Soup For The Teenage Soul";
price[2] = 21.54;
qty[2] = 0;
code[3] = 9429;
title[3] = "Dealing with People You Can't Stand";
price[3] = 24.03;
qty[3] = 3;
code[4] = 9101;
title[4] = "Emotional Intelligence";
price[4] = 16.33;
qty[4] = 11;
code[5] = 9222;
title[5] = "Follow Your Heart";
price[5] = 21.09;
qty[5] = 15;
return 6;
}
public static void OutOfStock (int[] code, String[] title, double[] price, int[] qty, int count, int serialnum)
{
System.out.println("Option 2.Display books out of stock");
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1; i<count; i++)
{
if(qty[i-1]==0)
{
System.out.println(serialnum + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
}
}
public static int NewBook (int[] code, String[] title, double[] price, int[] qty,int count)
{
int num=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter 1 to enter book or 0 to exit adding book menu:");
num = input.nextInt();
while(num==1)
{
System.out.println("Option 3. Add new book");
System.out.print("Enter a new code: ");
code[count] = input.nextInt();
input.nextLine();
System.out.print("Enter a new title: ");
title[count] = input.nextLine();
System.out.print("Enter a new price: ");
price[count] = input.nextDouble();
System.out.print("Enter a new quantity: ");
qty[count] = input.nextInt();
count++;
System.out.println("One book added");
System.out.print("Do you want to continue? 1 for Yes, 0 for no");
num = input.nextInt();
}
return count;
}
public static int updateQuantity (int[] code, String[] title, double[] price, int[] qty,int count, int num2)
{
System.out.println("Option 4. Update quantity");
int quantity=0;
Scanner input = new Scanner(System.in);
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1;i<count+1;i++)
{
System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
System.out.print("Enter the Serial Number of the book to update the quantity: ");
num2=input.nextInt();
System.out.print("Enter quantity to increase or decrease: ");
quantity=input.nextInt();
qty[num2-1] = qty[num2-1] + quantity;
System.out.println("Quantity updated");
return num2;
}
public static void lowestQuantity (int[] code, String[] title, double[] price, int[] qty,int count)
{
System.out.println("Option 5. Lowest quantity");
int lowestquantity = qty[0];
String bookTitle="";
for (int i=1;i<count+1;i++)
{
if (qty[i]<lowestquantity)
{
lowestquantity=qty[i];
bookTitle=title[i];
}
}
System.out.println("Lowest quantity book is "+bookTitle+" at "+lowestquantity);
}
public static int searchingforBook (int[] code,int searchKey,int count)
{
for(int i=1;i<count+1;i++)
{
if(code[i]==searchKey)
{
return i+1;
}
}
return -1;
}
public static int removingBook (int[] code,int searchKey,int count)
{
for(int i=1;i<count+1;i++)
{
if(code[i]==searchKey)
{
return i+1;
}
}
return -1;
}
public static void RemovedBook(int[] code, String[] title, double[] price, int[] qty,int count,int num2)
{
System.out.println("S/No"+"\t"+"Code"+"\t"+"Price"+"\t"+"Quantity"+"\t"+"Title"+"\n----"+"\t"+ "----"+"\t"+ "-----"+"\t"+"-----"+"\t"+"\t"+"-----");
for(int i=1;i<count+1;i++)
{
System.out.println(i + "\t" + code[i-1] + "\t" + price[i-1] + "\t" + qty[i-1] + "\t" +"\t"+ title[i-1]);
}
}
}