I'm struggling to call for a function that is located in a different package than my main class. The function I'm talking about is the one below:
wtchPrdct = createWatch(keyboard);
It was located within my main function to create a Watch object like below
Watch wtchPrdct = null;
Because my program became quite long as I was adding stuff to it, I wanted to take out some weight from my main class, and thus, I've created a package 'Utils' to which I've added my createWatch() function. However I struggle to call that function now. I get an 'error cannot find symbol' if I stick with my original line :
wtchPrdct = createWatch(keyboard);
And change the way I cal my method as such:
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);
but unfortunately got the same error.
I've forgot to mention that I've imported my package: import Utils.Create;
package SportswearProduct;
import DBClass.Model;
import java.sql.SQLException;
import java.util.*;
import java.sql.Date;
import Utils.Create;
public class SSD_CA2 {
public static void main(String[] args) throws SQLException {
System.out.println("\n================== MENU ==================");
do {
/* the user may choose beetwen CRUD interactions provided by the program */
System.out.println("\n1. Create a new Sportswear");
System.out.println("2. Read Watch");
System.out.println("3. Update Watch");
System.out.println("4. Delete Watch");
System.out.println("5. Exit");
System.out.print("\nEnter option: ");
/* optCommand interprets as a string the command from the user, parse it as an Integer
* & checks whether the command matches one of the CRUD interactions below */
String optCommand = keyboard.nextLine();
opt = Integer.parseInt(optCommand);
// The program prompts a command feedback
System.out.println("\nYou chose option " + opt);
// I use a do-while loop that iterates until the user prompts a correct command (see option 5)
switch (opt) {
case 1: {
System.out.println("\nWhat product would you like to add?");
System.out.println("1. A Watch");
System.out.println("2. Runners");
System.out.print("\nEnter option: ");
String createCmd = keyboard.nextLine();
int newOpt = Integer.parseInt(createCmd);
do {
switch (newOpt) {
case 1: {
System.out.println("\n================== Create Watch ==================");
/* spwrPrdct will store the data of an object watch created in the function createWatch to which we pass
* as a parameter the method Scanner() */
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);
// once returned, prdctWatch id passed to the addWatch() function in Model.java
model.addWatch(wtchPrdct);
break;
}
case 2: {
//same process as above
System.out.println("\n================== Create Runners ==================");
runPrdct = createRunners(keyboard);
model.addRunners(runPrdct);
break;
}
default:
if (opt > 2) {
/* if the user inputs a value greater than 5, instead of stoping, the program prompts an error message
* & iterates again */
System.out.println("\n================== ERROR ==================");
System.out.println("\nWrong command! select one or 2 :");
}
}
break;
} while (newOpt != 1 || newOpt != 2);
}
}
}
And here is my function:
package Utils;
import SportswearProduct.Sportswear;
import SportswearProduct.Runners;
import SportswearProduct.Watch;
import java.sql.Date;
import java.util.Scanner;
public class Create {
Watch wtchPrdct = null;
/* -------------------- CREATE WATCH FUNCTION -------------------- */
/* createWatch() passes Keyboard()so that the user may assign data to the object */
public static Watch createWatch(Scanner keyboard) {
/* the user enters data that is stored into a variable of matching datat type */
System.out.print("\nEnter the watch brand: ");
String brand = keyboard.nextLine();
System.out.print("Enter the date of sale (yyyy-mm-dd): ");
Date onSale = Date.valueOf(keyboard.nextLine());
...
Watch prdctWatch = new Watch(
brand,
onSale,
price,
movement,
chargingType,
batteryLife,
waterProof
);
return prdctWatch;
I cut the last part as it works and don't want to overwhelm the code.
Thanks for your help.
I think you have confused the grammar of Java and scripting languages like Pyhon.
Since your method createWatch
is declared in the class Create
, when you want to invoke that method you should:
Either invoke that method by using an instance of class
Create
:wtchPrdct = wtchPrdct.createWatch(keyboard);
or invoke that method by setting the method static and invoke it by using class name:
wtchPrdct = Create.createWatch(keyboard);