I'm confused about how I can implement conversion of different data types in my code below. Conversion includes (int to string, string to int, float to int, etc.) My teacher said this can be easily done when I am reading/writing files, but I'm still confused. I would appreciate any help or suggestions, thank you! Here's my code:
//files
var fs = require("fs");
//reading files
fs.readFile('sources.txt', (err, data) => {
console.log("File output: " + data.toString());
//writing files
fs.writeFile('written.txt',data,(err, result) => {
if(err) console.log('error', err);
});
});
// planet class
class Planet{
constructor(name, numberOfMoons, size) {
this.name = name;
this.numberOfMoons = numberOfMoons;
this.size = size;
}
orbit(){
//return value
return `${this.name} is a planet and therefore orbits around the sun.`
}
}
//inheritance class
class DwarfPlanet extends Planet{
constructor(name, numberOfMoons, size, orbitalNeighbourhood) {
super(name, numberOfMoons, size);
this.orbital = orbitalNeighbourhood;
}
getOrbital(){
//return value
return `${this.name} is a dwarf planet because it doesn't have a clear orbital neighnourhood "`
}
}
let earth = new Planet('Earth', 1 , 6371);
console.log(earth.orbit());
let pluto = new DwarfPlanet("Pluto", 5 , 1188, 'Kuiper Belt');
console.log(pluto.getOrbital());
//Array of Objects (anonymous option)
var stars = [
{
name: 'Sun',
temperature: 5778,
colour: 'White'
},
{
name: 'Pistol',
temperature: 11800,
colour: 'Blue'
},
{
name: "Antares",
temperature: 3500,
colour: "Red"
}
];
// Array of Objects (using Planet Class)
var planets = [
new Planet('Earth', 'One moon', '6,371 km'),
new Planet('Mars', 'Two mooons', '3,389 km'),
new Planet('Uranus', 'Twenty-seven moons', '25,362 km'),
new Planet('Saturn', 'Fifty-three moons', '58,232 km'),
];
console.log("Fun Fact: the biggest star isn't the sun, instead it is a blue star called 'Pistol'. Here's some information about it: ");
console.log(stars[1]);
console.log('Here are some planets and their properties:');
console.log(planets);
The most straightforward way to read and write files is to use their synchronous counterparts: readFileSync()
and writeFileSync()
. In order to work with JSON neatly, you can define your functions as:
const filePath = 'planets.json';
function readPlanets() {
const json = fs.readFileSync(filePath);
const planets = JSON.parse(json);
return planets.map(planet => new Planet(
planet.name,
planet.numberOfMoons,
planet.size
));
}
function writePlanets(planets) {
const json = JSON.stringify(planets, null, 4);
fs.writeFileSync(filePath, json);
}
Then, you can populate your planets.json
file:
const planets = [
new Planet('Earth', 1, 6371),
new Planet('Mars', 2, 3389),
new Planet('Uranus', 27, 25362),
new Planet('Saturn', 53, 58232),
];
writePlanets(planets);
To read it back, you use the readPlanets()
function:
const storedPlanets = readPlanets();
Notice how I manually map()
data to Planet
instances in readPlanets()
. JSON itself has no idea of the class - the objects created by JSON.parse()
are basic JavaScript objects.
Also notice how I changed the format of your data a bit. Instead of using values like 'Twenty-seven moons'
or '6,371 km'
, I just put a simple number in JSON: 27
and 6371
. That's because you usually want to keep your data as raw as possible, because display format is something that you could want to change anytime. The data should persist. If you want to translate these raw numbers to your format, I'd suggest adding methods to your Planet
class to return the formatted numbers of the moons and the sizes.