C
C#2w ago
HyperPixel

Can someone tell me what am i doing wrong?

using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace ObjectsProject { internal class Bucket { private int capacity; private double currentAmount; public Bucket(int capacity, double currentAmount) { this.capacity = capacity; currentAmount = 0.0; } public void SetCapacity(int capacity) { this.capacity = capacity; } public void SetCurrentAmount(double currentAmount) { this.currentAmount = currentAmount; } public void EmptyAll() { this.currentAmount = 0.0; } public void IsEmpty() { if (this.currentAmount == 0) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } } public void Empty() { if (currentAmount > 0) { currentAmount = 0; } } public void FillAll() { if (capacity > currentAmount) { this.capacity = (int)this.currentAmount; } else { Console.WriteLine("The bucket is already full!"); } } public void IsFill() { if (capacity == currentAmount) { Console.WriteLine("True"); } else { Console.WriteLine("False"); } } public void Fill() { if (currentAmount > 0) { currentAmount = capacity; } } public int GetCapacity() { return capacity; } public double GetCurrentAmount() { return currentAmount; } public void PourInto(Bucket otherBucket) { while (this.currentAmount > 0 && otherBucket.currentAmount < otherBucket.capacity) { this.currentAmount--; otherBucket.currentAmount++; } } public bool Equals(Bucket otherBucket) { return this.capacity == otherBucket.capacity && this.currentAmount == otherBucket.currentAmount; } public override string ToString() { return $"Bucket with capacity {capacity} and current amount {currentAmount}"; } } } using ObjectsProject; using System; class Program { static void Main() { Console.Write("Enter bucket capacity: "); int capacity = int.Parse(Console.ReadLine()); Console.Write("Enter current amount: "); double currentAmount = double.Parse(Console.ReadLine()); Bucket bucket = new Bucket(capacity, currentAmount); while (bucket.GetCurrentAmount() > 0 && bucket.GetCurrentAmount() < bucket.GetCapacity()) { Console.Write("Do you want to (add) or (remove) water? "); string action = Console.ReadLine().ToLower(); Console.Write("Enter amount: "); double amount = double.Parse(Console.ReadLine()); if (action == "add") { bucket.SetCurrentAmount(Math.Min(bucket.GetCurrentAmount() + amount, bucket.GetCapacity())); } else if (action == "remove") { bucket.SetCurrentAmount(Math.Max(bucket.GetCurrentAmount() - amount, 0)); } else { Console.WriteLine("Invalid option. Please enter 'add' or 'remove'."); } Console.WriteLine(bucket.ToString()); } Console.WriteLine("Bucket is either empty or full."); } }
No description
8 Replies
Jimmacle
Jimmacle2w ago
$code
MODiX
MODiX2w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
HyperPixel
HyperPixelOP2w ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ObjectsProject
{

internal class Bucket
{
private int capacity;
private double currentAmount;

public Bucket(int capacity, double currentAmount)
{
this.capacity = capacity;
currentAmount = 0.0;
}

public void SetCapacity(int capacity)
{
this.capacity = capacity;
}

public void SetCurrentAmount(double currentAmount)
{
this.currentAmount = currentAmount;
}

public void EmptyAll()
{
this.currentAmount = 0.0;
}

public void IsEmpty()
{
if (this.currentAmount == 0)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}

public void Empty()
{
if (currentAmount > 0)
{
currentAmount = 0;
}
}

public void FillAll()
{
if (capacity > currentAmount)
{
this.capacity = (int)this.currentAmount;
}
else
{
Console.WriteLine("The bucket is already full!");
}
}
public void IsFill()
{
if (capacity == currentAmount)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}

public void Fill()
{
if (currentAmount > 0)
{
currentAmount = capacity;
}
}

public int GetCapacity()
{
return capacity;
}

public double GetCurrentAmount()
{
return currentAmount;
}


public void PourInto(Bucket otherBucket)
{
while (this.currentAmount > 0 && otherBucket.currentAmount < otherBucket.capacity)
{
this.currentAmount--;
otherBucket.currentAmount++;
}
}

public bool Equals(Bucket otherBucket)
{
return this.capacity == otherBucket.capacity && this.currentAmount == otherBucket.currentAmount;
}
public override string ToString()
{
return $"Bucket with capacity {capacity} and current amount {currentAmount}";
}


}



}

using ObjectsProject;
using System;

class Program
{
static void Main()
{
Console.Write("Enter bucket capacity: ");
int capacity = int.Parse(Console.ReadLine());

Console.Write("Enter current amount: ");
double currentAmount = double.Parse(Console.ReadLine());

Bucket bucket = new Bucket(capacity, currentAmount);

while (bucket.GetCurrentAmount() > 0 && bucket.GetCurrentAmount() < bucket.GetCapacity())
{
Console.Write("Do you want to (add) or (remove) water? ");
string action = Console.ReadLine().ToLower();

Console.Write("Enter amount: ");
double amount = double.Parse(Console.ReadLine());

if (action == "add")
{
bucket.SetCurrentAmount(Math.Min(bucket.GetCurrentAmount() + amount, bucket.GetCapacity()));
}
else if (action == "remove")
{
bucket.SetCurrentAmount(Math.Max(bucket.GetCurrentAmount() - amount, 0));
}
else
{
Console.WriteLine("Invalid option. Please enter 'add' or 'remove'.");
}

Console.WriteLine(bucket.ToString());
}

Console.WriteLine("Bucket is either empty or full.");
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ObjectsProject
{

internal class Bucket
{
private int capacity;
private double currentAmount;

public Bucket(int capacity, double currentAmount)
{
this.capacity = capacity;
currentAmount = 0.0;
}

public void SetCapacity(int capacity)
{
this.capacity = capacity;
}

public void SetCurrentAmount(double currentAmount)
{
this.currentAmount = currentAmount;
}

public void EmptyAll()
{
this.currentAmount = 0.0;
}

public void IsEmpty()
{
if (this.currentAmount == 0)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}

public void Empty()
{
if (currentAmount > 0)
{
currentAmount = 0;
}
}

public void FillAll()
{
if (capacity > currentAmount)
{
this.capacity = (int)this.currentAmount;
}
else
{
Console.WriteLine("The bucket is already full!");
}
}
public void IsFill()
{
if (capacity == currentAmount)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}

public void Fill()
{
if (currentAmount > 0)
{
currentAmount = capacity;
}
}

public int GetCapacity()
{
return capacity;
}

public double GetCurrentAmount()
{
return currentAmount;
}


public void PourInto(Bucket otherBucket)
{
while (this.currentAmount > 0 && otherBucket.currentAmount < otherBucket.capacity)
{
this.currentAmount--;
otherBucket.currentAmount++;
}
}

public bool Equals(Bucket otherBucket)
{
return this.capacity == otherBucket.capacity && this.currentAmount == otherBucket.currentAmount;
}
public override string ToString()
{
return $"Bucket with capacity {capacity} and current amount {currentAmount}";
}


}



}

using ObjectsProject;
using System;

class Program
{
static void Main()
{
Console.Write("Enter bucket capacity: ");
int capacity = int.Parse(Console.ReadLine());

Console.Write("Enter current amount: ");
double currentAmount = double.Parse(Console.ReadLine());

Bucket bucket = new Bucket(capacity, currentAmount);

while (bucket.GetCurrentAmount() > 0 && bucket.GetCurrentAmount() < bucket.GetCapacity())
{
Console.Write("Do you want to (add) or (remove) water? ");
string action = Console.ReadLine().ToLower();

Console.Write("Enter amount: ");
double amount = double.Parse(Console.ReadLine());

if (action == "add")
{
bucket.SetCurrentAmount(Math.Min(bucket.GetCurrentAmount() + amount, bucket.GetCapacity()));
}
else if (action == "remove")
{
bucket.SetCurrentAmount(Math.Max(bucket.GetCurrentAmount() - amount, 0));
}
else
{
Console.WriteLine("Invalid option. Please enter 'add' or 'remove'.");
}

Console.WriteLine(bucket.ToString());
}

Console.WriteLine("Bucket is either empty or full.");
}
}
HyperPixel
HyperPixelOP2w ago
it always becomes that
No description
glhays
glhays2w ago
CurrentAmount = Capacity in the FillAll method?
Jimmacle
Jimmacle2w ago
follow your code step by step you should see it
HyperPixel
HyperPixelOP2w ago
what should i do with it? i put that if its not zero and smaller than capacity it should ask to add or not and all that and it wont work just straight goes to end the code
Jimmacle
Jimmacle2w ago
right, so bucket.GetCurrentAmount() > 0 && bucket.GetCurrentAmount() < bucket.GetCapacity() is evaluating to false and you need to work backwards to find out why that is i would start by looking at which of those conditions are evaluating to false

Did you find this page helpful?