Skip to content

@bitsYbytes /Actor.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Java Community Theater Program
package newcommunitytheatertest;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
/**
*
* @author
* This is a subclass of TheaterPerson, it implements the interface CalculatePay
*/
public class Actor extends TheaterPerson implements CalculatePay {
private String play;
private float payHourly;
private boolean equity;
private String roleName;
private int hoursWorked;
private Scanner fileIn;
public Actor()
{ //constructor that takes keyboard inputs
super();
readInKeyboard();
}
//file input constructor that calls readInFile method, takes in scanner object fileIn
public Actor(Scanner fileIn) throws FileNotFoundException
{
//getting info from file
super();
readInFile(fileIn);
}
//takes in Scanner fileIn to check string input from file with try and catch statements
public String getString(Scanner fileIn)
{
String temp = new String();
try
{
//Scanner sscan = new Scanner(inFile);
temp = fileIn.nextLine();
}
catch (Exception e)
{
System.out.printf("Exception reading string");
return "";
}
return temp;
}
//not in use, method for getting a string from keyboard
public String getString()
{
String temp = new String(); //create new temp
Scanner input = new Scanner(System.in);
try
{
temp = input.nextLine();
}
catch(NumberFormatException e)
{
System.out.printf("Exception reading string");
return "";
}
return temp;
}
//same as getString that takes in Scanner fileIn, for ints from file
public int getInt(Scanner fileIn)
{
int temp;
try
{
//Scanner sscan = new Scanner(inFile);
if (fileIn.hasNextInt())
{
temp = fileIn.nextInt();
}
else
{
fileIn.nextLine();
temp = fileIn.nextInt();
}
fileIn.nextLine();
}
catch (NumberFormatException e)
{
System.out.printf("Exception reading int");
return 0;
}
return temp;
}
//same as getString for keyboard, reads ints
public int getInt()
{
int temp; //create new temp
Scanner input = new Scanner(System.in);
try
{
temp = input.nextInt();
}
catch(Exception e)
{
System.out.printf("Exception reading integer");
return 0;
}
return temp;
}
//file input for floats
public float getFloat(Scanner fileIn)
{
float temp;
try
{
//Scanner sscan = new Scanner(inFile);
if (fileIn.hasNextFloat())
{
temp = fileIn.nextFloat();
}
else
{
fileIn.nextLine();
temp = fileIn.nextFloat();
}
fileIn.nextLine();
}
catch (NumberFormatException e)
{
System.out.printf("Exception reading float");
return 0;
}
return temp;
}
public String getPlay() {
return play;
}
public void setPlay(String play) {
this.play = play;
}
public float getPayHourly() {
return payHourly;
}
//set method error checks hourly pay
public void setPayHourly(float payHourly) {
if((payHourly <= 0) || (payHourly > 101))
{
System.out.printf("The pay entered is invalid. Default pay set");
payHourly= 35;
}
else{
this.payHourly = payHourly;
}
}
public boolean isEquity() {
return equity;
}
//sets boolean variable equity by taking in string and converting to bool
public void setEquity(String equityStr) {
if(equityStr.equalsIgnoreCase("true"))
this.equity = true;
else if(equityStr.equalsIgnoreCase("false"))
this.equity = false;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public int getHoursWorked() {
return hoursWorked;
}
//error checks hours worked
public void setHoursWorked(int hoursWorked) {
if((hoursWorked <=0) ||(hoursWorked > 100))
{
System.out.print("\nHours worked is invalid. Default hours per week set.");
}
else{
this.hoursWorked = hoursWorked;
}
}
//method to read files, takes in scanner fileIn
private void readInFile(Scanner fileIn) throws FileNotFoundException
{
System.out.println("Reading input from file...");
//perhaps unecessary temp variables for setting dates for birth & hire
int tempDay=0;
int tempMonth=0;
int tempYear=0;
int tempHDay = 0;
int tempHMonth = 0;
int tempHYear = 0;
this.setLastName(getString(fileIn));
this.setFirstName(getString(fileIn));
this.setIdNum(getInt(fileIn));
this.setPersonAddress(getString(fileIn));
tempDay = getInt(fileIn);
tempMonth = getInt(fileIn);
tempYear = getInt(fileIn);
this.setDateOfBirth(tempDay, tempMonth, tempYear);//setting date of birth
tempHDay = getInt(fileIn);
tempHMonth = getInt(fileIn);
tempHYear = getInt(fileIn);
this.setHireDate( tempHDay, tempHMonth, tempHYear);
this.setPersonAccount(getInt(fileIn));
this.setPlay(getString(fileIn));
this.setPayHourly(getFloat(fileIn));
this.setEquity(getString(fileIn));
this.roleName = getString(fileIn);
this.setHoursWorked(getInt(fileIn));
//return? to let the method from main know that info is done and to move to next?
}
//this method reads from keyboard
private void readInKeyboard()
{
Scanner keyInput = new Scanner(System.in);
System.out.println("\nReading input from keyboard...");
//ask individual questions read in
}
@Override
//interface for Actor's pay
public double getCalculatedPay()
{ double actorPay=0;
if(equity==false)
{
actorPay = payHourly * hoursWorked;
System.out.printf("The actor's pay is"+ actorPay);
}
if(equity==true)
{ double calc = 0;
calc = (hoursWorked -40) * payHourly;
if(calc > 0)
{
actorPay = payHourly * hoursWorked + calc;
System.out.printf("\nEquity is true. calc > 0 \nThe actor's pay is" + actorPay);
}
else
{
actorPay = payHourly * hoursWorked;
System.out.printf("\nEquity is true. calc < 0 \nThe actor's pay is" + actorPay);
}
}
return actorPay;
}
public String toString()
{
return String.format("%s \nis in the role of %s in the play %s. \nHours worked: %d \nPay: %f\nThis Actor %s", super.toString(), roleName, play, hoursWorked, payHourly,((equity==true) ? "has equity": "does not have equity"));
}
}
/**
Data in file is for an Actor. Items are one per line and are in the following order:
LastName
FirstName
IDNumber
Address
DayOfBirth
MonthOfBirth
YearOfBirth
DayOfHire
MonthOfHire
YearOfHire
BankAccountNumber
PlayName
PayPerHour
MemberOfActorsEquity // boolean
Role
HoursWorkedPerWeek
You may remove this commented header from the test file when you use it with your program.
*/
Cruise
Tom
8763345
123 Semantic St, NY, NY 10001
04
07
1962
09
06
1965
11110998
Cabaret
80.00
true
The Emcee
75
Roberts
Julia
3373663
883 Suter Dr., Nashville, IN, 54308
09
09
1980
02
04
2012
111163553
Gravity
85.00
true
Dr. Ellen Stone
88
Rooney
Mickey
56475
90 S. Ventura Blvd., Hollywood, CA 21454
02
02
1925
30
04
1993
111137254
Let's Have a Show
40.00
true
Andy Hardy
45
Michelle
Nichols
784564
845 Star Way, Bethesda, MD 34528
23
09
1938
17
05
1984
111183765
Undiscovered Country
45.00
false
Lt. Uhuru
50
package newcommunitytheatertest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author
*
* Lab 4 Main Class
*/
public class NewCommunityTheaterTest {
public static void main(String[] args) {
//Scanner theaterInfo = new Scanner(System.in);
Scanner choice = new Scanner(System.in);//variable for user to choose
System.out.printf("\nFile input (F) or keyboard input (K): ");
String choiceInput = choice.next();
CommunityTheater theaterObj;//object of community theater not in use at the moment
theaterObj = new CommunityTheater();
ArrayList<Actor> actorList = new ArrayList();//ArrayList of instances of Actors, will be moved to theaterObj
//user chooses keyboard or file input
if(choiceInput.equalsIgnoreCase("k"))
{ //not in use at the moment
System.out.println("\nYou've chosen keyboard input...");
}
else if(choiceInput.equalsIgnoreCase("f"))
{ //call method to read in the actor file
readActorFile(actorList);
}
}
//method for reading the actor file, taking in actor ArrayList- unsure of this
public static void readActorFile(ArrayList<Actor> actorList)
{
Scanner fileIn;
try
{
fileIn = new Scanner( new File("ActorsIn3.txt"));
while(fileIn.hasNext())
{
//loop for multiple actors may go here?
//adds one actor at the moment
Actor actor1 = new Actor(fileIn);
//adds actor1 to Actor ArrayList
actorList.add(actor1);
}
}
catch(FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening file.");
System.exit(1);
}
//print actor ArrayList
System.out.print(actorList);
}
}
package newcommunitytheatertest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
/**
*
* @author
* This class is the super class for the people of the theater.
*/
public class TheaterPerson {
private String lastName;
private String firstName;
private int idNum;
private String personAddress;
private Calendar dateOfBirth;
private Calendar hireDate;
private BankAccount personAccount;
private int bankAcctNum;
public TheaterPerson()
{
this.lastName = "";
this.firstName = "";
this.personAddress ="";
this.idNum = 0;
}//setting to null or empty
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Calendar getDateOfBirth() {
return dateOfBirth;
}
/**
* This method sets the date of birth for a person, uses java calendar class
* @param dayOfBirth
* @param monthOfBirth
* @param yearOfBirth
*/
public void setDateOfBirth(int dayOfBirth, int monthOfBirth, int yearOfBirth) {
dateOfBirth = Calendar.getInstance();
if(((dayOfBirth > 0)&&(dayOfBirth < 32))&&((monthOfBirth >0)&&(monthOfBirth <13)) && ((yearOfBirth > 1900)&& (yearOfBirth < 2013)))
{
this.dateOfBirth.set(yearOfBirth, (monthOfBirth-1), dayOfBirth);
}
else{
System.out.println("\nInvalid date entered. Default set.");
this.dateOfBirth.set(1980, 1, 1);
}
}
public double getIdNum() {
return idNum;
}
//sets ID number, checks for negative
public void setIdNum(int idNum) {
if(idNum > 0)
{//checking for positive ID number
this.idNum = idNum;
}
else
{
int n = 139999;
Random randomId = new Random();
//set random number for ID num
this.idNum = randomId.nextInt(n);
}
}//end method setIdNum
public String getPersonAddress() {
return personAddress;
}
public void setPersonAddress(String personAddress) {
this.personAddress = personAddress;
}
public Calendar getHireDate() {
return hireDate;
}
/**
*
* This method sets the hire date/date of purchase for theater person.
* @param month
* @param day
* @param year
*/
public void setHireDate(int day, int month, int year) {
hireDate = Calendar.getInstance();
if(((day > 0)&&(day < 32))&&((month >0)&&(month <13)) && ((year > 1900)&& (year < 2013)))
{
this.hireDate.set(year, (month-1), day);
}
else{
System.out.println("\nInvalid date entered. Default set.");
this.hireDate.set(2010, 1 , 1);
}
}
public BankAccount getPersonAccount() {
return personAccount;
}
public void setPersonAccount(int acctNum) {
this.personAccount = new BankAccount(acctNum);
}
public int getBankAcctNum()
{
return personAccount.getAccountNumber();
}
@Override
public String toString()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
return String.format("\n" + firstName +" "+ lastName + "\nDate of Birth: " + sdf.format(dateOfBirth.getTime()) +"\nHire Date: " + sdf.format(hireDate.getTime()) + "\nAddress: " + personAddress + "\nID Number: " + idNum );
}
}//end TheaterPerson class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.