Q:

Write a java program to run a gaming store. The program manages the store inventory, sells games to customers, and print their bills. Every game has a price

0

King Saud University College of Computer & Information Science

CSC111 – Lab05

Loops

Write a java program to run a gaming store. The program manages the store inventory, sells games to customers, and print their bills. Every game has a price. The user can add games to the store and sell games to customers. The program must compute and print the total bill for the customer. A customer can buy as many games as he wants but must not exceed the number of games in the store. Each customer is entitled to a 25% discount if he buys more than 2 games.

Your program should display a menu with three options: add games, sell games and exit as shown below:

***************************************************************

* Welcome to Gaming Center :) *

* -------------------------------------- *

* Please enter one of the following options: *

* 1) Add ==> this allows you to add a game to inventory *

* 2) Sell ==> this allows you to sell games to a customer *

* 3) Exit ==> to end this program *

*                                                                                             *

*************************************************************** 

 

  • If the user chooses Add then he will be able to add new games to inventory. When adding a game, the user needs to provide the game price only. Adding ends when the user enters -1 as the price.
  • If the user chooses Sell then program should start reading the sold games prices one after another until the user enter -1 as the price. The program should then print the total bill. You must make sure that a customer cannot buy more games than what is in the store. Also, you should make sure the discount is applied (if any) before printing the bill.
  • If the user chooses Exit. Your program should print “Goodbye” and exit.

 

When the program starts, the number of games in the store is zero. The user must add games before he starts selling. If the user attempts to sell games with zero inventory the program must print an appropriate message.

(Note: at this stage you do not have to match prices when selling and adding since you need arrays for this.)

Name your class GameStore.

Note: Unlike primitive data types like int and double, to compare two String variables s1 and s2 use s1.equals(s2) or s1.equalsIgnoreCase(s2). Do NOT use s1 == s2. Here is a sample run to show different cases:

Sample Run

******************************************************************

*                        Welcome to Gaming Center :)                           *

*                    --------------------------------------                       *

* Please enter one of the following options: *

* 1) Add ==> this allows you to add a game to inventory *

* 2) Sell ==> this allows you to sell games to a customer *

* 3) Exit ==> to end this program *

*                                                                                                  *

*****************************************************************

Enter your option :> sell ↵

Sorry. There are no more games in the store :(

*****************************************************************

*                        Welcome to Gaming Center :)                           *

*                   ------------------------------------                           *

* Please enter one of the following options: *

* 1) Add ==> this allows you to add a game to inventory *

* 2) Sell ==> this allows you to sell games to a customer *

* 3) Exit ==> to end this program *

*                                                                                                *

*****************************************************************

Enter your option :> add ↵

Please enter game price (-1 to end): 200 ↵

Please enter game price (-1 to end): 300 ↵

Please enter game price (-1 to end): 450 ↵

Please enter game price (-1 to end): 250 ↵

Please enter game price (-1 to end): 100 ↵

Please enter game price (-1 to end): -1 ↵

*****************************************************************

*                            Welcome to Gaming Center :)                       *

*                       ----------------------------------                       *

* Please enter one of the following options: *

* 1) Add ==> this allows you to add a game to inventory *

* 2) Sell ==> this allows you to sell games to a customer *

* 3) Exit ==> to end this program *

*                                                                                                *

*****************************************************************

Enter your option :> sell ↵

Please, enter game price (-1 to end): 200 ↵

Please, enter game price (-1 to end): 200 ↵

Please, enter game price (-1 to end): -1 ↵

Total price before discount: 400.0 SR Your discount is: 0.0 SR Total price after discount: 400.0 SR

*****************************************************************

*                         Welcome to Gaming Center :)                          *

*                    ----------------------------------                          *

* Please enter one of the following options:                              *

* 1) Add ==> this allows you to add a game to inventory             *

* 2) Sell ==> this allows you to sell games to a customer              *

* 3) Exit ==> to end this program *

*                                                                                                  *

*****************************************************************

Enter your option :> add ↵

Please enter game price (-1 to end): 100 ↵

Please enter game price (-1 to end): 2000 ↵

Please enter game price (-1 to end): 150 ↵

