Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + "]"; | ||
} | ||
} |