Advertisement
Guest User

Untitled

a guest
May 20th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. //Natalia Młodzik 210057 i Piotr Turoboś 210345
  2. import javax.swing.JOptionPane;
  3.  
  4. public class change
  5. {
  6.    
  7.     static int [] ileMonet= {0,0,0,0};
  8.     final static int zakresMin=0;
  9.     final static int zakresMax=100;
  10.     final static int maxPrób=4;
  11.    
  12.     public static void ZerujTablice()
  13.     {
  14.         for(int i=0; i<=3; i++)
  15.             ileMonet[i]=0;
  16.     }
  17.     public static int KontrolaWejścia ()
  18.     {
  19.         int reszta=0, ilePrób=0;
  20.         boolean odczytOK=false;
  21.         do
  22.         {
  23.             String ileString = JOptionPane.showInputDialog("Podaj wartość reszty (liczbę "
  24.                     + "całkowitą z zakresu od " + (zakresMin+1) + " do "  + (zakresMax-1)
  25.                     +").\nObliczę resztę w monetach:");
  26.             if (ileString != null)
  27.             {
  28.                 try
  29.                 {
  30.                     reszta = Integer.parseInt(ileString);
  31.                     if(reszta>zakresMin && reszta<zakresMax)
  32.                         odczytOK=true;
  33.                     else
  34.                         JOptionPane.showMessageDialog(null, "Niestety, podana wartośc "
  35.                                 + "reszty jest spoza zakresu.");
  36.                 }
  37.                 catch (NumberFormatException e)
  38.                 {
  39.                     JOptionPane.showMessageDialog(null, "Zła wartość reszty! ");
  40.                 } finally {ilePrób++;}
  41.             }
  42.             else
  43.                 System.exit(0);
  44.         } while (!odczytOK && ilePrób<maxPrób);
  45.         if(!odczytOK)
  46.         {
  47.             JOptionPane.showMessageDialog(null, "Zbyt wiele razy podano złą wartość,"
  48.                     + " program zakończy swoją pracę.");
  49.             System.exit(0);
  50.         }
  51.         return reszta;
  52.     }
  53.    
  54.     public static void Change (int reszta)
  55.     {
  56.         while(reszta>=25)
  57.         {
  58.             ileMonet[0]++;
  59.             reszta-=25;
  60.         }
  61.         while(reszta>=10)
  62.         {
  63.             ileMonet[1]++;
  64.             reszta-=10;
  65.         }
  66.         while(reszta>=5)
  67.         {
  68.             ileMonet[2]++;
  69.             reszta-=5;
  70.         }
  71.         while(reszta>=1)
  72.         {
  73.             ileMonet[3]++;
  74.             reszta-=1;
  75.         }
  76.     }
  77.  
  78.     public static boolean CzyKontynuować()
  79.     {
  80.         int czyKontynuować=JOptionPane.showConfirmDialog(null, "Czy chcesz "
  81.                 + "wyznaczyć ilość monet dla innej wartości reszty?",
  82.                 "Click Yes or No", JOptionPane.YES_NO_OPTION);
  83.         if (czyKontynuować==JOptionPane.YES_OPTION)
  84.             return true;
  85.         else
  86.             return false;
  87.     }
  88.    
  89.     public static void main(String[] args)
  90.     {
  91.         do
  92.         {
  93.         int reszta=KontrolaWejścia();
  94.         Change(reszta);
  95.         JOptionPane.showMessageDialog(null, reszta + " centów w monetach:\n"
  96.                 + ileMonet[0] + " quarters\n" + ileMonet[1] + " dimes\n" + ileMonet[2]
  97.                 + " nickels and\n" + ileMonet[3] + " pennies");
  98.         ZerujTablice();
  99.         }while(CzyKontynuować());
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement