Advertisement
safriansah

OVERLOADING

Jul 10th, 2018
2,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package swalayan;
  2. import java.util.Scanner;
  3. public class Swalayan{
  4.     String namaBarang;
  5.     int harga,total,id,jumlah;
  6.     public Scanner str=new Scanner(System.in);
  7.     public Scanner in=new Scanner(System.in);
  8.     public void setData(){
  9.         this.id=160500;
  10.     }
  11.     public void setData(int id){
  12.         this.id=1+id;
  13.         System.out.print("Masukkan nama barang   : ");
  14.         this.namaBarang=str.nextLine();
  15.         System.out.print("Masukkan harga barang  : ");
  16.         this.harga=in.nextInt();
  17.         this.jumlah=1;
  18.         this.total=this.harga;
  19.     }
  20.     public void setData(int id,int jumlah){
  21.         this.id=1+id;
  22.         System.out.print("Masukkan nama barang   : ");
  23.         this.namaBarang=str.nextLine();
  24.         System.out.print("Masukkan harga barang  : ");
  25.         this.harga=in.nextInt();
  26.         this.jumlah=jumlah;
  27.         this.total=this.harga*jumlah;
  28.     }
  29.     public int getId(){
  30.         return this.id;
  31.     }
  32.     public void tampil(){
  33.         System.out.println("Id penjualan : "+this.id);
  34.         System.out.println("Nama Barang  : "+this.namaBarang);
  35.         System.out.println("Harga Barang : "+this.harga);
  36.         System.out.println("Jumlah Barang: "+this.jumlah);
  37.         System.out.println("Total Harga  : "+this.total);
  38.     }
  39.     public static void main(String[] args){
  40.         int z=0,menu=0,a,jumlah,id;
  41.         Swalayan[] sw=new Swalayan[1024];
  42.         sw[0]=new Swalayan();
  43.         sw[0].setData();
  44.         while(menu!=6){
  45.             System.out.print("1.input\n2.view\n3.search\n4.update\n5.delete\n6.exit\n : ");
  46.             menu=sw[0].in.nextInt();
  47.             if(menu==1){
  48.                 jumlah=0;
  49.                 while(jumlah<1){
  50.                     System.out.print("masukkan jumlah barang : ");
  51.                     jumlah=sw[0].in.nextInt(); 
  52.                 }
  53.                 z++;
  54.                 sw[z]=new Swalayan();
  55.                 if(jumlah>1)
  56.                     sw[z].setData(sw[z-1].getId(),jumlah);
  57.                 else
  58.                     sw[z].setData(sw[z-1].getId());
  59.             }else if(menu==2){
  60.                 if(z<1)
  61.                     System.out.println("data kosong");
  62.                 else{
  63.                     a=0;
  64.                     while(a<z){
  65.                         a++;
  66.                         System.out.println("---"+a+"---");
  67.                         sw[a].tampil();
  68.                     }
  69.                 }
  70.             }else if(menu==3){
  71.                 if(z<1)
  72.                     System.out.println("data kosong");
  73.                 else{
  74.                     System.out.print("masukkan id pembeli yang dicari : ");
  75.                     id=sw[0].in.nextInt();
  76.                     a=1;
  77.                     while(a<=z){
  78.                         if(sw[a].getId()==id)
  79.                             break;
  80.                         a++;
  81.                     }
  82.                     if(a<=z)
  83.                         sw[a].tampil();
  84.                     else
  85.                         System.out.println("data tidak ditemukan");
  86.                 }
  87.             }else if(menu==4){
  88.                     if(z<1)
  89.                         System.out.println("data kosong");
  90.                     else{
  91.                         System.out.print("masukkan id pembeli yang diupdate : ");
  92.                         id=sw[0].in.nextInt();
  93.                         a=1;
  94.                         while(a<=z){
  95.                             if(sw[a].getId()==id)
  96.                                 break;
  97.                             a++;
  98.                         }
  99.                         if(a<=z){
  100.                             sw[a].tampil();
  101.                             jumlah=0;
  102.                             while(jumlah<1){
  103.                                 System.out.print("masukkan jumlah barang : ");
  104.                                 jumlah=sw[0].in.nextInt(); 
  105.                             }
  106.                             if(jumlah>1)
  107.                                 sw[a].setData(sw[z-1].getId(),jumlah);
  108.                             else
  109.                                 sw[a].setData(sw[z-1].getId());
  110.                             }
  111.                         else
  112.                             System.out.println("data tidak ditemukan");
  113.                     }
  114.             }else if(menu==5){
  115.                     if(z<1)
  116.                         System.out.println("data kosong");
  117.                     else{
  118.                         System.out.print("masukkan id pembeli yang didelete : ");
  119.                         id=sw[0].in.nextInt();
  120.                         a=1;
  121.                         while(a<=z){
  122.                             if(sw[a].getId()==id)
  123.                                 break;
  124.                             a++;
  125.                         }
  126.                         if(a<=z){
  127.                             while(a<z){
  128.                                 sw[a]=sw[a+1];
  129.                                 a++;
  130.                             }
  131.                             z--;
  132.                             System.out.println("data berhasil dihapus");
  133.                         }
  134.                         else
  135.                             System.out.println("data tidak ditemukan");
  136.                     }
  137.             }else if(menu==6)
  138.                     System.out.println("keluar...");
  139.             else
  140.                     System.out.println("menu ilegal");
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement