Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

Javakurs/Übungsaufgaben/Kassenbon++/Musterloesung

Receipt.java

import java.util.Vector;

/**
 * This class will be used to create a generic receipt
 */
public class Receipt {
	//! java api: vector storing items
	protected Vector<ReceiptItem> _list;
	
	//! Constructor clearing the receipt
	public Receipt() {
		_list = new Vector<ReceiptItem>();
	}

	//! add one item at the last position of list 
	public void add(ReceiptItem receiptItem) {
		_list.add(receiptItem);
	}

	//! prints all items to console, plus a fancy header ...
	public void print() {
		// print a nice header
		System.out.println("╔════════════════════════╗");
		System.out.println("║ FreitagsrundenShop 042 ║");
		System.out.println("║   Franklinstr. 28/29   ║");
		System.out.println("║       10587 Berlin     ║");
		System.out.println("║     ☏ 030 314 213 86   ║");
		System.out.println("╚════════════════════════╝");
		System.out.println();
		
		// print each item and add its price
		double sum = 0.0;
		for( ReceiptItem item : _list )
		{
			System.out.printf("%-25s\n", item.getName());
			System.out.printf("%3dx %21.2f\n", item.getAmount(),item.getPrice());
			
			sum += item.getPrice() * item.getAmount();
		}
		System.out.println();
		
		// add a nice underscore line (-3: 2 floating parts + one ".")
		String line = new String();
		for( int i = -3; i < Math.log(sum)/Math.log(10);++i) {
			line += "=";
		}
		
		System.out.printf("%26s\n", line);
		System.out.printf("Summe EUR %16.2f\n", sum);
		System.out.printf("%26s\n", line);
	}

}

ReceiptItem.java

/**
 * This class will represent one item of a receipt
 */
public class ReceiptItem {

	//! Identifier of given item
	protected String _name;
	
	//! amount of this items
	protected int _amount;
	
	//! price per item
	protected double _price;
	
	public ReceiptItem(String name, int amount, double price) {
		this.setName( name );
		this.setAmount( amount );
		this.setPrice( price );
	}

	public String getName() {
		return _name;
	}

	public int getAmount() {
		return _amount;
	}

	public double getPrice() {
		return _price;
	}

	public void setName(String name) {
		if( name.equals("") != true ) {
			this._name = name;
		}
	}

	public void setAmount(int amount) {
		if( amount > 0 ) {
			this._amount = amount;
		}
	}

	public void setPrice(double price) {
		if( price > 0.0 ) {
			this._price = price;
		}
	}
	
}