Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Widerstand {
  6.  
  7.     public static enum Type {PARALLEL, SERIAL};
  8.     private float firstValue, secValue;
  9.     private Type type;
  10.  
  11.  
  12.     public Widerstand(float firstValue, float secValue, Type type) {
  13.         this.firstValue = firstValue;
  14.         this.secValue = secValue;
  15.         this.type = type;
  16.     }
  17.  
  18.     public float getFirstValue() {
  19.         return firstValue;
  20.     }
  21.  
  22.     public float getSecValue() {
  23.         return secValue;
  24.     }
  25.  
  26.     public float getResult() {
  27.         // Berechnung in Abhängigkeit von type
  28.         return 0.0f;
  29.     }
  30.  
  31.     public static void main(String[] args) {
  32.         List<Widerstand> list = new ArrayList<Widerstand>();
  33.         SimpleScanner sscn = new SimpleScanner();
  34.  
  35.         System.out.println("Es gibt Parallelschaltung und Reihenschaltung");
  36.         String input = sscn.getString("Wählen Sie R bzw. r für Reihenschaltung, P bzw. p für Parallelschaltung oder e bzw. E um die Ergebnisse anzuzeigen> ");      
  37.  
  38.         while (!input.equalsIgnoreCase("e")) {
  39.             if (input.equalsIgnoreCase("p")) {
  40.                 System.out.println("Sie haben Parallelschaltung gewählt, geben sie nun die beiden Werte ein");
  41.                 list.add(new Widerstand(sscn.getFloat("Wert 1> "), sscn.getFloat("Wert 2> "), Widerstand.Type.PARALLEL));
  42.             } else if (input.equalsIgnoreCase("r")) {
  43.                 System.out.println("Sie haben Reihenschaltung gewählt, geben sie nun die beiden Werte ein");
  44.                 list.add(new Widerstand(sscn.getFloat("Wert 1> "), sscn.getFloat("Wert 2> "), Widerstand.Type.SERIAL));
  45.             } else {
  46.                 System.out.println("Sie haben keine gültige Auswahl getroffen");
  47.             }
  48.             input = sscn.getString("Wählen Sie R bzw. r für Reihenschaltung, P bzw. p für Parallelschaltung oder e bzw. E um die Ergebnisse anzuzeigen> ");
  49.         }
  50.  
  51.         if (list.isEmpty()) {
  52.             System.out.println("Es erfolgt keine Ausgabe, weil keine Widerstände definiert wurden");
  53.         } else {
  54.             for (Widerstand ws : list) {
  55.                 System.out.printf("Das Ergebnis der Werte %f und %f lautet %f", ws.getFirstValue(), ws.getSecValue(), ws.getResult());
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. class SimpleScanner {
  62.  
  63.     private static Scanner scn;
  64.  
  65.     SimpleScanner() {
  66.         scn = new Scanner(System.in);
  67.     }
  68.  
  69.     String getString(String prompt) {
  70.         System.out.print(prompt);
  71.         return scn.next();
  72.     }
  73.  
  74.     float getFloat(String prompt) {
  75.         System.out.print(prompt);
  76.         return scn.nextFloat();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement