import java.util.*;
public class Theater {
public static void main(String[] args) {
char[][] seats = new char [15][30];
double[] prices = new double [15];
Scanner key = new Scanner(System.in);
for (int i=1; i<seats.length; i++) {
for (int j=1; j<seats[0].length; j++) {
seats[i][j]='#';
}
}
for (int i=1; i<=15; i++) {
System.out.println("Please enter price for Row: #"+ i);
prices[i-1]=key.nextDouble();
if (prices[i-1]<0) {
System.out.println("Price should be a positive number");
i--;
}
}
displayMenu();
int seleccion=getChoice(key);
while (seleccion!=5) {
if (seleccion==1) {
System.out.println("Asientos disponibles # , Asientos ocupados *");
for (int i=0; i<seats.length; i++) {
for (int j=0; j<seats[0].length; j++) {
System.out.print(seats[i][j]+" ");
}
System.out.println();
}
}
else if (seleccion==2) {
displayPrices(prices);
}
else if (seleccion==3) {
double totalsales = 0;
System.out.println("Ticket sales:");
}
else if (seleccion==4) {
purchaseTicket(seats[][]);
}
else {
System.out.println("Invalid selection");
}
displayMenu(); //call the menu
seleccion=getChoice(key);
}
}
public static int getChoice(Scanner key) {
int numsel=key.nextInt();
if (numsel<0 || numsel>5) {
System.out.println("The selection should be between 1-5");
displayMenu();
return getChoice(key);
}
return numsel;
}
public static void displayMenu() {
System.out.println("THEATER SOFTWARE MENU");
System.out.println("1. View Available Seats");
System.out.println("2. View Seating Prices");
System.out.println("3. View Ticket Sales ");
System.out.println("4. Purchase a Ticket");
System.out.println("5. Exit the Program");
System.out.print("ENTER YOUR CHOICE: ");
}
public static void displayPrices(final double prices[]) {
System.out.println("Prices per row:");
for (int i=1; i<=15; i++)
{
System.out.print("Row: #"+i+": ");
System.out.println(prices[i-1]);
}
}
public static void purchaseTicket(char seats[][], final double prices[]){
System.out.println("Please enter row where you want to seat");
Scanner key = new Scanner(System.in);
int rownum=key.nextInt();
int col=key.nextInt();
if (seats[rownum][col]=='#') {
seats[rownum][col]='*';
double totalsales=0;
totalsales=totalsales+prices[rownum];
System.out.println("You have been purchase seat"+rownum+col);
}
}
}
19 Replies - 1468 Views - Last Post: 22 March 2009 - 12:43 PM
#1
Please help with this THEATER programa
Posted 19 March 2009 - 02:45 PM
Replies To: Please help with this THEATER programa
#2
Re: Please help with this THEATER programa
Posted 19 March 2009 - 02:48 PM
#3
Re: Please help with this THEATER programa
Posted 19 March 2009 - 02:51 PM
#4
Re: Please help with this THEATER programa
Posted 19 March 2009 - 03:03 PM
#5
Re: Please help with this THEATER programa
Posted 19 March 2009 - 03:55 PM
Now we have this problem: I'm trying to to ask the user if they want to see aviable seats before buy, but we are having troobles.
This is my code now:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Manuel Santamaria
*/
import java.util.*;
public class Theater {
public static void main(String[] args) {
char[][] seats = new char [15][30];
double[] prices = new double [16];
double totalsales=0;
Scanner key = new Scanner(System.in);
for (int i=1; i<seats.length; i++) {
for (int j=1; j<seats[0].length; j++) {
seats[i][j]='#';
}
}
for (int i=1; i<=15; i++) {
System.out.println("Please enter price for Row: #"+ i);
prices[i]=key.nextDouble();
if (prices[i]<0) {
System.out.println("Price should be a positive number");
i--;
}
}
displayMenu();
int seleccion=getChoice(key);
while (seleccion!=5) {
if (seleccion==1) {
System.out.println("Asientos disponibles # , Asientos ocupados *");
for (int i=0; i<seats.length; i++) {
for (int j=0; j<seats[0].length; j++) {
System.out.print(seats[i][j]+" ");
}
System.out.println();
}
}
else if (seleccion==2) {
displayPrices(prices);
}
else if (seleccion==3) {
System.out.println("Ticket sales:"+totalsales);
}
else if (seleccion==4) {
purchaseTicket(seats, prices,totalsales);
}
else {
System.out.println("Invalid selection");
}
displayMenu(); //call the menu
seleccion=getChoice(key);
}
}
public static int getChoice(Scanner key) {
int numsel=key.nextInt();
if (numsel<0 || numsel>5) {
System.out.println("The selection should be between 1-5");
displayMenu();
return getChoice(key);
}
return numsel;
}
public static void displayMenu() {
System.out.println("THEATER SOFTWARE MENU");
System.out.println("1. View Available Seats");
System.out.println("2. View Seating Prices");
System.out.println("3. View Ticket Sales ");
System.out.println("4. Purchase a Ticket");
System.out.println("5. Exit the Program");
System.out.print("ENTER YOUR CHOICE: ");
}
public static void displayPrices(final double prices[]) {
System.out.println("Prices per row:");
for (int i=1; i<=15; i++)
{
System.out.print("Row: #"+i+": ");
System.out.println("Price:$"+prices[i]);
}
}
public static void purchaseTicket(char seats[][],double prices[],double totalsales){
System.out.println("Do you wish to view chart of available seats?");
String question;
question=key.nextLine();
if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
}
System.out.println("Please enter row where you want to seat");
Scanner key = new Scanner(System.in);
int rownum=key.nextInt();
if (rownum>15 || rownum>1){
System.out.println("Row need to be between 1-15");
displayMenu();
}
System.out.println("Please enter col number you like to seat");
int col=key.nextInt();
if (seats[rownum][col]=='#') {
seats[rownum][col]='*';
totalsales=totalsales+prices[rownum];
System.out.println("You have been purchase seat "+rownum+","+col);
}
else if (seats[rownum][col]=='*') {
System.out.println("This seat is already sold");
}
}
}
#6
Re: Please help with this THEATER programa
Posted 19 March 2009 - 04:08 PM
#7
Re: Please help with this THEATER programa
Posted 19 March 2009 - 04:19 PM
is the selecion 1
if (seleccion==1) {
System.out.println("Asientos disponibles # , Asientos ocupados *");
for (int i=0; i<seats.length; i++) {
for (int j=0; j<seats[0].length; j++) {
System.out.print(seats[i][j]+" ");
}
System.out.println();
}
}
We add this to the method;
public static void purchaseTicket(char seats[][],double prices[],double totalsales){
System.out.println("Do you wish to view chart of available seats?");
String question;
question=key.nextLine();
if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
}
So if the user press Y or y, it should show the users the chart of aviable/unavelable seats.
But we are having problem , maybe declaring the string inside the method.
#8
Re: Please help with this THEATER programa
Posted 19 March 2009 - 04:24 PM
#9
Re: Please help with this THEATER programa
Posted 19 March 2009 - 04:29 PM
We will put as statement that..
But it is failing before that.
If you run the program and you type number 4 on the main menu you will see
#11
Re: Please help with this THEATER programa
Posted 19 March 2009 - 06:16 PM
for (int i=1; i<=15; i++) {
System.out.println("Please enter price for Row: #"+ i);
prices[i]=key.nextDouble();
if (prices[i]<0) {
System.out.println("Price should be a positive number");
i--; // <-----------------------------------------------------------------------
Basic programming practice: NEVER change the index of a for loop inside the for loop
#12
Re: Please help with this THEATER programa
Posted 20 March 2009 - 07:21 PM
Purchase confirmed, you have been purchase seat Row:1,Seat:24
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Anyone know why i am gettins this error?
#13
Re: Please help with this THEATER programa
Posted 20 March 2009 - 07:23 PM
I'll use pbl's idiom...
Trying to make us fix a problem without posting the code is like telling your mechanic to fix your engine without looking under the hood.
This post has been edited by Locke: 20 March 2009 - 07:23 PM
#14
Re: Please help with this THEATER programa
Posted 20 March 2009 - 09:09 PM
at java.lang.String.charAt(String.java:687)
at Theater.purchaseTicket(Theater.java:172)
at Theater.main(Theater.java:84)
Java Result: 1
This is the code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Manuel Santamaria
*/
import java.util.*;
public class Theater {
static double[] totalsales= new double [1];
static String question;
public static void main(String[] args) {
char[][] seats = new char [16][31];
double[] prices = new double [16];
Scanner key = new Scanner(System.in);
for (int i=1; i<seats.length; i++) {
for (int j=1; j<seats[0].length; j++) {
seats[i][j]='#';
}
}
for (int i=1; i<=15; i++) {
System.out.println("Please enter price for Row: #"+ i);
prices[i]=key.nextDouble();
if (prices[i]<0) {
System.out.println("Price should be a positive number");
i--;
}
}
displayMenu();
int seleccion=getChoice(key);
while (seleccion!=5) {
if (seleccion==1) {
System.out.println("Seats available # , Seats Taken *");
for (int i=0; i<seats.length; i++) {
for (int j=0; j<seats[0].length; j++) {
System.out.print(seats[i][j]+" ");
}
System.out.println();
}
}
else if (seleccion==2) {
displayPrices(prices);
}
else if (seleccion==3) {
System.out.println("Ticket sales:"+totalsales[0]);
}
else if (seleccion==4) {
purchaseTicket(seats, prices,totalsales);
}
else {
System.out.println("Invalid selection");
}
displayMenu(); //call the menu
seleccion=getChoice(key);
}
}
public static int getChoice(Scanner key) {
int numsel=key.nextInt();
if (numsel<0 || numsel>5) {
System.out.println("The selection should be between 1-5");
displayMenu();
return getChoice(key);
}
return numsel;
}
public static void displayMenu() {
System.out.println("THEATER SOFTWARE MENU");
System.out.println("1. View Available Seats");
System.out.println("2. View Seating Prices");
System.out.println("3. View Ticket Sales ");
System.out.println("4. Purchase a Ticket");
System.out.println("5. Exit the Program");
System.out.print("ENTER YOUR CHOICE: ");
}
public static void displayPrices(final double prices[]) {
System.out.println("Prices per row:");
for (int i=1; i<=15; i++)
{
System.out.print("Row: #"+i+": ");
System.out.println("Price:$"+prices[i]);
}
}
public static void purchaseTicket(char seats[][], final double prices[], double totalsales[]){
System.out.println("Do you wish to view chart of available seats?");
Scanner key = new Scanner(System.in);
question=key.nextLine();
if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
System.out.println("Seats available # , Seats Taken *");
for (int i=0; i<seats.length; i++) {
for (int j=0; j<seats[0].length; j++) {
System.out.print(seats[i][j]+" ");
}
System.out.println();
}
}
System.out.println("Please enter row where you want to seat(1-15)");
int rownum=key.nextInt();
System.out.println("Please enter seat number you like to seat(1-30)");
int seat=key.nextInt();
if (seats[rownum][seat]=='#') {
seats[rownum][seat]='*';
totalsales[0]+=prices[rownum];
System.out.println("Purchase confirmed, you have been purchase seat Row:"+rownum+",Seat:"+seat);
System.out.println("Do you want to purchase another ticket?");
question=key.nextLine();
if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
purchaseTicket(seats, prices, totalsales);
}
}
else if (seats[rownum][seat]=='*') {
System.out.println("This seat is already sold");
System.out.println("Do you want to purchase another ticket?");
question=key.nextLine();
if (question.charAt(0)=='Y' || question.charAt(0)=='y'){
purchaseTicket(seats, prices, totalsales);
}
}
}
}
|
|

New Topic/Question
Reply




MultiQuote





|