- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是包含方法 calculateTotalTipOnTable() 的类 calculateTotalTipOnTable 方法使用来自 Customer 类的方法,当我测试时该方法没有问题TableDequeue)
package com.kristopher.restaurantmanager;
import sofia.graphics.RectangleShape;
public class TableDeque
extends RectangleShape
implements Deque<Customer>
{
// ~ Instance/static variables ............................................
private Node<Customer> head;
private Node<Customer> tail;
private int size;
private double totalDue = 0;
private double totalTip = 0;
private boolean authorized;
private TableDeque duplicate;
/**
* Constructor of the TableDeque class.
*/
public TableDeque()
{
head = new Node<Customer>(null);
tail = new Node<Customer>(null);
head.join(tail);
size = 0;
this.setImage("table");
}
/**
* Insert a new Customer at the rear (the tail) of the Tabledequeue.
*
* @param value
* the Customer to insert.
*/
public void enqueueCustomerAtRear(Customer value)
{
Node<Customer> oldRear = tail.previous();
tail.previous().split();
oldRear.join((new Node<Customer>(value)).join(tail));
size++;
}
// ----------------------------------------------------------
/**
* Remove the Customer at the front (the head) of the deque.
*
* @return The Customer that was removed
*/
public Customer dequeueCustomerAtFront()
{
Node<Customer> oldFrontNext = head.next().next();
Node<Customer> oldFront = head.next();
head.next().split();
head.split();
head.join(oldFrontNext);
size--;
return oldFront.data();
}
// ----------------------------------------------------------
/**
* Insert a new Customer at the front (the head) of the deque.
*
* @param value
* the Customer to insert.
*/
public void enqueueCustomerAtFront(Customer value)
{
Node<Customer> oldFront = head.next();
head.split();
head.join((new Node<Customer>(value)).join(oldFront));
size++;
}
// ----------------------------------------------------------
/**
* Remove the Customer at the rear (the tail) of the deque.
*
* @return The Customer that was removed
*/
public Customer dequeueCustomerAtRear()
{
Node<Customer> oldRearPrevious = tail.previous().previous();
Node<Customer> oldRear = tail.previous();
oldRearPrevious.split();
oldRear.split();
oldRearPrevious.join(tail);
size--;
return oldRear.data();
}
// ----------------------------------------------------------
/**
* Get the Customer at the front (the head) of the deque. Does not alter the
* deque.
*
* @return the Customer at the front of the deque.
*/
public Customer frontCustomer()
{
return head.next().data();
}
// ----------------------------------------------------------
/**
* Get the Customer at the rear (the tail) of the deque. Does not alter the
* deque.
*
* @return the Customer at the rear of the deque.
*/
public Customer rearCustomer()
{
return tail.previous().data();
}
// ----------------------------------------------------------
/**
* Get the number of items in this deque. Does not alter the deque.
*
* @return The number of items this deque contains.
*/
public int size()
{
return size;
}
// ----------------------------------------------------------
/**
* Empty the deque.
*/
public void clear()
{
while (frontCustomer() != null)
{
dequeueCustomerAtRear();
}
}
// ----------------------------------------------------------
/**
* Returns a string representation of this deque. A deque's string
* representation is written as a comma-separated list of its contents (in
* front-to-rear order) surrounded by square brackets, like this:
*
* <pre>
* [koo, david, karl, tom]
* </pre>
* <p>
* An empty deque is simply <code>[]</code>.
* </p>
*
* @return Customer lists on a signle table.
*/
public String customerList()
{
String s = "[";
int count = 0;
Node<Customer> c = head.next();
while (c != tail)
{
if (count < size() - 1)
{
s = s + c.data().getName() + ", ";
c = c.next();
}
else
{
s = s + c.data().getName() + "]";
c = c.next();
}
count++;
}
return s;
}
// -----------------------------------------------------
// Table money process methods
// -----------------------------------------------------
// ----------------------------------------------------------
/**
* tableDue calculate the total due amount of the table. This method use
* duplicated information of the table not to hurt any stored value beside
* the double value totalDue
*
* @return total due on the table.
*/
public double tableDue()
{
duplicate = this.duplicate();
while (duplicate.size() != 0)
{
totalDue += duplicate.dequeueCustomerAtFront().getDueAmount();
}
duplicate = null;
return totalDue;
}
// ----------------------------------------------------------
// somehow this method change the table itself(not duplicated table.)
// has to be fixed
/**
* calculateTotalTipOnTable method calculate the total tip amount that
* customers gives you. This method must be called only when all of the
* customer has done payingProcess
*
* @return total tip amount on table
*/
public double calculateTotalTipOnTable()
{
duplicate = this.duplicate();
while (duplicate.size() != 0)
{
totalTip += duplicate.frontCustomer().totalTipAmount();
duplicate.dequeueCustomerAtFront();
}
duplicate = null;
return totalTip;
}
// ----------------------------------------------------------
/**
* This method will determine if it is okey to remove table based on the
* bollean value stored in this class, and customer class. This method count
* the number of customers that is already authorized(means they paid). If
* counts equal to the size of the deque, this table is authorized and ready
* to dismiss.
*
* @return name of the customer who didn't fully pay yet.
*/
public String payingProcess()
{
duplicate = this.duplicate();
int count = 0;
String str = "";
if (duplicate.size() != 0)
{
while (duplicate.size() != 0)
{
if (duplicate.frontCustomer().isPaid())
{
count++;
this.dequeueCustomerAtFront();
duplicate.dequeueCustomerAtFront();
}
else
{
str += duplicate.frontCustomer().getName();
duplicate.dequeueCustomerAtFront();
}
}
authorized = (count == this.size());
duplicate = null;
return str;
}
else
{
duplicate = null;
authorized = true;
return "table is empty";
}
}
// ----------------------------------------------------------
/**
* return boolean if this table is ready to be dismissed
*
* @return boolean authorized
*/
public boolean isPaid()
{
return authorized;
}
// ----------------------------------------------------------
/**
* this is the helper method to produce duplicate Table.
* @return duplicated table.
*/
public TableDeque duplicate()
{
TableDeque duplicate1 = new TableDeque();
duplicate1 = this;
return duplicate1;
}
**payingProcess method of Customer Class.**
public double payingProcess(double d, double t)
{
actualTip = t;
totalTip += t;
double x = d - this.getDueAmount();
if (d >= 0.0)
{
authorized = (x >= 0.0);
}
else
{
throw new IllegalStateException("negative value of payment");
}
if (authorized)
{
totalPayDue = 0;
paidAmount = paidAmount + d;
return x;
}
else
{
totalPayDue -= d;
paidAmount = paidAmount + d;
return Math.abs(x);
}
}
测试用例文件错误部分 >>//////assertEquals(table.calculateTotalTipOnTable(), 15.0);//////////////assertEquals(customer1.totalTipAmount(), 15.0);////
package com.kristopher.restaurantmanager;
import junit.framework.TestCase;
public class TableDequeTests
extends TestCase
{
private TableDeque table;
private Customer customer1;
private Customer customer2;
private Customer customer3;
private Customer customer4;
private Food food1;
private Food food2;
private Food food3;
public void setUp()
{
table = new TableDeque();
customer1 = new Customer("Kristopher");// customer who don't order
customer2 = new Customer("Mike");// customer who order one food
customer3 = new Customer("Yilong");// customer who order two foods
customer4 = new Customer("Barnette");// customer who order three foods
food1 = new Food("Kimchi", 10.00);
food2 = new Food("Bulgogi", 20.00);
food3 = new Food("GamzaTang", 30.00);
// -------------------------------------------------------------------
customer2.setOrderedFood(food1);// Mike
customer3.setOrderedFood(food1);// Yilong
customer3.setOrderedFood(food2);
customer4.setOrderedFood(food1);// Barnette
customer4.setOrderedFood(food2);
customer4.setOrderedFood(food3);
}
// ----------------------------------------------------------
/**
* Test if tableDue method correctly caclulate the total due of the table.
*/
public void testTableDue()
{
table.enqueueCustomerAtFront(customer3);
assertEquals(table.tableDue(), 30.0, 0.0);
}
// ----------------------------------------------------------
/**
* This method must calculate the total tip on the table.
*/
public void testCalculateTotalTipOnTable()
{
table.enqueueCustomerAtFront(customer1);
table.enqueueCustomerAtFront(customer2);
customer1.payingProcess(0.0, 15.0);
customer2.payingProcess(0.0, 35.0);
assertEquals(50.0, table.calculateTotalTipOnTable());
}
// ----------------------------------------------------------
/**
* Testing if enqueueCustomerAtRear correctly enque at rear of the table
* deque.
*/
public void testTableForSingleCustomer()
{
table.enqueueCustomerAtFront(customer1);
assertEquals(table.size(), 1);
assertEquals(table.frontCustomer().getName(), "Kristopher");
Exception thrown = null;
try
{
table.rearCustomer().getOrderFood();
}
catch (Exception e)
{
thrown = e;
}
assertTrue(thrown instanceof IllegalStateException);
assertEquals(
thrown.getMessage(),
"This customer did not order any foods yet");
assertEquals(table.customerList(), "[Kristopher]");
assertEquals(table.tableDue(), 0.0, 0.0);
customer1.payingProcess(0.0, 15.0);
//////assertEquals(table.calculateTotalTipOnTable(), 15.0);////////////
//// assertEquals(customer1.totalTipAmount(), 15.0);////
assertEquals(table.payingProcess(), "table is empty");
assertTrue(table.isPaid());
}
}
最佳答案
您没有在此处创建副本:
public TableDeque duplicate()
{
TableDeque duplicate1 = new TableDeque();
duplicate1 = this;
return duplicate1;
}
您正在返回对原始实例的引用。
您可以使用复制构造函数代替duplicate
方法:
public TableDeque (TableDeque copy)
{
// copy properties from the source TableDeque to the new instance
}
然后,而不是调用
duplicate = this.duplicate();
你会打电话
duplicate = new TableDeque(this);
关于java - 即使我复制了自己,我的一种方法也会以某种方式改变它的领域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31084820/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!