Friday, May 19, 2017

Bookstore a java example code

May 19, 2017

Bookstore a java example code
LEVEL: BEGINNER

java


This example is good for understanding
  1. Array
  2. methods
  3. conditions(if and else)
the process of this bookstore: 1.First, we have a collection of books name:
static String[] book = { "java", "c", "c++", "php", "javascript", "html" };
2.we ask the user to which book is he want? 3. we search that book name in our list of books 4. if we find that book we ask again to our user about his occupation 5. because we want to give a discount to our user. here all source code
package com.blogspot.shudiptotrafder;

import java.util.Scanner;

public class BookStore {

    static String[] book = { "java", "c", "c++", "php", "javascript", "html" };
    static Scanner scanner = new Scanner(System.in);

    final static float studentdiscount = .3f;
    final static float teacherdiscount = .2f;
    static boolean flag = true;

    public static void main(String[] args) {
        if (flag) {
            pl("*************WELCOME TO OUR BOOK STORE**********");
        }
        pl("Which book do you want?");
        p("Answer: ");
        String bookname = scanner.nextLine();

        if (bookname.toLowerCase().equals(book[0])) {
            wantedbook(bookname);
            discount();

        } else if (bookname.toLowerCase().equals(book[1])) {
            wantedbook(bookname);
            discount();

        } else if (bookname.toLowerCase().equals(book[2])) {
            wantedbook(bookname);
            discount();

        } else if (bookname.toLowerCase().equals(book[3])) {
            wantedbook(bookname);
            discount();

        } else if (bookname.toLowerCase().equals(book[4])) {
            wantedbook(bookname);
            discount();

        } else if (bookname.toLowerCase().equals(book[5])) {
            wantedbook(bookname);
            discount();

        } else {
            pl("Sorry we don't have " + bookname + " book.");
        }

        pl("\nDo you Want more book?");
        p("Answer: ");
        String ans = scanner.nextLine();
        flag = false;

        if (ans.toLowerCase().equals("yes")) {
            pl("\n");
            String[] ret = null;
            main(ret);
        } else {
            pl("\n");
            pl("********Thank YOU FOR SHOPPING********");
        }

    }

    // methods

    public static void discount() {
        pl("\nAre you Student or Teacher or General Customar?");
        p("Answer: ");
        String userinput = scanner.nextLine();

        if (userinput.toLowerCase().equals("student")) {
            calculateprice(studentdiscount);
        } else if (userinput.toLowerCase().equals("teacher")) {
            calculateprice(teacherdiscount);
        } else {
            pl("You Can not get Discount.");
            pl("your Total Payable Price is 200 TK.");

        }

    }

    public static void calculateprice(float discount) {
        double price = 200;
        price -= price * discount;
        showprice(price);
    }

    public static void showprice(double price) {
        String prices = String.format("%.2f", price);
        pl("your Total Payable Price is : " +prices+ " TK.");

    }

    public static void wantedbook(Object object) {
        pl("You Want " + object + " book. price 200 Tk.");
    }

    public static void p(Object object) {
        System.out.print(object);
    }

    public static void pl(Object object) {
        System.out.println(object);
    }

}

Read More

Java String Example

May 19, 2017

String in Java
java string

Strings Example

Example 1:

Get first two characters of any String
public String firstTwo(String str) {
  if (str.length() >= 2) {
    return str.substring(0, 2);
  } else {
    return str;
  }
  // Solution notes: need an if/else structure to call substring if the length
  // is 2 or more, and otherwise return the string itself
} 

Alternative example of getting first two characters of any strings

public String firstTwo(String str) {

    if (str.length() >= 2) {

        char s = str.charAt(0);
        char s1 = str.charAt(1);

    String string = String.valueOf(s) + s1;
 
 return string;
  }
  return str;
}

Example 2:

get last two characters of any string
public static void main(String[] args) {

    String str = "hello";

    System.out.println(str.substring(str.length()-2));
}

Example 3:

Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".
public String firstHalf(String str) {
  return str.substring(0,(str.length())/2); 
}

Example 4:

Given a string, return a new version without the first and last character
public String withoutEnd(String str) {
    return str.substring(1,str.length()-1);
  }

Example 5:

Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside
public String comboString(String a, String b) {
  
  int a1 = a.length();
  
  int b1 = b.length();
  
  if(a1 > b1){
     return b+a+b;
  }
  
  return a+b+a;
}

Example 6:

Given 2 strings, return their concatenation, except omit the first char of each.
public String nonStart(String a, String b) {
  
    String s = a.substring(1,a.length());
  
 String s1 = b.substring(1,b.length());
 
 return s+s1;
}

Example 7:

Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end
public String left2(String str) {
  
  if (str.length() >= 2) {
   String s = str.substring(0, 2);
   String s1 = str.substring(2, str.length());
   
   return s1+s;
  }
  return str;
}

Read More