Cara Membuat Aplikasi Parkir

Written By Unknown on Thursday, February 28, 2013 | 8:46 AM



  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 
  6 package aplikasiparkir;
  7 
  8 /**
  9  * Date: 15 Februari 2013
 10  * @author: Admin
 11  */
 12 import java.io.*;
 13 import java.util.*;
 14 import java.text.SimpleDateFormat;
 15 
 16 public class MenuParkir {
 17   private RecordParkir[] dafken = new RecordParkir[100];
 18   private int i;
 19 
 20   public MenuParkir() {
 21   }
 22 
 23   public String readInput() {
 24         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 25         String input="";
 26         try {
 27                 input = br.readLine();
 28         } catch (IOException ioe) {
 29                 System.out.println(ioe.getMessage());
 30         }
 31         return input;
 32   }
 33 
 34   public void ambilData() {
 35         try {
 36           BufferedReader br = new BufferedReader(new FileReader("parkir.txt"));
 37           String baris="";
 38           i=0;
 39           while ((baris=br.readLine()) != null) {
 40                 StringTokenizer st = new StringTokenizer(baris,"-");
 41 
 42                 RecordParkir parkir = new RecordParkir();
 43 
 44                 while (st.hasMoreTokens()) {
 45                         String np = st.nextToken();
 46                         parkir.setNoPolisi(np);
 47 
 48                         String jl = st.nextToken();
 49                         int jk = Integer.parseInt(jl);
 50                         parkir.setJenisKendaraan(jk);
 51 
 52                         String wm = st.nextToken();
 53                         parkir.setWaktuMasuk(wm);
 54 
 55                         String wk = st.nextToken();
 56                         parkir.setWaktuKeluar(wk);
 57 
 58                         String tl = st.nextToken();
 59                         double tk = Double.parseDouble(tl);
 60                         parkir.setTarifKendaraan(tk);
 61                 }
 62 
 63                 dafken[i] = parkir;
 64                 i++;
 65           }
 66 
 67         br.close();
 68       } catch (IOException ioe) {
 69                 System.out.println("Kesalahan!!");
 70         }
 71   }
 72 
 73   public void tampilData(){
 74         ambilData();
 75         System.out.println("Laporan Parkir");
 76         System.out.println("No."+"\tNo.Polisi"+"\tJenis Kendaraan"+
 77                            "\tWaktu Masuk"+"\tWaktu Keluar"+"\tTarif");
 78 
 79         for(int j=0;j<i;j++){
 80                 System.out.println ((j+1)+ "\t" +dafken[j].getNoPolisi()+
 81                  " \t " +dafken[j].getJenisKendaraan()+
 82                         " \t" +dafken[j].getWaktuMasuk()+ " \t " +
 83                         dafken[j].getWaktuKeluar()+ "\t"+dafken[j].getTarifKendaraan());
 84         }
 85         int l;
 86         int m=0;
 87         int n=0;
 88         for (l=0;l<i;l++){
 89                 if(dafken[l].getJenisKendaraan()==1){
 90                         m++;
 91                 }
 92                 else
 93                         n++;
 94         }
 95 
 96         int jumlahMotor=m;
 97         int jumlahMobil=n;
 98 
 99         double persenMotor=(double)jumlahMotor/(jumlahMobil+jumlahMotor)*100;
100         double persenMobil=(double)jumlahMobil/(jumlahMobil+jumlahMotor)*100;
101 
102         System.out.println ();
103         System.out.println ("Jumlah kendaraan");
104                 System.out.println ("Motor : " +jumlahMotor);
105                 System.out.println ("Mobil : " +jumlahMobil);
106                 System.out.println ();
107                 System.out.println ("Persentase kendaraan (dalam %)");
108         System.out.printf ("Motor : %.0f" ,persenMotor);
109         System.out.println ();
110         System.out.printf ("Mobil : %.0f" ,persenMobil);
111         System.out.println ("\n");
112         System.out.println ("Grafik Distribusi:");
113 
114 
115         int star=0;
116         double gMotor=0;
117         double gMobil=0;
118 
119                 if((persenMotor%10)>=5){
120                                 gMotor=(persenMotor/10)+1;
121                                 gMobil=(10-gMotor);
122                         }
123                         else {
124 
125                                 gMobil=(persenMobil/10)+1;
126                                 gMotor=(10-gMobil);
127                         }
128 
129                 System.out.print("Motor : ");
130                 for( int stars = 0; stars<(gMotor) ; stars ++ )
131                                 System.out.printf("*");
132                                 System.out.println("");
133 
134                 System.out.print("Mobil : ");
135                 for( int stars = 0; stars<(gMobil) ; stars ++ )
136                                 System.out.printf("*");
137                                 System.out.println("");
138 
139                 System.out.println ();
140                 }
141 
142   public void masuk(){
143         MenuParkir menu = new MenuParkir();
144         System.out.print("No. Polisi\t: ");
145         String np= menu.readInput();
146         System.out.println ();
147         System.out.println ("1. Motor , 2. Mobil");
148         System.out.print("Jenis Kendaraan: ");
149         String j1=menu.readInput();
150         int jk=Integer.parseInt(j1);
151 
152         SimpleDateFormat jam = new SimpleDateFormat("HH:mm:ss",Locale.getDefault());
153         Date sekarang = new Date();
154         String waktuMasuk = jam.format(sekarang);
155 
156         String tarifKendaraan="0";
157         String waktuKeluar="00:00:00";
158         try{
159                 boolean kondisi=false;
160                 File f=new File("parkir.txt");
161                 if (f.exists()){
162                         kondisi=true;
163                 }
164                 else{
165                         f.createNewFile();
166                 }
167                 PrintWriter pw=new PrintWriter(new FileWriter("parkir.txt",kondisi));
168                 pw.println(np+"-"+jk+"-"+waktuMasuk+"-"+waktuKeluar+"-"+tarifKendaraan);
169                 pw.close();
170         } catch(IOException ioe){
171                 System.out.println (ioe.getMessage());
172         }
173   }
174 
175   public void keluar(String np){
176         boolean ketemu=false;
177         int j;
178         for(j=0;j<i;j++){
179              if ((dafken[j].getNoPolisi()).equals(np) && dafken[j].getTarifKendaraan()==0){
180                   ketemu=true;
181                   break;
182              }
183         }
184         System.out.println ();
185         if (!ketemu){
186                 System.out.println ("Kendaraan tidak terdaftar!");
187         }
188         else{
189                 SimpleDateFormat jam = new SimpleDateFormat("HH:mm:ss",Locale.getDefault());
190                 Date sekarang = new Date();
191                 String waktuKeluar = jam.format(sekarang);
192                 StringTokenizer st2=new StringTokenizer(waktuKeluar,":");
193 
194                 String waktuMasuk=dafken[j].getWaktuMasuk();
195 
196                 StringTokenizer st=new StringTokenizer(waktuMasuk,":");
197 
198                 String jamM1 = st.nextToken();
199                 int jamM = Integer.parseInt(jamM1);
200                 String menitM1 = st.nextToken();
201                 int menitM = Integer.parseInt(menitM1);
202                 String detikM1 = st.nextToken();
203                 int detikM = Integer.parseInt(detikM1);
204 
205                 int totalMasuk=(jamM*3600)+(menitM*60)+detikM;
206 
207                 String jamK1 = st2.nextToken();
208                 int jamK = Integer.parseInt(jamK1);
209                 String menitK1 = st2.nextToken();
210                 int menitK = Integer.parseInt(menitK1);
211                 String detikK1 = st2.nextToken();
212                 int detikK = Integer.parseInt(detikK1);
213 
214                 int totalKeluar=(jamK*3600)+(menitK*60)+detikK;
215 
216                 int selisihWaktu=totalKeluar-totalMasuk;
217 
218                 int lamaParkir = selisihWaktu/3600;
219 
220                 int jamParkir;
221 
222                 if(selisihWaktu < 3600){
223                         jamParkir=1;
224                 }
225                 else{
226                         if ((selisihWaktu%3600)!=0){
227                                 jamParkir=lamaParkir+1;
228                         }
229                         else{
230                                 jamParkir=lamaParkir;
231                         }
232                 }
233 
234                 int tarif=0;
235                 if(dafken[j].getJenisKendaraan()==1){
236                         tarif=jamParkir*1000;
237                 }
238                 else{
239                         tarif=jamParkir*2000;
240                 }
241 
242 
243                 dafken[j].setTarifKendaraan(tarif);
244                 dafken[j].setWaktuKeluar(waktuKeluar);
245                 try{
246                         PrintWriter pw=new PrintWriter(new FileWriter("parkir.txt",false));
247                         for (int k=0;k<i;k++){
248                                 pw.println(dafken[k].getNoPolisi()+"-"+
249                                         dafken[k].getJenisKendaraan()+"-"+
250                                         dafken[k].getWaktuMasuk()+"-"+
251                                         dafken[k].getWaktuKeluar()+"-"+
252                                         dafken[k].getTarifKendaraan());
253                         }
254                         pw.close();
255                 }
256                 catch(IOException ioe){
257                         System.out.println ("Error!");
258                 }
259                 System.out.println ("Struk Parkir");
260                 System.out.println ("============");
261                 if (dafken[j].getJenisKendaraan()==1){
262                         System.out.print ("Jenis : Motor" );
263                 }
264                 else{
265                         System.out.print ("Jenis : Mobil");
266                 }
267                 System.out.println ("");
268                 System.out.println ("No.Polisi   : "+dafken[j].getNoPolisi());
269                 System.out.println ("Waktu Masuk : "+dafken[j].getWaktuMasuk());
270                 System.out.println ("Waktu Keluar: "+dafken[j].getWaktuKeluar());
271                 System.out.println ("Tarif       : Rp." +dafken[j].getTarifKendaraan());
272         }
273     }
274 
275     public static void main(String[] args) {
276        MenuParkir menu=new MenuParkir();
277        String ulang;
278        do{
279        System.out.println ("Sistem Parkir");
280        System.out.println ("1. Parkir Masuk");
281        System.out.println ("2. Parkir Keluar");
282        System.out.println ("3. Laporan");
283        System.out.println ("4. Keluar Program");
284        System.out.print   ("Masukkan pilihan [1/2/3/4]: ");
285        int pilih=Integer.parseInt(menu.readInput());
286        System.out.println ();
287        if (pilih==1){
288                 menu.masuk();
289        }
290        else
291         if(pilih==2) {
292                 menu.ambilData();
293                         System.out.print ("Masukkan No. Polisi: ");
294                 String np=menu.readInput();
295                 menu.keluar(np);
296        }
297        else
298         if(pilih==3){
299                 menu.tampilData();
300         }
301        else{
302         break;
303        }
304        System.out.print("Kembali ke Menu Utama[Y/T] ? ");
305        ulang = menu.readInput();
306        }while(ulang.equals("Y") || ulang.equals("y"));
307     }
308 }
309 

0 comments:

Post a Comment