ProcessArticles !

Status
Nicht offen für weitere Antworten.

pep0

Mitglied
Hallo.
Wer kann mir helfen und mir eine mögliche Lösung für dieses Problem liefern?

Wäre sehr dankbar...danke im voraus

mfg
pep0

---

Schreiben Sie ein Programm ProcessArticles2, das eine Datei einliest, die eine Folge von 0 oder mehr Kommandos der folgenden Form enthält (in Wirklichkeit steht jeder Teil eines Kommandos in einer eigenen Zeile):

changeprice Id price
addsale Id sales
printturnover Id
resetsales Id

Das Kommando changeprice setzt den Preis des Artikels Id auf price; das Kommando addSale registriert sales Verkäufe des Artikels Id; das Kommando printturnover druckt den aktuellen Umsatz des Artikels Id; das Kommando resetsales setzt die Verkaufszahlen des Artikels Id auf Null zurück (lässt aber seinen Preis unverändert).

Ihr Programm soll drei Artikel mit den Bezeichnungen “A”, “B”, und “C” verwalten und Kommandodateien wie die folgende verarbeiten:

changeprice A 5.5
changeprice B 2.4
addsale A 4
addsale B 3
addsale A 5
printturnover A
printturnover B
printturnover C
resetturnover A
addsale A 5
changeprice B 1.5
addsale B 2
addsale C 3
printturnover A
printturnover B
printturnover C

Dabei soll Ihr Programm eine Klasse Article mit der folgenden Schnittstelle implementieren und verwenden:

class Article
{
Article(String id);
void setPrice(float price);
boolean void addSale(int n);
boolean printTurnover();
void resetSales();
}

Der Konstruktor Article legt einen neuen Artikel mit der entsprechenden Bezeichnung an. Die Methode addSale überprüft, ob bereits ein Preis für den Artikel gesetzt wurde. Wenn ja, wird der Umsatz des Artikels entsprechend erhöht, und die Funktion liefert als Ergebnis true. Wenn nein, bleibt der Umsatz unverändert und die Funktion liefert als Ergebnis false. Entsprechend liefert die Methode printTurnover als Ergebnis false (und druckt nichts), wenn noch kein Preis für den Artikel gesetzt wurde; ansonsten druckt die Methode eine Zeile der Form

Artikel Id (Preis: Einzelpreis, Verkäufe: Verkäufe): Umsatz

Das Programm soll die Kommandos von einer Textdatei einlesen, deren Namen als Programmargument übergeben wird (siehe Übung 9). Treten bei der Bearbeitung eines Kommandos Fehler auf, soll eine Meldung ausgedruckt werden (und das Programm mit dem nächsten Kommando fortfahren).

Es gelten die üblichen Entwicklungs-, Programmier- und Testrichtlinien; testen Sie das Programm auch mit der oben gezeigten Kommandodatei.

Hinweis: der Ausdruck s0.equals(s1) mit den String-Objekten s0 und s1 liefert true, wenn der Inhalt von s0 dem Inhalt von s1 gleicht (String-Objekte können nicht mit dem Operator == auf inhaltliche Gleichheit getestet werden).
 

pep0

Mitglied
er hat schon ein problem wenn er die article.txt datei einlest...
weiß aber ned warum.
Bitte um Hilfe...

Code:
java ProcessArticles2 article.txt

Code:
article.txt
-----------
changeprice 
A 
5.5
changeprice 
B 
2.4
addsale
A 
4
addsale 
B 
3
addsale 
A 
5
printturnover 
A
printturnover 
B
printturnover 
C
resetturnover 
A
addsale 
A 
5
changeprice 
B 
1.5
addsale 
B 
2
addsale 
C 
3
printturnover 
A
printturnover 
B
printturnover 
C

Code:
public class ProcessArticles2 {
	public static void main(String[] args)
	{
    //create articles
    Article a = new Article("A");
    Article b = new Article("B");
    Article c = new Article("C");

    //get filename
		String fileName = args[0];  //holds the filename

		//+++++ open file +++++
    Input.openInput(fileName);
	  if(!Input.isOkay())
	  {
	    System.out.println("Datei \""+fileName+"\" nicht gefunden!\nProgramm wird beendet.");
	    System.exit(-1); //ERROR could not open file
	  }

    //+++++ read instructions +++++
    boolean printError = true;  //when true a error is printed in case of a read error
    int counter = 0;
    while(true)
    {
      String command = readInstruction();
      if (command == null)
      {
        if(Input.hasEnded())
        {
          if(counter==0)System.out.println("Die angegebene Datei \""+fileName+"\" war leer!");
          break;
        }
        //if(Input.hasEnded())break;
        if(printError) System.out.println("Ein fehlerhaftes Kommando wurde gelesen!");
        printError = false; //error has been printed
        continue;
      }
      printError = true;
      counter++;  //counts the amount of read instructions
      //+++++ read id +++++
      String id = readArticle(); //holds the read id
      if (id == null)
      {
        System.out.println("Ein fehlerhafter Artikel wurde gelesen.");
        continue;
      }
      Article article = null; //holds the actual article
      if(id.equals("A")) article = a;
      if(id.equals("B")) article = b;
      if(id.equals("C")) article = c;

      //+++++ process the read command +++++
      if(command.equals("changeprice"))
      {
        float price = readPrice();
        if (price == -1)
        {
          System.out.println("Ein fehlerhafter Preis wurde gelesen.");
          continue;
        }
        article.setPrice(price);
      }
      else if(command.equals("addsale"))
      {
        int sales = readSales();
        if (sales == -1)
        {
          System.out.println("Fehlerhafte Verkäufe wurde gelesen.");
          continue;
        }
        if(!article.addSale(sales))
        {
          System.out.println("Verkäufe (" + sales + ") konnten nicht zu Artikel " + article.id + " hinzugefügt werden!");
          System.out.println("Es wurde noch kein Preis für Artikel " + article.id + " hinterlegt!");
          continue;
        }
      }
      else if(command.equals("printturnover"))
      {
        if(!article.printTurnover())
        {
          System.out.println("Umsatz von Artikel " + article.id + " konnten nicht ausgegeben werden.");
          System.out.println("Es wurde noch kein Preis für Artikel " + article.id + " hinterlegt!");
          continue;
        }
      }
      else if(command.equals("resetsales"))
      {
        article.resetSales();
      }
    }

    //+++++ close file +++++
    Input.closeInput();
    if(!Input.isOkay()) System.exit(-1); //ERROR could not close file
	}

  //--------------------------------------------------------
  //instruction = readInstruction()
  //
  //Output condition:
  //instruction holds the instruction to execute on a given article
  //returns null when a read error occures or file has ended
  //
  //Effect:
  //Reads instructions from a given textfile
  //--------------------------------------------------------
  static String readInstruction()
  {
    if(!Input.hasEnded())
    {
      String instruction = Input.readString();
      if(!Input.isOkay())
      {
        Input.clearError();
        return null;
      }
      Input.clearError();
      if(instruction.equals("changeprice")||instruction.equals("addsale")||instruction.equals("printturnover")||instruction.equals("resetsales"))
        return instruction;
    }
    return null;
  }

  //--------------------------------------------------------
  //id = readArticle()
  //
  //Output condition:
  //id holds the id of an article read from standard input
  //returns null when a read error occures or file has ended
  //
  //Effect:
  //Reads article id from a given textfile
  //--------------------------------------------------------
  static String readArticle()
  {
    if(!Input.hasEnded())
    {
      String article = Input.readString();
      if(!Input.isOkay())
      {
        Input.clearError();
        return null;
      }
      Input.clearError();
      if(article.equals("A")||article.equals("B")||article.equals("C"))
      {
        return article;
      }
    }
    return null;
  }

