Я получаю ошибку переполнения стека в строке 87, где я определяю HotDogStans. Это работало вчера, но сегодня не работает. Любая помощь по этой проблеме будет принята с благодарностью.
Я пытаюсь разработать программу для 3 хот-доги в Сан-Д iego. Метод justSold отслеживает, сколько продано каждым стендом, а частная переменная stati c отслеживает общие продажи в методе частного мутатора. Три «за» петли для трех стендов определяют, сколько продается каждый день. Программа HotDogStand:
Алгоритм и описание проблемы:
Алгоритм:
- Разработка 3 стоек хот-дога с идентификаторами.
- Узнайте, сколько хот-догов продано.
- Сообщите о всех проданных хот-догах.
Описание проблемы:
Вы управляете несколькими стойками для хот-догов, распределенными по всему Сану. D iego Округ. Определите класс с именем HotDogStand, в котором есть переменная экземпляра для идентификатора стенда хот-дога и переменная экземпляра для количества хот-догов, проданных стендом в тот день.
Создайте конструктор, который позволит пользователю класса инициализировать оба значения. Также создайте метод с именем justSold, который увеличивает на единицу количество хот-догов, проданных стендом. Идея заключается в том, что этот метод будет вызываться каждый раз, когда на стенде продается хот-дог, чтобы вы могли отслеживать общее количество хот-догов, проданных на стенде. Добавьте метод, который возвращает количество хот-догов, проданных стендом, а также метод equals и toString.
Наконец, в классе HotDogStand добавьте переменную stati c, которая отслеживает общее количество хот-доги, продаваемые всеми хот-догами, и метод stati c, который возвращает значение в этой переменной. Эта переменная должна содержать сумму всех продаж хот-дога, и не должна иметь метода установщика (мутатора) publi c, хотя она может иметь приватный (мутантный) метод мутатора.
В основном, test ваш класс HotDogStand, создав по крайней мере три стойки для хот-догов. Каждый стенд должен продавать разное количество хот-догов. Сделайте это, создав каждый экземпляр с разным начальным количеством хот-догов, проданных за день. Затем используйте петли «for» (по одной на каждую подставку), чтобы продавать разное количество хот-догов (не менее 20) в течение дня с помощью метода justSold.
По завершении отобразите количество проданных хот-догов по каждому стенду за день, а также по всем хот-догам, проданным всеми стендами с использованием переменной stati c.
Не забудьте включить ваш метод main в свой собственный файл класса.
public class HotDogStand
{
/**
* Instance variables
**/
private String stand1 = " " ;
private String stand2 = " " ;
private String stand3 = " " ;
private String localename = " " ;
private int ID = 0 ;
private static int stand1numberSold = 0 ;
private static int stand2numberSold = 0 ;
private static int stand3numberSold = 0 ;
private static int totalSold = 0 ;
private static int grandTotalSold = 0 ;
/**
* Full constructor sets three instance variables.
* @param name the name and locale of the HotDogStand
* @param ID the ID of the HotDogStand
* @param sold the number of hot dogs sold
**/
public HotDogStand(String localename, int ID, int numberSold)
{
this.localename = localename ;
this.ID = ID ;
this.stand1numberSold = stand1numberSold ;
this.stand2numberSold = stand2numberSold ;
this.stand3numberSold = stand3numberSold ;
//3 hot dog stand objects
HotDogStand stand1 = new HotDogStand ("Escondido hotdogstand", 0123, 5) ;
HotDogStand stand2 = new HotDogStand ("La Jolla hotdogstand", 4591, 3) ;
HotDogStand stand3 = new HotDogStand ("Oceanside hotdogstand", 1171, 10) ;
}
/**
* Getters and setters for the hot dog stands
*/
public String getlocaleName()
{
return localename ;
}
public void setlocaleName(String newlocaleName)
{
localename = newlocaleName ;
}
public int getID()
{
return ID ;
}
public void setID(int ID)
{
ID = ID ;
}
/**
* justSold methods
*/
public void stand1justSold()
{
if (stand1numberSold > 0)
{
stand1numberSold++ ;
}
}
public void stand2justSold()
{
if (stand2numberSold > 0)
{
stand2numberSold++ ;
}
}
public void stand3justSold()
{
if (stand3numberSold > 0)
{
stand3numberSold++ ;
}
}
public static void totalSold()
{
grandTotalSold = stand1numberSold + stand2numberSold + stand3numberSold ;
System.out.println("stand1.getlocaleName + stand1.getID + stand1numberSold") ;
System.out.println("stand2.getlocaleName + stand2.getID + stand2numberSold") ;
System.out.println("stand3.getlocaleName + stand3.getID + stand3numberSold") ;
System.out.println("Total sold for all stands = " + grandTotalSold) ;
}
public String toString()
{
System.out.println("Total sold for all stands = " + grandTotalSold) ;
//return stand1.getnumberSold + stand2.getnumberSold + stand3.getnumberSold ;
return Integer.toString(grandTotalSold) ;
}
public boolean equals()
{
return stand1 == stand2 ;
//return stand2 == stand3 ;
//return stand1 == stand3 ;
}
}
Это HotDogStandTester:
import java.util.Scanner;
/**
* HotDogStandTester has the main method. Each stand has a "for" loop, and the
* number of hot dogs sold by each stand and the grand total is displayed.
*
* @Quang Pham
* @Homework10- Project2 4/22/20
*/
public class HotDogStandTester
{
public static String stand1 = " " ;
public static String stand2 = " " ;
public static String stand3 = " " ;
public String localename = " " ;
private int ID = 0 ;
private static int stand1numberSold = 0 ;
private static int stand2numberSold = 0 ;
private static int stand3numberSold = 0 ;
private static int totalSold = 0 ;
private static int grandTotalSold = 0 ;
public static void main(String[] args)
{
//3 hot dog stand objects
HotDogStand stand1 = new HotDogStand ("Escondido hotdogstand" , 0123 , 5) ;
HotDogStand stand2 = new HotDogStand ("La Jolla hotdogstand" , 4591 , 3) ;
HotDogStand stand3 = new HotDogStand ("Oceanside hotdogstand" , 1171 , 10) ;
while(totalSold < 1000)
{
System.out.println("Welcome to the Hot Dog Stand Program!") ;
System.out.println("Please enter the number of hot dogs sold by the stands.") ;
Scanner keyboard = new Scanner(System.in) ;
System.out.println("Hotdog Stand 1") ;
int stand1numberSold = keyboard.nextInt() ;
System.out.println("Hotdog Stand 2") ;
int stand2numberSold = keyboard.nextInt() ;
System.out.println("Hotdog Stand 3") ;
int stand3numberSold = keyboard.nextInt() ;
System.out.println("HOT DOG TABULATION") ;
System.out.println("------------------") ;
System.out.println("Hot dogs sold by " + stand1.getlocaleName()) ;
for(int i = 0; i < 5; i++)
{
//stand1 = 20 ;
//stand1justSold() ;
System.out.println("Day" + i + stand1numberSold) ;
//return numberSold ;
}
System.out.println("Hot dogs sold by " + stand2.getlocaleName()) ;
for(int i = 0; i < 5; i++)
{
//stand2 = 10 ;
//stand2justSold() ;
System.out.println("Day" + i + stand2numberSold) ;
//return numberSold ;
}
System.out.println("Hot dogs sold by " + stand3.getlocaleName()) ;
for(int i = 0; i < 5; i++)
{
//stand3 = 15 ;
//stand3justSold() ;
System.out.println("Day" + i + stand3numberSold) ;
//return numberSold ;
}
int grandTotal = stand1numberSold + stand2numberSold + stand3numberSold ;
//System.out.println("The totals sold was " + totalSold()) ;
//System.out.println("The grand totalsold by all the stands was " + HotDogStand.toString()) ;
System.out.println( stand1.getlocaleName() + " sold a total of " + stand1numberSold + " hotdogs.") ;
System.out.println( stand2.getlocaleName() + " sold a total of " + stand2numberSold + " hotdogs.") ;
System.out.println( stand3.getlocaleName() + " sold a total of " + stand3numberSold + " hotdogs.") ;
System.out.println("The total number of hotdogs sold by all the stands was " + grandTotal) ;
} //end while
} //end main
} //end class