Why wont Eclipse #Override? - java

I am following along with the Java for Dummies book and I ran into a problem. I can't figure out why #Override isn't working. I'm sure it has to do with my code because I have gotten a polymorphic array to work with override before, but it was too simple for me to mimic.
import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayrollTypeP {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner diskScanner = new Scanner(new File("EmpInfoNew.txt"));
Scanner kbdScanner = new Scanner (System.in);
for(int empNum = 1; empNum<=3; empNum++){
payOneFTEmployee (diskScanner);
}
for(int empNum = 4; empNum<=6; empNum++){
payOnePTEmployee(diskScanner, kbdScanner);
}
}
public static void payOneFTEmployee(Scanner diskScanner){
FullTimeEmployee ftemployee = new FullTimeEmployee();
ftemployee.setName(diskScanner.nextLine());
ftemployee.setJobTitle(diskScanner.nextLine());
ftemployee.setWeeklySalary(diskScanner.nextDouble());
ftemployee.setBenefitDeduction(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine();
ftemployee.cutCheck(ftemployee.findPaymentAmount());
out.println();
}
public static void payOnePTEmployee(Scanner diskScanner, Scanner kbdScanner) {
PartTimeEmployee ptemployee = new PartTimeEmployee();
ptemployee.setName(diskScanner.nextLine());
ptemployee.setJobTitle(diskScanner.nextLine());
ptemployee.setHourlyRate(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine(); //Reads the dashed line that
// separates two employees
out.print("Enter ");
out.print(ptemployee.getName());
out.print("'s hours worked this week: ");
int hours = kbdScanner.nextInt();
ptemployee.cutCheck(ptemployee.findPaymentAmount(hours));
out.println();
}
}
Next class:
import static java.lang.System.out;
public class Employee {
private String name;
private String jobTitle;
public void setName(String nameIn) {
name = nameIn;
}
public String getName() {
return name;
}
public void setJobTitle(String jobTitleIn) {
jobTitle = jobTitleIn;
}
public String getJobTitle() {
return jobTitle;
}
public void cutCheck(double amountPaid){
out.printf("Pay to the order of %s", name);
out.printf("(%s) ***$", jobTitle);
out.printf("%,.2f\n", amountPaid);
}
}
Next Class:
public class PartTimeEmployee extends Employee{
private double hourlyRate;
public void setHourlyRate(double hourlyRateIn) {
hourlyRate = hourlyRateIn;
}
public double getHourlyRate() {
return hourlyRate;
}
public double findPaymentAmount(int hours){
return hourlyRate * hours;
} //method that should be overriden
}
Class That should override:
public class PartTimeWithOver extends PartTimeEmployee{
#Override
public double findPaymentAmount(int hours) {
if(hours <= 40) {
return getHourlyRate() * hours;
} else {
return getHourlyRate() * 40 +
getHourlyRate() * 2 * (hours - 40);
}
}
}
EmpInfoNew.(txt) file
jo shmo
Ceo
5000.00
500.00
edd shmoe
Captain
5000.00
500.00
bob shmo
Honorary Exec
1000.00
200.00
Dave shmo
driver
7.25
edd blah
Cook
8.50
len shmo
Head of Kitchen
12.50

You have instantiated object as
PartTimeEmployee ptemployee = new PartTimeEmployee();
It is a base class of PartTimeWithOther so it cannot call overridden method in derived class.
Change to
PartTimeEmployee ptemployee = new PartTimeWithOver();
Here is an nice tutorial with diagrams that explains Polymorphism

You never use PartTimeWithOver, you only work with PartTimeEmployee.
So your overriden method is never called.

Related

How to insert value from user defined datatypes to object in java?

I'm creating an object of type Lightmode, which is a class.
There's also a Smartlamp class which is having a variable of custom data of type Lighmodes and I want to show my defined data type value over there.
I'm trying to create an object and then insert a string against my Lightmode data type which is showing error. I want to print like this:
Name:  lamp1 
Location: 3.1 
Switched On:  false 
Mode:  STANDARD 
Here mode is lightmode datatype and I'm getting a problem over it.
...
public class LightModes
{
String NIGHT_MODE;
String SOFT_MODE;
String STANDARD_MODE;
public String getNIGHT_MODE() {
return NIGHT_MODE;
}
public void setNIGHT_MODE(String NIGHT_MODE) {
this.NIGHT_MODE = NIGHT_MODE;
}
public String getSOFT_MODE() {
return SOFT_MODE;
}
public void setSOFT_MODE(String SOFT_MODE) {
this.SOFT_MODE = SOFT_MODE;
}
public String getSTANDARD_MODE() {
return STANDARD_MODE;
}
public void setSTANDARD_MODE(String STANDARD_MODE) {
this.STANDARD_MODE = STANDARD_MODE;
}
public LightModes() {
this.NIGHT_MODE = "NIGHT";
this.STANDARD_MODE = "STANDARD";
this.SOFT_MODE = "SOFT";
}
}...
...package smart.home.app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Step5 {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int size,mode;
int displayindex=1;
String name;
double location,temperature;
boolean status=false;
System.out.println("Enter number of size of Fridge you want to add.");
Scanner in = new Scanner(System.in);
size=in.nextInt();
// TODO code application logic here
SmartLamp[] smartLamp=new SmartLamp[size];// creating an array object of smartdevice class
//
for(int j=0;j<size;j++)
{
System.out.println("Enter Lamp name.");
name=input.readLine();
System.out.println("Enter Lamp Location.\\hint(1.1)");
Scanner devicelocation=new Scanner(System.in);
location=devicelocation.nextDouble();
System.out.println("Enter Lamp Mode.(1 for Night 2 for Soft 3 for Standard)");
Scanner lampmode=new Scanner(System.in);
mode=lampmode.nextInt();
System.out.println("Enter Lamp status.1 for ON, 0 for OFF.");
Scanner devicestatus=new Scanner(System.in);
int currentstatus=devicestatus.nextInt();
if(currentstatus==1)
{
status=true;
}
else if(currentstatus==0)
{
status=false;
}
LightModes light = null;
smartLamp[j]=new SmartLamp(light.NIGHT_MODE, name, location, status);
}
//////////////Display Data////////////////////////////
for(int i=0;i<size;i++)
{
System.out.println("-Smart lamp "+displayindex+" -");
System.out.println(smartLamp[i].toString());
System.out.println("---------------------------------------------");
displayindex++;
}
}
}...
...public class SmartLamp extends SmartDevice{
private LightModes lightModes;
public LightModes getLightModes() {
return lightModes;
}
public void setLightModes(LightModes lightModes) {
this.lightModes = lightModes;
}
public SmartLamp(String name, double location, boolean switchedOn) {
super(name, location, switchedOn);
}
public SmartLamp(LightModes lightModes, String name, double location, boolean switchedOn) {
super(name, location, switchedOn);
this.lightModes = lightModes;
}
#Override
public String toString() {
return "SmartLamp{"+"\nName."+getName()+"\nLocation."
+getLocation() + "\nSwitchedOn."+isSwitchedOn()+
"\nMode=" + getLightModes() + '}';
}
}...
I downloaded your code and added the missing class SmartDevice. Your code contains two compiler errors.
name = input.readLine();
This line throws java.io.IOException which is an unchecked exception and hence must be handled by your code. There are several ways to do this, one of which is to add throws IOException to method main() in class Step5.
smartLamp[j]=new SmartLamp(light.NIGHT_MODE, name, location, status);
The first argument to SmartLamp constructor is an instance of class LightModes but you are passing a String. You need to create an instance of LightModes.
Here is your code with my modifications that get rid of the two compiler errors.
(Note: The below code includes my guessed implementation of class SmartDevice.)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Step5 {
public static void main(String[] args) throws IOException { // Change here.
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int size, mode;
int displayindex = 1;
String name;
double location, temperature;
boolean status = false;
System.out.println("Enter number of size of Fridge you want to add.");
Scanner in = new Scanner(System.in);
size = in.nextInt();
SmartLamp[] smartLamp = new SmartLamp[size];// creating an array object of smartdevice class
for (int j = 0; j < size; j++) {
System.out.println("Enter Lamp name.");
name = input.readLine(); // throws java.io.IOException
System.out.println("Enter Lamp Location.\\hint(1.1)");
Scanner devicelocation = new Scanner(System.in);
location = devicelocation.nextDouble();
System.out.println("Enter Lamp Mode.(1 for Night 2 for Soft 3 for Standard)");
Scanner lampmode = new Scanner(System.in);
mode = lampmode.nextInt();
System.out.println("Enter Lamp status.1 for ON, 0 for OFF.");
Scanner devicestatus = new Scanner(System.in);
int currentstatus = devicestatus.nextInt();
if (currentstatus == 1) {
status = true;
}
else if (currentstatus == 0) {
status = false;
}
LightModes light = new LightModes(); // Change here.
light.setNIGHT_MODE(light.getNIGHT_MODE()); // Change here.
smartLamp[j] = new SmartLamp(light, name, location, status); // Change here.
}
//////////////Display Data////////////////////////////
for(int i=0;i<size;i++)
{
System.out.println("-Smart lamp "+displayindex+" -");
System.out.println(smartLamp[i].toString());
System.out.println("---------------------------------------------");
displayindex++;
}
}
}
class LightModes {
String NIGHT_MODE;
String SOFT_MODE;
String STANDARD_MODE;
public String getNIGHT_MODE() {
return NIGHT_MODE;
}
public void setNIGHT_MODE(String NIGHT_MODE) {
this.NIGHT_MODE = NIGHT_MODE;
}
public String getSOFT_MODE() {
return SOFT_MODE;
}
public void setSOFT_MODE(String SOFT_MODE) {
this.SOFT_MODE = SOFT_MODE;
}
public String getSTANDARD_MODE() {
return STANDARD_MODE;
}
public void setSTANDARD_MODE(String STANDARD_MODE) {
this.STANDARD_MODE = STANDARD_MODE;
}
public LightModes() {
this.NIGHT_MODE = "NIGHT";
this.STANDARD_MODE = "STANDARD";
this.SOFT_MODE = "SOFT";
}
}
class SmartDevice {
private String name;
private double location;
private boolean switchedOn;
public SmartDevice(String name, double location, boolean switchedOn) {
this.name = name;
this.location = location;
this.switchedOn = switchedOn;
}
public String getName() {
return name;
}
public double getLocation() {
return location;
}
public boolean isSwitchedOn() {
return switchedOn;
}
}
class SmartLamp extends SmartDevice {
private LightModes lightModes;
public LightModes getLightModes() {
return lightModes;
}
public void setLightModes(LightModes lightModes) {
this.lightModes = lightModes;
}
public SmartLamp(String name, double location, boolean switchedOn) {
super(name, location, switchedOn);
}
public SmartLamp(LightModes lightModes, String name, double location, boolean switchedOn) {
super(name, location, switchedOn);
this.lightModes = lightModes;
}
#Override
public String toString() {
return "SmartLamp{" + "\nName." + getName() + "\nLocation." + getLocation()
+ "\nSwitchedOn." + isSwitchedOn() + "\nMode=" + getLightModes() + '}';
}
}

Cant access classes through Object Java RPG character builder

Im trying to create a small character builder, using inheritance. i have CreateCharacter CharacterRace then a Dwarf class. i made a variable with type CharacterRace in CreateCharacter and a variable with type Dwarf in CharacterRace. i have an object of CreateCharacter in my main method demo and its not letting me call the methods from the Dwarf class, to make a dwarf character. im thinking ineed to pass a dwarf object in characterRace? im just not sure how. heres my code: (its a bit long my apologies)
package characterCreation;
public class CreateCharacter {
private CharacterClass characterClass;
private CharacterRace characterRace;
private Name name;
public CreateCharacter(String characterName,CharacterClass characterClass,CharacterRace characterRace) {
this.name = new Name(characterName);
this.characterClass = characterClass;
this.characterRace = characterRace;
}
public CreateCharacter(){
}
public CharacterClass getCharacterClass() {
return characterClass;
}
public void setCharacterClass(CharacterClass characterClass) {
this.characterClass = characterClass;
}
public CharacterRace getCharacterRace() {
return characterRace;
}
public void setCharacterRace(CharacterRace characterRace) {
this.characterRace = characterRace;
}
public Name getName(){
return name;
}
public void setName(Name name){
this.name = name;
}
#Override
public String toString() {
return "CreateCharacter [name=" + name + ", characterRace=" + characterRace + ", characterClass="
+ characterClass + "]";
}
}
package characterCreation;
public class CharacterRace {
protected String raceName;
protected double mana;
protected double hp;
private Dwarf dwarf;
public CharacterRace(String raceName,double mana, double hp) {
this.raceName = raceName;
this.mana = mana;
this.hp = hp;
}
public CharacterRace(){
}
public String getRaceName() {
return raceName;
}
public Dwarf getDwarf() {
return dwarf;
}
public void setDwarf(Dwarf dwarf) {
this.dwarf = dwarf;
}
public double getMana() {
return mana;
}
public double getHp() {
return hp;
}
#Override
public String toString() {
return "CharacterRace [dwarf=" + dwarf + "]";
}
}
package characterCreation;
public class Dwarf extends CharacterRace {
public Dwarf(String raceName,double mana, double hp) {
super(raceName,mana,hp);
}
public double getMana() {
mana = 5;
return mana;
}
public double getHp() {
hp = 10;
return hp;
}
public String getRaceName(){
return raceName = "Dwarf";
}
#Override
public String toString() {
return "Dwarf [mana=" + mana + ", hp=" + hp + ", getRaceName()=" + getRaceName() + "]";
}
}
package characterCreation;
import java.util.Scanner;
public class CharacterDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
CreateCharacter create = new CreateCharacter();
System.out.println("Choose your Race: ");
String userRace = input.next();
create.setName(new Name("Daxel"));
//create.setCharacterRace(race);
System.out.println(create.getName());
//Dwarf dwarf = new Dwarf();
System.out.println(create.getCharacterRace().getDwarf().getRaceName());
//System.out.println(create.getCharacterRace().setDwarf(new Dwarf("dwarf",10,5)));
}
}
You have to call setCharacterRace() on create; then call setDwarf() on the characterRace; otherwise create.getCharacterRace() would be null and create.getCharacterRace().getDwarf() would throw NullPointerException.
I don't understand the logic behind your code, but try the code below:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
CreateCharacter create = new CreateCharacter();
System.out.println("Choose your Race: ");
String userRace = input.next();
create.setName(new Name("Daxel"));
//***********new code starts******
CharacterRace myRace = new CharacterRace(userRace, 20, 9);
myRace.setDwarf(new Dwarf("dwarf",10,5));
create.setCharacterRace(myRace);
//***********new code ends********
//create.setCharacterRace(race);
System.out.println(create.getName());
//Dwarf dwarf = new Dwarf();
System.out.println(create.getCharacterRace().getDwarf().getRaceName());
//System.out.println(create.getCharacterRace().setDwarf(new Dwarf("dwarf",10,5)));
}

I called a method but it doesn't print

i'm a beginner, I barely know anything, I tired to do some classes and just mess around with methods, but for some reason the method wont print, also I'm really bad with arrays :(
main class :
public class Employees {
static Employee[] array =new Employee[3];
static int i=0;
public void insertEmployee(){
Scanner keyboard=new Scanner(System.in);
System.out.println("please fill in the information :");
System.out.println("name :");
String Name=keyboard.next();
System.out.println("ID :");
long ID=keyboard.nextLong();
System.out.println("Salary :");
double Salary =keyboard.nextDouble();
Citizen s2 =new Citizen(Name);
Employee E= new Employee(ID,s2,Salary);
array[i]=E;
}
public void main(String[] args) {
Employees m1=new Employees();
for ( i=0; i<3;++i){
Employees c1=new Employees();
c1.insertEmployee();
}
System.out.println("*****");
for (i=0;i<3;++i){
array[i].print();
}
}
}
second class :
public class Employee {
private Citizen employeeInfo =new Citizen();
private long employeeID;
private double employeeSalary;
Employee(long employeeID,Citizen employeeInfo ){
this.employeeID=employeeID;
this.employeeInfo=employeeInfo;
}
Employee(long employeeID,Citizen employeeInfo, double employeeSalary){
this.employeeID=employeeID;
this.employeeInfo=employeeInfo;
this.employeeSalary=employeeSalary;
}
void print(){
if (employeeSalary==0){
employeeSalary=-1;
}
System.out.println(employeeID+"-"+employeeInfo.getCitizenName()+"-"+employeeSalary);
}
}
and last one:
public class Citizen {
private String citizenName;
private long citizenID;
public Citizen(){
}
public Citizen(String Name){
this.citizenName=Name;
}
public Citizen(String citizenName,long citizenID){
this.citizenName=citizenName;
this.citizenID=citizenID;
}
public String getCitizenName(){
return citizenName;
}
public long getCitizenID(){
return citizenID;
}
}
thank you :)
It has to be public static void main(String[] args). The static is important

How do you add a single parameter of an object with multiple parameters to a string from an arraylist of that object?

I am trying to rewrite the toString() to print names. I use getNm() from each employee in an arraylist of object employees. Employees have the parameters (pay, nm, hoursWorked, overtimeHours)
I have bolded the code I am focused on.
public class Main {
public static void main(String[] args){
workers workerlist = new workers();
workerlist.setNumberEmployees();
workerlist.instantiateEmployees();
System.out.println(workerlist.toString());
}
}
public class Employees extends workers{
public double pay;
public String nm;
public double hours;
public double overtime;
public Employees(double pay, String nm, double hoursWorked, double overtimeHours){
}
public double getPay(){
return pay;
}
public void setPay(double pay){
}
public String getNm(){
return nm;
}
public void setNm(String nm){
}
public double getHours(){
return hours;
}
public void setHours(double hours){
}
public double getOvertime(){
return overtime;
}
public void setOvertime(double overtime){
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class workers{
public int employeenumber;
public String nm;
ArrayList<Employees> workerList = new ArrayList<Employees>();
Scanner input = new Scanner(System.in);
public void setNumberEmployees(){
System.out.println("How many employees do you have?");
employeenumber = input.nextInt();
}
public int getNumberEmployees(){
return employeenumber;
}
public void instantiateEmployees(){
for(int i=1; i<=employeenumber; i++){
workerList.add(new Employees(0.0, "nm", 0.0, 0.0));
}
}
public String toString(){
String st = "";
for(int i=0; i<employeenumber; i++){
**st += workerList.toString();**
// ".toString() is there to test the setting of parameters, I am interested in replacing this part.
}
return st;
}
}
Expected Output [Employee 1's name, Employee 2's name,... Employee n's name]
I think below code will give your expected result.
public class Main {
public static void main(String[] args) {
workers workerlist = new workers();
workerlist.setNumberEmployees();
workerlist.instantiateEmployees();
System.out.println(workerlist.toString()); //call toString to take workerlist
}
}
Override Employees toString method and do not forget update nm parameter in constructor
public class Employees extends workers {
public double pay;
public String nm;
public double hours;
public double overtime;
public Employees(double pay, String nm, double hoursWorked, double overtimeHours) {
this.nm = nm; //do not forget set value
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
}
public double getOvertime() {
return overtime;
}
public void setOvertime(double overtime) {
}
#Override
public String toString() {
return getNm(); //Employees toString method will return nm
}
}
override toString method and call arraylist's toString method.
public class workers {
public int employeenumber;
public String nm;
ArrayList<Employees> workerList = new ArrayList<Employees>();
Scanner input = new Scanner(System.in);
public void setNumberEmployees() {
System.out.println("How many employees do you have?");
employeenumber = input.nextInt();
}
public int getNumberEmployees() {
return employeenumber;
}
public void instantiateEmployees() {
for (int i = 1; i <= employeenumber; i++) {
workerList.add(new Employees(0.0, "nm", 0.0, 0.0));
}
}
public String toString() {
return workerList.toString(); //no need to iterate on arraylist, Arraylist.toString method will call each Employees toString method.
}
}
Yusuf K. and Andrew Tobilko have answered your question correctly. Just to give you some hints to improve your classes a bit:
Why do you call Employees eventhough your class can represent a single employee; so I would call it Employee instead.
Why do you define the nm field twice? It is declared in Workers class from which the Employees class is derived.
setNumberEmployees() method does not comply with JavaBeans convention; it must be declared as
public void setNumberEmployees(final String noEmployees) {
employeenumber = noEmployees;
}
And the call to setNumberEmployees() should be done in the main() method as
public static void main(String[] args){
workers workerlist = new workers();
Scanner input = new Scanner(System.in);
workerlist.setNumberEmployees(input.nextInt());
workerlist.instantiateEmployees();
System.out.println(workerlist.toString());
}
And lastly, you are speaking about parameter(s). They are actually called fields or instance variable(s).
public String toString(){
String st = "";
for(int i=0; i < employeenumber; i++)
st += workerList.get(i).toString();
return st;
}
But this solution is not the best. I suggest you the next one:
public String toString(){
StringBuilder builder = new StringBuilder();
workerList.stream().map(Employees::toString).forEach(builder::append);
return builder.toString();
}
And, of course, you have to override toString() in the Employees class. For example,
#Override public String toString() {
return new StringJoiner(", ")
.add(Double.toString(pay))
.add(nm)
.add(Double.toString(hours))
.add(Double.toString(overtime))
.toString();
}

My Tester Class in Incomplete and I'm trying to decide if I need to pull methods from my other classes

I am having problems deciding which methods I should pull out of my VendingMachine Class and placed in my TesterClass to test my program. I am attempting to test the VendingMachine and the Candy Classes by constructing Candy objects in my ArrayList but I'm not sure how to best accomplish this. The ArrayList of Candy objects contain the candy bar string and a double price. I want my tester to run the vending machine methods that prompt the user for the questions that are listed in the println methods.
Tester Class
public class Tester {
private static Object candyBarList;
public static void main(String args[])
{
WHAT METHODS SHOULD I PLACE HERE
}
Candy Class
public class Candy{
private double price;
private String candyBarName;
Candy()
{
price = 1.50;
candyBarName = "Pluto Bar";
//default constructor
}
Candy(double price1, String str){
price = price1;
candyBarName = str;
//constructor with parameters
}
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price = price;
}
public String getCandyBarName(){
return candyBarName;
}
public void setCandyBarName(String candyBarName){
this.candyBarName = candyBarName;
}
Vending Machine Class
}
import java.util.ArrayList;
import java.util.Scanner;
import vendingmachine.Candy;
package vendingmachine;
/**
*
* #author admin
*/
public class VendingMachine {
double money = 0.0;
Candy candyBar = new Candy();
ArrayList<Candy> candyBarList = new ArrayList<>();
double cashBox = 0.0;
public VendingMachine(){
candyBarList.add(candyBar);
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
candyBarList.add(new Candy(2.00, "Snickers Bar"));
}
/**
* #return
*/
public double paymentIn(){
Scanner input = new Scanner(System.in);
//double payAmount=0.0;
System.out.println("Insert Money: ");
double payAmount = input.nextDouble();
return payAmount;
}
public static boolean isPaymentEnough(double depositMoney, double price){
if (depositMoney >= price)
{
return true;
}
else{
return false;
}
}
public void dispenseCandy (double depositMoney, Candy selectedCandy){
cashBox+= depositMoney - selectedCandy.getPrice();
candyBarList.remove(selectedCandy);
}
public String candySelector(){
for (int i=0; candyBarList.size() >= i; i++){
System.out.println(candyBarList.get(i));
}
Scanner input = new Scanner(System.in);
String candyBarSelected = input.next();
System.out.println("Please type in the Candy Bar you would like");
candyBarSelected = input.next();
return candyBarSelected;
}
public Candy findCandy(){
String candyName = candySelector();
for (int i=0; candyBarList.size() >= i; i++){
if (candyBarList.get(i).getCandyBarName().equals(candyName)){
return candyBarList.get(i);
}
}
System.out.println("Your selection is invalid");
return null;
}

Categories

Resources