上题目
准备好业务逻辑流程图:
python 版本
import re
def is_numerical(orderamount):
print("The order amount is {0}".format(orderamount))
return re.match(r'[+-]?\d+.?\d*#39;,orderamount) != None
class TaxCalculator:
def __init__(this):
this.orderamount = 0
this.state = ""
this.tax = 5.5
def get_input(this):
orderamount = input("What is the order amount\n")
while is_numerical(orderamount) == False:
orderamount = input("Please provide numerical input\n What is the order amount\n")
state = input("What is the state\n")
this.orderamount = float(orderamount)
this.state = state.upper()
def judge_info(this):
if this.state == "WI":
this.print_with_tax()
if this.state != "WI":
this.print_without_tax()
def print_with_tax(this):
taxcharge = this.orderamount*this.tax/100
print("The tax is {0}".format(taxcharge))
print("The total is {0}".format(this.orderamount+taxcharge))
def print_without_tax(this):
print("The total is {0}".format(this.orderamount))
lesson14 = TaxCalculator()
lesson14.get_input()
lesson14.judge_info()
java 版本
import java.util.Scanner;
public
class TaxCaculator{
private Double orderamount;
private String state;
private Double tax;
TaxCaculator(){
this.orderamount = (double) 0;
this.state = "";
this.tax = 5.5;
}
private static boolean isNumeric(String secondstring) {
return secondstring != null & & secondstring.matches("-?\\d+(\\.\\d+)?");
}
void Get_input()
{
Scanner sc = new
Scanner(System. in);
System.out.println("What is the order amount? ");
String orderamountstr = sc.next();
while (TaxCaculator.isNumeric(orderamountstr) == false)
{
orderamountstr = sc.next();
}
System.out.println("What is the state? ");
String statestr = sc.next();
this.orderamount = Double.parseDouble(orderamountstr);
this.state = statestr.toUpperCase();
}
void JudgeInfo()
{
System.out.println("The state info is " + this.state);
if (this.state.equals("WI"))
{
this.printwithtax();
}
else
{
this.printwithouttax();
}
}
void printwithtax()
{
System.out.println("The tax is: " + this.orderamount * this.tax / 100);
System.out.println("The total is #34; + (this.orderamount + this.orderamount * this.tax / 100));
}
void printwithouttax()
{
System.out.println("The total is #34; + this.orderamount);
}
public static void main(String[] args){
TaxCaculator lesson14 = new TaxCaculator();
lesson14.Get_input();
lesson14.JudgeInfo();
}
}