Finish the Frequent Flyers exercise using the second change: replace type code with a state field.
main: FrequentFlyers class
And other classes (Customer, …)
With a few customer object instances to test your code
No need to have ‘charity’ stuff in the Platinum class
第二個作業
第二個作業
- 附加檔案
-
- FrequentFlyers.rar
- (43.08 KiB) 已下載 27 次
第二個作業
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
abstract class Customers
{
// type code
public Membership Type { get; set; }
public int custID { get; set; }
public int points { get; set; } //累積點數
public String name { get; set; }
public void SetType(Membership m)
{
Type = m;
}
// 依 purchaseAmount 增加點數
public int addPoints(int purchaseAmount)
{
int pts = 0;
pts = purchaseAmount / this.getPointFactor();
points = points + pts;
return pts;
}
public int redeemPoints(int zone)
{
int pts = getZoneThreshold(zone);
if (this.points < pts) return 0;
points = points - pts;
return pts;
}
protected abstract int getPointFactor();
protected abstract int getZoneThreshold(int zone);
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class Gold : Membership
{
public override void getPointFactor()
{
Console.WriteLine(this.GetType() + "getPointFactor()");
}
public override void getZoneThreshold()
{
Console.WriteLine(this.GetType() + "getZoneThreshold()");
}
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class GoldCustomers : Customers
{
protected override int getPointFactor()
{
return 18;
}
protected override int getZoneThreshold(int zone)
{
int pts = 0;
switch (zone)
{
case 1: pts = 1300; break;
case 2: pts = 3100; break;
case 3: pts = 10000; break;
case 4: pts = 22000; break;
default: return 0;
}
return pts;
}
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
public abstract class Membership
{
public abstract void getPointFactor();
public abstract void getZoneThreshold();
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class Platinum : Membership
{
public override void getPointFactor()
{
Console.WriteLine(this.GetType() + "getPointFactor()");
}
public override void getZoneThreshold()
{
Console.WriteLine(this.GetType() + "getZoneThreshold()");
}
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class PlatinumCustomers : Customers
{
protected override int getPointFactor()
{
return 15;
}
protected override int getZoneThreshold(int zone)
{
int pts = 0;
switch (zone)
{
case 1: pts = 1200; break;
case 2: pts = 2850; break;
case 3: pts = 9500; break;
case 4: pts = 20000; break;
default: return 0;
}
return pts;
}
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class Program
{
static void Main(string[] args)
{
//// a 金卡客戶
Customers a = new GoldCustomers();
//// 換 白金卡
a.Type = new Platinum();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// 換 金卡
a.Type = new Gold();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// 換 普通卡
a.Type = new Regular();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// a 白金客戶
a = new PlatinumCustomers();
//// 換 白金卡
a.Type = new Platinum();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// 換 金卡
a.Type = new Gold();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// 換 普通卡
a.Type = new Regular();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// a 普卡客戶
a = new RegularCustomers();
//// 換 白金卡
a.Type = new Platinum();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// 換 金卡
a.Type = new Gold();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
//// 換 普通卡
a.Type = new Regular();
a.Type.getPointFactor();
a.Type.getZoneThreshold();
Console.ReadLine();
}
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class Regular : Membership
{
public override void getPointFactor()
{
Console.WriteLine(this.GetType() + "getPointFactor()");
}
public override void getZoneThreshold()
{
Console.WriteLine(this.GetType() + "getZoneThreshold()");
}
}
}
代碼: 選擇全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrequentFlyers
{
class RegularCustomers : Customers
{
protected override int getPointFactor()
{
return 20;
}
protected override int getZoneThreshold(int zone)
{
int pts = 0;
switch (zone)
{
case 1: pts = 1500; break;
case 2: pts = 3500; break;
case 3: pts = 10500; break;
case 4: pts = 25000; break;
default: return 0;
}
return pts;
}
}
}
誰在線上
正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客