Skip to content

Commit

Permalink
complete
Browse files Browse the repository at this point in the history
  • Loading branch information
sanek83 committed Nov 12, 2011
1 parent f6b74c5 commit 3f81b43
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 11 deletions.
49 changes: 49 additions & 0 deletions src/javaoofund/sandwich/Main.java
@@ -0,0 +1,49 @@
package javaoofund.sandwich;

import java.util.ArrayList;
import java.util.List;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Sandwich usa = new Sandwich("U.S.A.", 300);
Sandwich cheese = new Sandwich("Cheese", 350);
Sandwich ham = new Sandwich("Ham", 300);

Supplement salad = new Supplement("Salad");
Supplement tomato = new Supplement("Tomato");
Supplement egg = new Supplement("Egg");
Supplement corn = new Supplement("Corn");

usa.getComponents().add(salad);
usa.getComponents().add(tomato);
cheese.getComponents().add(corn);
cheese.getComponents().add(tomato);
ham.getComponents().add(egg);
ham.getComponents().add(corn);

Worker marty = new Worker("Marty", 500);
Worker biff = new Worker("Biff", 1200);

marty.purchase(usa);
//marty.purchase(cheese);

biff.purchase(ham);
biff.purchase(cheese);
//biff.purchase(usa);


List<Sandwich> sandwiches = new ArrayList<Sandwich>();
sandwiches.add(usa);
sandwiches.add(cheese);
sandwiches.add(ham);

for(Sandwich s: sandwiches){
System.out.println(s);
}
}

}
43 changes: 39 additions & 4 deletions src/javaoofund/sandwich/Sandwich.java
@@ -1,20 +1,55 @@
package javaoofund.sandwich;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Sandwich {

private String name;
private List<String> components;
private List<Supplement> components;
private int price;

private Worker owner;

public Sandwich(String name, int price) {
super();
this.name = name;
this.price = price;

this.components = new ArrayList<Supplement>();
}

public String getName() {
return name;
}

public List<Supplement> getComponents() {
return components;
}

public int getPrice() {
return price;
}

public Worker getOwner() {
return owner;
}

public void setOwner(Worker worker) {
this.owner = worker;
}

@Override
public String toString() {
return "Sandwich [name=" + name + ", supplement=" + components
+ ", price=" + price + ", owner=" + owner + "]";
}

public boolean addComponents(String component){
return components.contains(component)? false: components.add(component)&&true;
}



}
17 changes: 17 additions & 0 deletions src/javaoofund/sandwich/Supplement.java
@@ -0,0 +1,17 @@
package javaoofund.sandwich;

public class Supplement {

private String name;

public Supplement(String name) {
super();
this.name = name;
}

@Override
public String toString() {
return "Supplement [name=" + name + "]";
}

}
45 changes: 38 additions & 7 deletions src/javaoofund/sandwich/Worker.java
@@ -1,25 +1,56 @@
package javaoofund.sandwich;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author alex
* @version 0.1
*/
public class Worker {

private String name;
private int money;


private List<Sandwich> sandwiches;

public List<Sandwich> getSandwiches() {
return sandwiches;
}

public Worker(String name, int money) {
super();
this.name = name;
this.money = money;
this.sandwiches = new ArrayList<Sandwich>();
}

public String getName() {
return name;
}

public int getMoney() {
return money;
}

public void purchase(){


public void purchase(Sandwich s) {

if ((s != null) && (s.getOwner() != null)) {
throw new RuntimeException(
"Sandwich is already owned by another Worker");
}

if (this.money - s.getPrice() < 0) {
throw new RuntimeException("Budget ist to low!");
}

this.sandwiches.add(s);
s.setOwner(this);
this.money -= s.getPrice();
}

@Override
public String toString() {
return "Worker [name=" + name + "]";
}
}

0 comments on commit 3f81b43

Please sign in to comment.