I'm trying to learn how to code in python, but am having trouble on finding ways to create custom classes online. I wrote a program in java and I am trying to convert it in python. I think I have the custom class down (I'm not sure), and I'm definitely having trouble with the driver.
my custom class (python):
class CostCalculator:
__item = ""
__costOfItem = 0.0
__tax = 0.0
__tip = 0.0
def set_item(self, item):
self.__item = item
def get_name(self):
return self.__item
def set_costOfItem(self, costOfItem):
self.__costOfItem = costOfItem
def get_costOfItem(self):
return self.__costOfItem
def get_tax(self):
__tax = self.__costOfItem * .0875
return self.__tax
def get_tip(self):
__tip = self.__costOfItem * .15
return self.__tip
My python driver attempt
import sys
from CostCalculator import CostCalculator
item = ""
cost = 0.0
totalTip = 0.0
totalTax = 0.0
overallTotal = 0.0
subtotal = 0.0
print("Enter the name of 3 items and their respective costs to get the total value of your meal")
print ("\n Enter the name of your first item: ")
item = sys.stdin.readline()
print("How much is " + item + "?")
cost = sys.stdin.readLine()
My java custom class and driver:
public class TotalCost
{
String item = " ";
double costOfItem = 0;
double tax = 0;
double tip = 0;
public void setItem ( String i )
{
item = i;
}
public String getItem()
{
return item;
}
public void setCostOfItem ( double c )
{
costOfItem = c;
}
public double getCostOfItem ()
{
return costOfItem;
}
public double getTax ()
{
double tax = costOfItem * .0875;
return tax;
}
public double getTip()
{
double tip = costOfItem * .15;
return tip;
}
public String toString()
{
String str;
str = "\nMeal: " + getItem() +
"\nCost of " + getItem() + ": " + getCostOfItem() +
"\nTax of " + getItem() + ": " + getTax() +
"\nTip of " + getItem() + ": " + getTip();
return str;
}
}
import java.util.Scanner;
public class Driver
{
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
String item ;
double cost ;
double totalTip = 0;
double totalTax = 0;
double OverallTotal = 0;
double subtotal;
TotalCost a = new TotalCost ();
TotalCost b = new TotalCost ();
TotalCost c = new TotalCost ();
System.out.println("Enter the name of 3 items and their respective costs to get the total value of your meal");
System.out.println("Enter the name of your first item: ");
item = input.nextLine();
a.setItem ( item );
System.out.println("How much is " + a.getItem() + "?" );
cost = input.nextDouble();
a.setCostOfItem (cost);
input.nextLine();
System.out.println("Enter the name of your second item: ");
item = input.nextLine();
b.setItem (item);
System.out.println("How much is a " + b.getItem() + "?");
cost = input.nextDouble();
b.setCostOfItem (cost);
input.nextLine();
System.out.println("Enter the name of your third item: ");
item = input.nextLine();
c.setItem (item);
System.out.println("How much is a " +c.getItem() + "?" );
cost = input.nextDouble();
c.setCostOfItem(cost);
System.out.println(a + "\n" + b + "\n" + c);
subtotal = a.getCostOfItem() + b.getCostOfItem() + c.getCostOfItem();
totalTip = a.getTip() + b.getTip() + c.getTip();
totalTax = a.getTax() + b.getTax() + c.getTax();
OverallTotal = subtotal + totalTip + totalTax;
System.out.println("\n\tSubtotal: $" + subtotal);
System.out.println("\tTax: $" + totalTax);
System.out.println("\tTip: $" + totalTip);
System.out.println("\tMeal Total: $" + OverallTotal);
}
}