Please enter game price (-1 to end): -1 ↵

*****************************************************************

*                             Welcome to Gaming Center :)                   *

*                        ----------------------------------                      *

* Please enter one of the following options: * * 1) Add ==> this allows you to add a game to inventory * * 2) Sell ==> this allows you to sell games to a customer * * 3) Exit ==> to end this program *

*                                                                                               *

****************************************************************

Enter your option :> sell ↵

Please, enter game price (-1 to end): 300 ↵

Please, enter game price (-1 to end): 300 ↵

Please, enter game price (-1 to end): 350 ↵

Please, enter game price (-1 to end): 600 ↵

Please, enter game price (-1 to end): 500 ↵

Please, enter game price (-1 to end): 300 ↵

Can not sell more games. Out of stock :( Total price before discount: 2350.0 SR Your discount is: 587.5 SR Total price after discount: 1762.5 SR

***************************************************************

*                       Welcome to Gaming Center :)                      *

*                       --------------------------------                       *

* Please enter one of the following options: *

* 1) Add ==> this allows you to add a game to inventory *

* 2) Sell ==> this allows you to sell games to a customer *

* 3) Exit ==> to end this program *

*                                                                                            *

**************************************************************

Enter your option :> sell ↵

Sorry. There are no more games in the store :(

**************************************************************

*                      Welcome to Gaming Center :)                      *

*                  --------------------------------------                  *

* Please enter one of the following options: *

* 1) Add ==> this allows you to add a game to inventory *

* 2) Sell ==> this allows you to sell games to a customer *

* 3) Exit ==> to end this program *

*                                                                                          *

*************************************************************

Enter your option :> exit ↵

Thanks Goodbye !

 

Create a new class and name it GameStore. Make sure you choose the public static void main option.

When you are done, save your program and run it. Make sure it prints the output as shown above

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
public class GameStoreWhile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String option;
int numberOfGames=0;
System.out.println("**********************************************************************");
System.out.println("* Welcome to Gaming Center :) *");
System.out.println("* --------------------------- *");
System.out.println("* Please enter one of the following options: *");
System.out.println("* 1) Add ==> this allows you to add a game to inventory *");
System.out.println("* 2) Sell ==> this allows you to sell games to a customer *");
System.out.println("* 3) Exit ==> to end this program *");
System.out.println("* *");
System.out.println("***********************************************************************");
option = input.next();
while (!option.equalsIgnoreCase("exit")) {
if (option.equalsIgnoreCase("add"))
{
System.out.print("Please enter game price (-1 to end): ");
double price = input.nextDouble();
while (price!=-1)
{ 
numberOfGames++;
System.out.print("Please enter game price (-1 to end): ");
price = input.nextDouble();
}
}
else if (option.equalsIgnoreCase("sell"))
if (numberOfGames==0)
System.out.println("Sorry. There are no more games in the store :(");
else
{
double total=0;
int count=0;
System.out.print("Please, enter game price (-1 to end): ");
double price = input.nextDouble();
while (price != -1 && numberOfGames > 0 )
{ 
total = total + price;
numberOfGames--;
count++;
if (numberOfGames==0)
System.out.println("Can not sell more games. Out of stock :(");
else { System.out.print("Please enter game price (-1 to end): ");
price = input.nextDouble();
}
}
double discount; 
if (count > 2 ) discount = 0.25 * total;
else discount = 0;
System.out.println("Total price before discount: "+total+"SR");
System.out.println("Your discount is: "+discount+"SR");
total = total - discount;
System.out.println("Total price after discount: "+total+"SR");
}
System.out.println("**********************************************************************");
System.out.println("* Welcome to Gaming Center :) *");
System.out.println("* --------------------------- *");
System.out.println("* Please enter one of the following options: *");
System.out.println("* 1) Add ==> this allows you to add a game to inventory *");
System.out.println("* 2) Sell ==> this allows you to sell games to a customer *");
System.out.println("* 3) Exit ==> to end this program *");
System.out.println("* *");
System.out.println("***********************************************************************");
System.out.print("Enter your option :> ");
option = input.next();
} 
System.out.println("Thanks Goodbye !");
input.close();
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now