Senin, 16 Juni 2014

UAS_Quiz_Preparation

import java.util.*;
    public class UAS_1100057920 {
        static Vector <String> namaBarang = new Vector();
        static Vector <Integer> harga = new Vector();
        static Vector <Integer> stock = new Vector();

        static ArrayList<Integer> cartID = new ArrayList();
        static ArrayList<Integer> cartJml = new ArrayList();

        static Scanner in = new Scanner (System.in);

        static int total;

        public static void clear (){
            for (int i = 0 ; i < 25 ; i++ ) {
                System.out.println();
            }
        }

        public static void memberCardGenerator()
        {
            //belajar Random
            Random rand = new Random();
            System.out.printf("Your Member Card: %d%d%d%d-%d%d%d%d-%d%d%d%d-%d%d%d%d\n\n", ((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)), ((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)), ((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)), ((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)),((rand.nextInt(9) +1)));
        }


        public static void init() {
            namaBarang.add("Airwalk Austin Shoes");
            namaBarang.add("Hush Puppies Shirts");
            namaBarang.add("Crocs Sandals");
            namaBarang.add("Crocodile Shirts");
            namaBarang.add("Casio Watches");
            namaBarang.add("Guess Bags");
            namaBarang.add("Giordano Pants");

            harga.add(450000);
            harga.add(350000);
            harga.add(700000);
            harga.add(525000);
            harga.add(1100000);
            harga.add(675000);
            harga.add(360000);

            stock.add(20);
            stock.add(25);
            stock.add(15);
            stock.add(18);
            stock.add(9);
            stock.add(15);
            stock.add(20);
        }

        public static void showCart(){
            //No.   | List Items           | Qty        | Price      | Subtotal
            System.out.printf ("\nCart\n%-6s%-31s%-11s%-11s%-11s\n","No.","List Items","Qty","Price","Subtotal");
            System.out.println ("==============================================================================");
            int n = cartID.size();
            int subtotal=0;
            total = 0;
            for ( int i = 0 ; i < n ; i++ ){
                subtotal = cartJml.get(i) * harga.get(cartID.get(i));
                System.out.printf ("%-6d%-31s%-11d%-11d%-11d\n",(i+1), namaBarang.get(cartID.get(i)),
                cartJml.get(i),harga.get(cartID.get(i)),subtotal);
                total+=subtotal;
            }
            System.out.printf("%55s %10d\n","Total =",total);
        }

        public static void showTable(){
            System.out.printf("Shooipy\n");
            System.out.printf("=======\n");
            memberCardGenerator();
            System.out.printf ("%-6s%-31s%-11s%-5s\n","No.","List Items","Prices","Stock");
            System.out.println ("=============================================================");
            int n = namaBarang.size();
            for ( int i = 0 ; i < n ; i++ ){
                System.out.printf ("%-6s%-31s%-11d%-5d\n",(i+1), namaBarang.get(i),harga.get(i),stock.get(i));
            }
        }

        public static void menu (){
            System.out.println ("\n\"New Thing Fashion\"  Online");
            System.out.println ("1. Sort by Price Asc");
            System.out.println ("2. Add to Cart");
            System.out.println ("3. Update Item");
            System.out.println ("4. Payment");
            System.out.println ("5. Exit");
            System.out.print ("Choose : ");
        }

        public static void swap(int i , int j){
            int temp;
            String tempS;

            tempS = namaBarang.get(i);
            namaBarang.set(i,namaBarang.get(j));
            namaBarang.set(j,tempS);

            temp = harga.get(i);
            harga.set(i,harga.get(j));
            harga.set(j,temp);

            temp = stock.get(i);
            stock.set(i,stock.get(j));
            stock.set(j,temp);
        }

        public static void bubbleSort(){
            int n = namaBarang.size();
            for ( int i = 0; i<n-i ; i++) {
                for ( int j = n-1; j>i ; j--) {

                    /*//.compareToIgnoreCase
                    if ( namaBarang.get(j) .compareTo (harga.get (j-10));
                    }*/

                    if (harga.get(j) < harga.get (j-1))
                    {
                        swap (j,j-1);
                    }
                }
            }
        }

        public static void menu1()
        {
            bubbleSort();
            cartID.clear();
            cartJml.clear();
            System.out.print ("Sorting by Price\nPress ENTER to Continue\n");
            in.nextLine();
        }

        public static void menu2()
        {
            int jumlahBarang = namaBarang.size();
            int input;
            do{
                System.out.printf("Choose Item [1..%d] : ",jumlahBarang);
                try{
                    input = in.nextInt();
                }catch(Exception e){
                    input = -1;
                }finally{
                    in.nextLine();
                }
            }while(input < 1 || input > jumlahBarang);

            int jumlah;
            int stockJml = stock.get(input-1);
            do{
                System.out.printf("Quantity [1..%d] : ",stockJml);
                try{
                    jumlah = in.nextInt();
                }catch(Exception e){
                    jumlah = -1;
                }finally{
                    in.nextLine();
                }
            }while(jumlah < 1 || jumlah > stockJml);


            if(cartID.contains(input-1)){
                int tempTotal = jumlah + cartJml.get(cartID.indexOf(input-1));
                if( tempTotal > stock.get(input-1)){
                    System.out.print("Maaf, stock tidak cukup");
                    in.nextLine();
                }else{
                    cartJml.set(cartID.indexOf(input-1),tempTotal);
                }
            }else{
                cartID.add(input-1);
                cartJml.add(jumlah);
            }
            System.out.printf("Press ENTER to continue");
        }

        public static void menu3()
        {
            if(cartID.size() == 0){
                System.out.printf("Your cart is empty!\n");
                in.nextLine();
            }else{
                showCart();
                int n;
                n = cartID.size();
                int idx;
                do{
                    try{
                        System.out.printf("Update Item [1..%d] : ",n);
                        idx = in.nextInt();
                    }catch(Exception e){
                        idx = -1;
                        System.out.printf("Input must be number\n");
                    }finally{
                        in.nextLine();
                    }
                }while(idx < 1 || idx > n);

                int qty;
                n = stock.get(idx-1);
                do{
                    try{
                        System.out.printf("Quantity [1..%d] : ",n);
                        qty = in.nextInt();
                    }catch(Exception e){
                        qty = -1;
                        System.out.printf("Input must be number\n");
                    }finally{
                        in.nextLine();
                    }
                }while(qty < 1 || qty > n);

                cartJml.set(idx-1,qty);
            }
            System.out.printf("Press ENTER to continue");
        }

        public static void menu4()
        {
            if(cartID.size() == 0){
                System.out.printf("Your cart is empty!\n");
            }else{
                showCart();
                int cash;
                do{
                    try{
                        System.out.print("Cash : ");
                        cash = in.nextInt();
                    }catch(Exception e){
                        cash = -1;
                        //e.printStackTrace(); //kasih tau errornya apa <--
                    }finally{
                        in.nextLine();
                    }
                }while(cash < 0);

                if(cash < total){
                    System.out.printf("Insufficient Money for Payment\n");
                }else{
                    System.out.printf("Your Change : %d\n", (cash-total));
                    System.out.printf("\n\nThank you :], come again :]\n");
                    cartID.clear();
                    cartJml.clear();
                }
            }
            System.out.printf("Press ENTER to continue\n");
        }

        public static void main (String args[]) {
            int pilihan;
            init();
            do {
                clear();
                showTable();
                if(cartID.size() != 0){
                    showCart();
                }
                menu();
                try{
                    pilihan = in.nextInt();
                    switch (pilihan){
                        case 1 : menu1(); break;
                        case 2 : menu2(); break;
                        case 3 : menu3(); break;
                        case 4 : menu4(); break;
                    }
                }catch ( Exception e){
                    pilihan = -1;
                }finally{
                    in.nextLine();
                }
            }while(pilihan !=5 );

     }
}

