public class Shopper extends Goods {
private ArrayList<Goods> list = new ArrayList<Goods>();
public Shopper(String name, int price) {
super(name, price); // レジ袋の店名、レジ袋代
}
public int getPrice() {
int sum = this.price; // レジ袋代
for(int i = 0 ; i < list.size() ; i++) {
sum += ((Goods)list.get(i)).getPrice();
}
return sum;
}
public void add(Goodsgoods) {
list.add(goods);
}
}
public class Shopper : Goods
{
private ArrayListlist = new ArrayList();
public Shopper(string name, int price) : base(name, price)
{
}
public override int getPrice()
{
int sum = price; // レジ袋代
for (int i = 0 ; i < list.Count ; i++)
sum += ((Goods)list[i]).getPrice();
return sum;
}
public void add(Goodsgoods)
{
list.Add(goods);
}
}
Enshu301.vb
Module Enshu301
Sub Main()
Dim s1 As Shopper = New Shopper("八百○", 5) ' レジ袋5円
s1.add(New Vegetable("大根", 125, 1))
s1.add(New Vegetable("レタス", 147, 2))
Dim s2 As Shopper = New Shopper("△肉店", 3) ' レジ袋3円
s2.add(New Meat("豚肉", 98, 250))
s2.add(New Meat("鶏肉", 78, 200))
s1.add(s2)
s1.add(New Vegetable("トマト", 69, 4))
Console.WriteLine("全商品の合計金額 " & Currency.displayComma(s1.getPrice()))
Console.WriteLine("△肉店の合計金額 " & Currency.displayComma(s2.getPrice()))
End Sub
End Module
本当に見ますか?
Shopper.vb
Public Class Shopper : Inherits Goods
Private list As ArrayList = New ArrayList()
Public Sub New(name As String, price As Integer)
MyBase.New(name, price)
End Sub
Public Overrides Function getPrice() As Integer
Dim sum As Integer = price ' レジ袋代
For i As Integer = 0 To list.Count - 1
sum += DirectCast(list.Item(i), Goods).getPrice()
Next
Return sum
End Function
Public Sub add(food As Food)
list.Add(food)
End Sub
End Class
require './Goods'
class Shopper < Goods
def initialize(name, price)
@name = name # レジ袋の店名
@price = price # レジ袋代
@list = []
end
def getPrice()
sum = @price # レジ袋代
for goods in @listsum += goods.getPrice()
end
return sum
end
def add(item)
@list.push(item)
end
end
from Goods import Goods
class Shopper(Goods):
def __init__(self, name, price):
super().__init__(name, price)
self.list = []
def getPrice(self):
sum = self.price # レジ袋代
for goods in self.list:
sum += goods.getPrice()
return sum
def add(self, goods):
self.list.append(goods)
class Shopper(name: String, price: Int) : Goods(name, price) {
private val list = ArrayList<Goods>()
override fun getPrice(): Int {
var sum: Int = price // レジ袋代
for (goods: Goods in list) {
sum += goods.getPrice()
}
return sum
}
fun add(goods: Goods) {
list.add(goods)
}
}
import scala.collection.mutable.ListBuffer
class Shopper(name: String, price: Int) extends Goods(name, price) {
private val list = new ListBuffer[Goods]()
override def getPrice(): Int = {
var sum: Int = price // レジ袋代
for (goods: Goods <- list) {
sum += goods.getPrice()
}
sum
}
def add(goods: Goods) {
list.addOne(goods)
}
}
class Shopper extends Goods {
private ArrayList<Goods> list = new ArrayList<Goods>()
Shopper(String name, int price) {
super(name, price) // レジ袋の店名、レジ袋代
}
int getPrice() {
int sum = this.price // レジ袋代
for(Goodsgoods in list) {
sum += goods.price
}
return sum
}
void add(Goodsgoods) {
list.add(goods)
}
}
import goods;
public class Shopper : Goods {
private Goods[] list;
public this(in string name, in int price) {
super(name, price); // レジ袋の店名、レジ袋代
}
public override int getPrice() {
int sum = this.price; // レジ袋代
foreach (int i; 0..list.length) {
sum += (cast(Goods)list[i]).getPrice();
}
return sum;
}
public void add(Goodsgoods) {
list ~= goods;
}
}
unit UnitShopper;
interface
uses
System.Generics.Collections,
UnitGoods;
type
Shopper = class(Goods)
private
list:TList<Goods>;
public
constructor Create(name:string; price:integer);
destructor Destroy(); override;
function getPrice():integer; override;
procedure add(_goods:Goods);
end;
implementation
constructor Shopper.Create(name:string; price:integer);
begin
inherited Create(name, price); // レジ袋の店名、レジ袋代
list := TList<Goods>.Create();
end;
destructor Shopper.Destroy();
var g:Goods;
begin
for g in list do begin
g.Free;
end;
list.Free;
end;
function Shopper.getPrice():integer;
var sum:integer;
var g:Goods;
begin
sum := price; // レジ袋代
for g in list do begin
sum := sum + g.getPrice();
end;
Result := sum;
end;
procedure Shopper.add(_goods:Goods);
begin
list.add(_goods);
end;
end.