mirror of
https://onedev.site.tesses.net/crosslang/crosslang-tutorials
synced 2026-02-08 17:15:46 +00:00
First commit
This commit is contained in:
97
4 Json/src/main.tcross
Normal file
97
4 Json/src/main.tcross
Normal file
@@ -0,0 +1,97 @@
|
||||
func main(args)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
Console.WriteLine("Type a command");
|
||||
Console.WriteLine("add: add a person");
|
||||
Console.WriteLine("remove: remove a person");
|
||||
Console.WriteLine("list: list all people");
|
||||
Console.WriteLine("clear: clear all people");
|
||||
Console.WriteLine("exit: leave");
|
||||
|
||||
const command = Console.ReadLine();
|
||||
|
||||
switch(command)
|
||||
{
|
||||
case "add":
|
||||
{
|
||||
Console.Write("Name: ");
|
||||
const name = Console.ReadLine();
|
||||
Console.Write("Age in years: ");
|
||||
const age = ParseLong(Console.ReadLine());
|
||||
|
||||
if(TypeIsLong(age))
|
||||
{
|
||||
var people=Json.Decode(
|
||||
FS.Local.FileExists("people.json") ?
|
||||
FS.ReadAllText(FS.Local,"people.json") : "[]"
|
||||
);
|
||||
var exists = false;
|
||||
each(var person : people)
|
||||
{
|
||||
if(person.name == name)
|
||||
{
|
||||
exists=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!exists)
|
||||
{
|
||||
people.Add({
|
||||
name,
|
||||
age
|
||||
});
|
||||
FS.WriteAllText(FS.Local,"people.json", Json.Encode(people,true));
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "remove":
|
||||
{
|
||||
Console.Write("Name: ");
|
||||
const name = Console.ReadLine();
|
||||
var people=Json.Decode(
|
||||
FS.Local.FileExists("people.json") ?
|
||||
FS.ReadAllText(FS.Local,"people.json") : "[]"
|
||||
);
|
||||
var exists = false;
|
||||
each(var person : people)
|
||||
{
|
||||
if(person.name == name)
|
||||
{
|
||||
people.Remove(person);
|
||||
exists=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(exists)
|
||||
{
|
||||
FS.WriteAllText(FS.Local,"people.json", Json.Encode(people,true));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "list":
|
||||
{
|
||||
var people=Json.Decode(
|
||||
FS.Local.FileExists("people.json") ?
|
||||
FS.ReadAllText(FS.Local,"people.json") : "[]"
|
||||
);
|
||||
|
||||
each(var person : people)
|
||||
{
|
||||
Console.WriteLine($"Name: {person.name}");
|
||||
Console.WriteLine($"Age: {person.age}");
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "clear":
|
||||
{
|
||||
FS.WriteAllText(FS.Local,"people.json", Json.Encode([],true));
|
||||
}
|
||||
break;
|
||||
case "exit":
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user