Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A class (called Customer) will represent a potential customer for a meal and discounted harbor cruise package. Various discounts will be offered for children and students.
The main function will create instances of the Customer class, and calculate the costing according to the child/student status of each Customer object.
Write a Customer class which stores the name and two Boolean data fields, one for whether the customer is a student, the other for whether the customer is a child. A child is defined as between 5 and 16 years (inclusive).
The class should have getter methods for these three data fields. Rather than prefix the Boolean getter’s name with get, use is, eg isChild().
The main function will create customer objects when run. Three values should be passed to the Customer’s constructor upon creation – name, age and a Boolean value representing the student’s status.
Use the following values for the customers:
• “Tony Stark”, 17, true
• “Steve Rogers”, 17, false
• “Peter Parker”, 16, true
• “Natasha Romanoff”, 25, true
• “Clint Barton”, 12, false
• “Wanda Maximoff”, 7, false
• “Bruce Banner”, 16, true
• “Carol Danvers”, 20, false

Sample output:

Write another function in the main.cpp called confirmBooking. This function will eventually display prices and confirm the booking. It will need to take a Customer object as a parameter.

The confirmBooking function should
• specify that a standard ticket price in RM56.00 and a standard meal price is RM30.00.
• calculate discounts
o Students and children receive half price tickets, but anyone else gets a 20% discount
o Children receive half-price meals. Everyone else gets a 10% discount.
• display the prices and the total for each customer.
The next task is to ask for confirmation of the booking. The user should confirm by pressing ‘y’.
The booking information will need to be stored. This will require another Boolean data field in the Customer class. Write a setter and getter methods for it.
Back in the main.cpp, finish your confirmBooking function. If the choices is ‘y’, set the customer’s booked field to true and display “Booked”.

Also, in the main.cpp, write another function which takes an instance of Customer as a parameter. All this function should do is print out the name of the customer if (and only if) they are booked.

What I have tried:

How can i store user input into array?


#include <iostream>
using namespace std;


class Customer {
	
	private:
		string name;
		int age;
		bool student;
		bool child ;

	public:
		
		string getName(string arr[]) {
		 return name;
		}
		
		string geAge(int arr[]) {
		 return age;
		}
		
		string getName(bool arr[]) {
		 return name;
		}
		
		void cus(string n, int a, bool s){
			name = n;
			age = a;
			student = s;
		
			
		
			
			
		}
		
		customerobj c1;
		
		confirmBooking (c1){
			
			int ticketPrice=56;
			int mealPrice=30;
		} 

}; 



int main() {

	
	string name[8];
	int age[8];
	bool student[8];
	string id;
	
	


	int i;
	
	

   for (i = 0; i < 8; i++) {
   	
	  
	cout << "Please enter name:";
	cin >> name[i];
	
	
	cout << "Please enter age:";
	cin >> age[i];

	cout << "Is student ID present?(y/n)";
	cin >> id;
	


	if (id == "y") {

		student[i] = true;

	}
	else if (id == "n") {

		student[i] = false;

	}
	else {
		cout << "Error!";
	}
}	


}
Posted
Updated 17-Oct-21 12:30pm
v3

You are creating arrays to store individual fields of input, instead of using the Customer class. You should create a vector of Customer objects, and as you read in the details, create a new Customer object and add it to the vector. That way you do not need to be concerned with how many customers are created as the vector will expand itself dynamically; hint: setting a starting size will improve its efficiency. You have also declared all your getter methods to accept arrays, which makes no sense, since they do not require any inputs. You also have a method named cus which sets the values, which I assume is supposed to be a constructor, in which case it should just be declared as follows:
C++
Customer(string n, int a, bool s, bool c){ // NB constructors do not have return values
    name = n;
    age = a;
    student = s;
    child = c;
}

I would also suggest you make use of proper names for your variables, single letters are largely meaningless, and can lead to confusion.
 
Share this answer
 
As Richard MacCutchan has already suggested, it would be good to save the complete data set in ONE vector.
C++
class Customer {
public:
	Customer(string n, int a, CUST_TYPE t) : name(n), age(a), type(t) {}
	string GetName() { return name; }
	int GetAge() { return age; }
	CUST_TYPE GetType() { return type; }
private:
	string name;
	int age;
	CUST_TYPE type;
};

When the Type is not overlapping (child != student) you could think about:
C++
enum CUST_TYPE { CU_UNKOWN, CU_CHILD, CU_STUDENT, CU_ADULT, CU_CNT };

And then use it in main like this:
C++
vector <Customer*> mycustomers;
...
Customer *newcust = new Customer(name, age, type);
mycustomers.push_back(newcust);
 
Share this answer
 
v2
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Just posting your homework assignment isn't going to get you anywhere.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900