Booklab
- 1. import java.util.*;
public class booklab
{
static Scanner in = new Scanner(System.in);
static String a, name,c="";
static int choice, flag,sort,no, judul, qty;
static String kata[] =
{"Programming","Accounting","Management","Science","Politics"};
static int n = kata.length;
static Vector<String> title = new Vector<String>() ;
public static void enter ()
{
for( int i=0;i<25;i++)
{
System.out.println("");
}
}
public static void init(){
n = kata.length;
for(int i=0;i<n;i++){
title.add(kata[i]);
}
title.remove(n-1);
}
public static void bubbleSortAsc(){
for(int i=0;i<n-1;i++){
for(int j=n-1;j>i;j--){
if(kata[j].compareTo (kata[j-1]) < 0){
String temp = kata[j-1];
kata[j-1] = kata[j];
kata[j] = temp;
}
}
}
}
public static void bubbleSortDesc(){
for(int i=0;i<n-1;i++){
for(int j=n-1;j>i;j--){
if(kata[j].compareTo (kata[j-1]) > 0){
String temp = kata[j-1];
kata[j-1] = kata[j];
kata[j] = temp;
}
}
}
}
public static void main (String[]args)
{
init();
do{
System.out.println("ttt=========");
System.out.println("tttBOOKS LAB");
System.out.println("ttt=========");
- 2. System.out.println("n1. View List of Books");
System.out.println("2. Borrow Book");
System.out.println("3. Exit");
do{
n = kata.length;
flag=0;
try{
System.out.print("nInput Your Choice : ");
choice = in.nextInt();
}catch(Exception e)
{
in.nextLine();
flag=1;
}
finally
{
in.nextLine();
}
}while(flag==1 || choice <1 ||choice >3);
switch(choice)
{
case 1:
enter();
do{
flag=0;
try{
System.out.print("nPlease input sorting type[by asc=1 /
desc=2] :");
sort = in.nextInt();
}catch(Exception e)
{
in.nextLine();
flag=1;
}
finally
{
in.nextLine();
}
}while(sort<1|| sort>2);
if(sort ==1)
{
bubbleSortAsc();
System.out.println("nSorting book by ascending");
System.out.println("============================");
for(int i=0;i<n;i++)
{
System.out.print("-"+kata[i]+" n");
in.nextLine();
}
n = title.size();
in.nextLine();
}
else if(sort==2)
{
bubbleSortDesc();
System.out.println("nSorting book by descending");
System.out.println("============================");
for(int i=0;i<n;i++)
{
- 3. System.out.print("-"+kata[i]+" n");
in.nextLine();
}
n = title.size();
//in.nextLine();
}
break;
case 2:
enter();
do{
flag=0;
try{
System.out.print("nInput your
name :");
name = in.nextLine();
for (int a = 0; a <
name.length(); a++) {
if(name.charAt(a) >48 &&
name.charAt(a) <57)
{
flag =1;
}
;
}
}catch(Exception e)
{
in.nextLine();
flag=1;
}
}while(flag==1);
do{
flag=0;
try{
System.out.print("Input book title
[prog=1,sci=2, acc=3, mene=4, pol=5] :");
judul =in.nextInt();
in.nextLine();
}catch(Exception e)
{
in.nextLine();
flag=1;
}
}while(flag==1|| judul < 1 || judul > 5);
do{
flag=0;
try{
System.out.print("Input book quantity :");
qty = in.nextInt();
in.nextLine();
}catch(Exception e)
- 4. {
in.nextLine();
flag=1;
}
}while(flag==1);
if(judul == 1)
{
System.out.println("nThanks
"+name);
System.out.println("You have
borrowed "+qty+ " Programming book");
}else if(judul == 2)
{
System.out.println("nThanks "
+name);
System.out.println("You have
borrowed "+qty+ " Science book");
}else if(judul ==3)
{
System.out.println("nThanks "
+name);
System.out.println("You have
borrowed "+qty+ " Accounting book");
}else if(judul==4)
{
System.out.println("nThanks "
+name);
System.out.println("You have
borrowed "+qty+ " Management book");
}else if(judul==5)
{
System.out.println("nThanks "
+name);
System.out.println("You have
borrowed "+qty+ " Politics book");
}
break;
}
}while(choice !=3);
}
}