Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //the class Eintrag.java
- public class Eintrag {
- private String produkt;
- private double preis;
- private int anzahl;
- public Eintrag(String produkt, int anzahl, double preis) {
- this.anzahl = anzahl;
- this.produkt = produkt;
- this.preis = preis;
- }
- public int getAnzahl() {
- return this.anzahl;
- }
- public double getPreis() {
- return this.preis * this.anzahl;
- }
- public String getProdukt() {
- return this.produkt;
- }
- public void setAnzahl(int anzahl) {
- this.anzahl = anzahl;
- }
- public String toString() {
- return (String.format("%-9s %2d x %5.2f EUR", this.produkt, this.anzahl, this.preis) +
- String.format("%30.2f EUR", anzahl * preis));
- }
- }
- //the class Kassenzettel.java
- import java.util.ArrayList;
- public class Kassenzettel {
- private ArrayList<Eintrag> kassenZettel = new ArrayList<Eintrag>();
- // Constructs a new empty grocery list.
- public void add(Eintrag item) {
- this.kassenZettel.add(item);
- }
- @Override
- public String toString() {
- double sum = 0;
- String ret = "";
- for (int i = 0; i < kassenZettel.size(); i++) {
- ret += kassenZettel.get(i).toString() + "\r\n";
- sum += kassenZettel.get(i).getPreis();
- }
- ret += "\r\nSumme: " + sum;
- return ret;
- }
- }
- //the class Main.java
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // create an instance of kassenzettel to add your produkts to
- Kassenzettel list = new Kassenzettel();
- // set variable to check if read or not (not needed if using break!)
- boolean readOn = true;
- // loop for reading until user inputs x or X
- while (readOn) {
- System.out.println("What would you like? ");
- String produkt = "";
- //read, while produkt is empty
- while(produkt == null || produkt.trim().equals("")){
- produkt = scanner.nextLine();
- }
- // check if produkt name is X or x => exit the loop
- if (produkt.equalsIgnoreCase("x")) {
- readOn = false;
- break;
- }
- System.out.println("How many pieces do you want?");
- int anzahl = scanner.nextInt();
- System.out.println("How much does " + produkt + "cost?");
- double preis = scanner.nextDouble();
- // add a new item to kassenzettel
- list.add(new Eintrag(produkt, anzahl, preis));
- }
- System.out.println("__________________________________");
- System.out.println(" IHRE RECHNUNG ");
- System.out.println("__________________________________");
- // print the output of kassenzettel
- System.out.println(list.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement