Java电话账单计算

URL图

Customer.java
import java.io.IOException;

public class Customer {
	/** The customer's name in a String object. */
	private String name;
	
	/** And have a reference to a Phone object. */
	private Phone cPhone;
	
	/** Accessor methods */
	public String getName() {
		return name;
	}
	public Phone getCPhone() {
		return cPhone;
	}
	
	/** Mutator methods */
	public void setName(String newName) {
		name = newName;
	}
	
	public void setcPhone(Phone newCPhone) {
		cPhone = newCPhone;
	}
	
	/** Constructor */
	public Customer() throws IOException {
		name = Input.getString("Enter name: ");
		int choice = Input.getInt("1:FLAT RATE 2:PRE-MINUTE Enter Choice: ", 1, 2);
		String cPhoneNumber = Input.getString("Enter Phone Number: ");
		double baseRate = Input.getDouble("Base Rate: ", 0.00, 100.00);
		if (choice == 1) {
			cPhone = new FlatRatePhone(cPhoneNumber, baseRate); 
		} else {
			cPhone = new PerMinutePhone(cPhoneNumber, baseRate); 
		}
	}
	
	/** display of output */
	public String toString() {
		return name + "    Number: " + cPhone.getPhoneNumber();
	}
}
FlatRatePhone.java
import java.io.IOException;

public class FlatRatePhone extends Phone{
	/** flatRatePremium: holds the additional cost of having a flat-rate plan. */
	private double flatRatePremium;
	
	/** Accessor methods */
	public double getFlatRatePremium() {
		return flatRatePremium;
	}
	
	/** Mutator methods */
	public void setFlatRatePremium(double newFlatRatePremium) {
		flatRatePremium = newFlatRatePremium;
	}
	
	/** Constructor */
	public FlatRatePhone(String intCPhoneNumber, double intBaseRate) throws IOException {
		super(intCPhoneNumber, intBaseRate);
		flatRatePremium = Input.getDouble("Flat Rate Premium: ", 0.00, 100.00);
		double taxAmt = calcuTax();
		super.setTaxAmt(taxAmt);
	}
	
	/** to calculate the before-tax bill total */
	public double calcuBefTaxBill() {
		return super.getBaseRate() + flatRatePremium;
	}
	
	/** The tax calculation method */
	public double calcuTax() {
		return super.getHST() * calcuBefTaxBill();		
	}
	
	/** display of output */
	public String toString() {
		return "  Amt: $ " + (super.getBaseRate() + flatRatePremium) + "\n"
			 + "  Tax: $ " + super.getTaxAmt() + "\n"
			 + "  TOTAL: $ " + (calcuBefTaxBill() + super.getTaxAmt()) + "\n"
		     + "    FLAT RATE PLAN: Premium: "  + flatRatePremium;
	}
}
Input.java
import java.io.*;

public class Input {
	private static BufferedReader stdIn = new BufferedReader(
			new InputStreamReader(System.in));
	private static PrintWriter stdOut = new PrintWriter(System.out, true);
	private static PrintWriter stdErr = new PrintWriter(System.err, true);

	public static int getInt(String prompt) throws IOException {
		do {
			try {
				stdOut.print(prompt);
				stdOut.flush();
				int temp = Integer.parseInt(stdIn.readLine());
				return temp;
			} catch (NumberFormatException nfe) {
				stdErr.print("Invalid number format");
				stdErr.flush();
			}
		} while (true);
	}

	public static int getInt(String prompt, int low, int high) throws IOException {
		do {
			try {
				stdOut.print(prompt);
				stdOut.flush();
				int temp = Integer.parseInt(stdIn.readLine());
				if (temp >= low && temp <= high) {
					return temp;
				}
			} catch (NumberFormatException nfe) {
				stdErr.print("Invalid number format");
				stdErr.flush();
			}
		} while (true);
	}

	public static double getDouble(String prompt, double low, double high) throws IOException {
		do {
			try {
				stdOut.print(prompt);
				stdOut.flush();
				double temp = Double.parseDouble(stdIn.readLine());
				if (temp >= low && temp <= high) {
					return temp;
				}
			} catch (NumberFormatException nfe) {
				stdErr.print("Invalid number format");
				stdErr.flush();
			}
		} while (true);
	}

	public static String getString(String prompt) throws IOException {
		do {
			stdOut.print(prompt);
			stdOut.flush();
			String temp = stdIn.readLine();
			return temp;
		} while (true);
	}
}
PerMinutePhone.java
import java.io.IOException;

public class PerMinutePhone extends Phone {
	/**
	 * minutes: holds a floating-point number which represents the total number
	 * of minutes used during that month.
	 */
	private double minutes;

	/** Accessor methods */
	public double getMinutes() {
		return minutes;
	}

	/** Mutator methods */
	public void setMinutes(double newMinutes) {
		minutes = newMinutes;
	}

	/** Constructor */
	public PerMinutePhone(String intCPhoneNumber, double intBaseRate)
			throws IOException {
		super(intCPhoneNumber, intBaseRate);
		minutes = Input.getDouble("Minutes Used: ", 0.00, 500.00);
		double taxAmt = calcuTax();
		super.setTaxAmt(taxAmt);
	}

	/** to calculate the before-tax bill total */
	public double calcuBefTaxBill() {
		return super.getBaseRate() + minutes * 0.25;
	}

	/** The tax calculation method */
	public double calcuTax() {
		return super.getHST() * calcuBefTaxBill();
	}

	/** display of output */
	public String toString() {
		return "  Amt: $ " + calcuBefTaxBill() + "\n" 
			 + "  Tax: $ " + super.getTaxAmt() + "\n" 
			 + "  TOTAL: $ " + (calcuBefTaxBill() + super.getTaxAmt()) + "\n"
			 + "    PER MINUTE PLAN: Rate:0.25  Minutes Used: " + minutes;
	}
}
Phone.java
public abstract class Phone {
	/**
	 * phoneNumber: holds the phone number in a String object. There is no need
	 * for any validation of this field during input.
	 */
	private String phoneNumber;

	/**
	 * baseRate: holds a floating-point number. This value is entered during
	 * keyboard input.
	 */
	private double baseRate;

	/**
	 * taxAmt: holds the calculated tax. This value is calculated when all
	 * customer bills are displayed.
	 */
	private double taxAmt;

	/** a constant for the HST tax (which is now going to be 13%). */
	private final double HST = 0.13;

	/** Accessor methods */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public double getBaseRate() {
		return baseRate;
	}

	public double getTaxAmt() {
		return taxAmt;
	}

	public double getHST() {
		return HST;
	}

	/** Mutator methods */
	public void setPhoneNumber(String newPhoneNumber) {
		phoneNumber = newPhoneNumber;
	}

	public void setBaseRate(double newBaseRate) {
		baseRate = newBaseRate;
	}

	public void setTaxAmt(double newTaxAmt) {
		taxAmt = newTaxAmt;
	}

	/** Constructor */
	public Phone(String intPhoneNumber, double intBaseRate) {
		phoneNumber = intPhoneNumber;
		baseRate = intBaseRate;
	}

	/** to calculate the before-tax bill total */
	public abstract double calcuBefTaxBill();

	/** The tax calculation method */
	public abstract double calcuTax();
}
PhoneCompany.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public class PhoneCompany {
	private static BufferedReader stdIn = new BufferedReader(
			new InputStreamReader(System.in));
	private static PrintWriter stdOut = new PrintWriter(System.out, true);
	private static PrintWriter stdErr = new PrintWriter(System.err, true);

	private ArrayList<Customer> customers;

	public static void main(String[] args) throws IOException {
		/**
		 * The PhoneCompany class is used to create an object, then the
		 * addCustomer() and calcBills() methods are called as a result of a
		 * user's menu choice.
		 */
		PhoneCompany ph = new PhoneCompany();
		ph.run();
	}

	/**
	 * Constructor
	 */
	public PhoneCompany() {
		customers = new ArrayList<Customer>();
	}

	/** Accessory methods */
	public ArrayList<Customer> getCustomers() {
		return customers;
	}
	
	/**
	 * Presents the user with a menu of options and executes the selected task.
	 */
	private void run() throws IOException {

		int choice = getChoice();

		while (choice != 0) {

			if (choice == 1) {
				/** Add Customer */
				addCustomer();
			} else if (choice == 2) {
				/** Display Customer Bill */
				calcBills();
			}
			choice = getChoice();
		}
	}

	/**
	 * Displays a menu of options and verifies the user's choice.
	 * 
	 * @return an integer in the range [0,2]
	 */
	private int getChoice() throws IOException {

		int input;

		do {
			try {
				stdOut.println();
				stdOut.print("[1]  Add Customer\n"
						+ "[2]  Display Customer Bills\n" + "[0]  Quit\n\n"
						+ "Enter Choice: ");
				stdOut.flush();

				input = Integer.parseInt(stdIn.readLine());

				stdOut.println();

				if (0 <= input && 2 >= input) {
					break;
				} else {
					stdErr.print("Invalid choice:  " + input);
					stdOut.flush();
				}
			} catch (NumberFormatException nfe) {
				stdErr.println(nfe);
			}
		} while (true);

		return input;
	}

	/**
	 * The addCustomer() method will create a new Customer object and create the
	 * correct type of Phone object based on the user's choice. The PhoneCompany
	 * class must ensure that the addition of new customers will fit in the
	 * collection that is used to manage all the added customers.
	 * 
	 * @throws IOException
	 */
	public void addCustomer() throws IOException {
		Customer customer = new Customer();
		customers.add(customer);
	}

	/**
	 * The calcBills() method will determine the billing total for each
	 * customer, then display the results.
	 */
	public void calcBills() {
		double total = 0.00;
		stdOut.println();
		stdOut.print("AirHead Phone Company\n");
		for (Customer customer : customers) {
			stdOut.println(customer.toString());
			stdOut.println((customer.getCPhone()).toString());
			total = total + customer.getCPhone().calcuBefTaxBill()
					+ customer.getCPhone().getTaxAmt();
		}
		stdOut.println("TOTAL: " + total);
	}
}
TestPhoneCompany.java
import java.io.IOException;
import java.io.PrintWriter;

