Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1.  
  2. public class Rueckgabeautomat {
  3.     final static int[] muenzWertigkeit = { 200, 100, 50, 20, 10, 5, 2, 1 };
  4.     int[] muenzAnzahl = new int[muenzWertigkeit.length];
  5.  
  6.     public String toString() {
  7.         String toString = "Muenzwertigkeit: { ";
  8.         for (int i = 0; i < muenzWertigkeit.length; i++) {
  9.             toString = toString + muenzWertigkeit[i] + " ";
  10.         }
  11.         toString = toString + "} Muenzbestand: { ";
  12.         for (int i = 0; i < muenzAnzahl.length; i++) {
  13.             toString = toString + muenzAnzahl[i] + " ";
  14.         }
  15.         toString = toString + "}";
  16.         return toString;
  17.     }
  18.  
  19.     // Konstruktor - Anzahl der Muenzen als Eingabe
  20.     public Rueckgabeautomat(int[] muenzAnzahl) {
  21.         this.muenzAnzahl = muenzAnzahl;
  22.     }
  23.  
  24.     // Ermittle den aktuellen Bestand der Muenzen
  25.     public int[] ermittleBestand() {
  26.         return muenzAnzahl;
  27.     }
  28.  
  29.     // Ermittle das Rueckgeld auf Basis des aktuellen Muenzbestands
  30.     public int[] ermittleRueckgeld(int einzahlung, int zahlbetrag) {
  31.         int rueckgeld = einzahlung - zahlbetrag;
  32.         int[] rueckgeldAnzahl = new int[muenzAnzahl.length];
  33.         for (int i = 0; i < muenzAnzahl.length; i++) {
  34.             if (rueckgeld >= muenzWertigkeit[i]) {
  35.                 while(muenzAnzahl[i] > 0) {
  36.                     if (rueckgeld >= muenzWertigkeit[i]) {
  37.                         muenzAnzahl[i]--;
  38.                         rueckgeldAnzahl[i]++;
  39.                         rueckgeld = rueckgeld - muenzWertigkeit[i];
  40.                     }
  41.                     else {
  42.                         break;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.         return rueckgeldAnzahl;
  48.     }
  49.  
  50.     public static void main(String[] args) {
  51.         int[] muenzAnzahl = { 2, 2, 2, 2, 2, 2, 2, 2 };
  52.         Rueckgabeautomat automat1 = new Rueckgabeautomat(muenzAnzahl);
  53.         System.out.println(automat1);
  54.         int[] rueckgeld = automat1.ermittleRueckgeld(100,57);
  55.         System.out.println(automat1);
  56.         for (int i = 0; i < rueckgeld.length; i++) {
  57.             System.out.print(rueckgeld[i] + " ");
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement