pablosotol

HilosQl

Aug 29th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1.  
  2. package prueba;
  3.  
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JTextField;
  10.  
  11.  
  12. public class Prueba extends JFrame implements ActionListener{
  13.  
  14.     JButton jp=new JButton("Inicio");
  15.     JTextField jf=new JTextField(50);
  16.     JTextField jd=new JTextField(50);
  17.     private HebraDeposito h;
  18.     private HebraGiro g;
  19.    
  20.     public Prueba(){
  21.        
  22.         setLayout(new FlowLayout());
  23.         add(jf);
  24.         add(jd);
  25.         jp.addActionListener(this);
  26.         add(jp);
  27.        
  28.         setSize(600,600);
  29.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  30.         setVisible(true);
  31.        
  32.     }
  33.    
  34.        
  35.         @Override
  36.         public void actionPerformed(ActionEvent e){
  37.            
  38.             if(e.getSource()==jp){
  39.                
  40.                h=new HebraDeposito(this);
  41.                h.start();
  42.                g=new HebraGiro(this);
  43.                g.start();
  44.                
  45.                
  46.                
  47.             }
  48.            
  49.         }
  50.        
  51.     private final int MAX_SALDO=100000;
  52.     private int saldo=0;
  53.    
  54.     public synchronized void deposito(int dinero){
  55.            try{
  56.                while(saldo>=MAX_SALDO){
  57.                    wait();
  58.                }
  59.                saldo+=dinero;
  60.                jf.setText(" Deposito: " +dinero + " Saldo: " +getSaldo());
  61.                notifyAll();
  62.                
  63.            }catch(InterruptedException ex){
  64.                
  65.            }
  66.     }
  67.    
  68.     public synchronized void giro(int dinero){
  69.         try{
  70.             while(saldo<=0){
  71.                 wait();
  72.             }
  73.            
  74.                saldo-=dinero;
  75.                jd.setText(" Giro: " +dinero + " Saldo: " +getSaldo());
  76.                notifyAll();
  77.                
  78.         }catch(InterruptedException ex){
  79.            
  80.         }
  81.        
  82.     }
  83.    
  84.     public int getSaldo(){
  85.         return saldo;
  86.     }
  87.    
  88.     public static void main(String[] args) {
  89.        
  90.         new Prueba();      
  91.     }
  92.    
  93. }
  94.  
  95.  
  96. package prueba;
  97.  
  98.  
  99. public class HebraDeposito extends Thread{
  100.    
  101.     Prueba p;
  102.    
  103.     public HebraDeposito(Prueba p){
  104.         this.p=p;
  105.     }
  106.  
  107.  
  108.    
  109.     @Override
  110.     public void run(){
  111.         while(true){
  112.             try{
  113.                 Thread.sleep((long)(1 + (int)(Math.random() * 5))*1000);
  114.                 p.deposito((int) (Math.random() * 10000));
  115.                
  116.                
  117.             }catch(InterruptedException ex){
  118.                
  119.             }
  120.         }
  121.              
  122.     }
  123.    
  124. }
  125.  
  126.  
  127. package prueba;
  128.  
  129. public class HebraGiro extends Thread {
  130.    
  131.     Prueba p;
  132.    
  133.     public HebraGiro(Prueba p){
  134.         this.p=p;
  135.     }
  136.    
  137.     @Override
  138.     public void run(){
  139.         while(true){
  140.            try{
  141.                Thread.sleep((long) (1 + (int) (Math.random() * 5)) *1000);
  142.                p.giro((int) (Math.random() * 10000));
  143.                
  144.            
  145.         }catch(InterruptedException ex){
  146.        
  147.         }
  148.         }
  149.        
  150.     }
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment