Jump to content

How would I add objects to an array that come from 3 subclasses?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
So I have been tasked with creating a program that is essentially a planner for a person. It has to have the abilities to add events of 3 types(Party, Dance Class,Club Meeting). It then has to be able to display everything enter, and be able to display the things that occur on the same date, date from the user. Finally it has to check if you have 2 events that occur on the same day, and then check the time against it. So for right now I have a decent amount of base code but I'm having trouble with the adding events part, and my gut tells me that I need to switch my array from the driver program to my SocialEvent Class. So my question is how would I go about adding objects to an array that come from 3 subclasses, and then be able to access that information in the superclass so that it can be checked. Heres what I have so far.

Java Syntax (Toggle Plain Text)
import java.util.Scanner;
 
 
 
public class TestSocialEvent {
    /**
     * @author Alexander Chamerlaion
     * @Version 1.0
     * @Date 10/12/11
     * This program creates and tests Social Events
     */
 
    static String title, attire, dayweek,input,time;
    static int day,month,year;
    public static void main(String[] args) {
 
        Scanner k = new Scanner(System.in);
 
        System.out.println("Social Planner");
 
        SocialEvent ev = new SocialEvent(attire, dayweek, title, day, month, year,time);
 
        while(true){
            System.out.println("Please enter the type of party you are attending \n(Party, Club Meeting, Dance Class, Display,Quit)");
            String input = k.nextLine();
            input.toLowerCase();
            if(input.equals("party")){
                System.out.println("Choosen a Party");
                Party party = new Party(title, attire, time, year,day,month,dayweek);
                System.out.println("Please enter a description of your event: ");
                title=k.nextLine();
                System.out.println("Please Enter an attire: ");
                attire = k.nextLine();
                System.out.println("Please Enter an time: ");
                time=k.nextLine();
                System.out.println("Please enter the date input the day first: ");
                day=k.nextInt();
                System.out.println("Month: ");
                month=k.nextInt();
                System.out.println("Year: ");
                year=k.nextInt();
                System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");
                dayweek=k.nextLine();
                party.setPartyTime(time);
                party.setPartyTitle(title);
                party.setPartyAttire(attire);
                party.setPartyMonth(month);
                party.setPartyYear(year);
                party.setPartyDay(day);
                ev.addParty(attire, dayweek, title, day, month, year,time);
 
            }
            else if(input.equals("club meeting")){
                System.out.println("Choosen a Club Meeting");
 
                System.out.println("Please enter a description of your event: ");
                title=k.nextLine();
                System.out.println("Please Enter an attire: ");
                attire = k.nextLine();
                System.out.println("Please Enter an time (hhmm): ");
                time=k.nextLine();
                System.out.println("Please enter the date input the day first: ");
                day=k.nextInt();
                System.out.println("Month: ");
                month=k.nextInt();
                System.out.println("Year: ");
                year=k.nextInt();
                System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");
                dayweek=k.nextLine();
                ClubMeeting club = new ClubMeeting(title, attire, time, year,day,month,dayweek);
                club.setClubTitle(title);
                club.setClubAttire(attire);
                club.setClubTime(time);
                club.setClubDay(day);
                club.setClubMonth(month);
                club.setClubYear(year);
                club.setClubDayweek(dayweek);
                ev.addClubMeeting(attire, dayweek, title, day, month, year,time);
 
            }
            else if (input.equals("dance class")){
                System.out.println("Choosen a Dance Class");
                System.out.println("Please enter a description of your event: ");
                title=k.nextLine();
                System.out.println("Please Enter an attire: ");
                attire = k.nextLine();
                System.out.println("Please Enter an time: ");
                time=k.nextLine();
                System.out.println("Please enter the date input the day first: ");
                day=k.nextInt();
                System.out.println("Month: ");
                month=k.nextInt();
                System.out.println("Year: ");
                year=k.nextInt();
                System.out.println("Please enter the day it occurs on: ");
                dayweek=k.next();
                DanceClass dance = new DanceClass(attire, title, time, day, month, year, dayweek);
                dance.setDanceTitle(title);
                dance.setDanceAttire(attire);
                dance.setDanceTime(time);
                dance.setDanceDay(day);
                dance.setDanceMonth(month);
                dance.setDanceYear(year);
                dance.setDanceDayweek(dayweek);
                ev.addDanceClass(attire, dayweek, title, day, month, year,time);
                ev.display();
            }
            else if(input.equals("display")){
 
 
                System.out.println("Choosen to Display Social Planner");
                ev.display();
            }
            else if(input.equals("quit"));
            break;
        }    
 
 
 
 
    }
}
Java Syntax (Toggle Plain Text)
public class SocialEvent {
    private String title;
    private String attire;
    private String time;
    private int day;
    private int month;
    private int year;
    private String dayweek;
    private int count=0;
    private SocialEvent[] events = new SocialEvent[100];
 
    public SocialEvent(String attire,String dayweek, String title,int day,int month,int year, String time){
        this.attire = attire;
        this.dayweek = dayweek;
        this.title = title;
        this.day = day;
        this.month = month;
        this.year = year;
        this.time = time;
    }
    public void addParty(String attire,String dayweek, String title,int day,int month,int year, String time) {
        if (count < events.length) {
            events[count] = new Party(attire, dayweek, title, day, month, year, time);
            count++;
        }
    }
    public void addDanceClass(String attire,String dayweek, String title,int day,int month,int year, String time) {
        if (count < events.length) {
            events[count] = new DanceClass(attire, dayweek, title, day, month, year, time);
            count++;
        }
    }
    public void addClubMeeting(String attire,String dayweek, String title,int day,int month,int year, String time) {
        if (count < events.length) {
            events[count] = new ClubMeeting(attire, dayweek, title, day, month, year, time);
            count++;
        }
    }
    public void display(){
        for(int i = 0; i<count; i++){                         
            System.out.println(events[i].toString());
        }
    }
 
 
    }
Java Syntax (Toggle Plain Text)
public class Party extends SocialEvent {
    private String partyAttire,partyTitle,partyDayweek,partyTime;
    private int partyDay,partyMonth,partyYear;
 
    public Party(String attire,String dayweek, String title,int day,int month,int year, String time){
        super(attire, dayweek, title, day, month, year,time);
        this.partyAttire=attire;
        this.partyDayweek=dayweek;
        this.partyTime=time;
        this.partyTitle=title;
        this.partyDay=day;
        this.partyMonth=month;
        this.partyYear=year;
    }
 
    public void setPartyTime(String partyTime){
        if(partyTime.length()>2||partyTime.length()<3){
            this.partyTime=partyTime.substring(0,1)+":"+partyTime.substring(1,2);
        }
        else if(partyTime.length()>3){
            this.partyTime=partyTime.substring(1,2)+":"+partyTime.substring(2,3);
        }
        else{
            System.out.println("Invalid Time ");
        }
 
    }
 
    public String getPartyAttire() {
        return partyAttire;
    }
 
    public void setPartyAttire(String partyAttire) {
        this.partyAttire = partyAttire;
    }
 
    public String getPartyTitle() {
        return partyTitle;
    }
 
    public void setPartyTitle(String partyTitle) {
        this.partyTitle = partyTitle;
    }
 
    public String getPartyDayweek() {
        return partyDayweek;
    }
 
    public void setPartyDayweek(String partyDayweek) {
        this.partyDayweek = partyDayweek;
    }
 
    public int getPartyDay() {
        return partyDay;
    }
 
    public void setPartyDay(int partyDay) {
        if(partyDay>31){
            System.out.println("You've Entered An Invalid Day ");
        }
        else{
            this.partyDay=partyDay;
        }
    }
 
    public int getPartyMonth() {
        return partyMonth;
    }
 
    public void setPartyMonth(int partyMonth) {
        if(partyMonth>12){
            System.out.println("You've Entered An Invalid Day ");
        }
        else{
            this.partyMonth=partyMonth;
        }
    }
 
    public int getPartyYear() {
        return partyYear;
    }
 
    public void setPartyYear(int partyYear) {
        this.partyYear = partyYear;
    }
    public String toString(){
        return "Title: "+partyTitle+"\nAttire: "+partyAttire+"\nDate: "+partyMonth+"/"+partyDay+"/"+partyYear+"\nDay of the Week: "+
                partyDayweek+"\nTime: "+partyTime;
    }
 
}
Java Syntax (Toggle Plain Text)
public class DanceClass extends SocialEvent {
 
    private String danceAttire,danceTitle,danceDayweek,danceTime;
    private int danceDay,danceMonth,danceYear;
 
    public DanceClass(String attire,String dayweek, String title,int day,int month,int year, String time){
        super(attire, dayweek, title,day,month,year, time);
        this.danceAttire=attire;
        this.danceTitle=title;
        this.danceDayweek=dayweek;
        this.danceTime=time;
        this.danceDay=day;
        this.danceMonth=month;
        this.danceYear=year;
 
 
    }
 
    public String getDanceAttire() {
        return danceAttire;
    }
 
    public void setDanceAttire(String danceAttire) {
        this.danceAttire = danceAttire;
    }
 
    public String getDanceTitle() {
        return danceTitle;
    }
 
    public void setDanceTitle(String danceTitle) {
        this.danceTitle = danceTitle;
    }
 
    public String getDanceDayweek() {
        return danceDayweek;
    }
 
    public void setDanceDayweek(String danceDayweek) {
        this.danceDayweek = danceDayweek;
    }
 
