Skip to content
Snippets Groups Projects
Commit f071308d authored by Lukas Friedrichsen's avatar Lukas Friedrichsen
Browse files

Initializing VInf Repository

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 408 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/bin/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>VINF_Briefmarkenautomat</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
package briefmarkenautomat;
public class Briefmarkenautomat {
private static UserInteraction ui;
private static Sendungstyp sendung;
private static Kasse kasse;
public Briefmarkenautomat() {
ui = new UserInteraction();
sendung = new Sendungstyp();
kasse = new Kasse();
}
private static int briefmarkenWaehlen(Typ sendungstyp) throws IllegalArgumentException{
String uiInput;
int anzahlBriefmarken = 0;
System.out.println("Geben Sie die Anzahl der zu erwerbenden Briefmarken ein (max. 10)!\n");
uiInput = ui.scan();
try {
anzahlBriefmarken = Integer.parseInt(uiInput);
}
catch (NumberFormatException e){
System.out.println("Ungueltige Eingabe!");
IllegalArgumentException err = new IllegalArgumentException(UserInteraction.UE_EXCEPTION);
throw err;
}
if (anzahlBriefmarken <= 10 && anzahlBriefmarken > 0){
return anzahlBriefmarken;
}
else if (anzahlBriefmarken <= 0){
return 1;
}
else {
return 10;
}
}
private static void automat(){
while (true) {
try {
String uiInput;
double sendungshoehe; // Initialisiere Variablen dermaßen, dass eine Exception geworfen wird, falls diese nicht mit Werten belegt werden.
int sendungsgewicht;
Typ sendungseigenschaften;
int anzahlBriefmarken;
System.out.println("%------------------------------ Programmstart ------------------------------%\n");
System.out.println("Geben Sie die Hoehe Ihrer Sendung in Zentimetern ein!\n");
uiInput = ui.scan();
sendungshoehe = Double.parseDouble(uiInput);
System.out.println("Geben Sie das Gewicht Ihrer Sendung in Gramm ein!\n");
uiInput = ui.scan();
sendungsgewicht = Integer.parseInt(uiInput);
sendungseigenschaften = sendung.typ(sendungshoehe, sendungsgewicht);
System.out.println("Sendungstyp: " + sendungseigenschaften.getBezeichnung() + "\nPorto: " + sendungseigenschaften.getPorto());
anzahlBriefmarken = briefmarkenWaehlen(sendungseigenschaften);
if (anzahlBriefmarken < 0){
continue;
}
double betrag = anzahlBriefmarken*sendungseigenschaften.getPorto();
kasse.bezahlen(betrag, ui);
System.out.println("%--------------------------- Auftrag abgeschlossen! ---------------------------%\n");
System.out.println("%------------------------------ Programmneustart ------------------------------%\n");
}
catch (NumberFormatException e){
System.out.println("Ungueltige Eingabe!");
continue;
}
catch (IllegalArgumentException e){
if (e.getMessage() == UserInteraction.EXIT_EXCEPTION){
break;
}
else if (e.getMessage() == UserInteraction.PN_EXCEPTION || e.getMessage() == UserInteraction.UE_EXCEPTION){
continue;
}
else {
e.printStackTrace();
break;
}
}
}
ui.closeScanner();
System.out.println("%------------------------------ Programmsabbruch ------------------------------%\n");
}
public static void main(String[] args) {
ui = new UserInteraction();
sendung = new Sendungstyp();
kasse = new Kasse();
automat();
}
}
package briefmarkenautomat;
public class Kasse {
final static double ZWEIEURO = 2.0;
final static double EINEURO = 1.0;
final static double FUENFZIGCENT = 0.5;
final static double ZWANZIGCENT = 0.2;
final static double ZEHNCENT = 0.1;
final static double FUENFCENT = 0.05;
final static Muenze[] MUENZEN = {new Muenze(ZWEIEURO, 5),
new Muenze(2.0, 5),
new Muenze(1.0, 5),
new Muenze(0.5, 5),
new Muenze(0.2, 5),
new Muenze(0.1, 5),
new Muenze(0.05, 5)
};
public Kasse(){
// Nothing to do...
}
public void bezahlen(double betrag, UserInteraction ui) throws IllegalArgumentException {
double differenz = Math.round(1000.0 * betrag) / 1000.0;;
while (true){
if (differenz <= 0){
wechselgeldAusgeben(-differenz);
}
String uiInput;
double zahlung;
System.out.println("Zu zahlender Betrag: " + differenz + " Euro\n"
+ "Geben Sie den zu zahlenden Betrag in Euro ein!\n"
+ "Zulaessige Betraege: 2 Euro, 1 Euro, 0.5 Euro, 0.2 Euro, 0.1 Euro, 0.05 Euro\n");
uiInput = ui.scan();
try {
zahlung = Double.parseDouble(uiInput);
}
catch (NumberFormatException e){
System.out.println("Ungueltige Eingabe!");
IllegalArgumentException err = new IllegalArgumentException(UserInteraction.UE_EXCEPTION);
throw err;
}
int counter = 0;
for(Muenze geld : MUENZEN){
if (zahlung == geld.getWert()){
differenz = differenz-zahlung;
differenz = Math.round(1000.0 * differenz) / 1000.0;
geld.increment();
if (differenz <= 0){
wechselgeldAusgeben(-differenz);;
}
break;
}
counter++;
}
if (counter == MUENZEN.length){
System.out.println("Ungueltige Eingabe!");
IllegalArgumentException e = new IllegalArgumentException(UserInteraction.UE_EXCEPTION);
throw e;
}
}
}
public void wechselgeldAusgeben(double rueckgeld){
double differenz = Math.round(1000.0 * rueckgeld) / 1000.0;;
for(int i = 0; i < MUENZEN.length; i++){
double betrag = MUENZEN[i].getWert();
int counter = 0;
while(differenz >= betrag && MUENZEN[i].getAnzahl() > 0){
differenz = differenz-betrag;
differenz = Math.round(1000.0 * differenz) / 1000.0;
MUENZEN[i].decrement();
counter++;
}
if (counter > 0){
System.out.println("Wechselgeld: " + counter + " mal " + betrag + " Euro");
}
}
if (differenz > 0.0){
System.out.println("" + differenz + "Euro können nicht gewechselt werden!\n");
}
}
}
package briefmarkenautomat;
public class Muenze {
private final double wert;
private int anzahl;
public Muenze(double wert, int anzahl){
this.wert = wert;
this.anzahl = anzahl;
}
public double getWert(){
return wert;
}
public int getAnzahl(){
return anzahl;
}
public void increment(){
anzahl++;
}
public void decrement(){
anzahl--;
}
}
package briefmarkenautomat;
public class Sendungstyp {
private static final Typ[] SENDUNGSTYP = new Typ[] {
new Typ("Postkarte", 0.45, 0.0, 0),
new Typ("Standardbrief", 0.7, 0.5, 20),
new Typ("Kompaktbrief", 0.85, 1, 50),
new Typ("Grossbrief", 1.45, 2, 500),
new Typ("Maxibrief", 2.6, 5, 1000)
};
public Sendungstyp(){
// Nothing to do...
}
public Typ typ(double hoehe, int gewicht) throws IllegalArgumentException{
for (Typ sendung : SENDUNGSTYP){
if (hoehe <= sendung.getMaxHoehe() && gewicht <= sendung.getMaxGewicht()){
return sendung;
}
}
IllegalArgumentException e = new IllegalArgumentException("Unbekannter Sendungstyp!");
throw e;
}
/*public static void main(String[] args){
postkarte = new Typ("Postkarte", 0.45, 0.0, 0);
standardbrief = new Typ("Standardbrief", 0.7, 0.5, 20);
kompaktbrief = new Typ("Kompaktbrief", 0.85, 1, 50);
grossbrief = new Typ("Grossbrief", 1.45, 2, 500);
maxibrief = new Typ("Maxibrief", 2.6, 5, 1000);
sendungstyp = new Typ[] {postkarte, standardbrief, kompaktbrief, grossbrief, maxibrief};
System.out.println(typ(0.3, 400).getBezeichnung());
System.out.println(typ(3.3, 900).getBezeichnung());
System.out.println(typ(0.3, 2000).getBezeichnung());
System.out.println(typ(0.3, 400).getBezeichnung());
}*/
}
package briefmarkenautomat;
public class Typ {
private String bezeichnung;
private double porto;
private double maxHoehe;
private int maxGewicht;
public Typ(String bezeichnung, double porto, double maxHoehe, int maxGewicht){
this.bezeichnung = bezeichnung;
this.porto = porto;
this.maxHoehe = maxHoehe;
this.maxGewicht = maxGewicht;
}
public String getBezeichnung(){
return bezeichnung;
}
public double getPorto(){
return porto;
}
public double getMaxHoehe(){
return maxHoehe;
}
public int getMaxGewicht(){
return maxGewicht;
}
}
package briefmarkenautomat;
import java.util.Scanner;
public class UserInteraction {
final static String UE_EXCEPTION = "Ungueltige Eingabe";
final static String EXIT_EXCEPTION = "Exit";
final static String PN_EXCEPTION = "Programmneustart";
private Scanner sc;
public UserInteraction(){
sc = new Scanner(System.in);
}
public String scan() throws IllegalArgumentException {
String message = sc.next();
if (message.equals("a")){
IllegalArgumentException e = new IllegalArgumentException(PN_EXCEPTION);
throw e;
}
else if (message.equals("exit")){
IllegalArgumentException e = new IllegalArgumentException(EXIT_EXCEPTION);
throw e;
}
else {
return message;
}
}
public void closeScanner(){
sc.close();
}
}
package briefmarkenautomat;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author lukas
*
*/
public class SendungstypTest {
private static Typ testTyp;
private static Sendungstyp testSendungstyp;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void SetUpBeforeClass() throws Exception {
testSendungstyp = new Sendungstyp();
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
testTyp = null;
}
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
testTyp = new Typ("Postkarte", 0.45, 0.0, 0);
}
/**
* Test method for {@link briefmarkenautomat.Sendungstyp#typ(double, int)}.
*/
@Test
public final void testTyp() {
Typ rueckgabe = testSendungstyp.typ(0, 0);
assertEquals(rueckgabe.getBezeichnung(), "Postkarte");
assertEquals(rueckgabe.getMaxGewicht(), 0);
assertEquals(rueckgabe.getMaxHoehe(), 0, 0.0001);
assertEquals(rueckgabe.getPorto(), 0.45, 0.0001);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment