I need to create a program for a TV store. This program should ask for the brand
ID: 3536890 • Letter: I
Question
I need to create a program for a TV store. This program should ask for the brand, screen zize, working condition, and the price of the TVs. This information will be store in an array list. Additionally, the user should be able to see the TVs stock in the store one at a time or view all the information at once. Finally, the user should be able to delete any TV in stock. This program is written in Java. Please comment each step so that I can learn how to program for the future. Also, include a brief summary at the end explaining step by step the process in making this program. Thank you very much for your assistance.
Explanation / Answer
Here is the working code for you. With comments and logic. Sample output is provided above
import java.util.ArrayList;
import java.util.Scanner;
public class TV {
private String brand; // variable brand of String type
private double screenSize; // variable screenSize of double type
private String workingCondition; // variable workingCondition of String Type
private double price;// variable price of double type
public TV() {
// default constructor
super();
}
public TV(String brand, double screenSize, String workingCondition,
double price)
{
//parameterized constructor
super();
this.brand = brand;
this.screenSize = screenSize;
this.workingCondition = workingCondition;
this.price = price;
}
public String getBrand() // ideal condition to code is to set the variables
// private and then produce getter and setter methods.
//so this is getter method for brand
{
return brand;
}
public void setBrand(String brand)
//setter method for brand
{
this.brand = brand;
}
public double getScreenSize()
// getter method for screen size
{
return screenSize;
}
public void setScreenSize(double screenSize)
// setter method for screen size
{
this.screenSize = screenSize;
}
public String getWorkingCondition()
// getter method for Working Condtion. I prefered string not boolean. So that you can write
// the status or error
{
return workingCondition;
}
public void setWorkingCondition(String workingCondition)
// setter method for working condition
{
this.workingCondition = workingCondition;
}
public double getPrice()
// getter method for price
{
return price;
}
public void setPrice(double price)
// setter method for price
{
this.price = price;
}
public static void main(String[] args)
// main method
{
ArrayList <TV> aList = new ArrayList<TV>();
//an arrayList named aList which is generic type of class TV
while (true)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Here is the menu for you");
System.out.println("Enter your choice");
System.out.println("1. Add a TV in to the list");
System.out.println("2. Display a single TV details");
System.out.println("3. Display all TV details");
System.out.println("4. Delete a TV detail from the list");
System.out.println("5. Exit the program");
int choice = scanner.nextInt();
//above statements will run in loop until you press exit i.e 5
//using scanner the value is stored
TV tvObj = new TV();// making a new object of class TV
if (choice == 5)
{
System.exit(1); // if input given by the user is 5, terminate the program
}
switch(choice) // else check for other things
{
// this case will take care of adding a new TV in to the list
case 1 : System.out.println(" You selected choice 1");
System.out.println("You can now add a TV");
System.out.println("Enter the brand of tv");
String brand = scanner.next();
tvObj.setBrand(brand);// storing the value of brand taken by user into the object
System.out.println("Enter the screen size of the TV");
double screenSize = scanner.nextDouble();
tvObj.setScreenSize(screenSize); //storing the value of screen size taken by user into the object
System.out.println("Enter the working condition of the TV. ");
String workingCondition = scanner.next();
tvObj.setWorkingCondition(workingCondition);
//storing the value of working condition taken by user into the object
System.out.println("Enter the price of the TV");
double price = scanner.nextDouble();
tvObj.setPrice(price);
//storing the value of price taken by user into the object
aList.add(tvObj); // finally, when the object has stored the values of
// brand, working condition, size and price, the object is finally added into arraylist
System.out.println("Your TV is successfully added");
break;
case 2 : // this case will display one TV at once.
// I wrote a logic that will ask user to interact with it.
// if the user presses 1, it will show the next TV else it will not.
System.out.println("You selected choice 2");
System.out.println("You can display TV details one by one");
System.out.println("We have total " + aList.size() + " TVs available");
System.out.println("Press 1 if you want to see first TV else press 0 to exit");
int binaryChoice = scanner.nextInt(); // storing 0 or 1 from user.
// If user presses any other key, the program will be back to top of the main program
int j = 0;
if (binaryChoice == 0)
{
System.exit(1); // if choice given by the user is 0, then terminate the program
}
else if(binaryChoice == 1) // if the input by user is 1
{
System.out.println("First TV details is ");
System.out.println(aList.get(j)); // print the details of first tv.
// as j is initialized to 0 above. in the arraylist (aList), the first index stores
//the value of the firstly added object. So, we are printing the same
j++; // incrementing j, now value of j =1
while (j < aList.size()) // checking the condition that value of j should be less than size of
// the arraylist (aList), this loop will run till this condition is satisfied.
{
System.out.println(" Do you want to see next TV details?");
System.out.println("Press 1 if you want to see next TV else press 0 to exit");
binaryChoice = scanner.nextInt();
if (binaryChoice == 0)
{
System.exit(1); // if choice given by the user is 0, then terminate the program
}
else if(binaryChoice == 1)
{
System.out.println(" Next TV Details are");
System.out.println(aList.get(j)); // printing the value of next object. i.e object at 2nd
// location. first object was stored at aList[0]. second is stored on aList[1].
// here value of j was 1.
j++; // incrementing the value of j and loop continues.
}
else if(j == aList.size())
// if the value of j becomes equal to the size of the arrayList, then printing a message
{
System.out.println("No more elements available");
}
else
{
System.out.println("Enter a valid choice"); // for inside if
}
}
}
else
{
System.out.println("Enter a valid choice"); // for outside if
}
break;
case 3 : System.out.println("You chose choice 3");
System.out.println("All the TVs in the list will be displayed to you.");
// displaying all the elements of the arrayList
// using for each loop
// here TV is the class, tvObj2 is a new object and aList is the arrayList
// so this loop itereates the object over the arrayList and print the value of the object every time
for(TV tvObj2:aList) {
System.out.println(tvObj2);
}
break;
case 4 : // here I applied an entirely new and different logic. It will be quite difficult for
// the user to delete a TV detail on basis of brand and price or anything else.
// So Here, I display all the TV details in the arraylist first.
// before displaying the elements of the arrayList, in every line you will find a sequence number
//which starts from 1. i.e first element has sequence number 1. second element has
//sequence number 2. To delete an element, all you need is to type in the sequence of that TV
// and you are done.
System.out.println("You chose for option 4");
System.out.println("By doing so you can delete a TV");
System.out.println("List of TVs are ");
int i =1;
for(TV tvObj2:aList)
//displaying the list of TVs once again. (same as above)
// but it displays a number before the object.
// that will be used to delete the object from the arrayList
{
System.out.println(i + ". " +tvObj2);
i++;
}
System.out.println("Enter the Sequence number of TV you want to delete");
int seq = scanner.nextInt();
aList.remove(seq-1); // as arrayList indexing start from 0 and not 1. it is done seq -1;
// for example, suppose you selected line number 1 to be deleted. that is stored in arrayList
// in 0th index.
break;
default : System.out.println("Make a valid choice");
}
}
}
@Override
public String toString()
// to string function to display the output in readable form.
{
return "TV [brand=" + brand + ", screenSize=" + screenSize
+ ", workingCondition=" + workingCondition + ", price=" + price
+ "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.