Commit e2ee5003 authored by Matti Henning's avatar Matti Henning
Browse files

inital src/ commit

parent c962b9f7
import java.util.Date;
import java.util.List;
import model.Customer;
import model.Order;
import model.OrderItem;
import system.Builder;
import system.DataAccess;
import system.Logic;
import model.Article;
public class Application_2 {
public static void main( String[] args ) {
System.out.println( "Bestellsystem SE1 (v2)." );
Builder builder = Builder.getInstance();
builder.startup();
DataAccess data = builder.dataAccess();
Logic logic = builder.logic();
Customer cNadine = data.getCustomer( 2 );
// build Nadine's 2nd order from existing data
Order o7356 = new Order( 7356613535L, new Date(), cNadine );
Article teller = data.getArticle( 3 );
o7356.addItem( // 6 Teller
new OrderItem( teller.getDescription() + " blau/weiss Keramik", teller, 6 )
);
data.add( o7356 );
List<Order> orders = data.getOrders();
logic.printInventory();
logic.printOrders( orders );
logic.printInventory();
builder.shutdown();
}
}
\ No newline at end of file
package model;
public class Article {
private String id;
private String description;
private long unitPrice;
private int unitsInStore;
public Article(String id, String description, long unitPrice, int unitsInStore) {
this.id = id;
this.description = description;
this.unitPrice = unitPrice;
this.unitsInStore = unitsInStore;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public long getUnitPrice() {
return unitPrice;
}
public void setUnitprice(long unitPrice) {
this.unitPrice = unitPrice;
}
public int getUnitsInStore() {
return unitsInStore;
}
public void setUnitsInStore(int unitsInStore) {
this.unitsInStore = unitsInStore;
}
}
package model;
public class Customer {
private String id;
private String firstName;
private String lastName;
private String contact;
public Customer(String id, String fullName, String contact) {
this.id = id;
setName(fullName);
this.contact = contact;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setName(String fullName) {
if (fullName.contains(",")) {
String[] nameArr = fullName.trim().split("\\s*,\\s*", 2);
this.firstName = nameArr[1];
this.lastName = nameArr[0];
}
else {
int border = fullName.trim().lastIndexOf(" ");
this.firstName = fullName.trim().substring(0, border).trim();
this.lastName = fullName.trim().substring(border, fullName.length()).trim();
}
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
package model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Order {
private long id;
private Date date;
private Customer customer;
private List<OrderItem> items;
public Order(long id, Date date, Customer customer) {
this.id = id;
this.date = date;
this.customer = customer;
this.items = new ArrayList<OrderItem>();
}
public long getId() {
return id;
}
public Date getDate() {
return date;
}
public Order setDate(Date date) {
this.date = date;
return this;
}
public Customer getCustomer() {
return customer;
}
public List<OrderItem> getItems() {
return items;
}
public Order addItem(OrderItem item) {
items.add(item);
return this;
}
public Order removeItem(OrderItem item) {
items.remove(item);
return this;
}
}
package model;
public class OrderItem {
private String description;
private Article article;
private int unitsOrdered;
public OrderItem(String description, Article article, int unitsOrdered) {
this.description = description;
this.article = article;
this.unitsOrdered = unitsOrdered;
}
public String getDescription() {
return description;
}
public void setDescritopn(String description) {
this.description = description;
}
public Article getArticle() {
return article;
}
public int getUnitsOrdered() {
return unitsOrdered;
}
public void setUnitsOrdered(int number) {
this.unitsOrdered = number;
}
}
package system;
public class Builder implements ComponentLifecycle {
private static Builder instance = new Builder();
private final Logic logic;
private final DataAccess dataAccess;
private Builder() {
this.logic = new LogicImpl();
this.dataAccess = new MockDataImpl();
}
public static Builder getInstance() {
return instance;
}
@Override
public void startup() {
dataAccess.startup();
logic.startup();
}
@Override
public void shutdown() {
logic.shutdown();
dataAccess.shutdown();
}
public DataAccess dataAccess() {
return dataAccess;
}
public Logic logic() {
return logic;
}
}
package system;
public interface ComponentLifecycle {
public void startup();
public void shutdown();
}
package system;
import java.util.List;
import model.Article;
import model.Customer;
import model.Order;
public interface DataAccess extends ComponentLifecycle {
public List<Customer> getCustomers();
public List<Article> getArticles();
public List<Order> getOrders();
public Customer getCustomer(int i);
public Article getArticle(int i);
public Order getOrder(int i);
public DataAccess add(Order order);
}
package system;
import java.util.List;
import model.Order;
public interface Logic extends ComponentLifecycle {
public void printOrders(List<Order> orders);
public void printInventory();
public long calculateTotal(Order order);
public long calculateIncludedVAT(Order order);
}
package system;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;
import model.Article;
import model.Order;
import model.OrderItem;
class LogicImpl implements Logic {
@Override
public void startup() {
// TODO Auto-generated method stub
}
@Override
public void shutdown() {
// TODO Auto-generated method stub
}
@Override
public void printOrders(List<Order> orders) {
for( Order order : orders ) {
long id = order.getId();
String name = order.getCustomer().getFirstName();
StringBuffer sb = new StringBuffer(); // unwind ordered articles
sb.append( name ).append( "'s Bestellung #(" ).append( id ).append( "): " );
for( int i=0; i < order.getItems().size(); i++ ) {
OrderItem item = order.getItems().get( i );
sb.append( i > 0? ", " : "" )
.append( item.getUnitsOrdered() )
.append( "x " ) .append( item.getDescription() );
item.getArticle().setUnitsInStore(item.getArticle().getUnitsInStore()-item.getUnitsOrdered());
}
System.out.println( sb.append( "." ).toString() );
StringBuffer sb2 = new StringBuffer();
sb2.append("\t==> ");
for (int i=0; i < order.getItems().size(); i++) {
OrderItem item = order.getItems().get( i );
sb2.append( i > 0? "; " : "")
.append( item.getUnitsOrdered() )
.append( "x " )
.append( fmtPrice(item.getArticle().getUnitPrice(), ""));
}
sb2.append(" ==> Total: ")
.append(fmtPrice( calculateTotal(order), "EUR" ))
.append(" (")
.append( fmtPrice( calculateIncludedVAT( order ), "MWSt"))
.append(")");
System.out.println(sb2.append("\n").toString());
}
StringBuffer sb = new StringBuffer();
long totalAllOrders = 0;
long totalVATAllOrders = 0;
for (Order order : orders) {
totalAllOrders += calculateTotal(order);
}
totalVATAllOrders = (long) ( Math.round(totalAllOrders - ( totalAllOrders / 1.19 )));
sb.append("\t== ==> Total (all orders): ")
.append(fmtPrice( totalAllOrders, "EUR"))
.append(" (")
.append(fmtPrice( totalVATAllOrders, "MWSt"))
.append(")");
System.out.println(sb.toString());
}
@Override
public void printInventory() {
StringBuffer sb = new StringBuffer();
sb.append("\n")
.append("Available Inventory:\n");
List<Article> articles = Builder.getInstance().dataAccess().getArticles();
int widthBefore = 0;
int widthThis = 0;
int widthMax = 0;
int total = 0;
StringBuffer sb2 = new StringBuffer();
for (int i = 0; i < articles.size(); i++ ) {
Article article = articles.get(i);
sb2.append(article.getId())
.append(", ")
.append(article.getDescription().length() > 10? article.getDescription().substring(0, 11) : article.getDescription())
.append("\t")
.append("-->")
.append(String.format("%6s", article.getUnitsInStore()))
.append(" units x ")
.append(String.format("%6s", fmtPrice(article.getUnitPrice(), "")))
.append(", total value: ")
.append(String.format("%15s", fmtPrice((article.getUnitPrice()*article.getUnitsInStore()), "EUR")))
.append("\n");
widthThis = sb2.length() - widthBefore;
widthBefore = sb2.length();
total += article.getUnitPrice()*article.getUnitsInStore();
if (widthThis > widthMax) {
widthMax = widthThis;
}
if ((i+1) == articles.size()) {
for (int i1 = 1; i1 < widthMax; i1++) {
sb2.append("-");
}
}
}
sb2.append("\n")
.append(String.format("%" + (widthMax - 1) + "s", fmtPrice(total, "EUR")));
System.out.print(sb);
System.out.println(sb2);
}
@Override
public long calculateTotal(Order order) {
long total = 0;
for (int i=0; i < order.getItems().size(); i++) {
OrderItem item = order.getItems().get( i );
total += ( item.getArticle().getUnitPrice() * item.getUnitsOrdered() );
}
return total;
}
@Override
public long calculateIncludedVAT(Order order) {
//double vatRate = 19.0;
double vat = 0;
for (int i = 0; i < order.getItems().size(); i++) {
OrderItem item = order.getItems().get(i);
double unitPriceGROSS = item.getArticle().getUnitPrice();
vat += (unitPriceGROSS - ( unitPriceGROSS / 1.19 )) * item.getUnitsOrdered();
}
return (long) Math.round(vat);
}
private String fmtPrice( long price, String currency ) {
double total = ( (double)price ) / 100.0; // convert cent to Euro
DecimalFormat df = new DecimalFormat( "#,##0.00", new DecimalFormatSymbols(new Locale( "de" ) ) ); // round to 2 digits
String fmtPrice = df.format( total );
fmtPrice += ( currency != null && currency.length() > 0 )? " " + currency : "";
return fmtPrice; }
}
package system;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import model.Article;
import model.Customer;
import model.Order;
import model.OrderItem;
class MockDataImpl implements DataAccess {
private final List<Customer> customers;
private final List<Article> articles;
private final List<Order> orders;
MockDataImpl() {
this.articles = new ArrayList<Article>();
this.customers = new ArrayList<Customer>();
this.orders = new ArrayList<Order>();
}
@Override
public void startup() {
Customer cEric = new Customer( "C86516", "Eric Schulz-Mueller", "eric2346@gmail.com" );
Customer cAnne = new Customer( "C64327", "Meyer, Anne", "+4917223524" );
Customer cNadine = new Customer( "C12396", "Nadine Ulla Blumenfeld", "+4915292454" );
Customer cTimo = new Customer( "C14348", "Timo Werner", "tw@gmail.com" );
Customer cSandra = new Customer( "C09234", "Sandra Müller", "samue62@gmx.de" );
customers.add( cEric );
customers.add( cAnne );
customers.add( cNadine );
customers.add( cTimo );
customers.add( cSandra );
Article a1 = new Article( "SKU-458362", "Tasse", 299, 2000 );
Article a2 = new Article( "SKU-693856", "Becher", 149, 8400 );
Article a3 = new Article( "SKU-518957", "Kanne", 2000, 2400 );
Article a4 = new Article( "SKU-638035", "Teller", 649, 7000 );
Article a5 = new Article( "SKU-945867", "Kaffeemaschine", 2999, 500 );
Article a6 = new Article( "SKU-592756", "Teekocher", 1999, 2000);
articles.add( a1 );
articles.add( a2 );
articles.add( a3 );
articles.add( a4 );
articles.add( a5 );
articles.add( a6 );
// Eric's 1st order
Order o5234 = new Order( 5234968294L, new Date(), cEric );
OrderItem oi1 = new OrderItem( a3.getDescription(), a3, 1 ); // 1 Kanne (a3)
o5234.addItem( oi1 ); // add OrderItem to Order 5234968294L
orders.add(o5234);
// Eric's 2nd order
Order o8592 = new Order( 8592356245L, new Date(), cEric );
o8592.addItem( // add three OrderItems to Order 8592356245L
new OrderItem( a4.getDescription(), a4, 4 ) // 4 Teller (a4)
).addItem(
new OrderItem( a2.getDescription(), a2, 8 ) // 8 Becher (a2)
).addItem(
new OrderItem( "passende Tassen", a1, 4 ) // 4 passende Tassen (a1)
);
orders.add(o8592);
// Anne's order
Order o3563 = new Order( 3563561357L, new Date(), cAnne );
o3563.addItem(
new OrderItem( a3.getDescription() + " aus Porzellan", a3, 1 )
);
orders.add(o3563);
// Nadine's order
Order o6135 = new Order( 6135735635L, new Date(), cNadine );
o6135.addItem( // 12 Teller
new OrderItem( a4.getDescription() + " blau/weiss Keramik", a4, 12 )
);
orders.add(o6135);
// Timo's order
Order o8348 = new Order( 9235856245L, new Date(), cTimo );
o8348.addItem(
new OrderItem( a5.getDescription(), a5, 1 )
).addItem(
new OrderItem( a1.getDescription(), a1, 6 )
);
orders.add(o8348);
// Sandra's order
Order o7322 = new Order( 7335856245L, new Date(), cSandra );
o7322.addItem(
new OrderItem( a6.getDescription(), a6, 1 )
).addItem(
new OrderItem( a2.getDescription(), a2, 4 )
).addItem(
new OrderItem( a4.getDescription(), a4, 4 )
);
orders.add(o7322);
}
@Override
public void shutdown() {
// TODO Auto-generated method stub
}
@Override
public List<Customer> getCustomers() {
return customers;
}
@Override
public List<Article> getArticles() {
return articles;
}
@Override
public List<Order> getOrders() {
return orders;
}
@Override
public Customer getCustomer(int i) {
if (i >= 0 && i < customers.size()) {
return customers.get(i);