Я делаю эту программу для цветочного магазина, и мне нужно хранить заказы в каком-то динамическом массиве, но мне нельзя использовать следующие массивы: ArrayList, вектор, HashMap и т. д. Я думал, что отсортировал их. создав массив с именемorders, но в моей IDE он продолжает говорить, что заказы не могут быть преобразованы в переменную... вот код, который у меня есть на данный момент. Я выделил проблемы с //
import java.util.Scanner;
public class MenuTest2B {
public final String[] FLOWERS = { "Choose your flower:", "Roses", "Lilys", "Carnations", "Daffodils", "Gerberas",
"Chrysanthemums", "Assorted" };
public final double[] FLOWER_PRICES = { 0, 1.2, 1.3, 1.0, 1.0, 1.1, 1.1, 0.8 };
// create colours + price arrays
public final String[] COLOURS = { "Choose your colour:", "White", "Red", "Pink", "Yellow", "Blue", "Mixed" };
public final double[] COLOUR_PRICES = { 0, 1.3, 1.2, 1.1, 1.1, 1.2, 1.0 };
// create size + price arrays
public final String[] SIZES = { "Choose your size:", "Small", "Medium", "Large" };
public final double[] SIZE_PRICES = { 0, 5.5, 7.5, 9.5 };
private Scanner scanner;
public final String[] menu = { "Main Menu", "1. Order a bouquet and get the price.", "2. Display statistics",
"3. Exit" };
public void exec() {
scanner = new Scanner(System.in);
int choice;
do {
displayMenu(menu);
choice = getUserChoice();
switch (choice) {
case 1:
// use scanner to display menu options for flowers colours and size
orderDetailsAndPriceCalculation();
break;
case 2:
summaryStatistics();
break;
case 3:
System.out.println("You are exiting the program... Goodbye");
scanner.close();
break;
default:
System.out.println("Invalid input. Please select a valid option.");
}
} while (choice != 3);
}
public void orderDetailsAndPriceCalculation() {
// adds values for chosen ammounts for orders and puts them into the scanner
System.out.println("");
displayMenu(FLOWERS);
int flowerChoice = getUserChoice();
if (flowerChoice == -1) {
return;
}
displayMenu(COLOURS);
int colourChoice = getUserChoice();
if (colourChoice == -1) {
return;
}
displayMenu(SIZES);
int sizeChoice = getUserChoice();
if (sizeChoice == -1) {
return;
}
// calculates the input order details (Flower+Colour)*Size
double totalPrice = (FLOWER_PRICES[flowerChoice - 1]
+ COLOUR_PRICES[colourChoice - 1])
* SIZE_PRICES[sizeChoice - 1];
// PRINT TOTAL AMOUNT
System.out.println("Bouquet Price: £" + totalPrice);
// Stores the new orders details in the 2d orders array
orders[orderCount][0] = flowerChoice; // this is where the issue is
orders[orderCount][1] = colourChoice; // this is where the issue is
orders[orderCount][2] = sizeChoice; // this is where the issue is
orders[orderCount][3] = totalPrice; // this is where the issue is
orderCount++; // this is where the issue is
}
public void summaryStatistics() {
System.out.println("Summary statistics provided");
}
private void displayMenu(String obj[]) {
for (int i = 0; i < obj.length; i++) {
if (i == 0) {
System.out.println(obj[i]);
} else {
System.out.println(i + ". " + obj[i]);
}
}
}
int getUserChoice() {
try {
return scanner.nextInt();
} catch (Exception e) {
return -1;
}
}
public static void main(String[] args) {
MenuTest2B mt2 = new MenuTest2B();
mt2.exec();
}
}
Я пробовал добавить int и строку, но, честно говоря, это ничего не дало. Мне нужно, чтобы выбор порядка каким-то образом сохранялся, чтобы при выборе меню 2 отображалось статистика. Эта часть уже готова, и мне просто нужно добавить ее в код, как только заказы каким-то образом будут сохранены.
Я делаю эту программу для цветочного магазина, и мне нужно хранить заказы в каком-то динамическом массиве, но мне нельзя использовать следующие массивы: ArrayList, вектор, HashMap и т. д. Я думал, что отсортировал их. создав массив с именемorders, но в моей IDE он продолжает говорить, что заказы не могут быть преобразованы в переменную... вот код, который у меня есть на данный момент. Я выделил проблемы с // [code]import java.util.Scanner;
public class MenuTest2B { public final String[] FLOWERS = { "Choose your flower:", "Roses", "Lilys", "Carnations", "Daffodils", "Gerberas", "Chrysanthemums", "Assorted" }; public final double[] FLOWER_PRICES = { 0, 1.2, 1.3, 1.0, 1.0, 1.1, 1.1, 0.8 }; // create colours + price arrays public final String[] COLOURS = { "Choose your colour:", "White", "Red", "Pink", "Yellow", "Blue", "Mixed" }; public final double[] COLOUR_PRICES = { 0, 1.3, 1.2, 1.1, 1.1, 1.2, 1.0 }; // create size + price arrays public final String[] SIZES = { "Choose your size:", "Small", "Medium", "Large" }; public final double[] SIZE_PRICES = { 0, 5.5, 7.5, 9.5 }; private Scanner scanner; public final String[] menu = { "Main Menu", "1. Order a bouquet and get the price.", "2. Display statistics", "3. Exit" };
public void exec() { scanner = new Scanner(System.in); int choice; do { displayMenu(menu); choice = getUserChoice(); switch (choice) { case 1: // use scanner to display menu options for flowers colours and size orderDetailsAndPriceCalculation(); break; case 2: summaryStatistics(); break; case 3: System.out.println("You are exiting the program... Goodbye"); scanner.close(); break; default: System.out.println("Invalid input. Please select a valid option."); } } while (choice != 3); }
public void orderDetailsAndPriceCalculation() { // adds values for chosen ammounts for orders and puts them into the scanner System.out.println(""); displayMenu(FLOWERS); int flowerChoice = getUserChoice(); if (flowerChoice == -1) { return; } displayMenu(COLOURS); int colourChoice = getUserChoice(); if (colourChoice == -1) { return; } displayMenu(SIZES); int sizeChoice = getUserChoice(); if (sizeChoice == -1) { return; } // calculates the input order details (Flower+Colour)*Size double totalPrice = (FLOWER_PRICES[flowerChoice - 1] + COLOUR_PRICES[colourChoice - 1]) * SIZE_PRICES[sizeChoice - 1]; // PRINT TOTAL AMOUNT
// Stores the new orders details in the 2d orders array orders[orderCount][0] = flowerChoice; // this is where the issue is orders[orderCount][1] = colourChoice; // this is where the issue is orders[orderCount][2] = sizeChoice; // this is where the issue is orders[orderCount][3] = totalPrice; // this is where the issue is orderCount++; // this is where the issue is }
public void summaryStatistics() { System.out.println("Summary statistics provided"); }
private void displayMenu(String obj[]) { for (int i = 0; i < obj.length; i++) { if (i == 0) { System.out.println(obj[i]); } else { System.out.println(i + ". " + obj[i]); } } }
public static void main(String[] args) { MenuTest2B mt2 = new MenuTest2B(); mt2.exec(); } } [/code] Я пробовал добавить int и строку, но, честно говоря, это ничего не дало. Мне нужно, чтобы выбор порядка каким-то образом сохранялся, чтобы при выборе меню 2 отображалось статистика. Эта часть уже готова, и мне просто нужно добавить ее в код, как только заказы каким-то образом будут сохранены.