Question: Need help with the bolded parts mostly init. #include #include GalaxyTrader.h using namespace::std; //constructor GalaxyTrader::GalaxyTrader() { } //copy constructor GalaxyTrader::GalaxyTrader(GalaxyTrader& other) { } //destructor GalaxyTrader::~GalaxyTrader()
Need help with the bolded parts mostly init.
#include
#include "GalaxyTrader.h"
using namespace::std;
//constructor
GalaxyTrader::GalaxyTrader()
{
}
//copy constructor
GalaxyTrader::GalaxyTrader(GalaxyTrader& other)
{
}
//destructor
GalaxyTrader::~GalaxyTrader()
{
}
//method name: init
//parameters: the name of the file to read from
//purpose: initializes the planet array using the information in the given file
// should use the init method of the Planet class
// if the input file contains a planet outside the bounds of the array immediately return false
//input file format:
// position of planet
// name of planet
// Number of: Wolves Steaks Rabbits then Prices of: Wolves Steaks Rabbits Fuel
//example: 2
// Mars
// 4 13 15 100 10 20 8
//returns: false if any planets were going to be placed out of bounds, true otherwise
// You may assume that the file format is correct, you do not need to error check anything except
// Planet position
bool GalaxyTrader::init(const string filename)
{
// open file
ifstream file;
file.open(filename.c_str());
if (!file) {
cout << "Invalid filename";
return false;
}
while (file.)
// read each planet in the file
// read location
int location;
file << location;
// read name
string name;
file << name;
// read data (7 values)
int data[NUM_DATA];
for (int i = 0; i < NUM_DATA; i++;)
{
file << economics[i];
}
// return false if planet location invalid
if ( location > SIZE || location < 0 )
{
return false;
}
// create planet object and place it in planets array
else
{
Planet p;
p.init(name, data);
planets[location] = p;
}
return true;
}
//method name: buy
//parameters: none
//purpose: ask the user which good they want to buy and how many they want to buy
// then this calls the buyFrom method of the planet the ship is on
// finally if the transaction was sucessful the goods purhased are added to the ship's cargo
//valid inputs: w/s/r for goods, any non negative number for quantity
//returns: true if the transaction was successful, false otherwise
bool GalaxyTrader::buy()
{
char good;
int amount;
//ask user for input
cout << "What good do you wish to buy Captain? (w, s, r) ";
cin >> good;
while (good != 'w' && good != 's' && good != 'r')
{
cout << "I don't think this planet is familiar with that good, captain. ";
cout << "What good do you wish to buy Captain? (w, s, r) ";
cin >> good;
}
if (amount <= 0)
{
cout << "We can't buy negative goods Captain, maybe you meant to sell? ";
cout << "How many do you wish to buy Captain? ";
cin >> amount;
}
if
{
switch (good)
{
case 'w':
wolves += amount;
break;
case 's':
steaks += amount;
break;
case 'r':
rabbits += amount;
break;
}
}
return true;
}
//method name: sell
//parameters: none
//purpose: ask the user which good they want to sell and how many they want to buy
// then this calls the sellTo method of the planet the ship is on
// finally if the transaction was sucessful the goods purhased are removed from the ship's cargo
//valid inputs: w/s/r for goods, any non negative number up to the number of the good in the ship's cargo for quantity
//returns: true if the transaction was successful, false otherwise
bool GalaxyTrader::sell()
{
char good;
int amount;
//ask user for input
cout << "What good do you wish to sell Captain? (w, s, r) ";
cin >> good;
while (good != 'w' && good != 's' && good != 'r')
{
cout << "I don't think this planet is familiar with that good, captain. ";
cout << "What good do you wish to sell Captain? (w, s, r) ";
cin >> good;
}
if (amount <= 0)
{
cout << "We can't sell negative goods Captain, maybe you meant to buy? ";
cout << "How many do you wish to sell Captain? ";
cin >> amount;
}
if
{
switch (good)
{
case 'w':
wolves -= amount;
break;
case 's':
steaks -= amount;
break;
case 'r':
rabbits -= amount;
break;
}
}
return true;
}
//method name: refuel
//parameters: none
//purpose: buys as much fuel as possible from the planet the ship is on, at most enough to fill the reserves to MAX_FUEL
//returns: true if any fuel was purchased, false otherwise
bool GalaxyTrader::refuel()
{
return true;
}
//method name: move
//parameters: none
//purpose: immediately returns false if the user has no fuel
// otherwise it asks the user what direction they want to move
// then the ship's position is updated to reflect the move and fuel is decreased by one
//note: if the ship exceeds the limits of the array it should "reapear" on the other side
//valid inputs: l for left, r for right
//returns: true if the ship's position changed, false otherwise
bool GalaxyTrader::move()
{
return true;
}
//method name: print_status
//parameters: none
//purpose: print the coordinates of the ship
// the credits availble
// currentfuel/MAX_FUEL
// and everything in the ships cargo (should say it is empty if it contains nothing)
//returns: none
void GalaxyTrader::print_status() const
{
}
//method name: print_planet
//parameters: none
//purpose: checks to see if the Planet the ship is on is empty
// if it is print a message saying so
// otherwise call the print method of the planet the ship is on
//returns: none
void GalaxyTrader::print_planet() const
{
}
//method name: scan
//parameters: none
//returns: false if the Planet the ship is at is empty, true if it is not
bool GalaxyTrader::scan() const
{
return true;
}
Planet.cpp
#include
//constructor Planet::Planet() { empty = true; name = "default"; wolves = 0; steaks = 0; rabbits = 0; price_wolves = 0; price_steaks = 0; price_rabbits = 0; price_fuel = 99999; }
//copy constructor Planet::Planet(Planet& other) { empty = other.empty; name = other.name; wolves = other.wolves; steaks = other.steaks; rabbits = other.rabbits; price_wolves = other.price_wolves; price_steaks = other.price_steaks; price_rabbits = other.price_rabbits; }
//destructor Planet::~Planet() { }
//method name: init //parameters: the name of the planet // an array containing the quantities and prices for goods on the Planet //purpose: sets a Planet as none empty and populates it's member variables with the given values //returns: none void Planet::init(const string n, const int data[]) { name = n; wolves = data[NUM_W]; steaks = data[NUM_S]; rabbits = data[NUM_R]; price_wolves = data[PRICE_W]; price_steaks = data[PRICE_S]; price_rabbits = data[PRICE_R]; price_fuel = data[PRICE_F]; empty = false; }
//method name: print //parameters: none //purpose: display all meaningful values the Planet has to the user // in the case that the planet is uninitialized print only a message indicating this //returns: none void Planet::print() const { if(!empty) { cout << "\tWelcome to planet " << name << endl; cout << "\tOur " << wolves << " wolves are going for " << price_wolves << " credits each." << endl; cout << "\tOur " << steaks << " steaks are going for " << price_steaks << " credits each." << endl; cout << "\tOur " << rabbits << " rabbits are going for " << price_rabbits << " credits each." << endl; cout << "\tFuel can be purchased for " << price_fuel << "credits." << endl; } else cout << "This planet is uninhabited "; }
//method name: buyFrom //parameters: the first letter of the good the user wished to buy // the quantity the user wishes to purchase // a reference to the credits the user has //purpose: check to make sure that the offer makes sense // if it does update the number of goods on the planet and the credits the user has //returns: true if the offer made sense, false otherwise bool Planet::buyFrom(const char good, const int amount, int& credits) { bool valid = false; switch(good) { case 'w': if(amount * price_wolves <= credits && amount <= wolves) { wolves -= amount; credits -= amount * price_wolves; valid = true; } break; case 's': if(amount * price_steaks <= credits && amount <= steaks) { steaks -= amount; credits -= amount * price_steaks; valid = true; } break; case 'r': if(amount * price_rabbits <= credits && amount <= rabbits) { rabbits -= amount; credits -= amount * price_rabbits; valid = true; } break; } return valid; }
//method name: sellTo //parameters: the first letter of the good the user wished to buy // the quantity the user wishes to purchase // a reference to the credits the user has //purpose: update the number of goods on the planet and the credits the user has //returns: false if the given good doesn't exist, true otherwise
bool Planet::sellTo(const char good, const int amount, int& credits) { //we are adding the amount user wants to sell to the respective quantities of planet and updating the credits //accordingly
switch (good) {
case 'w':
wolves += amount; credits += amount * price_wolves; valid = true; break;
case 's': steaks += amount; credits += amount * price_steaks; valid = true; break;
case 'r': rabbits += amount; credits += amount * price_rabbits; valid = true; break;
}
return valid; }
//method name: getFuel //parameters: none //returns: the price of fuel on this planet int Planet::getFuel() const { return price_fuel; }
//method name: isEmpty //parameters: none //returns: true if the planet is empty, false otherwise bool Planet::isEmpty() const { return empty; }
//method name: getName //parameters: none //returns: the name of the Planet string Planet::getName() const { return name; }
Planet.h
#include
const int NUM_DATA = 7; // The number of data items stored about each planet
const int NUM_W = 0; // The number of wolves for sale const int NUM_S = 1; // The number of steaks for sale const int NUM_R = 2; // The number of rabbits for sale const int PRICE_W = 3; // The price for a wolf const int PRICE_S = 4; // The price for a steak const int PRICE_R = 5; // The price for a rabbit const int PRICE_F = 6; // The price for fuel
class Planet { public: Planet(); Planet(Planet& other); ~Planet();
void print() const; void init(const string n, const int data[7]);
bool buyFrom(const char good, const int amount, int& credits); // add function prototype for sellTo here int getFuel() const; bool isEmpty() const; string getName() const;
private: string name; bool empty;
int wolves; int steaks; int rabbits;
int price_wolves; int price_steaks; int price_rabbits;
int price_fuel; };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
