Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- public class FeedingCat {
- public static final int catsCount = 4;
- public static final int plateFood = 300;
- public static final int [] catApetite = {50, 62, 44, 170};
- static public class MyWindow extends JFrame{
- public MyWindow(){
- setBounds(500,500,400,300);
- setTitle("Покорми котов!");
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setVisible(true);
- setLayout(new GridLayout(5,1));
- setCatsNames(0,0);
- setCatsNames(1,0);
- setCatsNames(2,0);
- setCatsNames(3,0);
- JButton getNames = new JButton("Кормить котов");
- add(getNames);
- getNames.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String [] catsNamesArr = new String[catsCount];
- Cat [] cat = new Cat[4];
- for (int i = 0; i<catsCount; i++){
- catsNamesArr[i] = ((JTextField)MyWindow.this.getContentPane().getComponents()[i]).getText();
- cat[i] = new Cat(catsNamesArr[i],catApetite[i]);
- setCatsNames(i,1)
- }
- System.out.println(Arrays.toString(catsNamesArr));
- Plate plate = new Plate(plateFood);
- plate.info();
- allCatsEat(cat, plate);
- System.out.println("Осталось еды: "+Plate.getFood());
- getNames.setText("Кормить котов!");
- }
- });
- }
- private String setCatsNames (int n, int k){
- if (k == 0) {
- JTextField catName = new JTextField("Введите имя " + (n + 1) + "-го кота");
- catName.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- catName.setText("");
- }
- });
- add(catName);
- return catName.getText();
- } else {
- JTextField catName = new JTextField("оно работает ");
- add(catName);
- return catName.getText();
- }
- }
- /*private void setCatsNames(int n) {
- for (int i = 0; i<catsCount; i++){
- JTextField catName = new JTextField("Введите имя "+(i+1)+" го кота");
- catName.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e){
- catName.setText("");
- }
- });
- add(catName);
- }
- }*/
- }
- public static void main(String[] args) {
- new MyWindow();
- /*Cat [] cat = new Cat[4];
- cat[0] = new Cat("Барабас", 50);
- cat[1] = new Cat("Федот", 62);
- cat[2] = new Cat("Матроскин", 44);
- cat[3] = new Cat("Мясополк", 170);
- Plate plate = new Plate(plateFood);
- plate.info();
- allCatsEat(cat, plate);
- System.out.println("Осталось еды: "+Plate.getFood());*/
- }
- private static void allCatsEat(Cat[] cat, Plate plate) {
- for (int i=0;i<4;i++){
- cat[i].info();
- plate.checkFood(cat[i].getAppetite());
- System.out.println("Кот " +cat[i].getName()+" сыт: "+cat[i].satiety(cat[i].getAppetite(), Plate.getFood()));
- cat[i].eat(plate);
- if (i<3){
- plate.info();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement