Simple ATM task, problems with array. - java

I want to overwrite the first element of my transaction array when it gets full. So when I print the array, it is always showing the latest transactions.
I think the problem is in the moveTrans method or the findNr method but I'm not sure and I can't figure out what is wrong.
Code:
import java.util.Scanner;
import java.util.*;
public class BankoTest {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int amount = 0;
int choice = 0;
int [] trans = new int[4];
int sum;
int balance = 0;
while (choice != 4)
{
choice = menu();
switch(choice)
{
case 1://
System.out.print("Deposit. Amount? :");
amount = scan.nextInt();
balance = balance + amount;
makeTransactions(trans, amount);
break;
case 2://
System.out.print("Withdra. Amount?");
amount = scan.nextInt();
balance = balance - amount;
makeTransactions(trans, -amount);
break;
case 3:
showTransactions(trans, balance);
break;
case 4:
System.out.println("Thank you. ");
break;
}
}
}
public static int menu()
{
Scanner scan = new Scanner(System.in);
int choice = 0;
System.out.println("1. Deposit ");
System.out.println("2. Withdraw ");
System.out.println("3. Saldo ");
System.out.println("4. End ");
System.out.println();
System.out.println("Choice: ");
choice = scan.nextInt();
return choice;
}
public static void showTransactions(int [] trans, int balance)
{
System.out.println();
System.out.println("Transactions summary :");
System.out.println();
for(int i = 0; i < trans.length-1; i++)
{
if(trans[i] == 0)
{
System.out.print("");
}
else
{
System.out.print(trans[i] + "\n");
balance = balance + trans[i];
}
}
// Printing saldo.
System.out.println();
System.out.println("Current balance: " + balance + " kr" + "\n" );
System.out.println();
}
//Puts amount last among the transactions that are stored in the array. Using the findNr method to find the first available spot
//in the array. moveTrans is used to make room for the new transaction when the array is full.
public static void makeTransactions(int [] trans, int amount )
{
int position = findNr(trans);
if(position == -1)
{
moveTrans(trans);
position = findNr(trans);
trans[position] = amount;
}
else
{
trans[position] = amount;
}
}
public static int findNr(int [] trans)
{
int position = -1;
for(int i = 0; i <= trans.length-1; i++)
{
if(trans[i] == 0)
{
position = i;
break;
}
}
return position;
}
public static void moveTrans(int [] trans)
{
for(int i = 0; i < trans.length-1; i++)
trans[0] = trans[i + 1] ;
}
}

just modify the following method alone
public static void makeTransactions(int[] trans, int amount) {
int position = findNr(trans);
if (position == -1) {
moveTrans(trans);
position = findNr(trans);
trans[position] = amount;
} else {
if (position != 0 && position == trans.length - 1) {
// shift the elements back
for (int i = 0; i < position; i++)
trans[i] = trans[i] + 1;
trans[position - 1] = amount;
}else
trans[position] = amount;
}
}

Please check this it may help some thing to you
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int amount = 0;
int choice = 0;
int[] trans = new int[4];
int sum;
int balance = 0;
while (choice != 4) {
choice = menu();
switch (choice) {
case 1://
System.out.print("Deposit. Amount? :");
amount = scan.nextInt();
balance = balance + amount;
makeTransactions(trans, amount);
break;
case 2://
System.out.print("Withdra. Amount?");
amount = scan.nextInt();
balance = balance - amount;
makeTransactions(trans, -amount);
break;
case 3:
showTransactions(trans, balance);
break;
case 4:
System.out.println("Thank you. ");
break;
}
}
}
public static int menu() {
Scanner scan = new Scanner(System.in);
int choice = 0;
System.out.println("1. Deposit ");
System.out.println("2. Withdraw ");
System.out.println("3. Saldo ");
System.out.println("4. End ");
System.out.println();
System.out.println("Choice: ");
choice = scan.nextInt();
return choice;
}
public static void showTransactions(int[] trans, int balance) {
System.out.println();
System.out.println("Transactions summary :");
System.out.println();
for (int i = 0; i < trans.length - 1; i++) {
if (trans[i] == 0) {
System.out.print("");
}
else {
System.out.print(trans[i] + "\n");
// balance = trans[i];
}
}
// Printing saldo.
System.out.println();
System.out.println("Current balance: " + balance + " INR" + "\n");
System.out.println();
}
// Puts amount last among the transactions that are stored in the array.
// Using the findNr method to find the first available spot
// in the array. moveTrans is used to make room for the new transaction when
// the array is full.
public static void makeTransactions(int[] trans, int amount) {
int position = findNr(trans);
if (position == -1) {
moveTrans(trans);
System.out.println("Your transaction limit is over.");
// position = findNr(trans);
// System.out.println("Position -------> "+position);
// trans[position] = amount;
} else {
trans[position] = amount;
}
}
public static int findNr(int[] trans) {
int position = -1;
for (int i = 0; i <= trans.length - 1; i++) {
if (trans[i] == 0) {
position = i;
break;
}
}
return position;
}
public static void moveTrans(int[] trans) {
System.out.println("------Your Transaction Details----------");
for (int i = 0; i < trans.length; i++) {
System.out.println("Transation " + (i + 1) + " :: " + trans[i]);
trans[0] = trans[i];
}
System.out.println("----------------------------------------");
}

Related

Creating a method which specifically calculates the average of the users entered values

I am making a program which allows the user to look at student's grades, find the average, find the highest grade, the lowest etc. For one of the methods I have, it checks for the average of the values that the user entered. I tried to do this but to no avail. Here is the specific code:
public static void classAvg(int numOfKids) {
int average = 0;
for (int i = 0; i < numOfKids; i++) {
average += studentGrade[i];
}
average = (average/numOfKids) * 100;
System.out.println("The average of the class will be " + average + "%");
}
For some better context, here is the rest of the code:
import java.util.Scanner;
public class StudentGradeArray {
static Scanner input = new Scanner(System.in);
static String[] studentName;
static String letterGrade = " ";
static int[] studentGrade;
static int gradeMax = 0;
static int gradeMin = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("How many students are you entering into the database?");
int numOfKids = input.nextInt();
studentGrade = new int[numOfKids];
studentName = new String[numOfKids];
for (int i = 0; i < numOfKids; i++) {
System.out.println("Enter the student's name:");
studentName[i] = input.next();
System.out.println("Enter " + studentName[i] + "'s grade");
studentGrade[i] = input.nextInt();
}
do {
System.out.println("");
System.out.println("Enter a number for the following options:");
System.out.println("");
System.out.println("1. Student's letter grade");
System.out.println("2. Search for a student and their grade");
System.out.println("3. The class average");
System.out.println("4. The student with the highest grade");
System.out.println("5. The student with the lowest grade");
System.out.println("6. List of students that are failing");
System.out.println("7. Quit the program");
int options = input.nextInt();
switch(options) {
case 1:
letterGrade(options); break;
case 2:
searchStudent(); break;
case 3:
classAvg(options); break;
case 4:
markHighest(options); break;
case 5:
markLowest(options); break;
case 6:
markFailing(options); break;
case 7:
return;
default:
System.out.println("");
System.out.println("Please enter a valid option.");
System.out.println(""); break;
}
} while (!input.equals(7));
System.out.println("Program Terminated.");
}
public static void letterGrade(int numOfKids) {
System.out.println("Enter a grade: A, B, C, D, F");
letterGrade = input.next();
if (letterGrade.equalsIgnoreCase("a")) {
gradeMax = 100;
gradeMin = 80;
}
if (letterGrade.equalsIgnoreCase("b")) {
gradeMax = 79;
gradeMin = 70;
}
if (letterGrade.equalsIgnoreCase("c")) {
gradeMax = 69;
gradeMin = 60;
}
if (letterGrade.equalsIgnoreCase("d")) {
gradeMax = 59;
gradeMin = 50;
}
if (letterGrade.equalsIgnoreCase("f")) {
gradeMax = 49;
gradeMin = 0;
}
for (int i = 0; i < numOfKids; i++) {
if (studentGrade[i] <= gradeMax && studentGrade[i] >= gradeMin) {
System.out.println(studentName[i] + " has a " + letterGrade);
System.out.println(letterGrade + " is equivalent to " + gradeMin + " - " + gradeMax + "%");
}
}
}
public static void searchStudent() {
}
public static void classAvg(int numOfKids) {
int average = 0;
for (int i = 0; i < numOfKids; i++) {
average += studentGrade[i];
}
average = (average/numOfKids) * 100;
System.out.println("The average of the class will be " + average + "%");
}
public static void markHighest(int numOfKids) {
int highestNum = 0;
for (int i = 0; i < numOfKids; i++) {
if (studentGrade[i] > highestNum) {
highestNum = studentGrade[i];
}
}
System.out.println("The highest mark in the class is " + highestNum + "%");
}
public static void markLowest(int numOfKids) {
int lowestNum = 0;
for (int i = 0; i < numOfKids; i++) {
if (studentGrade[i] < lowestNum) {
lowestNum = studentGrade[i];
}
}
System.out.println("The highest mark in the class is " + lowestNum + "%");
}
public static void markFailing(int numOfKids) {
for (int i = 0; i < numOfKids; i++) {
if (studentGrade[i] < 50) {
System.out.println(studentName[i] + " is failing with a mark of " + studentGrade[i] + "%");
}
}
}
}
Looks like the argument passed to the classAvg() is not the numOfKids (or size of array) but the option selected by the user from the menu. Trying passing 'numOfKids' instead of passing 'options' as an argument to the function
case 3:
classAvg(options); break;
Better still use studentGrade.length instead of passing argument.
case 3:
classAvg(options); break;
You are passing in the options, but the method wants numOfKids. In this case options will always be 3.
Try passing in numOfKids instead.
Another problem I see is that both average and numOfKids are integers, which causes chopping remainders and rounding to 0 if the denominator is greater. Perhaps change from
average = (average/numOfKids) * 100;
to
average = ((double) average)/ numOfKids * 100;
I think it is better to create a class called Student, with properties name and grade. for more OOP design.
public class Student{
int[] grades;
String name;
public Student(int[] grade, String name){
this.grade = grade;
this.name = name;
}
public double getAverage(){
return (average/(double)numOfKids) * 100;
}
...
}
And average is of type int and numOfKids too, so the division is not gonna be precise. try this average = (average/(double)numOfKids) * 100;

How do I pass the user inputs to a constructor in a different class?

My professor told me that I have to Design a class "SharePattern" that has the following two fields: "numberOfDaysInPeriod" and
"SharePointsOnFirstDay".
The class should have a constructor to set the values of these two fields.
In a separate class he said to get user input in the main of my other class. So what goes into the constructor?
First class:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner more = new Scanner(System.in);
System.out.print("Number of days in the period: ");
int input1 = more.nextInt();
while(input1 < 10 || input1 > 20)
{
System.out.println("The number of days that is entered must not be less than 10 and more than 20. The number of days doesn't meet the required criteria, enter it again");
System.out.print("Number of days in the period: ");
input1 = more.nextInt();
}
System.out.print("Share points on the first day: ");
int input2 = more.nextInt();
int half = input1 / 2;
more.close();
SharePattern sp = new SharePattern(input1, input2, half);
sp.findFinalDaySharePoints(input1, input2, half);
}
}
2nd class:
package hw4Question2;
public class SharePattern {
public SharePattern(int input1, int input2, int half)//constructor
{
}
public void findFinalDaySharePoints(int input1, int input2, int half)
{
System.out.println(input2);
if(input1%2 == 0) {
for(int i = 1; i <= input1 ; ++i)
{
if(i<half)
{
input2 = input2 + 50;
System.out.println(input2);
}
else if(i>half)
{
input2 = input2 - 25;
System.out.println(input2);
}
}
}
else
{
for(int i = 1; i <= input1 ; ++i)
{
if(i<=half)
{
input2 = input2 + 50;
System.out.println(input2);
}
else if(i>half)
{
input2 = input2 - 25;
System.out.println(input2);
}
}
System.out.println("The final share value is "+input2);
}
}
}
First class
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner more = new Scanner(System.in);
System.out.print("Number of days in the period: ");
int input1 = more.nextInt();
while(input1 < 10 || input1 > 20)
{
System.out.println("The number of days that is entered must not be less than 10 and more than 20. The number of days doesn't meet the required criteria, enter it again");
System.out.print("Number of days in the period: ");
input1 = more.nextInt();
}
System.out.print("Share points on the first day: ");
int input2 = more.nextInt();
int half = input1 / 2;
more.close();
SharePattern sp = new SharePattern(input1, input2);
sp.findFinalDaySharePoints(half);
}
SharePattern.class
int numberOfDaysInPeriod;
int sharePointsOnFirstDay;
public SharePattern(int input1, int input2) {
this.numberOfDaysInPeriod = input1;
this.sharePointsOnFirstDay = input2;
}
public void findFinalDaySharePoints(int half)
{
System.out.println(sharePointsOnFirstDay);
if(numberOfDaysInPeriod %2 == 0) {
for(int i = 1; i <= numberOfDaysInPeriod; ++i)
{
if(i<half)
{
sharePointsOnFirstDay = sharePointsOnFirstDay + 50;
System.out.println(sharePointsOnFirstDay);
}
else if(i>half)
{
sharePointsOnFirstDay = sharePointsOnFirstDay - 25;
System.out.println(sharePointsOnFirstDay);
}
}
}
else
{
for(int i = 1; i <= numberOfDaysInPeriod; ++i)
{
if(i<=half)
{
sharePointsOnFirstDay = sharePointsOnFirstDay + 50;
System.out.println(sharePointsOnFirstDay);
}
else if(i>half)
{
sharePointsOnFirstDay = sharePointsOnFirstDay - 25;
System.out.println(sharePointsOnFirstDay);
}
}
System.out.println("The final share value is "+ sharePointsOnFirstDay);
}
}

Java_Fill an array by getters

Trying to fill an array using getter, the problem the array is full with the same number and if the value change in the second input the hole array changes again :
public class Payment {
Scanner kb = new Scanner(System.in);
private int operationNum = 0;
private int subOperationNum = 0;
private double reMoney = 0;
public int getOperationNum() {
return operationNum;
}
public int getSubOperationNum() {
return subOperationNum;
}
public double getReMoney() {
return reMoney;
}
public void setOperationNum(int operationNum) {
this.operationNum = operationNum;
}
public void setSubOperationNum(int subOperationNum) {
this.subOperationNum = subOperationNum;
}
public void setReMoney(double reMoney) {
this.reMoney = reMoney;
}
public void operationsLoop() {
double [] a = new double[17];
do {
System.out.println("\n1- Cash");
System.out.println("2- Car");
System.out.println("3- Clothing");
System.out.println("4- Credit Card");
System.out.println("5- Food");
System.out.println("6- Education");
System.out.println("7- Electronics");
System.out.println("8- Groceries");
System.out.println("9- Health & Fitness");
System.out.println("10- Medical");
System.out.println("11- Travel");
System.out.println("12- Utilities");
System.out.println("13- Finish");
System.out.print("\nEnter the number of operation : ");
this.setOperationNum(kb.nextInt());
this.operation2();
this.operation12();
this.collectReMoney();
**for(int i = 0; i<a.length;i++){
a[i] = this.getReMoney();
System.out.print(a[i] + ", ");
}**
} while (operationNum < 13);
}
public void operation2() {
if (this.getOperationNum() == 2) {
System.out.println("\t1- Gas");
System.out.println("\t2- Repair");
System.out.println("\t3- Monthly Payment");
System.out.print("\nEnter your chose : ");
this.setSubOperationNum(kb.nextInt());
}
}
public void operation12() {
if (this.getOperationNum() == 12) {
System.out.println("\t1- Electricity");
System.out.println("\t2- Gas");
System.out.println("\t3- Telephone");
System.out.println("\t4- Water");
System.out.print("\nEnter your chose : ");
this.setSubOperationNum(kb.nextInt());
}
}
public void collectReMoney() {
if (this.getOperationNum() == 1) {
System.out.print("Withdraw = ");
this.setReMoney(kb.nextDouble());
} else if (this.getOperationNum() == 4 || (this.getOperationNum() == 2 && this.getSubOperationNum() == 3)) {
System.out.print("Payment = ");
this.setReMoney(kb.nextDouble());
} else if (this.getOperationNum() == 9 || this.getOperationNum() == 10) {
System.out.print("Pay = ");
this.setReMoney(kb.nextDouble());
} else if (this.getOperationNum() != 13) {
System.out.print("Spend = ");
this.setReMoney(kb.nextDouble());
}
}
and if I add to the array loop "this.setReMoney(0);" only the first value change
when the user enter a value i want to insert it in the array according to the operation number and the rest values of the other operations should be zero
In the for loop you are assigning the same number to each of the elements of the array, to avoid that you should take the assigning operation outside the for loop:
a[operationNum - 1] = this.getReMoney();
for(int i = 0; i<a.length;i++){
System.out.print(a[i] + ", ");
}
Also, make sure that you check for ArrayIndexOutOfBoundsException in the case the user selects an option number grater than the size of the array.

How to print the array index of an object class?

I have a somewhat big code and I'm triying to print the slot number of an array from an object class that resulted to be the one with the highest weight.
On my main I have declared my array like this:
Car x[] = new car[2];
Now when triying to get which car has the highest weight I've made this validation inside a for loop
//.getweight obtains the weight of each car
if(x[i].getWeight() > max){
max = x[i].getWeight();
}
That code gives me the actual weight number of the heaviest car, but what I want to print is the actual car number. The comparing works but I can't find a way to just print the slot that corresponds to the heaviest car.
UPDATE with full code:
My object car:
public class Car
{
private String color;
private int weight;
private int state;
private int fuel;
private int Maxspeed ;
private int engine;
public Car(){
this.color = "White";
this.weight = 1000;
this.state = 0;
this.fuel =0;
this.Maxspeed = 0;
this.engine = 0;
}
public Car( String color, int weight,
int state, int fuel, int Maxspeed
){
this.color = color;
this.weight = weight;
this.state = state;
this.fuel = fuel;
this.Maxspeed = Maxspeed;
}
public String getColor(){
return this.color;
}
public int getweight(){
return this.weight;
}
public int getstate(){
return this.state;
}
public int getfuel(){
return this.fuel;
}
public int getMaxspeed(){
return this.Maxspeed;
}
public int getengine(){
return this.engine;
}
public void setColor( String color ){
this.color = color;
}
public void setweight( int weight ){
this.weight = weight;
}
public void setstate( int state ){
this.state = state;
}
public void setfuel( int fuel ){
this.fuel = fuel;
}
public void setMaxspeed( int Maxspeed ){
this.Maxspeed = Maxspeed;
}
public void setengine(int engine){
this.engine = engine;
}
public void showdata(){
System.out.println( "\nCar's color is: " + this.getColor() );
System.out.println( "Car's weight is: " + this.getweight() );
System.out.println( "State: " + this.getstate() );
System.out.println( "Fuel: " + this.getfuel());
System.out.println( "Max speed: " + this.getMaxspeed());
}
public void accelerate( int speed ){
if( this.getstate() == 0 ||
this.getstate() == 3 ||
this.getstate() == 4 ||
this.getMaxspeed() < speed )
{
System.out.println("\nCar cannot accelerate...");
}
else{
System.out.println("\nCar is accelerating...");
this.setfuel(this.getfuel()-2);
this.setstate(2);
if( this.getfuel() <= 0 ){
this.setstate(4);
}
}
}
public void crash(){
this.setstate(3);
System.out.println("\nCrash!!!");
}
public void stop(){
this.setstate(1);
System.out.println("\nCar has stopped.");
}
public void addfuel(int fuel){
if(this.getstate() == 0 || this.getstate() == 4){
this.setfuel(this.getfuel()+ fuel);
}
else{
System.out.println("You can't add fuel.");
}
}
public void repair(){
if(this.getstate() == 3){
this.setstate(1);
System.out.println("The car has been repaired");
}
else{
System.out.println("The car is not broken");
}
}
}
My Main:
import java.util.Scanner;
public class aaa{
public static void main (String args []){
Car x[] = new Car[2];
int keep=1;
int counter = 0;
int counter_stopped = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int maxIndex = 0;
int maxweight = 0;
int index_engine = 0;
int min_engine = 0;
Scanner input = new Scanner(System.in);
for(int i = 0; i < x.length; i++){
String color;
int weight;
int fuel;
int Maxspeed;
int engine;
x[i] = new Car();
System.out.print("\nEnter car color " + (i + 1) + ": ");
color = input.next();
System.out.print("Enter car weight " + (i + 1) + ": ");
weight = input.nextInt();
System.out.print("Enter car fuel " + (i + 1) + ": ");
fuel = input.nextInt();
System.out.print("Enter car max speed " + (i + 1) + ": ");
Maxspeed = input.nextInt();
System.out.print("Enter car engine weight " + (i + 1) + ": ");
engine = input.nextInt();
x[i].setColor(color);
x[i].setweight(weight);
x[i].getstate();
x[i].setfuel(fuel);
x[i].setMaxspeed(Maxspeed);
x[i].setengine(engine);
}
for(int i = 0; i < x.length; i++){
int state;
System.out.print("\nEnter car state " + (i + 1) + ": ");
state = input.nextInt();
x[i].setstate(state);
while(state > 4 || state < 0){
System.out.print("state not valid.\nTry again: ");
state = input.nextInt();
x[i].setstate(state);
}
do {
keep = menu();
switch( keep ){
case 1:
accelerate(x[i]);
break;
case 2:
stop(x[i]);
break;
case 3:
crash(x[i]);
break;
case 4:
addfuel(x[i]);
break;
case 5:
repair(x[i]);
break;
case 6:
x[i].showdata();
}
} while(keep != 7);
if(x[i].getstate() == 4 || x[i].getfuel() <= 0){
counter += 1;
}
if(x[i].getstate() == 1){
counter_stopped += 1;
}
int weight = x[i].getweight();
if(weight > max){
maxweight = weight;
maxIndex = i;
}
int weightengine = x[i].getengine();
if(weightengine < min){
min_engine = weightengine;
index_engine = i;
}
}
System.out.println("\nSUMMARY");
System.out.println("Amount of cars with no fuel: " + counter);
System.out.println("Amount of stopped cars: " + counter_stopped);
System.out.println("Heaviest car: " + maxIndex);
System.out.println("Car with the smallest engine: " + index_engine);
System.out.println("=============================================");
}
public static int menu(){
int option = 0;
Scanner s = new Scanner(System.in);
System.out.println("\n1. Accelerate Car ");
System.out.println("2. Stop Car ");
System.out.println("3. Crash Car ");
System.out.println("4. Add fuel ");
System.out.println("5. Repair ");
System.out.println("6. Show data ");
System.out.println("7. Exit ");
System.out.println("=============================================");
System.out.print("Choose an option : ");
option = s.nextInt();
System.out.println("=============================================");
return option;
}
public static void accelerate(Car myCar){
Scanner input = new Scanner(System.in);
int s;
System.out.print("Enter speed: ");
s = input.nextInt();
myCar.accelerate(s);
//myCar.showdata();
}
public static void stop(Car myCar){
myCar.stop();
}
public static void crash(Car myCar){
myCar.crash();
}
public static void addfuel(Car myCar){
int fuel;
Scanner input = new Scanner(System.in);
System.out.print("Amount to add: ");
fuel = input.nextInt();
myCar.addfuel(fuel);
}
public static void repair(Car myCar){
myCar.repair();
}
}
Now, when I compile and test which engine or car is smaller or heaviest I get the number 1 as result.
The array index would be i
The actual car would be x[i]
You need to hold on to a reference to the index to the heaviest car. In this example I am using maxIndex.
float maxWeight = 0;
int maxIndex = 0;
for (int i = 0; i < x.length; i++) {
float weight = x[i].getWeight();
if (weight > max) {
maxWeight = weight;
maxIndex = i;
}
}
System.out.println("Slot of heaviest car: " + maxIndex);

Cannot find symbol

class airlne {
static String seatsArray[][] = new String[30][6];
static String columnLetter[] = {"A", "B", "C", "D", "E", "F"};
static String passName[][] = new String[30][6];
static String name;
static int count;
static double cost;
static String reservedSeat;
static int i, j;
public static void main(String[] args) { //main
String pickedSeat = new String("");
System.out.println("\n********************* Welcome To The Fair Airlines **********************");
System.out.println("********************* **********************");
System.out.println("********************* Please Book *********************");
System.out.println("********************* Your Seat **********************");
System.out.println("********************* **********************");
System.out.println("********************* \1\2\3\4\5\6 **********************");
createSeatingArrangment();
String option = "m";
while (!option.equals("exit")) {
printMenu();
option = Keyboard.readString();
switch (option) { //switch for menu
case ("1"):
showSeatMap();
bookSeat(pickedSeat);
break;
case ("2"):
cancelSeat(pickedSeat);
break;
case ("3"):
seatsRemaining();
break;
case ("4"):
showSeatMap();
break;
case ("5"):
ticketCost();
break;
case ("6"):
printTicket();
break;
case ("exit"):
System.out.println("Thank you. Goodbye");
System.exit(0);
break;
default:
System.out.println("Invalid option");
break;
}
}
}
static void createSeatingArrangment() {
for (i = 0; i < 30; i++) {
for (j = 0; j < 6; j++) {
seatsArray[i][j] = new String((i + 1) + columnLetter[j]);
}
}
}
public static void printMenu() { //just prints menu
System.out.println("Please select an option below:\n"
+ "[1] Book your seat \n"
+ "[2] Cancel your seat \n"
+ "[3] Check how many seats are remaining \n"
+ "[4] See our seats \n"
+ "[5] Show seat cost\n"
+ "[6] Print Ticket"
+ "\nEnter \'exit\' to exit");
}
static void showSeatMap() { // goes through arr and prints
for (i = 0; i < 30; i++) {
for (j = 0; j < 6; j++) {
System.out.print(seatsArray[i][j] + "\t");
}
System.out.println("\4");
}
}
static void ticketCost() {
count = 0;
for (i = 0; i < 30; i++) {
for (j = 0; j < 6; j++) {
if (seatsArray[i][j].equals("X")) {
count++;
}
}
}
double splendidum = 0;
splendidum = 180 - count;
cost = 150 + (180 / splendidum);
System.out.println(" ******* Your ticket will cost " + cost + " ******* ");
}
static void bookSeat(String pickSeatIn) { //booking seat method
System.out.print("Enter Seat Details: ");
pickSeatIn = Keyboard.readString();
if (isSeatAvailable(pickSeatIn)) {
System.out.println("Enter name");
passName[i][j] = Keyboard.readString();
System.out.println("Reservation Confirmed!");
System.out.println("Dear " + passName[i][j] + " Thanks for booking with us!");
System.out.println(pickSeatIn + " is now reserved");
reservedSeat = pickSeatIn;
System.out.println("");
ticketCost();
}
else {
System.out.println("This seat is taken!");
bookSeat(pickSeatIn);
}
}
static void cancelSeat(String pickSeatIn) {
System.out.print("Enter Seat Reservation: ");
pickSeatIn = Keyboard.readString();
if (toCancel(pickSeatIn)) {
System.out.println(passName[i][j] + " your seat will be cancelled.");
}
else {
System.out.println("This is not your seat");
}
System.out.println("Enter name");
passName[i][j] = Keyboard.readString();
if (!passName[i][j].equalsIgnoreCase(passName[i][j])) {
System.out.println("wrong name");
}
showSeatMap();
}
static void printTicket() {
System.out.println("Enter name");
name = Keyboard.readString();
System.out.println("Name: " + name);
System.out.println("Seat Allocation: " + reservedSeat);
System.out.println("Price:" + cost);
}
static boolean isSeatAvailable(String pickSeatIn) {
for (i = 0; i < 30; i++) {
for (j = 0; j < 6; j++) {
if ((seatsArray[i][j]).equalsIgnoreCase(pickSeatIn)) {
seatsArray[i][j] = "X";
return true;
}
}
}
return false;
}
static boolean toCancel(String pickSeatIn) {
for (i = 0; i < 30; i++) {
for (j = 0; j < 6; j++) {
if ((seatsArray[i][j]).equalsIgnoreCase("X")) {
seatsArray[i][j] = pickSeatIn.toUpperCase();
return true;
}
}
}
return false;
}
static void seatsRemaining() {
count = 0;
for (i = 0; i < 30; i++) {
for (j = 0; j < 6; j++) {
if (seatsArray[i][j].equals("X")) {
count++;
}
}
}
System.out.println("seats remaining =" + (180 - count));
}
}
Where the keyboard.nextString() is it says it cant find the symbol anybody know whats wrong or how to fix this ? I know it will be a pretty easy error to fix but I just cant crack it
It seems you are missing an import. Have you declared a Keyboard class somewhere? If so, import it. Otherwise, have a look at Scanner:
Scanner sc = new Scanner(System.in);
String input = sc.next();

Categories

Resources