/** To test PhoneCompany class */
public class TestPhoneCompany {
	private static PrintWriter stdOut = new PrintWriter(System.out, true);
	private static PrintWriter stdErr = new PrintWriter(System.err, true);

	public static void assertTrue(String message, boolean condition) {
		if (!condition) {
			stdErr.print("** Test failure ");
			stdErr.println(message);
		}
	}

	public static void main(String[] args) throws IOException {

		/** Testing constructor and accessor */
		PhoneCompany ph = new PhoneCompany();
		assertTrue("1: testing constructor and accessor", 
				ph.getCustomers().size() == 0);

		/**
		 * Testing method addCustomer(), the customer infor as followings: 
		 * Bill Jones
		 * 613-111-1111 
		 * Base Rate: 29.00 
		 * Flat Rate Premium: 10.00
		 */
		ph.addCustomer();
		assertTrue("2: testing addCustomer", ph.getCustomers().size() == 1);
		assertTrue("3: testing addCustomer", (ph.getCustomers().get(0)
				.getName().equals("Bill Jones")));
		assertTrue("4: testing addCustomer", (ph.getCustomers().get(0)
				.getCPhone().getPhoneNumber().equals("613-111-1111")));
		assertTrue("5: testing addCustomer", (ph.getCustomers().get(0)
				.getCPhone().getBaseRate() == 29.00));
		assertTrue("6: testing addCustomer", (ph.getCustomers().get(0)
				.getCPhone().getTaxAmt() == 5.07));
		assertTrue("7: testing addCustomer", (ph.getCustomers().get(0)
				.getCPhone().calcuBefTaxBill() + ph.getCustomers().get(0)
				.getCPhone().getTaxAmt() == 44.07));
		
		/**
		 * Testing method addCustomer(), the customer infor as followings: 
		 * Jill Wilson
		 * 819-222-2222 
		 * Base Rate: 25.00 
		 * Minutes Used: 300
		 */
		ph.addCustomer();
		assertTrue("8: testing addCustomer", ph.getCustomers().size() == 2);
		assertTrue("9: testing addCustomer", (ph.getCustomers().get(1)
				.getName().equals("Jill Wilson")));
		assertTrue("10: testing addCustomer", (ph.getCustomers().get(1)
				.getCPhone().getPhoneNumber().equals("819-222-2222")));
		assertTrue("11: testing addCustomer", (ph.getCustomers().get(1)
				.getCPhone().getBaseRate() == 25.00));
		assertTrue("12: testing addCustomer", (ph.getCustomers().get(1)
				.getCPhone().getTaxAmt() == 13.00));
		assertTrue("13: testing addCustomer", (ph.getCustomers().get(1)
				.getCPhone().calcuBefTaxBill() + ph.getCustomers().get(1)
				.getCPhone().getTaxAmt() == 113.00));
		
		stdOut.println("Done!");
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值