Hello there! So I've got this program over registerd animals in a zoo, the idea is that when an animal is 'created' and added in a room, it gets saved into a registry, I know my code isn't the prettiest of codes, and has it's bugs, but that's a problem for another day, as I like to move on with the program I thought it would be nice with a GUI (eclipse swing).
I've been reading, watching tutorials and stuff, but i'm stuck, what I have figured out is how to make buttons, and baisc layouts, I can't get any further.
I need to make a menu (as it is in the program) then when the user presses 'create mammal' another screen should appear, with 'text fields' (correct?) where the user should input name, speicies or w/e, this should then be saved the same way as I have it now, I think you get the basic point.
Is there anyone hwo can make an example program, with a button and user input, I just really need an example to get started!
As always, forever grateful for any contribution you can give.
The program (so far):
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Menu implements Serializable { private static List<Room> rooms = new ArrayList<Room>(); private static List<Room> Arooms = new ArrayList<Room>(); private static List<Mammal> mammals = new ArrayList<Mammal>(); private static List<Registry> registrys = new ArrayList<Registry>(); public static void main(String[] args) { Registered(); while (true) { System.out.println(""); System.out.println("Select an option from below: \n"); System.out.println("(1) Create Room"); System.out.println("(2) Show Rooms"); System.out.println("(3) Create Mammal"); System.out.println("(4) Add animal to room"); System.out.println("(5) View residents"); System.out.println("(6) Write to file"); System.out.println("(7) Exit"); Scanner input = new Scanner( System.in ); int choice = input.nextInt(); if(choice == 1){ CreateRoom2(); } else if(choice == 2){ ShowRooms(); } else if(choice == 3){ CreateMammal(); } else if (choice == 4){ CreateRegistry(); } else if (choice == 5){ System.out.println(registrys); } else if (choice == 6){ WriteToFile(); } else if (choice == 7){ break ; } } } public static void CreateRoom2() { Room aRoom = new Room(); String nBuilding; int nNumber; int nAvailable; Scanner in = new Scanner (System.in); System.out.println ("Enter Building"); nBuilding = in.nextLine(); aRoom.setBuilding(nBuilding); Scanner in2 = new Scanner (System.in); System.out.println ("Enter Number"); nNumber = in2.nextInt(); aRoom.setNumber(nNumber); Scanner in3 = new Scanner (System.in); System.out.println ("Available? \n" + "1. Yes \n" + "2. No"); nAvailable = in3.nextInt(); if (nAvailable == 1) aRoom.setAvailable(true); else aRoom.setAvailable(false); Scanner in4 = new Scanner (System.in); System.out.println ("Save or Delete? \n" + "1. Save \n" + "2. Delete"); if (in4.nextInt() == 1) rooms.add(aRoom); else ; Scanner in5 = new Scanner (System.in); System.out.println ("Add new room? \n" + "1. Yes \n" + "2. No"); if (in5.nextInt() == 1) CreateRoom2(); else; } public static void CreateRegistry() { Registry aRegistry = new Registry(); String nBuilding; int nNumber; String nName; String nHabitat; String nSpecies = null; String nDangerous = null; int index; int index2; int index3=0; int index4=0; Scanner in = new Scanner (System.in); System.out.println ("Choose Room"); for (Room s : rooms) if (s.isAvailable(true)){ System.out.println(String.valueOf(index3++)+": "+s); Arooms.add(s); } index = in.nextInt(); nBuilding = Arooms.get(index).getBuilding(); nNumber = Arooms.get(index).getNumber(); aRegistry.setBuilding(nBuilding); aRegistry.setNumber(nNumber); Scanner in2 = new Scanner (System.in); System.out.println ("Choose Animal"); for(Mammal s3 : mammals){ System.out.println(String.valueOf(index4++)+": "+s3); } index2 = in.nextInt(); nName = mammals.get(index2).getName(); nHabitat = mammals.get(index2).getHabitat(); nSpecies = mammals.get(index2).getSpecies(); nDangerous = mammals.get(index2).getDangerous(); aRegistry.setName(nName); aRegistry.setHabitat(nHabitat); aRegistry.setSpecies(nSpecies); aRegistry.setDangerous(nDangerous); registrys.add(aRegistry); System.out.println(mammals.get(index2).getName()+" is now in Room: "+aRegistry.getBuilding()+aRegistry.getNumber()); } public static void CreateMammal() { Mammal aMammal = new Mammal(); String nName; String nHabitat; String nSpecies = "Mammal"; String nDangerous; int index; Scanner in = new Scanner (System.in); System.out.println ("Enter Name"); nName = in.nextLine(); aMammal.setName(nName); Scanner in2 = new Scanner (System.in); System.out.println ("Enter Habitat"); nHabitat = in2.nextLine(); aMammal.setHabitat(nHabitat); Scanner in3 = new Scanner (System.in); System.out.println ("Is this animal Dangerous?"); nDangerous = in3.nextLine(); aMammal.setDangerous(nDangerous); aMammal.setSpecies(nSpecies); mammals.add(aMammal); System.out.println(mammals); } public static void Registered() { for ( int i = 0 ; i < 4 ; i++ ) { rooms.add( new Room() ); } rooms.get( 0 ).setBuilding( "A" ); rooms.get( 0 ).setNumber( 1 ); rooms.get( 0 ).setAvailable( false ); rooms.get( 1 ).setBuilding( "A" ); rooms.get( 1 ).setNumber( 2 ); rooms.get( 1 ).setAvailable( false ); rooms.get( 2 ).setBuilding( "B" ); rooms.get( 2 ).setNumber( 1 ); rooms.get( 2 ).setAvailable( true ); rooms.get( 3 ).setBuilding( "B" ); rooms.get( 3 ).setNumber( 2 ); rooms.get( 3 ).setAvailable( true ); for ( int i = 0 ; i < 4 ; i++ ) { mammals.add( new Mammal() ); } mammals.get( 0 ).setName( "Astro Boy" ); mammals.get( 0 ).setHabitat("Domesticated"); mammals.get( 0 ).setSpecies("Horse"); mammals.get( 0 ).setDangerous("yes"); mammals.get( 1 ).setName( "Chop" ); mammals.get( 1 ).setHabitat("Domesticated"); mammals.get( 1 ).setSpecies("Dog"); mammals.get( 1 ).setDangerous("No"); mammals.get( 2 ).setName( "Jumbo" ); mammals.get( 2 ).setHabitat("savannahs, forests, deserts and marshes"); mammals.get( 2 ).setSpecies("Elephant"); mammals.get( 2 ).setDangerous("yes"); mammals.get( 3 ).setName( "Bai Yun" ); mammals.get( 3 ).setHabitat("a cool, moist climate."); mammals.get( 3 ).setSpecies("Panda"); mammals.get( 3 ).setDangerous("yes"); } public static void WriteToFile() { try { System.out.println("Writing to file \n" + " Serializing...\n" + "Done!"); FileOutputStream fout = new FileOutputStream("registrys.ser"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(registrys); oos.close(); } catch (Exception e) { e.printStackTrace(); } } public static void ShowRooms() { System.out.println("All Rooms: \n"); for (Room s : rooms) System.out.println(s); System.out.println("Available Rooms: \n"); for (Room s : rooms) if (s.isAvailable(true)){ System.out.println(s); } } }
and ofc there are other classes, but I didnt think it was necessary to post them