diff --git a/src/App.js b/src/App.js index 5205a48..bfb06de 100644 --- a/src/App.js +++ b/src/App.js @@ -6,339 +6,386 @@ import Button from 'material-ui/Button' import TextField from 'material-ui/TextField' import Grid from 'material-ui/Grid' import Seat from './components/Seat' +//Declaring variables total and occupied to keep a track of the number of seats occupied among the total number of seats +let total = 0 +let occupied = 0 const distance = require('manhattan') + const styles = theme => ({ - root: { - textAlign: 'center', - paddingTop: theme.spacing.unit * 20, - flexGrow: 1, - }, - textField: { - marginLeft: theme.spacing.unit, - marginRight: theme.spacing.unit, - width: 200, - }, - chartContainer: { - width: '100%', - }, - row: { - margin: '8px', - display: 'flex', - justifyContent: 'center', - alignItems: 'center', - }, + root: { + textAlign: 'center', + paddingTop: theme.spacing.unit * 20, + flexGrow: 1, + }, + textField: { + marginLeft: theme.spacing.unit, + marginRight: theme.spacing.unit, + width: 200, + }, + chartContainer: { + width: '100%', + }, + row: { + margin: '8px', + display: 'flex', + justifyContent: 'center', + alignItems: 'center', + }, }) class App extends Component { - state = { - seatingChart: [], - showChart: false, - rows: 0, - seatsPerRow: 0, - seat: 0, - row: 0, - seatNotAvailable: false, - seatsNotAvailable: false, - numOfSeats: 0, - } - - handleChange = name => event => { - this.setState({ - [name]: event.target.value, - }) - } + state = { + seatingChart: [], + showChart: false, + rows: 0, + seatsPerRow: 0, + seat: 0, + row: 0, + seatNotAvailable: false, + seatsNotAvailable: false, + numOfSeats: 0, + } - makeSeatingChart = (rows, seatsPerRow) => { - // Create two dimensional array to represent seating - let seatingChart = new Array(rows) - for (let i = 0; i < rows; i++) { - seatingChart[i] = new Array(seatsPerRow) - for (let j = 0; j < seatsPerRow; j++) { - seatingChart[i][j] = 0 - } + handleChange = name => event => { + this.setState({ + [name]: event.target.value, + }) } - this.setState({ seatingChart }) - } - handleClick = (rows, seatsPerRow) => { - this.makeSeatingChart(rows, seatsPerRow) - this.setState({ - showChart: true, - }) - } + makeSeatingChart = (rows, seatsPerRow) => { + total = rows * seatsPerRow; //Counting total number of seats + console.log(total) + // Create two dimensional array to represent seating + let seatingChart = new Array(rows) + for (let i = 0; i < rows; i++) { + seatingChart[i] = new Array(seatsPerRow) + for (let j = 0; j < seatsPerRow; j++) { + seatingChart[i][j] = 0 + } + } + this.setState({ seatingChart }) + } - cancelReservation = (row, col) => { - let newChart = this.state.seatingChart - newChart[row][col] = 0 - this.setState({ seatingChart: newChart }) - } + handleClick = (rows, seatsPerRow) => { + this.makeSeatingChart(rows, seatsPerRow) + this.setState({ + showChart: true, + }) + } - reserveSeat = (row, column) => { - this.setState({ seatNotAvailable: false }) - let arr = this.state.seatingChart - // Check to see if seat exists. Is seat open? Return true. Else, return false - if ( - row >= 0 && - column >= 0 && - arr.length > row && - arr[row].length > column && - arr[row][column] === 0 - ) { - const newArr = arr - newArr[row][column] = 1 - // seatsRemaining(newArr) - this.setState({ seatingChart: newArr }) - } else { - this.setState({ seatNotAvailable: true }) + cancelReservation = (row, col) => { + let newChart = this.state.seatingChart + newChart[row][col] = 0 + occupied-- //Vacating seats + console.log(occupied) + this.setState({ seatingChart: newChart }) } - } - reserveSeats = (arr, numOfSeats) => { - this.setState({ seatsNotAvailable: false }) - // Find optimum seat (middle of row 1) - const optimumSeat = [0, Math.floor(arr[0].length / 2)] + reserveSeat = (row, column) => { + this.setState({ seatNotAvailable: false }) + let arr = this.state.seatingChart + // Check to see if seat exists. Is seat open? Return true. Else, return false + if ( + row >= 0 && + column >= 0 && + arr.length > row && + arr[row].length > column && + arr[row][column] === 0 + ) { + const newArr = arr + newArr[row][column] = 1 + occupied++ //Filling of seats + console.log(occupied) + + // seatsRemaining(newArr) + this.setState({ seatingChart: newArr }) + } else { + this.setState({ seatNotAvailable: true }) + } + } - let potentialSeatCombinations = [] - // Reject requests for more than 10 seats - if (numOfSeats > 10) { - console.log('Sorry, no more than 10 seats can be reserved at one time.') - } else { - // Find contiguous seats closest to optimum seat - // Reject requests for more seats than one row can hold - for (let i = 0; i < arr.length; i++) { - let seatNumbers = [] + reserveSeats = (arr, numOfSeats) => { + this.setState({ seatsNotAvailable: false }) + // Find optimum seat (middle of row 1) + const optimumSeat = [0, Math.floor(arr[0].length / 2)] - if (arr[i].length < numOfSeats) { - console.log( - 'Sorry, the rows are not long enough to accomidate that request.' - ) - } else if (arr[i].filter(item => item === 0).length < numOfSeats) { - // Skip to next row if there are not enough available seats - console.log('Not enough open seats in row ' + (i + 1)) + let potentialSeatCombinations = [] + // Reject requests for more than 10 seats + if (numOfSeats > 10) { + console.log('Sorry, no more than 10 seats can be reserved at one time.') } else { - // Reject requests if there are not that many contiguous seats available - // loop over each row - // make an array of all the contiguous seats that are open, if there are none return false - // find manhattan distance of each of those seats - // return the group of seats with that distance - // Use the seatData scores to find sets of seats with the lowest score. - for (let j = 0; j < arr[i].length; j++) { - if (arr[i][j] === 1) { - // Clear seatNumbers array if this seat is already reserved - seatNumbers = [] - } else { - // Save this seat to the seatNumbers array - seatNumbers = [...seatNumbers, { row: i, column: j }] + // Find contiguous seats closest to optimum seat + // Reject requests for more seats than one row can hold + for (let i = 0; i < arr.length; i++) { + let seatNumbers = [] + + if (arr[i].length < numOfSeats) { + console.log( + 'Sorry, the rows are not long enough to accomidate that request.' + ) + } else if (arr[i].filter(item => item === 0).length < numOfSeats) { + // Skip to next row if there are not enough available seats + console.log('Not enough open seats in row ' + (i + 1)) + } else { + // Reject requests if there are not that many contiguous seats available + // loop over each row + // make an array of all the contiguous seats that are open, if there are none return false + // find manhattan distance of each of those seats + // return the group of seats with that distance + // Use the seatData scores to find sets of seats with the lowest score. + for (let j = 0; j < arr[i].length; j++) { + if (arr[i][j] === 1) { + // Clear seatNumbers array if this seat is already reserved + seatNumbers = [] + } else { + // Save this seat to the seatNumbers array + seatNumbers = [...seatNumbers, { row: i, column: j }] + if (seatNumbers.length == numOfSeats) { + //Add seats to array of potential seat combinations + potentialSeatCombinations = [ + ...potentialSeatCombinations, + seatNumbers, + ] + } else if (seatNumbers.length > numOfSeats) { + // Remove seats from seatNumbers array + seatNumbers.splice(0, 1) - if (seatNumbers.length == numOfSeats) { - //Add seats to array of potential seat combinations - potentialSeatCombinations = [ - ...potentialSeatCombinations, - seatNumbers, - ] - } else if (seatNumbers.length > numOfSeats) { - // Remove seats from seatNumbers array - seatNumbers.splice(0, 1) + if (seatNumbers.length == numOfSeats) { + //Add seats to array of potential seat combinations + potentialSeatCombinations = [ + ...potentialSeatCombinations, + seatNumbers, + ] + } + } + } + } - if (seatNumbers.length == numOfSeats) { - //Add seats to array of potential seat combinations - potentialSeatCombinations = [ - ...potentialSeatCombinations, - seatNumbers, - ] } - } + } - } } - } - } - if (potentialSeatCombinations.length === 0) { - this.setState({ seatsNotAvailable: true }) - } else { - let manhattanTotalsArr = [] - potentialSeatCombinations.map(item => { - let manhattanTotal = 0 - item.map( - seat => - (manhattanTotal = - manhattanTotal + distance(optimumSeat, [seat.row, seat.column])) - ) - manhattanTotalsArr = [...manhattanTotalsArr, manhattanTotal] - }) + if (potentialSeatCombinations.length === 0) { + this.setState({ seatsNotAvailable: true }) + } else { + let manhattanTotalsArr = [] + potentialSeatCombinations.map(item => { + let manhattanTotal = 0 + item.map( + seat => + (manhattanTotal = + manhattanTotal + distance(optimumSeat, [seat.row, seat.column])) + ) + manhattanTotalsArr = [...manhattanTotalsArr, manhattanTotal] + }) - const minArrVal = Math.min(...manhattanTotalsArr) - const indexOfMinArrVal = manhattanTotalsArr.indexOf(minArrVal) - const arrVal = potentialSeatCombinations[indexOfMinArrVal] + const minArrVal = Math.min(...manhattanTotalsArr) + const indexOfMinArrVal = manhattanTotalsArr.indexOf(minArrVal) + const arrVal = potentialSeatCombinations[indexOfMinArrVal] - arrVal.map(item => this.reserveSeat(item.row, item.column)) + arrVal.map(item => this.reserveSeat(item.row, item.column)) + } } - } - render() { - const { classes } = this.props - const { - seatingChart, - showChart, - rows, - seatsPerRow, - seat, - row, - seatNotAvailable, - seatsNotAvailable, - numOfSeats, - } = this.state - return ( -