Huhu, hab ein kleines Prob in meinem programm.
Er sortiert und löscht irgendwie falsch, bzw eher das sortieren funktioniert nicht wie es sollte.
hoffe ihr könnt mir helfen :)
Notiz.java
Notizbuch.java
Statistik.java
TestNotiz.java
Er sortiert und löscht irgendwie falsch, bzw eher das sortieren funktioniert nicht wie es sollte.
hoffe ihr könnt mir helfen :)
Notiz.java
Code:
public class Notiz
{
//Attribute
private String betreff;
private String inhalt;
public Notiz(){
betreff = "";
inhalt = "";
}
public Notiz(String be, String in){
betreff = be;
inhalt = in;
}
public String getBetreff(){
return betreff;
}
public String getInhalt(){
return inhalt;
}
public void setBetreff(String be){
betreff = be;
}
public void setInhalt(String in){
inhalt = in;
}
public String textForm(){
return "Betreff: "+ betreff + "\nInhalt: "+ inhalt;
}
}
Code:
public class Notizbuch implements Statistik
{
//Attribut
private Notiz[] buch;
public Notizbuch(Notiz notiz){
buch = new Notiz[1];
buch[0] = notiz;
}
/**
* Eine Methode die die vorhandene Notiz löscht
*
* @param notiz die Notiz die gelöscht werden soll
*/
public void loeschen(Notiz notiz){
Notiz[] temp = new Notiz[buch.length -1];
int neuIndex = 0;
for(int altIndex = 0; altIndex < buch.length; altIndex++){
if(!(buch[altIndex].getBetreff().equals(notiz.getBetreff())) && (buch[altIndex].getInhalt().equals(notiz.getInhalt()))) {
temp[neuIndex] = buch[altIndex];
neuIndex++;
}
}
buch = temp;
}
/**
* Sucht den Betreff in der Notiz
*
* @param be der gesuchte Betreff
*/
public Notiz suchen(String be){
for(int i = 0; i < buch.length; i++){
if(buch[i].getBetreff().equals(be)){
return buch[i];
}
}
return buch[0];
}
/**
* Eine Methode die den Betreff löscht
*
* @param be der Betreff der gelöscht werden soll
*/
public void loeschen(String be){
Notiz[] temp = new Notiz[buch.length -1];
int neuIndex = 0;
for(int altIndex = 0; altIndex < buch.length -1; altIndex++){
if(!(buch[altIndex].getBetreff().equals(be))){
temp[neuIndex] = buch[altIndex];
neuIndex++;
}
}
buch = temp;
}
/**
* Eine Methode die eine Notiz hinzufügt
*
* @param notiz die Notiz
*/
public void hinzufuegen(Notiz notiz) {
Notiz[] buffer = new Notiz[buch.length +1];
int i = 0;
for( ; i < buch.length; i++) {
buffer[i] = buch[i];
}
buffer[i] = notiz;
buch = buffer;
}
/**
* Eine Methode die die Notiz sotiert
*/
public void sortieren() {
for(int i = 0; i < buch.length -1; i++) {
if(buch[i].getBetreff().compareTo(buch[i+1].getBetreff()) < 0) {
Notiz buffer = buch[i];
buch[i] = buch[i+1];
buch[i+1] = buffer;
}
}
}
public String liste() {
String ausgabe = "";
for(int i=0; i < buch.length; i++) {
ausgabe += buch[i].getBetreff() + "\n" + buch[i].getInhalt();
ausgabe += "\n";
}
return ausgabe;
}
@Override
public double summe(){
return (double)buch.length;
}
@Override
public int anzahl(Object o){
int anz = 0;
for(int i = 0; i < buch.length;i++){
if(buch[i].equals((Notiz) o)){
anz++;
}
}
return anz;
}
}
Code:
public interface Statistik {
/**
* Berechnet die Summe aller relevanten Werte der Klasse und gibt sie zurück.
*
* @return die Summe der relevanten Werte
*/
public double summe();
/**
* Berechnet die Anzahl von den Vorkommnissen des Parameters o innerhalb
* des Objekts und gibt sie zurück.
*
* @param o der Wert, dessen Vorkommen gezählt wird
* @return die Anzahl von o
*/
public int anzahl(Object o);
}
Code:
import javax.swing.JOptionPane;
public class TestNotiz
{
public static void main(String []args) {
String betreff = JOptionPane.showInputDialog("Bitte schreiben Sie einen Betreff ein: ");
String inhalt = JOptionPane.showInputDialog("Bitte schreiben Sie einen Inhalt ein: ");
Notiz s1 = new Notiz(betreff, inhalt);
Notizbuch n1 = new Notizbuch(s1);
int anz = Integer.parseInt(JOptionPane.showInputDialog("Wie viele Notizen wollen Sie eingeben ?"));
for(int i=0; i < anz; i++){
betreff = JOptionPane.showInputDialog("Bitte schreiben Sie einen Betreff ein: ");
inhalt = JOptionPane.showInputDialog("Bitte schreiben Sie einen Inhalt ein: ");
s1.setBetreff(betreff);
s1.setInhalt(inhalt);
n1.hinzufuegen(s1);
}
betreff = JOptionPane.showInputDialog("Bitte geben Sie einen Betreff einer Notiz ein, welche Sie löschen wollen: ");
n1.loeschen(betreff);
betreff = JOptionPane.showInputDialog("Bitte geben Sie einen Betreff einer Notiz ein, welche Sie suchen wollen: ");
Notiz notiz = n1.suchen(betreff);
if(!(notiz == null)){
JOptionPane.showMessageDialog(null, notiz.textForm());
}
else{
JOptionPane.showMessageDialog(null, "Die Notiz wurde nicht gefunden!");
}
JOptionPane.showMessageDialog(null, n1.liste());
n1.sortieren();
JOptionPane.showMessageDialog(null, n1.liste());
int anzahl = n1.anzahl(s1);
JOptionPane.showMessageDialog(null, anzahl);
double summe = n1.summe();
JOptionPane.showMessageDialog(null, summe);
}
}