Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. //the class Eintrag.java
  2. public class Eintrag {
  3.  
  4.     private String produkt;
  5.     private double preis;
  6.     private int anzahl;
  7.  
  8.     public Eintrag(String produkt, int anzahl, double preis) {
  9.         this.anzahl = anzahl;
  10.         this.produkt = produkt;
  11.         this.preis = preis;
  12.     }
  13.  
  14.     public int getAnzahl() {
  15.         return this.anzahl;
  16.     }
  17.  
  18.     public double getPreis() {
  19.         return this.preis * this.anzahl;
  20.     }
  21.  
  22.     public String getProdukt() {
  23.         return this.produkt;
  24.     }
  25.  
  26.     public void setAnzahl(int anzahl) {
  27.         this.anzahl = anzahl;
  28.     }
  29.  
  30.     public String toString() {
  31.         return (String.format("%-9s %2d x %5.2f EUR", this.produkt, this.anzahl, this.preis) +
  32.                 String.format("%30.2f EUR", anzahl * preis));
  33.  
  34.     }
  35. }
  36.  
  37.  
  38.  
  39. //the class Kassenzettel.java
  40. import java.util.ArrayList;
  41.  
  42. public class Kassenzettel {
  43.  
  44.     private ArrayList<Eintrag> kassenZettel = new ArrayList<Eintrag>();
  45.  
  46.     // Constructs a new empty grocery list.
  47.     public void add(Eintrag item) {
  48.         this.kassenZettel.add(item);
  49.     }
  50.  
  51.     @Override
  52.     public String toString() {
  53.         double sum = 0;
  54.         String ret = "";
  55.         for (int i = 0; i < kassenZettel.size(); i++) {
  56.             ret += kassenZettel.get(i).toString() + "\r\n";
  57.             sum += kassenZettel.get(i).getPreis();
  58.         }
  59.         ret += "\r\nSumme: " + sum;
  60.         return ret;
  61.     }
  62.  
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //the class Main.java
  69. import java.util.Scanner;
  70.  
  71. public class Main {
  72.  
  73.     public static void main(String[] args) {
  74.         Scanner scanner = new Scanner(System.in);
  75.  
  76.         // create an instance of kassenzettel to add your produkts to
  77.         Kassenzettel list = new Kassenzettel();
  78.  
  79.         // set variable to check if read or not (not needed if using break!)
  80.         boolean readOn = true;
  81.  
  82.         // loop for reading until user inputs x or X
  83.         while (readOn) {
  84.             System.out.println("What would you like? ");
  85.             String produkt = "";
  86.             //read, while produkt is empty
  87.             while(produkt == null || produkt.trim().equals("")){
  88.                 produkt = scanner.nextLine();
  89.             }
  90.  
  91.             // check if produkt name is X or x => exit the loop
  92.             if (produkt.equalsIgnoreCase("x")) {
  93.                 readOn = false;
  94.                 break;
  95.             }
  96.             System.out.println("How many pieces do you want?");
  97.             int anzahl = scanner.nextInt();
  98.             System.out.println("How much does " + produkt + "cost?");
  99.             double preis = scanner.nextDouble();
  100.  
  101.             // add a new item to kassenzettel
  102.             list.add(new Eintrag(produkt, anzahl, preis));
  103.         }
  104.  
  105.         System.out.println("__________________________________");
  106.         System.out.println("    IHRE RECHNUNG             ");
  107.         System.out.println("__________________________________");
  108.         // print the output of kassenzettel
  109.         System.out.println(list.toString());
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement