How to modify array elements from a separate class?
I am trying to modify array elements in a class which populates these fields by reading in a text file. I have got mutator methods to modify the array elements and they work fine in the same class.
public class Outlet {
private Outlet[] outlet;
private String outletName;
private int phoneNumber;
private int category;
private int operatingDays;
static final int DEFAULT = 99;
public void setPhoneNumber(int newPhoneNumber) {
phoneNumber = newPhoneNumber;
}
public void setCategory(int newCategory) {
category = newCategory;
}
public void setOperatingDays(int newOperatingDays) {
operatingDays = newOperatingDays;
}
public static void readFile(Outlet[] outlet) throws FileNotFoundException {
Scanner inFile = new Scanner(new FileReader("outlets.txt"));
int rowNo = 0;
int i = 0;
String outletValue;
while (inFile.hasNext() && rowNo < MAXOUTLETS) {
outlet[rowNo] = new Outlet();
outletValue = inFile.nextLine();
outlet[rowNo].setOutletName(outletValue.toUpperCase());
outlet[rowNo].setPhoneNumber(DEFAULT);
outlet[rowNo].setCategory(DEFAULT);
outlet[rowNo].setOperatingDays(DEFAULT);
}
inFile.close();
}
public static void displayUnassignedOutlets(Outlet[] outlet) {
int i = 0;
System.out.println("Showing all unassigned Outlets");
System.out.println(STARS);
for (i = 0; i < MAXOUTLETS; i++ ) {
if (outlet.getCategory() == DEFAULT) {
System.out.println("\nOutlet Number: " + (i + 1) + "\t" +
outlet.getOutletName());
}
}
}
Now in the other class that I want to modify the array elements I use the following code but I get an error that "The expression type must be an array but a Class ' Outlet' is resolved".
So how can I modify the array elements? What do I have to instantiate to get the following code to work?
public class Franchise {
private Franchise[] franchise;
public Outlet[] outlet;
public static void createFranchise(Franchise[] franchise) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
int choice = -1;
++++++++++++++++++++++++++++++++++++
Outlet outlet = new Outlet();
Outlet.readFile(outlet.getOutlet());
++++++++++++++++++++++++++++++++++++
boolean invalidChoice = true;
while (invalidChoice) {
System.out.println("\nCreating a New Franchise...");
System.out.println(STARS);
System.out.println("Please select an outlet from the list below");
Outlet.displayUnassignedOutlets(outlet.getOutlet());
choice = console.nextInt();
if (choice < 0 || choice > 10) {
System.out.println("Error! Please choose a single number between 1 and 10");
}
else {
invalidChoice = false;
}
}
invalidChoice = true;
while (invalidChoice) {
System.out.println("Please enter the Phone Number for this Outlet");
choice = console.nextInt();
String phone = new String();
phone = new Integer(choice).toString();
if (phone.length() < 8 || phone.length() > 10) {
System.out.println("Error! Please enter 8 to 10 digits only");
}
else {
+++++++++++++++++++++++++++++++++++++++
outlet[(choice - 1)].setPhoneNumber(choice);
+++++++++++++++++++++++++++++++++++++++
invalidChoice = false;
}
}
}
Hi Pete!
Thanks for your comments. I have included my full classes below with their respective driver modules. Hope this helps out a bit more using the code tags. Sorry, it was my first posting. Thanks for the heads up!
import java.util.*;
import java.io.*;
public class Outlet {
public Outlet[] outlet;
private String outletName;
private int phoneNumber;
private int category;
private int operatingDays;
//private Applicant chosenApplicant;
static boolean SHOWDETAILS = false;
static final String STARS = "****************************************";
static final int MAXOUTLETS = 10;
static final int DEFAULT = 99;
public Outlet[] getOutlet() {
return outlet;
}
public String getOutletName() {
return outletName;
}
public int getPhoneNumber() {
return phoneNumber;
}
public int getCategory() {
return category;
}
public int getOperatingDays() {
return operatingDays;
}
public void setOutletName(String newOutletName) {
outletName = newOutletName;
}
public void setPhoneNumber(int newPhoneNumber) {
phoneNumber = newPhoneNumber;
}
public void setCategory(int newCategory) {
category = newCategory;
}
public void setOperatingDays(int newOperatingDays) {
operatingDays = newOperatingDays;
}
public Outlet() {
outlet = new Outlet[10];
}
public static void readFile(Outlet[] outlet) throws FileNotFoundException {
Scanner inFile = new Scanner(new FileReader("outlets.txt"));
int rowNo = 0;
int i = 0;
String outletValue;
while (inFile.hasNext() && rowNo < MAXOUTLETS) {
outlet[rowNo] = new Outlet();
outletValue = inFile.nextLine();
outlet[rowNo].setOutletName(outletValue.toUpperCase());
//System.out.println(rowNo % 2);
//if (rowNo % 2 == 0) {
outlet[rowNo].setPhoneNumber(DEFAULT);
outlet[rowNo].setCategory(DEFAULT);
outlet[rowNo].setOperatingDays(DEFAULT);
//}
//System.out.println("Outlet Name+++++++ " + rowNo + "\n" + outlet[rowNo].getOutlet());
rowNo++;
//System.out.println(rowNo);
}
if (SHOWDETAILS) {
if (rowNo > 6) {
for (i = 0; i < rowNo; i++ ) {
System.out.println("\nOutlet Name+++++++ " + (i + 1) + "\t" +
outlet[i].getOutletName());
}
}
}
inFile.close();
}
public static void displayAllOutlets(Outlet[] outlet) {
int i = 0;
System.out.println("Showing All Outlets");
System.out.println(STARS);
for (i = 0; i < MAXOUTLETS; i++ ) {
System.out.println("\nOutlet Number: " + (i + 1) + "\t" +
outlet[i].getOutletName());
}
}
public static void displayUnassignedOutlets(Outlet[] outlet) {
int i = 0;
System.out.println("Showing all unassigned Outlets");
System.out.println(STARS);
for (i = 0; i < MAXOUTLETS; i++ ) {
if (outlet[i].getCategory() == DEFAULT) {
System.out.println("\nOutlet Number: " + (i + 1) + "\t" +
outlet[i].getOutletName());
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Outlet start = new Outlet();
Outlet.readFile(start.getOutlet());
Outlet.displayUnassignedOutlets(start.getOutlet());
}
}
================================
So in the below Franchise class, when I specify:
outlet[(choice - 1)].setPhoneNumber(choice);
I get the error that an array is required but the class Outlet is resolved. Any feedback is greatly appreciated!
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class Franchise {
private Franchise[] franchise;
public Outlet[] outlet;
static final int MAXOUTLETS = 10;
static final int DEFAULT = 99;
static boolean SHOWDETAILS = false;
static final String STARS = "****************************************";
static final double REGHOTDOG = 2.50;
static final double LARGEHOTDOG = 4;
static final int SALESPERIOD = 28;
static final int OPERATINGHOURS = 8;
public Franchise[] getFranchise() {
return franchise;
}
public Franchise() {
}
public static void createFranchise(Franchise[] franchise) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
int choice = -1;
//franchise[i] = new Franchise();
Outlet outlet = new Outlet();
//outlet[i] = new Franchise();
Outlet[] myOutlet = new Outlet[10];
Outlet.readFile(outlet.getOutlet());
boolean invalidChoice = true;
while (invalidChoice) {
System.out.println("\nCreating a New Franchise...");
System.out.println(STARS);
System.out.println("Please select an outlet from the list below");
Outlet.displayUnassignedOutlets(outlet.getOutlet());
choice = console.nextInt();
if (choice < 0 || choice > 10) {
System.out.println("Error! Please choose a single number between 1 and 10");
}
else {
invalidChoice = false;
}
}
//System.out.println(j);
invalidChoice = true;
while (invalidChoice) {
System.out.println("Please enter the Phone Number for this Outlet");
choice = console.nextInt();
String phone = new String();
phone = new Integer(choice).toString();
if (phone.length() < 8 || phone.length() > 10) {
System.out.println("Error! Please enter 8 to 10 digits only");
}
else {
outlet[(choice - 1)].setPhoneNumber(choice);
invalidChoice = false;
}
}
invalidChoice = true;
while (invalidChoice) {
System.out.println("Please enter the category number for this Outlet");
choice = console.nextInt();
if (choice < 1 || choice > 4) {
System.out.println("Error! Please choose a single number between 1 and 4");
}
else {
outlet.setCategory(choice);
invalidChoice = false;
}
}
invalidChoice = true;
while (invalidChoice) {
System.out.println("Please enter the Operating Days for this Outlet");
choice = console.nextInt();
if (choice < 5 || choice > 7) {
System.out.println("Error! Please choose a single number between 5 and 7");
}
else {
outlet.setOperatingDays(choice);
invalidChoice = false;
}
}
//Applicant chosenApplicant = new Applicant();
//Applicant.readFile(chosenApplicant.getApplicant());
//Applicant.checkCriteria(chosenApplicant.getApplicant());
//System.out.println("This Franchise has been assigned to : " +
//chosenApplicant.displayOneEligibleApplicant());
Outlet.displayUnassignedOutlets(outlet.getOutlet());
}
public static void main(String[] args) throws FileNotFoundException {
Franchise start = new Franchise();
Franchise.testing(start.getFranchise());
//Franchise.createFranchise(start.getFranchise());
//Franchise.displaySalesForcast();
//Franchise.displayAllFranchises(start.getOutlet());
}
}