    public String getDanceTime() {
        return danceTime;
    }
 
    public void setDanceTime(String danceTime) {
        if(danceTime.length()>2||danceTime.length()<3){
            this.danceTime=danceTime.substring(0,1)+":"+danceTime.substring(1,2);
        }
        else if(danceTime.length()>3){
            this.danceTime=danceTime.substring(1,2)+":"+danceTime.substring(2,3);
        }
        else{
            System.out.println("Invalid Time ");
        }
 
    }
 
 
    public int getDanceDay() {
        return danceDay;
    }
 
    public void setDanceDay(int danceDay) {
        if(danceDay>31){
            System.out.println("You've Entered An Invalid Day ");
        }
        else{
            this.danceDay=danceDay;
        }
    }
 
    public int getDanceMonth() {
        return danceMonth;
    }
 
    public void setDanceMonth(int danceMonth) {
        if(danceMonth>12){
            System.out.println("You've Entered An Invalid Month ");
        }
        else{
            this.danceDay=danceDay;
        }
    }
    public int getDanceYear() {
        return danceYear;
    }
 
    public void setDanceYear(int danceYear) {
        this.danceYear = danceYear;
    }
    public String toString(){
        return "Title: "+danceTitle+"\nAttire: "+danceAttire+"\nDate: "+danceMonth+"/"+danceDay+"/"+danceYear+"\nDay of the Week: "+
                danceDayweek+"\nTime: "+danceTime;
}
}
Java Syntax (Toggle Plain Text)
public class ClubMeeting extends SocialEvent {
 
    private String clubAttire,clubTitle,clubDayweek,clubTime;
    private int clubDay,clubMonth,clubYear;
 
    public ClubMeeting(String attire,String dayweek, String title,int day,int month,int year, String time) {
        super(attire,dayweek,title,day,month,year,time);
        this.clubAttire=attire;
        this.clubTitle=title;
        this.clubDay=day;
        this.clubMonth=month;
        this.clubYear=year;
        this.clubTime=time;
        this.clubDayweek=dayweek;
 
    }
 
    public String getClubAttire() {
        return clubAttire;
    }
 
    public void setClubAttire(String clubAttire) {
        this.clubAttire = clubAttire;
    }
 
    public String getClubTitle() {
        return clubTitle;
    }
 
    public void setClubTitle(String clubTitle) {
        this.clubTitle = clubTitle;
    }
 
    public String getClubDayweek() {
        return clubDayweek;
    }
 
    public void setClubDayweek(String clubDayweek) {
        this.clubDayweek = clubDayweek;
    }
 
    public String getClubTime() {
        return clubTime;
    }
 
    public void setClubTime(String clubTime) {
        if(clubTime.length()>2||clubTime.length()<3){
            this.clubTime=clubTime.substring(0,1)+":"+clubTime.substring(1,2);
        }
        else if(clubTime.length()>3){
            this.clubTime=clubTime.substring(1,2)+":"+clubTime.substring(2,3);
        }
        else{
            System.out.println("Invalid Time ");
        }
 
    }
 
    public void setClubDay(int clubDay) {
        if(clubDay>31){
            System.out.println("You've Entered An Invalid Day ");
        }
        else{
            this.clubDay=clubDay;
        }
    }
 
    public int getClubDay() {
        return clubDay;
    }
 
    public int getClubMonth() {
        return clubMonth;
    }
 
    public void setClubMonth(int clubMonth) {
        if(clubMonth>12){
            System.out.println("You've Entered An Invalid Month ");
        }
        else{
            this.clubMonth=clubMonth;
        }
    }
 
    public int getClubYear() {
        return clubYear;
    }
 
    public void setClubYear(int clubYear) {
        this.clubYear = clubYear;
    }
    public String toString(){
        return "Title: "+clubTitle+"\nAttire: "+clubAttire+"\nDate: "+clubMonth+"/"+clubDay+"/"+clubYear+"\nDay of the Week: "+
                clubDayweek+"\nTime: "+clubTime;
}
}

Edited by WingedPanther, 15 October 2011 - 02:25 PM.
add code tags (the # button)


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Laxman2809 said:

So my question is how would I go about adding objects to an array that come from 3 subclasses, and then be able to access that information in the superclass so that it can be checked.
I'm not sure what you mean by this. Can you give a short example?

Laxman2809 said:

my gut tells me that I need to switch my array from the driver program to my SocialEvent Class
Is your array not already in your SocialEvent class?

I'd create a Planner class that holds a schedule for each date.
I'd then create a Schedule class that holds a list of SocialEvents, where each social event can be a Party, DanceClass, ClubMeeting, etc. The Schedule object will hold SocialEvent objects that occur on a certain date.

class Planner { 

    private ArrayList<Schedule> listOfSchedules...


    public void addSchedule(Schedule schedule){...}

    public ArrayList<Schedule> getScheduleList() { return listOfSchedules; }

}


class Schedule {

    private ArrayList<SocialEvent> listOfEvents ...

    private Date date;


    public Schedule(Date date){ ... }

    public Date getDate(){ ... }

    void addEvent(SocialEvent se) { ... }

    void removeEvent(SocialEvent se) { ... }

    SocialEvent getEvent(int index) { ... }

    public ArrayList<SocialEvent) getSocialEventList() {...}

    other methods continued

}


public class SocialEvent{ ... }

public class Party extends SocialEvent{ ... }

public class ClubMeeting extends SocialEvent{ ... }

public class DanceClass extends SocialEven{ ... }


public class simpleTest {

    void main() {

        Planner planner = new Planner();

        while(if more events wanted) {

           Date date = get date for event

           otherInput = get other input from user

           if( date does NOT exist in planner yet){ // then create a new schedule for this date and add it to the planner

                Schedule schedule = new Schedule(date);

                schedule.addEvent( new [SocialEvent/DanceClass/etc](otherInput) );

                planner.add( schedule);

           } else { // if the date DOES already exist in the planner then add the event 

                schedule.addEvent( new [SocialEvent/DanceClass/etc](otherInput) );

        }

    }

}


This will make it easier to find conflicts of events for each day.
Also, printing data for each event is simple.

for( Schedule s : planner.getScheduleList ) {

     for( SocialEvent se : s.getSocialEventList() ) {

         System.out.println(se);

      }

}

// checking for collisions is as simple as checking to see if a schedule has more than 1 SocialEvent attached to it.

for( Schedule s : planner.getScheduleList() ) {

    if(s.getListOfEvents().size() > 1) // there is more than 1 event for this Date

         // fix conflict, etc.

    else // there is no conflict

}


I hope this sounds right. It's a little late in my neck of the woods.

#3
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
Ok so i sorta took your idea, but did it my way, unfortunately I didn't quite understand it, but I think that I should be able to complete this program through this way. Anyways I have now created a SocialEventController class. Through this class I am hoping that I gave myself the ability to both display and check if it occur on that date. Now my trouble is i don't know how to get the data from the different class in order for me to check it.

public class SocialEventController {

	private int count = 0;

	private SocialEvent[] events = new SocialEvent[10]; 


	public void addParty(String attire,String dayweek, String title,int day,int month,int year, String time) {

		if (count < events.length) {

			events[count] = new Party(attire, dayweek, title, day, month, year, time);

			count++;

		}

	}


	public void addDanceClass(String attire,String dayweek, String title,int day,int month,int year, String time) {

		if (count < events.length) {

			events[count] = new DanceClass(attire, dayweek, title, day, month, year, time);

			count++;

		}

	}

	public void addClubMeeting(String attire,String dayweek, String title,int day,int month,int year, String time) {

		if (count < events.length) {

			events[count] = new ClubMeeting(attire, dayweek, title, day, month, year, time);

			count++;

		}

	}

	public void occursOn(String attire,String dayweek, String title,int day,int month,int year, String time){

		if

		//dont know how to call the date from the different objects

		else



	}

}




---------- Post added at 08:34 AM ---------- Previous post was at 08:24 AM ----------

	public void display(){

		for(int i = 0; i<count; i++){                         

			System.out.println(events[i].toString());

		}

	}

}

Is now in the controller class as well

---------- Post added at 09:29 AM ---------- Previous post was at 08:34 AM ----------

Ok so I got tired of trying to make this work, and I have started to rework from the original. I think i am now using an object array, and because the different events are extensions of social event i can add them through
				sel.add(p);

But now I am once again stuck at displaying all of the different events, and then I am completely lost on where to begin on a way to check for the occurs on.
import java.util.Scanner;

public class Test {


	/**

	 * @param args

	 */

	static String title, attire, dayweek,input,time;

	static int day,month,year;

