Rabu, 27 November 2019

Kuis POS - Point of Sales

Tugas Kuis POS
Sebelum diisi

Sesudah diisi (output)
 import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.scene.text.Text.*;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.collections.*;
public class pointof extends Application
{
    int hargaitung;
    @Override
    public void start (Stage stage) throws Exception
    {
     
        GridPane gridPane = new GridPane();
        gridPane.setMinSize(200, 200);
        gridPane.setPadding(new Insets(10, 10, 10, 10));
        gridPane.setVgap(5);
        gridPane.setHgap(5);
     
        Text LabelKasir = new Text ("Kasir \t\t");
        Text LabelNama = new Text ("Nama Barang \t\t");
        Text LabelHarga = new Text ("Harga Satuan \t\t");
        Text LabelJumlah = new Text ("Jumlah \t\t");
     
        Button cetak = new Button("Cetak");
        Text welcome = new Text("Welcome to PoS");
        TextField NamaKasir = new TextField();
        TextField NamaBarang = new TextField();
        TextField HargaBarang = new TextField();
        TextField JumlahBarang = new TextField();
        Text HargaTotal = new Text();
        gridPane.add(welcome, 0, 0);
        gridPane.add(LabelKasir, 0, 1);
        gridPane.add(LabelNama, 0, 2);
        gridPane.add(LabelHarga, 0, 3);
        gridPane.add(LabelJumlah, 0, 4);
     
        gridPane.add(cetak,3,8);
        gridPane.add(NamaKasir, 2,1);
        gridPane.add(NamaBarang, 2,2);
        gridPane.add(HargaBarang, 2,3);
        gridPane.add(JumlahBarang, 2,4);
     
        EventHandler<ActionEvent> ebutton = new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent e)
            {
                VBox box = new VBox();
                box.setPadding(new Insets(20));
                box.setSpacing(20);
                box.setAlignment(Pos.TOP_LEFT);
             
                Text msgEnd = new Text("Terimakasih Sudah Berbelanja");
                Text namaKasirEnd = new Text("Nama Kasir\t: " +NamaKasir.getText());
                Text namaBarangEnd = new Text("Nama Barang\t: "+NamaBarang.getText());
                Text jumlahBarangEnd = new Text("Jumlah Barang\t: "+JumlahBarang.getText());
                Text hargaSatuanEnd = new Text("Harga Barang\t: " +HargaBarang.getText());
                Text hargaTotalEnd = new Text("Total Bayar\t: Rp. "+Integer.parseInt(JumlahBarang.getText()) * Integer.parseInt(HargaBarang.getText()));
             
                box.getChildren().add(msgEnd);
                box.getChildren().add(namaKasirEnd);
                box.getChildren().add(namaBarangEnd);
                box.getChildren().add(jumlahBarangEnd);
                box.getChildren().add(hargaSatuanEnd);
                box.getChildren().add(hargaTotalEnd);
             
                Scene scene1= new Scene(box,500,500);
                stage.setTitle (" Point Of Sales");
                stage.setScene(scene1);
                stage.show();
            }
        };
        cetak.setOnAction(ebutton);
     
     
        Scene scene= new Scene(gridPane,700,700);
        stage.setTitle (" Point Of Sales");
        stage.setScene(scene);
    stage.show();
    }
}


Minggu, 24 November 2019

Mesin Tiket

Implementasi dari mesin tiket.

Kode :
Untuk Main.java :

import java.util.Scanner; 
 public class Main 
 { 
 public static void main(String args[]) 
 { 
  Scanner scan= new Scanner(System.in); 
  int cost,menu; 
  System.out.println("Masukkan harga tiket \n"); 
  cost=scan.nextInt(); 
  TicketMachine ticket=new TicketMachine(cost);
 
  while (true)
  {
  System.out.println("1. Dapatkan harga"); 
  System.out.println("2. Dapatkan uang"); 
  System.out.println("3. Masukkan uang"); 
  System.out.println("4. Print tiket"); 
  System.out.println("5. Keluar");
  menu=scan.nextInt(); 
 
  switch(menu) 
  { 
   case 1: 
   cost=ticket.getPrice(); 
   System.out.println("Harga untuk tiket ini adalah Rp. "+cost); 
   break; 
   case 2: 
   System.out.println(ticket.getBalance()); 
   break; 
   case 3: 
   System.out.println("Masukkan balance baru Anda.");
   int money=scan.nextInt();
   ticket.insertMoney(money); 
   break; 
   case 4: 
   ticket.printTicket(); 
   break; 
   case 5:
   ticket.exit();
   break;
  } 
  } 
 }
}  


