Я в основном получаю незаконные запуски выражений и точек с запятой, ожидаемых до фигурных скобок в моих методах. Мне действительно нужна какая-то помощь в этом, поскольку я не могу, чтобы жизнь меня определяла, почему у меня так много ошибок
public class stockItem{
//Declaring Variables
private double price;
private boolean delivery;
private static int counter = 0;
// New array with all the catagories stored in it
private String[] catagoryArray = new String[] {"furniture", "silver", "mirrors", "jewelry", "miscellaneous"};
private String Catagory;
private String code;
//default constructor for stockItem
public stockItem(){
price = 0;
Catagory = catagoryArray[0];
delivery = false;
counter++;
code = "default";
}
// Constructor for stockItem made by the user with all nessacey inputs
public stockItem(String Catagory, double price, boolean delivery){
for (int i =0; i < catagoryArray.length; i++){
if (Catagory.equalsIgnoreCase(catagoryArray[i])){
this.Catagory = catagoryArray[i];
} else {
System.out.println("This is not a valid catagory!");
}
// Variables overwrite old variables from default stockItem
this.price = price;
this.delivery = delivery;
counter++;
code = code();
}
// Generates codes for each catagory and each item created
private String getCode(){
String furnCode = catagoryArray[0].substring(0,4) + counter;
String silverCode = catagoryArray[1].substring(0,4) + counter;
String mirrorsCode = catagoryArray[2].substring(0,4) + counter;
String jewelry = catagoryArray[3].substring(0,4) + counter;
String miscCode = catagoryArray[4].substring(0,4) + counter;
}
//Methods
//Returns price of an object
public double getPrice(){
return price;
}
//Returns true of false for delivery of an object
public boolean getDelivery(){
return delivery;
}
//Returns the counter value
public int getCounter(){
return counter;
}
// Returns the catagory of an object
public String getCatagory(){
return Catagory;
}
// gets code of particular item
public String getCode(){
return code;
}
// Returns the amount of objects created
public int objectAmount(){
return counter;
}
//changes price of an object when called
public void changePrice(double Price){
this.price = Price;
}
// changes delivery option
public void changeDelivery(boolean changeDelivery){
this.delivery = changeDelivery;
}
// Changes catagory of an object
public void changeCatagory(String changeCatagory){
this.Catagory = changeCatagory;
}
//Prints a string containing all information about an object
public String changeToString(){
String Print = "Catagory:" + Catagory + "Price:" + price + "Amount Created" + counter + "Code" + code;
return Print;
}
}
}
Это выглядит не так:
code = code(); // where is code() defined?
Ваш getCode()
также выглядит не так, поскольку он имеет возвращаемое значение, но ничего не возвращает.
В заключение:
//Prints a string containing all information about an object
public String changeToString(){
String Print = "Catagory:" + Catagory + "Price:" + price + "Amount Created" + counter + "Code" + code;
return Print;
} // this should be removed
}
getCode()
возвращал что-то. добавить return ...
в конце этого метода.
В дополнение к тому, что было указано Эраном, также есть непревзойденная фигурная фигурная скобка. В цикле for отсутствует конечная фигурная скобка.
public stockItem(String Catagory, double price, boolean delivery){
for (int i =0; i < catagoryArray.length; i++){
if (Catagory.equalsIgnoreCase(catagoryArray[i])){
this.Catagory = catagoryArray[i];
} else {
System.out.println("This is not a valid catagory!");
}
} // This one.
// Variables overwrite old variables from default stockItem
// ... code ...
}
Ниже приведен действующий код. Мало того, что нужно отметить:
Как уже упоминалось, вы должны рассмотреть возможность использования некоторых IDE.
public class StockItem {
//Declaring Variables
private double price;
private boolean delivery;
private static int counter = 0;
// New array with all the catagories stored in it
private String[] catagoryArray = new String[] {"furniture", "silver", "mirrors", "jewelry", "miscellaneous"};
private String Catagory;
private String code;
//default constructor for StockItem
public StockItem() {
price = 0;
Catagory = catagoryArray[0];
delivery = false;
counter++;
code = "default";
}
// Constructor for StockItem made by the user with all nessacey inputs
public StockItem(String Catagory, double price, boolean delivery) {
for (int i = 0; i < catagoryArray.length; i++) {
if (Catagory.equalsIgnoreCase(catagoryArray[i])) {
this.Catagory = catagoryArray[i];
} else {
System.out.println("This is not a valid catagory!");
}
} // not sure if the closing bracket should be here
// Variables overwrite old variables from default StockItem
this.price = price;
this.delivery = delivery;
counter++;
code = getCode();
}
// Generates codes for each catagory and each item created
private String getCode1() {
String furnCode = catagoryArray[0].substring(0, 4) + counter;
String silverCode = catagoryArray[1].substring(0, 4) + counter;
String mirrorsCode = catagoryArray[2].substring(0, 4) + counter;
String jewelry = catagoryArray[3].substring(0, 4) + counter;
String miscCode = catagoryArray[4].substring(0, 4) + counter;
return ""; // this method should return a string!
}
//Methods
//Returns price of an object
public double getPrice() {
return price;
}
//Returns true of false for delivery of an object
public boolean getDelivery() {
return delivery;
}
//Returns the counter value
public int getCounter() {
return counter;
}
// Returns the catagory of an object
public String getCatagory() {
return Catagory;
}
// gets code of particular item
public String getCode() {
return code;
}
// Returns the amount of objects created
public int objectAmount() {
return counter;
}
//changes price of an object when called
public void changePrice(double Price) {
this.price = Price;
}
// changes delivery option
public void changeDelivery(boolean changeDelivery) {
this.delivery = changeDelivery;
}
// Changes catagory of an object
public void changeCatagory(String changeCatagory) {
this.Catagory = changeCatagory;
}
//Prints a string containing all information about an object
public String changeToString() {
String Print = "Catagory:" + Catagory + "Price:" + price + "Amount Created" + counter + "Code" + code;
return Print;
}
}