	public static void main(String[] args) {

		Scanner k = new Scanner(System.in);

		SocialEventList sel = new SocialEventList();

		SocialEvent se = new SocialEvent(attire, dayweek, title, day, month, year,time);

		Party p = new Party(attire, dayweek, title, day, month, year,time);

		DanceClass d = new DanceClass(attire, title, time, day, month, year, dayweek);


		while(true){

			System.out.println("Please enter the type of party you are attending \n(Party, Club Meeting, Dance Class, Display,Quit)");

			String input = k.nextLine();

			input.toLowerCase();

			if(input.equals("party")){

				System.out.println("Please enter a description of your event: ");

				title=k.nextLine();

				System.out.println("Please Enter an attire: ");

				attire = k.nextLine();

				System.out.println("Please Enter an time: ");

				time=k.nextLine();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.nextLine();

				p.setPartyTime(time);

				p.setPartyTitle(title);

				p.setPartyAttire(attire);

				p.setPartyMonth(month);

				p.setPartyYear(year);

				p.setPartyDay(day);

				sel.add(p);

				sel.display();

			}

		else if (input.equals("dance class")){

			System.out.println("Choosen a Dance Class");

			System.out.println("Please enter a description of your event: ");

			title=k.nextLine();

			System.out.println("Please Enter an attire: ");

			attire = k.nextLine();

			System.out.println("Please Enter an time: ");

			time=k.nextLine();

			System.out.println("Please enter the date input the day first: ");

			day=k.nextInt();

			System.out.println("Month: ");

			month=k.nextInt();

			System.out.println("Year: ");

			year=k.nextInt();

			System.out.println("Please enter the day it occurs on: ");

			dayweek=k.next();

			d.setDanceTitle(title);

			d.setDanceAttire(attire);

			d.setDanceTime(time);

			d.setDanceDay(day);

			d.setDanceMonth(month);

			d.setDanceYear(year);

			d.setDanceDayweek(dayweek);

			sel.add(d);

		}

	}	

	

}

}

public class Party extends SocialEvent {

	private String partyAttire,partyTitle,partyDayweek,partyTime;

	private int partyDay,partyMonth,partyYear;


	public Party(String attire,String dayweek, String title,int day,int month,int year, String time){

		super(attire, dayweek, title, day, month, year,time);

		this.partyAttire=attire;

		this.partyDayweek=dayweek;

		this.partyTime=time;

		this.partyTitle=title;

		this.partyDay=day;

		this.partyMonth=month;

		this.partyYear=year;

	}


	public void setPartyTime(String partyTime){

		if(partyTime.length()>2||partyTime.length()<3){

			this.partyTime=partyTime.substring(0,1)+":"+partyTime.substring(1,2);

		}

		else if(partyTime.length()>3){

			this.partyTime=partyTime.substring(1,2)+":"+partyTime.substring(2,3);

		}

		else{

			System.out.println("Invalid Time ");

		}


	}


	public String getPartyAttire() {

		return partyAttire;

	}


	public void setPartyAttire(String partyAttire) {

		this.partyAttire = partyAttire;

	}


	public String getPartyTitle() {

		return partyTitle;

	}


	public void setPartyTitle(String partyTitle) {

		this.partyTitle = partyTitle;

	}


	public String getPartyDayweek() {

		return partyDayweek;

	}


	public void setPartyDayweek(String partyDayweek) {

		this.partyDayweek = partyDayweek;

	}


	public int getPartyDay() {

		return partyDay;

	}


	public void setPartyDay(int partyDay) {

		//if(partyDay>31){

			//System.out.println("You've Entered An Invalid Day ");

		//}

		//else{

			this.partyDay=partyDay;

		//}

	}


	public int getPartyMonth() {

		return partyMonth;

	}


	public void setPartyMonth(int partyMonth) {

		if(partyMonth>12){

			System.out.println("You've Entered An Invalid Day ");

		}

		else{

			this.partyMonth=partyMonth;

		}

	}


	public int getPartyYear() {

		return partyYear;

	}


	public void setPartyYear(int partyYear) {

		this.partyYear = partyYear;

	}

	public String toString(){

		return "Title: "+partyTitle+"\nAttire: "+partyAttire+"\nDate: "+partyMonth+"/"+partyDay+"/"+partyYear+"\nDay of the Week: "+

				partyDayweek+"\nTime: "+partyTime;

	}


}



public class SocialEventList {

	private SocialEvent[] thelist = new SocialEvent[5];

	private int i = 0;

	

	

	public void add(SocialEvent se){

		if(i<thelist.length){

			thelist[i]=se;

			++i;

			System.out.println(thelist[i]);

		}

	}

	public void display(){

		//for(int i = 0; i<thelist.length; i++){                         

			//System.out.println(thelist[i].toString());

		//}

	}

public void occursOn(){

		

	}

}



#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Laxman2809 said:


	public void occursOn(String attire,String dayweek, String title,int day,int month,int year, String time){

		if

		//dont know how to call the date from the different objects

		else



	}

How do you know what 2 events you're comparing in this method?
If I were using your code I might try writing something similar to:

void occursOn(SocialEvent event1, SocialEvent event2) {

      if( event1.getTime() == event2.getTime() ) //where getTime() perhaps converts your month/day/year to comparable value

          println(collision)

      else

          println(no collision)

}


Laxman2809 said:

Now my trouble is i don't know how to get the data from the different class in order for me to check it.
In your SocialEvent class you should have/create accessor methods which enable access to information from the objects.