  //--------------------------------------------------------
  //sales = readSales()
  //
  //Output condition:
  //sales holds an the amount of sales read from standard input
  //returns -1 when a read error occures or file has ended
  //
  //Effect:
  //Reads sales from a given textfile
  //--------------------------------------------------------
  static int readSales()
  {
    if(!Input.hasEnded())
    {
      int sales = Input.readInt();
      if(!Input.isOkay())
      {
        Input.clearError();
        return -1;
      }
      Input.clearError();
      return sales;
    }
    return -1;
  }

  //--------------------------------------------------------
  //price = readPrice()
  //
  //Output condition:
  //price holds a new price read from standard input
  //returns -1 when a read error occures or file has ended
  //
  //Effect:
  //Reads prices from a given textfile
  //--------------------------------------------------------
  static float readPrice()
  {
    if(!Input.hasEnded())
    {
    float price = Input.readFloat();
    if(!Input.isOkay())
    {
      Input.clearError();
      return -1;
    }
    return price;
    }
    return -1;
  }
}

Code:
class Article
{
	String id;  //holds article id
	float price; //holds article price
	int sales;  //holds the sum of article sales
  float turnover;

	//constructor for article
	Article(String id)
	{
    //set id
	  this.id=id;
	}

  //--------------------------------------------------------
  //article.setPrice(price)
  //
  //Effect:
  //sets a new price for article
  //--------------------------------------------------------
	void setPrice(float price)
	{
    //set price
		this.price = price;
	}

  //--------------------------------------------------------
  //article.addSale(sales)
  //
  //Output condition:
  //returns true if operation is OK else returns false
  //
  //Effect:
  //adds sales to the amount of sales of the article
  //calculates and adds turnover to the field turnover
  //--------------------------------------------------------
	boolean addSale(int n)
	{
    if(price != 0)
    {
      sales = sales+n;
      turnover = turnover + price*n;
      return true;
    }
    return false;
	}

  //--------------------------------------------------------
  //printTurnover()
  //
  //Effect:
  //prints the id, price, sales and turnover of an article on screen
  //--------------------------------------------------------
	boolean printTurnover()
	{
	  if(price != 0)
    {
	    System.out.println("Artikel " + id + " (Preis: " + price + ", Verkäufe: " + sales + "): " + turnover);
	    return true;
    }
    return false;
	}

  //--------------------------------------------------------
  //article.resetSales()
  //
  //Effect:
  //sets the field sales and turnover from article to 0
  //--------------------------------------------------------
	void resetSales()
	{
	  sales=0;
    turnover=0;
	}
}

//Edit sebastian: Code-Tags
 

pep0

Mitglied
wer kann mir helfen...komme nicht mehr weiter...bräuchte dringend eure hilfe...danke im voraus
 
L

Leroy42

Gast
pep0 hat gesagt.:
wer kann mir helfen...komme nicht mehr weiter...bräuchte dringend eure hilfe...danke im voraus

Wenn du weiterhin im Telegrammstil schreibst kann dir kaum geholfen werden.

Erstensmal verlieren viele Leser des Forums schon die Lust wenn du die komplette
Hausaufgabenspezifikation schickst und dann in der Art nun mach mal... nach
Hilfe fragst.

Du hast ja immerhin schon angefangen und selbst etwas geschrieben, aber der Satz

er hat schon ein problem wenn er die article.txt datei einlest...
weiß aber ned warum.

ist vollkommen daneben. Beschreib doch erstmal was für ein Problem er hat,
also was sollte der Teil des Programms tun und was für ein Fehler tritt auf?
 

pep0

Mitglied
habe mein problem schon gelöst...

es waren noch leerzeichen nach manchen befehlen in der article.txt.
darum der fehler bei der einlese methode.

danke trotzdem
 

KSG9|sebastian

Top Contributor
denkst du dass wir den kompletten unformatierten code lesen wollen ?

1. code-Tags erhöhen deine Chance auf Antworten (hab ich jetzt eingefügt)
2. relevante Teile des Codes posten erhöht ebenfalls die Chance
3. debuggen, wo genau liegt das Problem ? was passiert ? passiert nix?
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben