In: Computer Science
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code. What is a better way for me to write this?
//********************************************************
// Event Class code
//********************************************************
package java1;
import java.util.Scanner;
public class Event {
public final static double lowerPricePerGuest =
32.00;
public final static double higherPricePerGuest =
35.00;
public final static int cutOffValue = 50;
public boolean largeEvent;
private String eventNum;
private int numGuests;
private double price;
private double pricePerGuest;
public Event(String event, int guests) {
eventNum = event;
numGuests = guests;
}
public Event() {
this("A000", 0);
}
private Scanner input = new Scanner(System.in);
public void setEventNumber() {
System.out.print("Please enter your event number (i.e.
A001): ");
eventNum = input.nextLine();
}
public void setNumOfGuests() {
System.out.print("Please enter the number of guests
that will be attending? ");
numGuests = input.nextInt();
if (isLargeEvent()) {
pricePerGuest = lowerPricePerGuest;
}
else {
pricePerGuest = higherPricePerGuest;
}
price = numGuests * pricePerGuest;
largeEvent = (numGuests >= cutOffValue);
System.out.println("");
}
public boolean isLargeEvent() {
if (this.getNumOfGuests() > 50) {
return true;
}
else {
return false;
}
}
public String getEventNumber() {
return eventNum;
}
public int getNumOfGuests() {
return numGuests;
}
public double getPrice() {
return price;
}
}
//********************************************************
// Event Demo Class code
//********************************************************
package java1;
import java.util.Scanner;
public class EventDemo {
public static void main(String[] args) {
Event eventOne = new Event();
Event eventTwo = new Event();
Event eventThree = new
Event();
System.out.println("Enter Details
of event # 1:");
eventOne.setEventNumber();
eventOne.setNumOfGuests();
System.out.println("Enter Details
of event # 2:");
eventTwo.setEventNumber();
eventTwo.setNumOfGuests();
System.out.println("Enter Details
of event # 3:");
eventThree.setEventNumber();
eventThree.setNumOfGuests();
displayMotto();
System.out.println();
display(eventOne);
display(eventTwo);
display(eventThree);
System.out.println("Get largest
event between event # 1 and event # 2 is :");
display(getLargestEvent(eventOne,
eventTwo));
System.out.println("Get largest
event between event # 2 and event # 3 is :");
display(getLargestEvent(eventTwo,
eventThree));
System.out.println("Get largest
event from event # 1 and event # 3 is :");
display(getLargestEvent(eventOne,
eventThree));
runLoop();
}
public static void runLoop()
{
final int
SIZE=4;
Event[]
eventObjects = new Event[SIZE];
int
numGuests;
Scanner scanner
= new Scanner(System.in);
for (int i = 0;
i < eventObjects.length; i++)
{
eventObjects[i]=new Event();
eventObjects[i].setEventNumber();
do
{
System.out.println("Enter number of guests : ");
numGuests=scanner.nextInt();
if(numGuests<5 ||numGuests>100)
System.out.println("Number of guests must be between 5 to
100");
}while(numGuests<5 ||numGuests>100);
eventObjects[i].setGuests(numGuests);
}
Event
oneEvent=eventObjects[0];
System.out.println("For first event , printing message ");
System.out.println(oneEvent.getGuestsCount()+" times");
for (int i = 0;
i < oneEvent.getGuestsCount(); i++)
{
System.out.println("Please come to my event!");
}
}
public static void display(Event e)
{
System.out.println("Event number: "
+ e.getEventNumber());
System.out.println("Total guests: "
+ e.getNumOfGuests());
System.out.println("Total price: $"
+ String.format("%.2f", e.getPrice()));
System.out.println("Is Large event:
" + e.isLargeEvent());
System.out.println("");
}
public static Event
getLargestEvent(Event e1, Event e2) {
if (e1.getNumOfGuests() >
e2.getNumOfGuests()) {
return e1;
}
else {
return e2;
}
}
public static void displayMotto() {
System.out.println();
System.out.println("MOTTO");
}
}
import java.util.Scanner;
class Event {
public final static double lowerPricePerGuest = 32.00;
public final static double higherPricePerGuest = 35.00;
public final static int cutOffValue = 50;
public boolean largeEvent;
private String eventNum;
private int numGuests;
private double price;
private double pricePerGuest;
public Event(String event, int guests) {
eventNum = event;
numGuests = guests;
}
public Event() {
this("A000", 0);
}
private Scanner input = new Scanner(System.in);
public void setEventNumber() {
System.out.print("Please enter your event number (i.e. A001): ");
eventNum = input.nextLine();
}
public void setNumOfGuests() {
System.out.print("Please enter the number of guests that will be attending? ");
numGuests = input.nextInt();
if (isLargeEvent()) {
pricePerGuest = lowerPricePerGuest;
} else {
pricePerGuest = higherPricePerGuest;
}
price = numGuests * pricePerGuest;
largeEvent = (numGuests >= cutOffValue);
System.out.println("");
}
public boolean isLargeEvent() {
if (this.getNumOfGuests() > 50) {
return true;
} else {
return false;
}
}
public String getEventNumber() {
return eventNum;
}
public int getNumOfGuests() {
return numGuests;
}
public double getPrice() {
return price;
}
public int getGuestsCount() {
return numGuests;
}
public void setGuests(int aNumGuests) {
numGuests=aNumGuests;
}
}
//********************************************************
// Event Demo Class code
//********************************************************
public class EventDemo {
public static void main(String[] args) {
Event eventOne = new Event();
Event eventTwo = new Event();
Event eventThree = new Event();
System.out.println("Enter Details of event # 1:");
eventOne.setEventNumber();
eventOne.setNumOfGuests();
System.out.println("Enter Details of event # 2:");
eventTwo.setEventNumber();
eventTwo.setNumOfGuests();
System.out.println("Enter Details of event # 3:");
eventThree.setEventNumber();
eventThree.setNumOfGuests();
displayMotto();
System.out.println();
display(eventOne);
display(eventTwo);
display(eventThree);
System.out.println("Get largest event between event # 1 and event # 2 is :");
display(getLargestEvent(eventOne, eventTwo));
System.out.println("Get largest event between event # 2 and event # 3 is :");
display(getLargestEvent(eventTwo, eventThree));
System.out.println("Get largest event from event # 1 and event # 3 is :");
display(getLargestEvent(eventOne, eventThree));
runLoop();
}
public static void runLoop() {
final int SIZE = 4;
Event[] eventObjects = new Event[SIZE];
int numGuests;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < eventObjects.length; i++) {
eventObjects[i] = new Event();
eventObjects[i].setEventNumber();
do {
System.out.println("Enter number of guests : ");
numGuests = scanner.nextInt();
if (numGuests < 5 || numGuests > 100)
System.out.println("Number of guests must be between 5 to 100");
} while (numGuests < 5 || numGuests > 100);
eventObjects[i].setGuests(numGuests);
}
Event oneEvent = eventObjects[0];
System.out.println("For first event , printing message ");
System.out.println(oneEvent.getGuestsCount() + " times");
for (int i = 0; i < oneEvent.getGuestsCount(); i++) {
System.out.println("Please come to my event!");
}
}
public static void display(Event e) {
System.out.println("Event number: " + e.getEventNumber());
System.out.println("Total guests: " + e.getNumOfGuests());
System.out.println("Total price: $" + String.format("%.2f", e.getPrice()));
System.out.println("Is Large event: " + e.isLargeEvent());
System.out.println("");
}
public static Event getLargestEvent(Event e1, Event e2) {
if (e1.getNumOfGuests() > e2.getNumOfGuests()) {
return e1;
} else {
return e2;
}
}
public static void displayMotto() {
System.out.println();
System.out.println("MOTTO");
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me