└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # corn-harvest 2 | import math 3 | import json 4 | 5 | class CornHarvest: 6 | def __init__(self, file_name="corn_harvest.json"): 7 | self.file_name = file_name 8 | self.load_data() 9 | 10 | def load_data(self): 11 | """Load harvest data from a file or create a new one.""" 12 | try: 13 | with open(self.file_name, "r", encoding="utf-8") as file: 14 | self.harvest_data = json.load(file) 15 | except (FileNotFoundError, json.JSONDecodeError): 16 | self.harvest_data = [] 17 | 18 | def save_data(self): 19 | """Save harvest data to a file.""" 20 | with open(self.file_name, "w", encoding="utf-8") as file: 21 | json.dump(self.harvest_data, file, indent=4, ensure_ascii=False) 22 | 23 | def record_harvest(self, field_name, area, yield_per_hectare): 24 | """Record a new corn harvest entry.""" 25 | total_yield = area * yield_per_hectare 26 | entry = { 27 | "field_name": field_name, 28 | "area_hectares": area, 29 | "yield_per_hectare": yield_per_hectare, 30 | "total_yield": total_yield 31 | } 32 | self.harvest_data.append(entry) 33 | self.save_data() 34 | print(f"🌽 Recorded {total_yield} tons of corn from {field_name}.") 35 | 36 | def list_harvests(self): 37 | """List all recorded harvests.""" 38 | if not self.harvest_data: 39 | print("No harvest data available.") 40 | return 41 | for harvest in self.harvest_data: 42 | print(f"Field: {harvest['field_name']}, Area: {harvest['area_hectares']} ha, " 43 | f"Yield: {harvest['yield_per_hectare']} t/ha, Total: {harvest['total_yield']} tons") 44 | 45 | # Example usage 46 | def main(): 47 | corn_harvest = CornHarvest() 48 | while True: 49 | print("\n🌽 Corn Harvest Management") 50 | print("1. Record Harvest") 51 | print("2. List Harvests") 52 | print("3. Exit") 53 | choice = input("Select an option: ") 54 | 55 | if choice == "1": 56 | field_name = input("Enter field name: ") 57 | area = float(input("Enter field area (hectares): ")) 58 | yield_per_hectare = float(input("Enter yield per hectare (t/ha): ")) 59 | corn_harvest.record_harvest(field_name, area, yield_per_hectare) 60 | elif choice == "2": 61 | corn_harvest.list_harvests() 62 | elif choice == "3": 63 | break 64 | else: 65 | print("Invalid choice. Try again.") 66 | 67 | if __name__ == "__main__": 68 | main() 69 | --------------------------------------------------------------------------------