Develop a program that allows the customer to buy online from an electronics sto
ID: 3542815 • Letter: D
Question
Develop a program that allows the customer to buy online from an electronics store. The program starts by displaying the following message:
<<Welcome to BestBuy Electronics Store>>
Buy more than 500 KD and get a promotion
The program asks the customers to enter the number of items they want to buy, and then it displays the following menu which shows the available items in the store and the price of each item:
1. Desktop Computer 120 KD
2. Laptop 570 KD
3. Printer 55.900 KD
4. Scanner 60.500 KD
The customer should enter the item number as shown in the sample output. At the end, the program displays the total price of all purchased items. If the total price exceeds 500 KD, the program chooses a promotion for the customer. There are three kinds of promotions the store offers to the customers:
1. Free Mobile 2. Free MP3 Player 3. Free Camera
The program consists of two classes: OnlineStore and OnlineStoreTest. Class OnlineStore includes two instance variables: number (type int) and totalPrice (type double). It contains following methods:
? public OnlineStore ( int num): The class constructor. It initializes the instance variable number, and set totalPrice to zero.
? public void buy(): This method displays the menu and asks the user to enter the items' numbers. After that, the method displays the total price. If the user enters invalid choice, an error message should be displayed asking the user to re-enter the item number. Use a switch statement. If the total price is grater than 500 KD, the method calls method promotion().
? public void promotion (): This method selects a promotion for the user:
If the total price >= 1000: The customer wins a mobile
If the total price >= 750: The customer wins a MP3 player
Else: The customer wins a camera.
Sample Output 1:
<<Welcome to BestBuy Electronics Store>>
Buy more than 500 KD and get a promotion
How many items do you want to buy? 3
Enter the item number:
1.Desktop Computer 120 KD
2.Laptop 570 KD
3.Printer 55.900 KD
4.Scanner 60.500 KD
Item 1: 1
Item 2: 2
Item 3: 2
The total price: 1260.000 KD
The total price +500 KD
You win a Mobile
Explanation / Answer
import java.io.*;
public class OnlineStore
{
int number;
double totalPrice;
public OnlineStore(int num)
{
number = num;
totalPrice = 0;
}
public void buy()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("<<Welcome to BestBuy Electronics Store>>");
System.out.println("Buy more than 500 KD and get a promotion");
System.out.print("How many items do you want to buy? ");
number = Integer.parseInt(in.readLine());
OnlineStore obj = new OnlineStore(number);
System.out.println(" Enter the item number:");
System.out.println("1.Desktop Computer 120 KD");
System.out.println("2.Laptop 570 KD");
System.out.println("3.Printer 55.900 KD");
System.out.println("4.Scanner 60.500 KD");
for(int i=0; i<number; i++)
{
String tmp = in.readLine();
int itemNumber = 0, quantity = 0;
if(tmp.length() >= 8)
{
itemNumber = Integer.parseInt(tmp.charAt(5)+"");
quantity = Integer.parseInt(tmp.charAt(8)+"");
}
if((tmp.length() >=8)&&tmp.substring(0,4).equalsIgnoreCase("Item") && (itemNumber<4))
{
switch (itemNumber)
{
case 1:
obj.totalPrice += quantity * 120.0;
break;
case 2:
obj.totalPrice += quantity * 570.0;
break;
case 3:
obj.totalPrice += quantity * 55.900;
break;
case 4:
obj.totalPrice += quantity * 60.500;
break;
}
}
else
{
System.out.println("ERROR! Invalid choice. Try again.");
i--;
}
}
System.out.println("The total price : "+obj.totalPrice+" KD");
if(obj.totalPrice > 500)
{
System.out.println("The total Price + 500 KD");
promotion(obj.totalPrice);
}
}
public void promotion(double totalPrice)
{
if(totalPrice >= 1000)
System.out.println("You win a mobile");
else if(totalPrice >= 750)
System.out.println("You win a MP3 Player");
else
System.out.println("You win a camera");
}
}
class OnlineStoreTest
{
public static void main()throws IOException
{
OnlineStore object = new OnlineStore(0);
object.buy();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.