Я изо всех сил пытаюсь вызвать функцию, которая находится в другом пакете, чем мой основной класс. Функция, о которой я говорю, приведена ниже:
wtchPrdct = createWatch(keyboard);
Она была расположена в моей основной функции для создания объекта Watch, как показано ниже
Watch wtchPrdct = null;
Поскольку моя программа работала довольно долго, так как я добавлял в нее что-то, я хотел взять некоторый вес из своего основного класса, и, таким образом, я создал пакет 'Utils', к которому я добавил моя функция createWatch () Однако я изо всех сил пытаюсь вызвать эту функцию сейчас. Я получаю сообщение «ошибка не может найти символ», если я придерживаюсь своей исходной строки:
wtchPrdct = createWatch(keyboard);
и изменяю способ, которым я называю свой метод, следующим образом:
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);
но, к сожалению, возникла та же ошибка.
Я забыл упомянуть, что импортировал свою посылку: 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);
}
}
}
И вот моя функция:
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;
Я вырезал последнюю часть, как она работает, и не хочу перегружать код.
Спасибо за вашу помощь.