Rabu, 11 Juni 2014

Quiz : Part I

import java.util.*;

/*
    ini yang di atas
    ini multiline comment
*/

//ini yang sesuai soal

public class Review_1234567890
{
    static Vector<String> namaItem = new Vector<String>();
    static Vector<Integer> price = new Vector<Integer>();
    static Vector<Integer> stock = new Vector<Integer>();
    static Scanner in = new Scanner(System.in);

    public static void menu()
    {
        //ini one line comment
        System.out.println("\"New Thing Fashion\"  Online");
        System.out.println("1. Sort by Price Asc");
        System.out.println("2. Add to Cart");
        System.out.println("3. Update Item");
        System.out.println("4. Payment");
        System.out.println("5. Exit");
        System.out.print("Choose : ");
    }

    public static void init()
    {
        namaItem.add("Airwalk Austin Shoes");
        namaItem.add("Hush Puppies Shirts");
        namaItem.add("Crocs Sandals");
        namaItem.add("Crocodile Shirts");
        namaItem.add("Casio Watches");
        namaItem.add("Guess Bags");
        namaItem.add("Giordano Pants");

        price.add(450000);
        price.add(350000);
        price.add(700000);
        price.add(525000);
        price.add(1100000);
        price.add(675000);
         price.add(360000);

        stock.add(20);
        stock.add(25);
        stock.add(15);
        stock.add(18);
        stock.add(9);
        stock.add(15);
        stock.add(20);
    }

    public static void tampilData()
    {
        System.out.printf("%-5s | %-25s | %-10s | %-10s\n","No.",
        "List Items","Prices","Stock");
        System.out.printf("=============================================================\n");
        for(int i=0;i<namaItem.size();i++)
        {
            System.out.printf("%-5d | %-25s | %-10d | %-10d\n",
            (i+1),namaItem.get(i),price.get(i),stock.get(i));
        }
    }

    public static void swap(int i, int j)
    {
        int tempAngka;
        String tempStr;
        tempAngka = i;
        i = j;
        j = tempAngka;

        tempStr = namaItem.get(j);
        namaItem.set(j,namaItem.get(j-1));
        namaItem.set(j-1,tempStr);

        tempAngka = price.get(j);
        price.set(j,price.get(j-1));
        price.set(j-1,tempAngka);

        tempAngka = stock.get(j);
        stock.set(j,stock.get(j-1));
        stock.set(j-1,tempAngka);

    }

    public static void bubbleSort(int n)
    {
        for(int i=0;i<n-1;i++)
        {
            for(int j=n-1;j>i;j--)
            {
                if( price.get(j) < price.get(j-1) )
                {
                    swap(j,j-1);
                }
            }
        }
    }

    public static void menu1(){
        int n = namaItem.size();
        bubbleSort(n); //passing by value
        System.out.println("Sorted..");
        in.nextLine();
    }

    public static void menu2(){
        System.out.print("2");
    }

    public static void menu3(){
        System.out.print("3");
    }

    public static void menu4(){
        System.out.print("4");
    }

    public static void clear(){
        for(int i=0;i<25;i++)
            System.out.println();
    }

    public static void main(String args[])
    {
        int pilih;
        init();
        do{
            clear();
            tampilData();
            menu();
            try{
                pilih = in.nextInt();
                switch(pilih){
                    case 1: menu1(); break;
                    case 2: menu2(); break;
                    case 3: menu3(); break;
                    case 4: menu4(); break;
                }
            }catch(Exception e){
                pilih = -1;
            }finally{
                in.nextLine();
            }
        }while(pilih != 5);
    }
}