Untuk TicketMachine.java

 public class TicketMachine 
 {  
 private int price; 
 private int balance;   
 private int total; 

 public TicketMachine(int ticketCost) 
 { 
  price = ticketCost; 
  balance = 0; 
  total = 0; 
 } 

 public int getPrice() 
 { 
  return price; 
 } 

 public int getBalance() 
 { 
   return balance; 
 } 

 public void insertMoney(int amount) 
 { 
   balance = balance + amount; 
 } 

 public void exit()
 {
     System.exit(0);
 }
 public void printTicket() 
 { 
   System.out.println("##################"); 
   System.out.println("# Ticket"); 
   System.out.println("# Harga : Rp. " + price + "."); 
   System.out.println("##################"); 
   System.out.println();   
   total = total + balance; 
   balance = 0; 
 } 
 } 


Rabu, 13 November 2019

Implementasi Game Kartu dengan Library

Implementasi game kartu di Java :

Main.java :


import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class main
{
   private static Scanner scanner;
   public static void main(String[]args) throws IOException
   {
        int ulang = 5;
        int start = 1;
        System.out.println("Selamat datang di game tebak kartu");
        System.out.println("Dimana 0 = Tidak ada kartu ,1 = A , 11 = J , 12= Q , 13= K ");
        System.out.println("Dan kamu punya 5 kesempatan untuk menukar kartu");
        System.out.println("Selamat Bermain!!");
        Pemain [] playerCom = new Pemain[2];
        Pemain [] player1 = new Pemain[2];
      
        for (int i = 0; i < playerCom.length; i++) {
            playerCom[i] = new Pemain();
        }
      
        for (int i = 0; i < playerCom.length; i++) {
            player1[i] = new Pemain();
        }
     
        Random tandRandom = new Random();
        player1[0].kartunya();
        player1[1].kartunya();
        playerCom[0].kartunya();
        playerCom[1].kartunya();
                while (true) {
                    System.out.println("Kartumu: ");
       
                    player1[0].currentKartu();
                    player1[1].currentKartu();
                    boolean choose = tandRandom.nextBoolean();
                    int kartuke = tandRandom.nextInt(1);
                     
                    System.out.println();
                    System.out.println("Kartu Yang Ingin di ubah...(1/2/0 untuk Cukup)");         
                     
                    ulang--;
                     
                    if(ulang == 0) {
                        finish(playerCom, player1);
                        break;
                    }
                     
                    scanner = new Scanner(System.in);
                    int temp = scanner.nextInt();
                     
                    if(temp != 0) {
                        player1[temp-1].kartunya();
                        if(choose == true) {
                            playerCom[kartuke].kartunya();
                        }
                    }else {
                        finish(playerCom, player1);
                        break;
                    }
                  
                  
                }

   }
   
        static void finish(Pemain[] playerCom, Pemain[] player1) {
            int jumlah_kartu1=0, jumlah_kartu2=0;
            System.out.println("Kartumu :");
            for (int i = 0; i < playerCom.length; i++) {
                player1[i].currentKartu();
                jumlah_kartu1 += player1[i].getAngka();
            }
            System.out.println("Kartu Komputer:");
            for (int i = 0; i < playerCom.length; i++) {
                playerCom[i].currentKartu();
                jumlah_kartu2 += playerCom[i].getAngka();
            }
            System.out.println("Hasil Akhirnya: ");
            System.out.println("Punyamu: " + jumlah_kartu1 );
            System.out.println("Punya Komputer: " + jumlah_kartu2 );
            if((jumlah_kartu1) < (jumlah_kartu2))
            {
                System.out.println("Maaf, Kamu kalah");
            }else if((jumlah_kartu1) == (jumlah_kartu2))
            {
                System.out.println("Skor Imbang");
            }else
            {
                System.out.println("Selamat, Kamu menang!!!");
        }
    }
  
}


Pemain.java :

import java.util.Random;

public class Pemain{
    private int panjang;
    private int lebar;
    private static String attribute[] = {"Keriting", "Diamond", "Hati", "Sekop"};
    private int x;
    private String stateBungaString;
  
    void kartunya() {
        x = setNomor();
        stateBungaString = attribute[setAttribute()];
    }
  
    void currentKartu() {
        System.out.print(" " + x + " ");
        System.out.println(stateBungaString);
    }
  
    int getAngka() {
        return x;
    }
 
  
    private int setNomor() {
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(13);
        return randomInt;
    }
  
    private int setAttribute() {
        Random randomGenerator = new Random();
        int randomInt2 = randomGenerator.nextInt(4);
        return randomInt2;
    }
}