Ursprünglich programmiert(Aufgabe in der Umschulung-,-):
Ja es muss laut UML so behindert sein^^. So jetzt wollte ich ein wenig weiter gehen und einen Gui machen. Die Probleme fangen somit an und sind warscheinlich simple zu lösen. Es geht darum wie ich die Ergebnisse der einzelnen Berechnungen am besten anzeigen kann. Ich habe einen GUI- Entwurf gemacht und das einzigste was geht ist exit. Ich habe also zumindest verstanden wo die Funktionen reinkommen und wie man den GUI unterteilt nur wo ist jetzt der genaue Unterschied zur Konsole. Ich bin auf die Idee gekommen, dass man System.out.* ect. nicht mehr verwenden kann und habe versucht es umzuschreiben:
Wie implementiere ich jetzt die Ausgabe
Code:
package zweiteWoche;
public class Animal {
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void showYourself() {
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Animal other = (Animal) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package zweiteWoche;
import javax.swing.JOptionPane;
public class AnimalShelter {
Animal animal[] = new Animal[10];
public boolean addAnimal(Animal newAnimal) {
for(int i =0;i<animal.length;i++) {
if(animal[i]!= null) {
System.out.println("this place is coatet");
}else {
animal[i]= newAnimal;
System.out.println("animal has been added");
newAnimal.showYourself();
return true;
}
}
System.err.println("Something has gone wrong");
return false;
}
public void animalInformation() {
for(int i=0;i<animal.length;i++) {
System.out.println(animal[i]);
}
}
public void removeAnimal(int index) {
animal[index]=null;
System.out.println("Thank you for comming");
}
}
package zweiteWoche;
public class Cat extends Animal {
public Cat(String name, int age) {
super(name, age);
}
@Override
public void showYourself() {
System.out.println("I´m a cat!");
}@Override
public String toString()
{
return "cat [name=" + super.getName() + ", age=" +super.getAge() + "]";
}
}
package zweiteWoche;
public class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
@Override
public void showYourself() {
System.out.println("I´m a dog!");
}@Override
public String toString()
{
return "dog [name=" + super.getName() + ", age=" +super.getAge() + "]";
}
}
package zweiteWoche;
import javax.swing.JOptionPane;
public class Start {
@SuppressWarnings("unused")
public static void main(String[] args) {
AnimalShelter animalShelter = new AnimalShelter();
//Menu
String option = toStringOption();
System.out.println(option);
String menuString = JOptionPane.showInputDialog("Push the indexnumber of menue");
while(menuString != null) {
int menu = Integer.parseInt(menuString);
switch(menu) {
case 1:animalShelter.animalInformation();
try {
Thread.sleep(1000*2);
} catch (InterruptedException e) {
e.printStackTrace();
}
option = toStringOption();
System.out.println(option);
menuString = JOptionPane.showInputDialog("Push the indexnumber of menue");break;
case 2:String querry= JOptionPane.showInputDialog("Have you got a [dog]/ a [cat]?");
if(querry.contains("dog")) {
querry= JOptionPane.showInputDialog("name:");
String querrytwo= JOptionPane.showInputDialog("age:");
int input = Integer.parseInt(querrytwo);
Dog dog = new Dog(querry, input);
animalShelter.addAnimal(dog);
option = toStringOption();
System.out.println(option);
menuString = JOptionPane.showInputDialog("Push the indexnumber of menue");break;
}
else if(querry.contains("cat")) {
querry= JOptionPane.showInputDialog("name:");
String querrytwo= JOptionPane.showInputDialog("age:");
int input = Integer.parseInt(querrytwo);
Cat cat = new Cat(querry, input);
animalShelter.addAnimal(cat);
option = toStringOption();
System.out.println(option);
menuString = JOptionPane.showInputDialog("Push the indexnumber of menue");break;
}else {
System.out.println("only dogs and cats");
option = toStringOption();
System.out.println(option);
menuString = JOptionPane.showInputDialog("Push the indexnumber of menue");break;
}
case 3:for(int i=0;i<animalShelter.animal.length;i++) {
System.out.println(animalShelter.animal[i]);
}
option = JOptionPane.showInputDialog("Set index to remove");
int input = Integer.parseInt(option);
animalShelter.removeAnimal(input);
option = toStringOption();
System.out.println(option);
menuString = JOptionPane.showInputDialog("Push the indexnumber of menue");break;
case 4: System.exit(5);break;
default: System.err.println("Error");
}
}
} public static String toStringOption() {
return "########## ########## ########## ##########\n"
+" Welcome to Animalhotel \n"
+"########## ########## ########## ##########\n\n"
+" [1]Information [2]emit animal \n"
+" [3]Pick up your animal [4]exit ";
}
}
Ja es muss laut UML so behindert sein^^. So jetzt wollte ich ein wenig weiter gehen und einen Gui machen. Die Probleme fangen somit an und sind warscheinlich simple zu lösen. Es geht darum wie ich die Ergebnisse der einzelnen Berechnungen am besten anzeigen kann. Ich habe einen GUI- Entwurf gemacht und das einzigste was geht ist exit. Ich habe also zumindest verstanden wo die Funktionen reinkommen und wie man den GUI unterteilt nur wo ist jetzt der genaue Unterschied zur Konsole. Ich bin auf die Idee gekommen, dass man System.out.* ect. nicht mehr verwenden kann und habe versucht es umzuschreiben:
Code:
public class Animal {
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String showYourself() {
return "";
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Animal other = (Animal) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
import javax.swing.JOptionPane;
public class AnimalShelter {
Animal animal[] = new Animal[10];
@SuppressWarnings("unused")
public String addAnimal(Animal newAnimal) {
for(int i =0;i<animal.length;i++) {
if(animal[i]!= null) {
return "this place is coatet";
}else {
animal[i]= newAnimal;
newAnimal.showYourself();
return "animal has been added";
}
}
return "Something has gone wrong";
}
@SuppressWarnings("unused")
public String animalInformation() {
for(int i=0;i<animal.length;i++) {
return animal[i].toString();
}return null;
}
public String removeAnimal(int index) {
animal[index]=null;
return"Thank you for comming";
}
}
public class Cat extends Animal {
public Cat(String name, int age) {
super(name, age);
}
@Override
public String showYourself() {
return "I´m a cat";
}@Override
public String toString()
{
return "cat [name=" + super.getName() + ", age=" +super.getAge() + "]";
}
}
public class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
@Override
public String showYourself() {
return "I´m a dog";
}@Override
public String toString()
{
return "dog [name=" + super.getName() + ", age=" +super.getAge() + "]";
}
}
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
public class GUIs {
private JFrame frame;
private JTextField textField;
AnimalShelter as = new AnimalShelter();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUIs window = new GUIs();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUIs() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 523, 404);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton information = new JButton("Information");
information.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
information.setText(as.animalInformation());
}
});
JButton addAnimal = new JButton("Add animal");
addAnimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//
}
});
JButton getAnimal = new JButton("get Animal");
getAnimal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JButton exit = new JButton("exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(4);
}
});
JTextArea textArea = new JTextArea();
JLabel lblNewLabel = new JLabel("");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(39)
.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
.addGap(138))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addGap(18)
.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(exit, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(information, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(addAnimal, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(getAnimal, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(21)
.addComponent(getAnimal, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(22)
.addComponent(information, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(22)
.addComponent(addAnimal, GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)))
.addGap(26)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
.addGap(42)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addGap(27)
.addComponent(exit, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
.addGap(31))
);
frame.getContentPane().setLayout(groupLayout);
}
}
Wie implementiere ich jetzt die Ausgabe