I'm thinking the problem with your strategy of finding collisions is you're going to need to loop through each event and check it against all other events.
So you'll need to check event A against event B,C,D,E,...,Z.
Then you'll need to check event B against C,D,E,...,Z
Then C against D,E,...,Z
This results in Z(Z+1)/2 comparisons(I think) which correlates with Big-OH of (N^2).

---------- Post added at 12:44 PM ---------- Previous post was at 12:34 PM ----------

Your post disappeared!

#5
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
Ok so I got tired of trying to make this work, and I have started to rework from the original. I think i am now using an object array, and because the different events are extensions of social event i can add them through
				sel.add(p);

But now I am once again stuck at displaying all of the different events, and then I am completely lost on where to begin on a way to check for the occurs on.Also on a side note for some reason a scanner never starts in the program here
 System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.nextLine();
import java.util.Scanner;

[CODE]import java.util.Scanner;

public class Test {


	/**

	 * @param args

	 */

	static String title, attire, dayweek,input,time;

	static int day,month,year;

	public static void main(String[] args) {

		Scanner k = new Scanner(System.in);

		SocialEventList sel = new SocialEventList();

		SocialEvent se = new SocialEvent(attire, dayweek, title, day, month, year,time);

		Party p = new Party(attire, dayweek, title, day, month, year,time);

		DanceClass d = new DanceClass(attire, title, time, day, month, year, dayweek);


		while(true){

			System.out.println("Please enter the type of party you are attending \n(Party, Club Meeting, Dance Class, Display,Quit)");

			String input = k.nextLine();

			input.toLowerCase();

			if(input.equals("party")){

				System.out.println("Please enter a description of your event: ");

				title=k.nextLine();

				System.out.println("Please Enter an attire: ");

				attire = k.nextLine();

				System.out.println("Please Enter an time: ");

				time=k.nextLine();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.nextLine();

				p.setPartyTime(time);

				p.setPartyTitle(title);

				p.setPartyAttire(attire);

				p.setPartyMonth(month);

				p.setPartyYear(year);

				p.setPartyDay(day);

				sel.add(p);

				sel.display();

			}

		else if (input.equals("dance class")){

			System.out.println("Choosen a Dance Class");

			System.out.println("Please enter a description of your event: ");

			title=k.nextLine();

			System.out.println("Please Enter an attire: ");

			attire = k.nextLine();

			System.out.println("Please Enter an time: ");

			time=k.nextLine();

			System.out.println("Please enter the date input the day first: ");

			day=k.nextInt();

			System.out.println("Month: ");

			month=k.nextInt();

			System.out.println("Year: ");

			year=k.nextInt();

			System.out.println("Please enter the day it occurs on: ");

			dayweek=k.next();

			d.setDanceTitle(title);

			d.setDanceAttire(attire);

			d.setDanceTime(time);

			d.setDanceDay(day);

			d.setDanceMonth(month);

			d.setDanceYear(year);

			d.setDanceDayweek(dayweek);

			sel.add(d);

		}

	}	

	

}

}

public class Party extends SocialEvent {

	private String partyAttire,partyTitle,partyDayweek,partyTime;

	private int partyDay,partyMonth,partyYear;


	public Party(String attire,String dayweek, String title,int day,int month,int year, String time){

		super(attire, dayweek, title, day, month, year,time);

		this.partyAttire=attire;

		this.partyDayweek=dayweek;

		this.partyTime=time;

		this.partyTitle=title;

		this.partyDay=day;

		this.partyMonth=month;

		this.partyYear=year;

	}


	public void setPartyTime(String partyTime){

		if(partyTime.length()>2||partyTime.length()<3){

			this.partyTime=partyTime.substring(0,1)+":"+partyTime.substring(1,2);

		}

		else if(partyTime.length()>3){

			this.partyTime=partyTime.substring(1,2)+":"+partyTime.substring(2,3);

		}

		else{

			System.out.println("Invalid Time ");

		}


	}


	public String getPartyAttire() {

		return partyAttire;

	}


	public void setPartyAttire(String partyAttire) {

		this.partyAttire = partyAttire;

	}


	public String getPartyTitle() {

		return partyTitle;

	}


	public void setPartyTitle(String partyTitle) {

		this.partyTitle = partyTitle;

	}


	public String getPartyDayweek() {

		return partyDayweek;

	}


	public void setPartyDayweek(String partyDayweek) {

		this.partyDayweek = partyDayweek;

	}


	public int getPartyDay() {

		return partyDay;

	}


	public void setPartyDay(int partyDay) {

		//if(partyDay>31){

			//System.out.println("You've Entered An Invalid Day ");

		//}

		//else{

			this.partyDay=partyDay;

		//}

	}


	public int getPartyMonth() {

		return partyMonth;

	}


	public void setPartyMonth(int partyMonth) {

		if(partyMonth>12){

			System.out.println("You've Entered An Invalid Day ");

		}

		else{

			this.partyMonth=partyMonth;

		}

	}


	public int getPartyYear() {

		return partyYear;

	}


	public void setPartyYear(int partyYear) {

		this.partyYear = partyYear;

	}

	public String toString(){

		return "Title: "+partyTitle+"\nAttire: "+partyAttire+"\nDate: "+partyMonth+"/"+partyDay+"/"+partyYear+"\nDay of the Week: "+

				partyDayweek+"\nTime: "+partyTime;

	}


}



public class SocialEventList {

	private SocialEvent[] thelist = new SocialEvent[5];

	private int i = 0;

	

	

	public void add(SocialEvent se){

		if(i<thelist.length){

			thelist[i]=se;

			++i;

			System.out.println(thelist[i]);

		}

	}

	public void display(){

		//for(int i = 0; i<thelist.length; i++){                         

			//System.out.println(thelist[i].toString());

		//}

	}

public void occursOn(){

		

	}

}



#6
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
Ok so i actually ended up rewriting most of the project and have gotten some great results. I am now working on your suggestion for the occurs on. The issue I have ran into is not being create the getTime method, how do you suggest to write that.
import java.util.Scanner;

public class Test {


	/**

	 * @param args

	 */

	static String title, attire, dayweek,input,time;

	static int day,month,year;

	public static void main(String[] args) {

		Scanner k = new Scanner(System.in);

		SocialEventList sel = new SocialEventList();

		SocialEvent se = new SocialEvent(attire, dayweek, title, day, month, year,time);

		Party p = new Party(attire, dayweek, title, day, month, year,time);

		DanceClass d = new DanceClass(attire, title, time, day, month, year, dayweek);


		while(true){

			System.out.println("Please enter the type of party you are attending \n(Party, Club Meeting, Dance Class, Display,Quit)");

			String input = k.nextLine();

			input.toLowerCase();

			if(input.equals("party")){

				System.out.println("Choosen a Party");

				System.out.println("Please enter a description of your event: ");

				title=k.nextLine();

				System.out.println("Please Enter an attire: ");

				attire = k.nextLine();

				System.out.println("Please Enter an time: ");

				time=k.nextLine();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.next();

				p.setPartyTime(time);

				p.setPartyTitle(title);

				p.setPartyAttire(attire);

				p.setPartyMonth(month);

				p.setPartyYear(year);

				p.setPartyDay(day);

				p.setPartyDayweek(dayweek);

				sel.add(p);

				sel.display();

			}

			else if (input.equals("dance class")){

				System.out.println("Choosen a Dance Class");

				System.out.println("Please enter a description of your event: ");

				title=k.nextLine();

				System.out.println("Please Enter an attire: ");

				attire = k.nextLine();

				System.out.println("Please Enter an time: ");

				time=k.nextLine();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on: ");

				dayweek=k.next();

				d.setDanceTitle(title);

				d.setDanceAttire(attire);

				d.setDanceTime(time);

				d.setDanceDay(day);

				d.setDanceMonth(month);

				d.setDanceYear(year);

				d.setDanceDayweek(dayweek);

				sel.add(d);

				sel.display();

			}

			else if(input.equals("Display")){

				sel.display();

			}

			else if(input.equals("quit")){

				break;

			}

		}	

	}

}

public class SocialEvent {

	private String title;

	private String attire;

	private String time;

	private int day;

	private int month;

	private int year;

	private String dayweek;

	private int count=0;


	public SocialEvent(String attire,String dayweek, String title,int day,int month,int year, String time){

		this.attire = attire;

		this.dayweek = dayweek;

		this.title = title;

		this.day = day;

		this.month = month;

		this.year = year;

		this.time = time;

	}

	public Object getTime() {

		return null;

	}



	public String toString(int i) {

		return "Title: "+title+"\nAttire: "+attire+"\nDate: "+month+"/"+day+"/"+year+"\nDay of the Week: "+

				dayweek+"\nTime: "+time;

	}


}



public class DanceClass extends SocialEvent {


	private String danceAttire,danceTitle,danceDayweek,danceTime;

	private int danceDay,danceMonth,danceYear;


	public DanceClass(String attire,String dayweek, String title,int day,int month,int year, String time){

		super(attire, dayweek, title,day,month,year, time);

		this.danceAttire=attire;

		this.danceTitle=title;

		this.danceDayweek=dayweek;

		this.danceTime=time;

		this.danceDay=day;

		this.danceMonth=month;

		this.danceYear=year;



	}


	public String getDanceAttire() {

		return danceAttire;

	}


	public void setDanceAttire(String danceAttire) {

		this.danceAttire = danceAttire;

	}


	public String getDanceTitle() {

		return danceTitle;

	}


	public void setDanceTitle(String danceTitle) {

		this.danceTitle = danceTitle;

	}


	public String getDanceDayweek() {

		return danceDayweek;

	}


	public void setDanceDayweek(String danceDayweek) {

		this.danceDayweek = danceDayweek;

	}


	public String getDanceTime() {

		return danceTime;

	}


	public void setDanceTime(String danceTime) {

		

			this.danceTime=danceTime;

	}



	public int getDanceDay() {

		return danceDay;

	}


	public void setDanceDay(int danceDay) {

		if(danceDay>31){

			System.out.println("You've Entered An Invalid Day ");

		}

		else{

			this.danceDay=danceDay;

		}

	}


	public int getDanceMonth() {

		return danceMonth;

	}


	public void setDanceMonth(int danceMonth) {

			this.danceDay=danceDay;

	}

	public int getDanceYear() {

		return danceYear;

	}


	public void setDanceYear(int danceYear) {

		this.danceYear = danceYear;

	}

	public String toString(int i){

		return "Dance Class"+ "\nTitle: "+danceTitle+"\nAttire: "+danceAttire+"\nDate: "+danceMonth+"/"+danceDay+"/"+danceYear+"\nDay of the Week: "+

				danceDayweek+"\nTime: "+danceTime;

	}

}

public class Party extends SocialEvent {

	private String partyAttire,partyTitle,partyDayweek,partyTime;

	private int partyDay,partyMonth,partyYear;


	public Party(String attire,String dayweek, String title,int day,int month,int year, String time){

		super(attire, dayweek, title, day, month, year,time);

		this.partyAttire=attire;

		this.partyDayweek=dayweek;

		this.partyTime=time;

		this.partyTitle=title;

		this.partyDay=day;

		this.partyMonth=month;

		this.partyYear=year;

	}


	public void setPartyTime(String partyTime){

		this.partyTime=partyTime;

	}


	public String getPartyAttire() {

		return partyAttire;

	}


	public void setPartyAttire(String partyAttire) {

		this.partyAttire = partyAttire;

	}


	public String getPartyTitle() {

		return partyTitle;

	}


	public void setPartyTitle(String partyTitle) {

		this.partyTitle = partyTitle;

	}


	public String getPartyDayweek() {

		return partyDayweek;

	}


	public void setPartyDayweek(String partyDayweek) {

		this.partyDayweek = partyDayweek;

	}


	public int getPartyDay() {

		return partyDay;

	}


	public void setPartyDay(int partyDay) {

		this.partyDay=partyDay;

	}


	public int getPartyMonth() {

		return partyMonth;

	}


	public void setPartyMonth(int partyMonth) {

		this.partyMonth=partyMonth;

	}



	public int getPartyYear() {

		return partyYear;

	}


	public void setPartyYear(int partyYear) {

		this.partyYear = partyYear;

	}

	public String toString(int i){

		return "Party "+"\nTitle: "+partyTitle+"\nAttire: "+partyAttire+"\nDate: "+partyMonth+"/"+partyDay+"/"+partyYear+"\nDay of the Week: "+

				partyDayweek+"\nTime: "+partyTime;

	}


}



public class SocialEventList {

	private SocialEvent[] thelist = new SocialEvent[5];

	private int i = 0;



	public void add(SocialEvent se){

		if(i<thelist.length){

			thelist[i]=se;

			++i;

			System.out.println(thelist[i]);

		}

	}

	public void display(){

		for(int j = 0; j < i; j++){                         

			System.out.println(thelist[j].toString(i));

		}

	}

	public void occursOn(SocialEvent event1, SocialEvent event2) {

		if( event1.getTime() == event2.getTime() ) 

			System.out.println("You have an event that occurs on the same day, check to make sure you have enough time.");

		else

			System.out.println("The are no two events occur on this same day");

	}

}



#7
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
Alright so i have gotten my display down and also my occurs on, sorta. I am unfortunately keep getting a nullpointererror, and I can't think of anything to stop this. hopefully you will be able to decipher some of the code.
else if(input.equals("date check")){

				System.out.println("Please enter a date to be checked: ");

				System.out.println("Please enter the day: ");

				day=k.nextInt();

				System.out.println("Please enter the month: ");

				month=k.nextInt();

				System.out.println("Please enter the year: ");

				year=k.nextInt();

				int chnum=(year*day*month);

				sel.occursOn();

			}
public void occursOn() {

		for(SocialEvent se: thelist)

			for(int h = 0; h < i; h++){

		if( se.getTime(h) == se.getTime(h) ) 

			System.out.println("You have an event that occurs on the same day, check to make sure you have enough time.");

		else

			System.out.println("The are no two events occur on this same day");

	}

	}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users