Created
April 27, 2019 10:40
-
-
Save dimasmds/04b59f9e6702db1e0d5a6e4f37171557 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedWriter; | |
| import java.io.FileWriter; | |
| import java.io.IOException; | |
| import java.text.DecimalFormat; | |
| import java.text.DecimalFormatSymbols; | |
| import java.text.NumberFormat; | |
| import java.util.ArrayList; | |
| import java.util.Scanner; | |
| public class TokoBuku { | |
| public static ArrayList<String> listBuku = new ArrayList<>(); | |
| private static Toko t = new Toko(); | |
| public static void main(String[] args) throws IOException { | |
| int pil = 5; | |
| do { | |
| t.setNameToko("--Toko Buku Widyatama--"); | |
| System.out.println("\t" + t.getNameToko()); | |
| t.setDaftarBuku("DAFTAR BUKU"); | |
| System.out.println("" + t.getDaftarBuku()); | |
| t.setNameBuku1("1. Java Programming 1 Rp. 20.000"); | |
| System.out.println("" + t.getNameBuku1()); | |
| t.setNameBuku2("2. Java Programming 2 Rp. 22.500"); | |
| System.out.println("" + t.getNameBuku2()); | |
| t.setNameBuku3("3. Java Programming 3 Rp. 20.000"); | |
| System.out.println("" + t.getNameBuku3()); | |
| t.setNameBuku4("4. Java Programming 4 Rp. 35.000"); | |
| System.out.println("" + t.getNameBuku4()); | |
| t.setNameBuku5("5. Java Programming 5 Rp. 25.000"); | |
| System.out.println("" + t.getNameBuku5()); | |
| System.out.println("6. Cetak Invoice"); | |
| System.out.print("\n"); | |
| Scanner all = new Scanner(System.in); | |
| System.out.print("Masukan pilihan buku yang akan anda beli : "); | |
| pil = all.nextInt(); | |
| switch (pil) { | |
| case 1: | |
| buku1(); | |
| break; | |
| case 2: | |
| buku2(); | |
| break; | |
| case 3: | |
| buku3(); | |
| break; | |
| case 4: | |
| buku4(); | |
| break; | |
| case 5: | |
| buku5(); | |
| break; | |
| case 6: | |
| hitungHarga(); | |
| break; | |
| } | |
| } while (pil != 6); | |
| } | |
| private static void buku5() { | |
| listBuku.add(t.nameBuku5); | |
| System.out.println("Buku " + t.nameBuku5 + " telah dimasukan ke dalam keranjang!"); | |
| } | |
| private static void buku4() { | |
| listBuku.add(t.nameBuku4); | |
| System.out.println("Buku " + t.nameBuku4 + " telah dimasukan ke dalam keranjang!"); | |
| } | |
| private static void buku3() { | |
| listBuku.add(t.nameBuku3); | |
| System.out.println("Buku " + t.nameBuku3 + " telah dimasukan ke dalam keranjang!"); | |
| } | |
| private static void buku2() { | |
| listBuku.add(t.nameBuku2); | |
| System.out.println("Buku " + t.nameBuku2 + " telah dimasukan ke dalam keranjang!"); | |
| } | |
| private static void buku1() { | |
| listBuku.add(t.nameBuku1); | |
| System.out.println("Buku " + t.nameBuku1 + " telah dimasukan ke dalam keranjang!"); | |
| } | |
| private static void hitungHarga() { | |
| ArrayList<Integer> prices = new ArrayList<>(); | |
| for (String item : listBuku) { | |
| int indexOfChar = item.indexOf("Rp.") + 4; | |
| StringBuilder temp = new StringBuilder(); | |
| int length = item.length(); | |
| for (int i = indexOfChar; i < item.length(); i++) { | |
| temp.append(item.charAt(i)); | |
| } | |
| String price = temp.toString().replace(".", ""); | |
| prices.add(Integer.parseInt(price)); | |
| } | |
| try { | |
| cetakInvoice(prices); | |
| } catch (IOException e) { | |
| System.out.println("Gagal Mencetak Invoice"); | |
| } | |
| } | |
| private static void cetakInvoice(ArrayList<Integer> prices) throws IOException { | |
| int totalHarga = 0; | |
| for (int harga : prices) { | |
| totalHarga += harga; | |
| } | |
| StringBuilder finalString = new StringBuilder("===================================\n" + | |
| " Daftar Pembelian\n" + | |
| "===================================\n\n"); | |
| for (String s : listBuku) { | |
| finalString.append(s).append("\n"); | |
| } | |
| finalString.append("\n\n"); | |
| finalString.append("Total Harga: ").append(convertCurrency(totalHarga)).append("\n"); | |
| finalString.append("Terbilang: ").append(getTerbilang(totalHarga)); | |
| BufferedWriter writer = new BufferedWriter(new FileWriter("invoice.txt", false)); | |
| writer.append(finalString.toString()); | |
| writer.close(); | |
| System.out.println("Invoice Berhasil dicetak !"); | |
| } | |
| private static String convertCurrency(int value) { | |
| DecimalFormat indonesianCurrency = (DecimalFormat) DecimalFormat.getCurrencyInstance(); | |
| DecimalFormatSymbols formatRp = new DecimalFormatSymbols(); | |
| formatRp.setCurrencySymbol("Rp. "); | |
| formatRp.setMonetaryDecimalSeparator(','); | |
| formatRp.setGroupingSeparator('.'); | |
| indonesianCurrency.setDecimalFormatSymbols(formatRp); | |
| return indonesianCurrency.format(value); | |
| } | |
| private static String getTerbilang(int totalHarga) { | |
| String[] terbilang = {"", "satu", "dua", "tiga", "empat", "lima", "enam", | |
| "tujuh", "delapan", "sembilan", "sepuluh", "sebelas"}; | |
| if (totalHarga < 12) { | |
| return terbilang[totalHarga]; | |
| } | |
| if (totalHarga <= 19) { | |
| return terbilang[totalHarga % 10] + " belas "; | |
| } | |
| if (totalHarga <= 99) { | |
| return terbilang[totalHarga / 10] + " puluh " + terbilang[(int) totalHarga % 10]; | |
| } | |
| if (totalHarga <= 199) { | |
| return "seratus " + getTerbilang(totalHarga % 100); | |
| } | |
| if (totalHarga <= 999) { | |
| return terbilang[totalHarga / 100] + " ratus " + getTerbilang(totalHarga % 100); | |
| } | |
| if (totalHarga <= 1999) { | |
| return "seribu " + getTerbilang(totalHarga % 1000); | |
| } | |
| if (totalHarga <= 999999) { | |
| return getTerbilang(totalHarga / 1000) + " ribu " + getTerbilang(totalHarga % 1000); | |
| } | |
| if (totalHarga <= 999999999) { | |
| return getTerbilang(totalHarga / 1000000) + " juta " + getTerbilang(totalHarga % 1000000); | |
| } | |
| return ""; | |
| } | |
| public static final class Toko { | |
| private String nameToko; | |
| private String nameBuku1; | |
| private String nameBuku2; | |
| private String nameBuku3; | |
| private String nameBuku4; | |
| private String nameBuku5; | |
| private String daftarBuku; | |
| private String notaPembelian; | |
| private String harga; | |
| public String getHarga() { | |
| return harga; | |
| } | |
| public void setHarga(String harga) { | |
| this.harga = harga; | |
| } | |
| public String getNotaPembelian() { | |
| return notaPembelian; | |
| } | |
| public void setnotaPembelian(String notaPem) { | |
| this.notaPembelian = notaPembelian; | |
| } | |
| String getDaftarBuku() { | |
| return daftarBuku; | |
| } | |
| void setDaftarBuku(String daftarBuku) { | |
| this.daftarBuku = daftarBuku; | |
| } | |
| String getNameBuku1() { | |
| return nameBuku1; | |
| } | |
| void setNameBuku1(String nameBuku1) { | |
| this.nameBuku1 = nameBuku1; | |
| } | |
| String getNameBuku2() { | |
| return nameBuku2; | |
| } | |
| void setNameBuku2(String nameBuku2) { | |
| this.nameBuku2 = nameBuku2; | |
| } | |
| String getNameBuku3() { | |
| return nameBuku3; | |
| } | |
| void setNameBuku3(String nameBuku3) { | |
| this.nameBuku3 = nameBuku3; | |
| } | |
| String getNameBuku4() { | |
| return nameBuku4; | |
| } | |
| void setNameBuku4(String nameBuku4) { | |
| this.nameBuku4 = nameBuku4; | |
| } | |
| String getNameBuku5() { | |
| return nameBuku5; | |
| } | |
| void setNameBuku5(String nameBuku5) { | |
| this.nameBuku5 = nameBuku5; | |
| } | |
| String getNameToko() { | |
| return nameToko; | |
| } | |
| void setNameToko(String nameToko) { | |
| this.nameToko = nameToko; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment