HelloWorld521/Java

Java code

Opened this issue · 2 comments

Hey guys

I have been trying to create an inheritance class where it extends on place but does not know how to go about.

Below is my PlaceApp class

package ass;
import java.util.*;
import java.util.Scanner;
public class PlaceApp {

public static void main (String [] args) {
	
	saywelcome(); 

	int num, i;

	Scanner sc = new Scanner (System.in);
	
	 // Display the menu
    System.out.println("1\t Enter a Place");
    System.out.println("2\t Display Places");
    System.out.println("3\t Quit");

    System.out.println("Please enter your choice:");
    
    //Get user's choice
    int choice = sc.nextInt();
     
    //Display the contents of the chosen option
    switch (choice) {
    
        case 1: 
        	
        	  System.out.println("Welcome!");
        	    System.out.println("\nEnter the number of places to be added: ");
        	    num = sc.nextInt();
        	    sc.nextLine();

        	    Place add[] = new Place[num];
        	    ArrayList<Place> address = new ArrayList<>(); 
        	    
        	   
        	    for(i = 0; i<add.length; i++)
        	    {
        	    	add[i] = new Place();
        	    	
        	        System.out.println("\nEnter Place Name: ");
        	        add[i].setplacename(sc.nextLine());
        	       
        	        System.out.println("Enter Street Name: ");
        	        add[i].setplacestreetname(sc.nextLine());
        	        System.out.println("Enter City: ");
        	        add[i].setcity(sc.nextLine());
        	        System.out.println("Enter Country: ");
        	        add[i].setcountry(sc.nextLine());

        	        address.add(new Place(add[i].getname(), add[i].getstreetname(), add[i].getcity(), add[i].getcountry()));
        	        System.out.println(address.get(0));


        	        System.out.println("\nYou have successfully added a new Place!");
        	    }

        	    for (i = 0; i < add.length; i++)
        	    {
        	        System.out.println("\nAddress " + (i+1) + " Information");
        	        System.out.println("===================================");
        	        
        	        add[i].toString();
        	        System.out.println("PLACE ADDED" +"\nName:"+ add[i].getname()+"\nStreet Name:"+ add[i].getstreetname()+"\nCity: "
    			    		+ add[i].getcity()+"\nCountry: "+ add[i].getcountry());
        	      
        	    }
        	    
        	    sc.close();
        	
		
		  
   break;
        case 2:  
        	
                break;
                
        case 3: System.exit(0); 
                break;
        
        default: System.out.println("Invalid choice");
    }//end of switch
	

}
public static void saywelcome( ) {
	System.out.println("Welcome Admin");
}

}

class placeGroup{
Place places[];
int classsize;
public placeGroup() {
classsize=0;
}
public placeGroup(int capacity) {
classsize = 0;
places = new Place[capacity];
}
public void enrol(String placename) {
places[classsize++] = new Place();
}

}

class museum extends Place {
String _type;
public museum(String placename, String placestreetname, String city, String country) {
super(placename, placestreetname, city, country);
_type = "Art";
}
public museum(String placename, String placestreetname, String city, String country, String type) {
super(placename, placestreetname, city, country);
_type = type;
}
public String getType() {
return _type;
}
@OverRide
public String toString() {
return super.toString() + ", Type = " + _type;
}
}

class restaurants extends Place {
String _food;
public restaurants(String placename, String placestreetname, String city, String country) {
super(placename, placestreetname, city, country);
_food = "French";
}
public restaurants(String placename, String placestreetname, String city, String country, String food) {
super(placename, placestreetname, city, country);
_food = food;
}
public String getFood() {
return _food;
}
@OverRide
public String toString() {
return super.toString() + ", Type of Food = " + _food;
}
}

Below is my place class

package ass;
import java.text.*;

public class Place {

 String name;
 String streetname;
 private String city;
 private String country;
 private static int placescount = 0; 
 
//Default constructor
 public Place ()
 {
     name = "Null";
     streetname = "Null";
     city = "Null";
     country = "Null";
     placescount = 0;
 }
 
//Parameterized Constructor
 public Place (String placename, String placestreetname, String city, String country) {
	 name = placename;
	 streetname = placestreetname;
	 this.setcity(city);
	 this.setcountry(country); 
	 placescount++;
	 System.out.println("Place Count: " + placescount);
 }
 
 //Getters
 public String getname ()
 {
     return name;
 }
 public String getstreetname ()
 {
     return streetname;
 }
 
 
//Setters
public void setplacename (String placename)
{
   name = placename;
}
public void setplacestreetname (String placestreetname)
{
   streetname = placestreetname;
}


 public int getplacescount() {
	 return placescount;
 }
		public void setcity(String city) {
			if (city.equals("chester") || city.equals("liverpool")) {
				this.city = city;
				
		} else {
			this.city = "NIL";
		}

		}
		
		public String getcity() {
			return city;
		} 
		
		public void setcountry(String country) {
			if (country.equals("United Kingdom") ) {
				this.country = country;
				
		} else {
			this.country = "NIL";
		}

		}
		
		public String getcountry() {
			return country;
		} 
		
		//To string method
		public String toString ()
		{
			
		    return "PLACE ADDED" +"\nName:"+ getname()+"\nStreet Name:"+ getstreetname()+"\nCity: "
		    		+ getcity()+"\nCountry: "+ getcountry();
		}
}

I want to make my museum and restaurant class inherit the place class, I have been trying to display my array also but to no avail

Hello,
actually, inheritance works like this: https://www.geeksforgeeks.org/inheritance-in-java/.
As you will see the child inherits its parent's attributes via the keyword inside the constructor so you do not need to add those attributes as parameters only the unique child's attributes.
I hope that I helped you.

  1. In your museum and restaurants classes, you've used @OverRide instead of @OverRide. The correct annotation is @OverRide (case-sensitive).
  2. You have two constructors in the museum and restaurants classes. It might be beneficial to call the more specific constructor from the other, reducing redundancy in code.