So I have a method that displays the information from my arrays. I want to have another method where I can add information to those arrays. You can see in the code how I am trying to do this. I am trying to use the addTV() method to create a TV and put it in the array which is in displayTVs().
package harveynorman;
import java.util.Scanner;
/**
* Created by tiern on 11/06/2017.
*/
public class HarveyNorman
{
public static void main(String args[])
{
displayTVs();
}
public static void addTV()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the product code: ");
String productCode = scanner.nextLine();
if (productCode.startsWith("UE")) {
modelNo.add(productCode);
}
}
public static void displayTVs()
{
String[] modelNo = new String[]{"UE43MU5500", "UE49MU6200", "UE50MU6100", "UE55MU6100", "UE55MU6200", "UE55MU6500", "UE55MU7000", "UE55MU9000", "UE65MU6100", "UE65MU6100"};
int[] quantity = new int[] {1, 2, 1, 1, 1, 2, 1, 2, 2, 2};
int[] price = new int[] {729, 949, 929, 1079, 1199, 1299, 1739, 2394, 2399, 2759};
String[] location = new String[] {"Opposite Samsung Stand", "Samsung Stand", "6th from Left, Bottom Row", "Top Row, 2nd from Right", "Top Row, Second from Left", "N/A", "Samsung Stand", "Samsung Stand", "N/A", "N/A"};
System.out.print("SAMSUNG TVS");
System.out.println("--------------------");
for (int i = 0; i<modelNo.length; i++)
{
System.out.println(i+1 + ":\t" + "Model No:\t" + modelNo[i] + ".\tIn stock: " + quantity[i] + ".\t€ " + price[i] + "\tLocation:\t" + location[i] + ".");
}
}
}
Listobject to the two methods. One option is to define the List of televisions in the main() method. If you're not allowed to use aList, pass a fixed-size array to the two methods, and handle overflow. Also, you'll have to call addTV() somewhere.Televisionclass an have aTelevision[]rather than four separate arrays. I'd then probably return that fromdisplayTVs. I'd then consider using a list instead of an array.