Friday, May 19, 2017

Bookstore a java example code

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);
    }

}

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :