Guest User

Untitled

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. import java.util.regex.Pattern;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JTextField;
  10. import java.io.*;
  11. import java.util.Calendar;
  12. import java.util.GregorianCalendar;
  13. import javax.swing.*;
  14.  
  15. public class Parqueo extends JFrame implements Serializable, ActionListener
  16. {
  17. String placa; // cedula de identidad
  18. int nit; // nombre
  19. String hi;
  20. String hs;
  21. String fechita;
  22. JLabel lb1,lb2;
  23. JTextField tf1;
  24. JButton b1,b2,b3;
  25. int pt=0;
  26. int min;
  27.  
  28. public Parqueo()
  29. {
  30. super("Parqueo");
  31. setLayout(null);
  32. setVisible(true);
  33. lb1=new JLabel("DETALLE DE PAGO POR HORAS ESTACIONADAS");
  34. lb1.setBounds(195,10,270,30);
  35. lb2=new JLabel("Fecha de proceso");
  36. lb2.setBounds(320,40,200,30);
  37. Calendar fecha = new GregorianCalendar();
  38. int anio = fecha.get(Calendar.YEAR);
  39. int mes = fecha.get(Calendar.MONTH);
  40. int dia = fecha.get(Calendar.DAY_OF_MONTH);
  41. fechita=dia +"/" + (mes+1) + "/" + anio;
  42. tf1=new JTextField(fechita);
  43. tf1.setBounds(440,40,200,30);
  44. b1=new JButton("Adicionar nuevos registros");
  45. b1.setBounds(40,80,200,30);
  46. b2=new JButton("Reporte");
  47. b2.setBounds(240,80,200,30);
  48. b3=new JButton("Finalizar");
  49. b3.setBounds(440,80,200,30);
  50. add(lb1);
  51. add(lb2);
  52. add(tf1);
  53. add(b1);add(b2);add(b3);
  54. b1.addActionListener(this);
  55. b2.addActionListener(this);
  56. b3.addActionListener(this);
  57. }
  58.  
  59. public Parqueo(String placa,int nit,String hi,String hs)
  60. { this.placa=placa;
  61. this.nit=nit;
  62. this.hi=hi;
  63. this.hs=hs;
  64. }
  65.  
  66. public String get_placa() {
  67. return placa;
  68. }
  69. public int get_nit() {
  70. return nit;
  71. }
  72. public String get_hi() {
  73. return hi;
  74. }
  75.  
  76. public String get_hs() {
  77. return hs;
  78. }
  79.  
  80. public String get_fechita() {
  81. return fechita;
  82. }
  83.  
  84. public String toString()
  85. { return get_placa()+ "," + get_nit() + "," + get_hi()+ ","+ get_hi()+","+get_fechita();
  86. }
  87.  
  88. public int get_min(String hi, String hs){
  89. int h,m;
  90. String s = Pattern.quote(":");
  91. String pi[]= hi.split(s);
  92. int ih= Integer.parseInt(pi[0]);
  93. int im= Integer.parseInt(pi[1]);
  94. String s1 = Pattern.quote(":");
  95. String ps[]= hs.split(s1);
  96. int sh= Integer.parseInt(ps[0]);
  97. int sm= Integer.parseInt(ps[1]);
  98. if(ih<=sh) {
  99. h=sh-ih;
  100. }
  101. else {
  102. h=ih-sh;
  103. }
  104. if(im<=sm) {
  105. m=sm-im;
  106. }
  107. else {
  108. m=im-sm;
  109. }
  110. min=(h*60)-m;
  111. return min;
  112. }
  113.  
  114. public int get_pago(String hi, String hs) {
  115. int pago,h,m,time;
  116. String s = Pattern.quote(":");
  117. String pi[]= hi.split(s);
  118. int ih= Integer.parseInt(pi[0]);
  119. int im= Integer.parseInt(pi[1]);
  120. String ps[]= hs.split(s);
  121. int sh= Integer.parseInt(ps[0]);
  122. int sm= Integer.parseInt(ps[1]);
  123. if(ih<=sh) {
  124. h=sh-ih;
  125. }
  126. else {
  127. h=ih-sh;
  128. }
  129. if(im<=sm) {
  130. m=sm-im;
  131. }
  132. else {
  133. m=im-sm;
  134. }
  135. time=h-(m/60);
  136. if(time<1) pago=5;
  137. if(time<3) pago=10;
  138. if(time<6) pago=15;
  139. else pago=20;
  140. return pago;
  141. }
  142.  
  143. public int get_pt(int p) {
  144. pt+=p;
  145. return pt;
  146. }
  147.  
  148. public int fech(String f) {
  149. String s = Pattern.quote(":");
  150. String parts[]= f.split(s);
  151. int h= Integer.parseInt(parts[0]);
  152. int m= Integer.parseInt(parts[1]);
  153. System.out.println(h+" "+m);
  154. if (h<=23){
  155. if (m<=59){
  156. return 1;
  157. }
  158. else {
  159. JOptionPane.showMessageDialog(null, "Introduzca de nuevo la fecha");
  160. return 0;
  161. }
  162. }
  163. else {
  164. JOptionPane.showMessageDialog(null, "Introduzca de nuevo la fecha");
  165. return 0;
  166. }
  167. }
  168.  
  169. public void write() throws IOException,ClassNotFoundException
  170. {
  171. try {
  172. int aux=0;
  173. String hi = null;
  174. String hs = null;
  175. File arch = new File("Vehiculos.bin");
  176. String placa=JOptionPane.showInputDialog("Placa del vehiculo:");
  177. String x1=JOptionPane.showInputDialog("NIT:");
  178. while(aux==0) {
  179. hi=JOptionPane.showInputDialog("Hora de ingreso:");
  180. aux=fech(hi);
  181. }
  182. aux=0;
  183. while(aux==0) {
  184. hs=JOptionPane.showInputDialog("Hora de salida:");
  185. aux=fech(hs);
  186. }
  187. int nit=Integer.parseInt(x1);
  188. Parqueo o=new Parqueo(placa,nit,hi,hs);
  189.  
  190. if(arch.exists()){
  191.  
  192. AppendableObjectOutputStream fs = new AppendableObjectOutputStream(new FileOutputStream(arch, true));
  193. fs.writeUnshared(o);
  194. fs.close();
  195.  
  196. }
  197. else
  198. {
  199. ObjectOutputStream fs = new ObjectOutputStream(new FileOutputStream(arch, true));
  200. fs.writeUnshared(o);
  201. fs.close();
  202. }
  203.  
  204. } catch (Exception e){
  205. e.printStackTrace();
  206. }
  207. }
  208.  
  209. public void read() throws IOException,ClassNotFoundException
  210. {
  211. try{
  212. String filename1="Vehiculos.bin";
  213. int p=0;
  214. FileInputStream fs2=new FileInputStream(filename1);
  215. ObjectInputStream fs1=new ObjectInputStream(fs2);
  216. String cad="PlacatHra. de IngresotHra. de SalidatTiempo en minutostPago Totaln";
  217. cad+="==============================================================================n";
  218. Parqueo objobrero;
  219. while (fs2.available()!=0)
  220. {
  221. objobrero = (Parqueo) fs1.readObject();
  222. cad+=objobrero.get_placa()+"t"+objobrero.get_hi()+"t"+objobrero.get_hs()+"n"+objobrero.get_min(get_hi(),get_hs()); /*+"t"+objobrero.get_pago(get_hi(),get_hs())+"n"
  223. p=get_pago(get_hi(),get_hs());
  224. get_pt(p);*/
  225. }
  226. cad+="===============================================================================n";
  227. cad+="Total general:t"+get_pt(p);
  228. JTextArea out=new JTextArea(10,50);
  229. out.setText(cad);
  230. JScrollPane barra=new JScrollPane(out);
  231. JOptionPane.showMessageDialog(null,barra);
  232. fs2.close();
  233. }
  234. catch(IOException e)
  235. {
  236. System.err.println("Error en el archivo"+e.getMessage());
  237.  
  238. }
  239.  
  240. }
  241.  
  242. public void actionPerformed(ActionEvent event)
  243. {
  244. if(event.getSource()==b1)
  245. {
  246. try {
  247. write();
  248.  
  249. } catch (IOException ex) {
  250. Logger.getLogger(ejer1.class.getName()).log(Level.SEVERE, null, ex);
  251. } catch (ClassNotFoundException ex) {
  252. Logger.getLogger(ejer1.class.getName()).log(Level.SEVERE, null, ex);
  253. }
  254. }
  255.  
  256. if(event.getSource()==b2)
  257.  
  258. {
  259. try {
  260. System.out.println("dddd");
  261. read();
  262.  
  263. } catch (IOException ex) {
  264. Logger.getLogger(Parqueo.class.getName()).log(Level.SEVERE, null, ex);
  265. } catch (ClassNotFoundException ex) {
  266. Logger.getLogger(Parqueo.class.getName()).log(Level.SEVERE, null, ex);
  267. }
  268. }
  269.  
  270. if(event.getSource()==b3)
  271.  
  272. { System.exit(0);
  273. dispose();
  274. }
  275. }
  276.  
  277. public static void main(String args[]) throws IOException,ClassNotFoundException
  278. {
  279. Parqueo obj=new Parqueo();
  280. obj.setBounds(0,0,680,350);
  281. obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  282. }
  283. }
Add Comment
Please, Sign In to add comment