using System;
namespace ParkingManagement
{
class Program
{
static void Main(string[] args) //진입점
{
Car car1 = new Car();
car1.plateNumber = "123가 4567"; //번호판 지정
car1.InTime = DateTime.Now;
Console.WriteLine(car1.InTime);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ParkingManagement
{
class Car //설계도
{
public string plateNumber; //private라는 접근제한자를 가지고 있음 -> 공개해야함 / 멤버변수 plateNumber 만듦
public DateTime InTime; //몇시에 들어와서 언제 나갔나 멤버변수에 포함
public DateTime OutTime; //
}
}
using System;
namespace ParkingManagement
{
class Program
{
static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car();
car1.plateNumber = "123가 4567"; //번호판 지정
car1.InTime = DateTime.Now;
car1.OutTime = car1.InTime.AddMinutes(rnd.Next(200)); //다음 난수를 만들어줘
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
car1.plateNumber, car1.InTime, car1.OutTime);
}
}
}
주차요금 계산
Car car1 = new Car(); 오류가 생김
기능추가해서 완성하기
설계도에 요금 추가하기
calculate charge붙이기
void를 붙이기
static 넣어서 정적멤버로 바꿈
---------------------
아직 작성중인 코드
using System;
namespace ParkingManagement
{
class Park
{
static int baseCharge1 = 1000; // 소형 기본(30분) static 넣어서 정적멤버로 바꿈
static int baseCharge2 = 1500; // 대형 기본(30분)
static int Rate1 = 500; // 소형 요율(30분)
static int Rate2 = 700; // 대형 요율(30분)
static void CalculateCharge(Car car) //요금계산하는 메서드 -> 메서드 반환을 생각해야함 void유요하지 않다, 빈손으로 돌아간다
{
Random rnd = new Random();
car.OutTime = car.InTime.AddMinutes(rnd.Next(200));
TimeSpan duration = car.OutTime - car.InTime;
car.Duration=(int)duration.TotalMinutes;
if (car.Duration <= 30)
{
if(car.IsSUV)
{
car.PartCharge = baseCharge2;
}
else
{
}
}
else
{
}
}
//다음 난수를 만들어줘 //결과값 정수 정수값으로 반환으로 정의해도 됨, 자동차 설계도에 사용하지 않았던 멤버변수
static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car("123가 4567", false);
CalculateCharge(car1);
}
/* static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car("123가 4567", false);
car1.OutTime = car1.InTime.AddMinutes(rnd.Next(200)); //다음 난수를 만들어줘
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
car1.PlateNumber, car1.InTime, car1.OutTime); }*/
}
}
----------------------------------
ParkingManagement
using System;
namespace ParkingManagement
{
class Park
{
static int baseCharge1 = 1000; // 소형 기본(30분) static 넣어서 정적멤버로 바꿈
static int baseCharge2 = 1500; // 대형 기본(30분)
static int Rate1 = 500; // 소형 요율(30분)
static int Rate2 = 700; // 대형 요율(30분)
static void CalculateCharge(Car car) //요금계산하는 메서드 -> 메서드 반환을 생각해야함 void유요하지 않다, 빈손으로 돌아간다
{
Random rnd = new Random();
car.OutTime = car.InTime.AddMinutes(rnd.Next(200));
TimeSpan duration = car.OutTime - car.InTime;
car.Duration=(int)duration.TotalMinutes;
if (car.Duration <= 30)
{
if(car.IsSUV == true)
{
car.PartCharge = baseCharge2;
}
else
{
car.PartCharge = baseCharge1;
}
}
else
{
if (car.IsSUV == true)
{
car.PartCharge = baseCharge2 +
(int)Math.Ceiling((car.Duration-30)/30.0)*Rate2; //정수 나누기 정수가 안되서 30.0으로 바꿈
}
else
{
car.PartCharge = baseCharge1 +
(int)Math.Ceiling((car.Duration - 30) / 30.0) * Rate1;
}
}
}
//다음 난수를 만들어줘 //결과값 정수 정수값으로 반환으로 정의해도 됨, 자동차 설계도에 사용하지 않았던 멤버변수
static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car("123가 4567", false);
CalculateCharge(car1);
}
/* static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car("123가 4567", false);
car1.OutTime = car1.InTime.AddMinutes(rnd.Next(200)); //다음 난수를 만들어줘
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
car1.PlateNumber, car1.InTime, car1.OutTime); }*/
}
}
Car
using System;
using System.Collections.Generic;
using System.Text;
namespace ParkingManagement
{
class Car //설계도
{
public string PlateNumber; //private라는 접근제한자를 가지고 있음 -> 공개해야함 / 멤버변수 plateNumber 만듦
public bool IsSUV; //대소형차
public DateTime InTime; //몇시에 들어와서 언제 나갔나 멤버변수에 포함
public DateTime OutTime;
public int Duration; // 주차시간(분) - 정수형 변수 / 멤버변수 추가 interger형으로 멤버변수 추가
public int PartCharge; // 주차요금(원)
public Car(string plate, bool isSUV) //bool true or false //생성자
{
PlateNumber = plate; //위에서 정의하고 있는 public 멤버변수값을 결정할수 있음 / PlateNumber 매개변수값으로 겨정
IsSUV = isSUV;
InTime = DateTime.Now;
}
}
}
최종
Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ParkingManagement
{
class Car //설계도
{
public string PlateNumber; //private라는 접근제한자를 가지고 있음 -> 공개해야함 / 멤버변수 plateNumber 만듦
public bool IsSUV; //대소형차
public DateTime InTime; //몇시에 들어와서 언제 나갔나 멤버변수에 포함
public DateTime OutTime;
public int Duration; // 주차시간(분) - 정수형 변수 / 멤버변수 추가 interger형으로 멤버변수 추가
public int PartCharge; // 주차요금(원)
public Car(string plate, bool isSUV) //bool true or false //생성자
{
PlateNumber = plate; //위에서 정의하고 있는 public 멤버변수값을 결정할수 있음 / PlateNumber 매개변수값으로 겨정
IsSUV = isSUV;
InTime = DateTime.Now;
}
}
}
Program.cs
using System;
namespace ParkingManagement
{
class Park
{
static int baseCharge1 = 1000; // 소형 기본(30분) static 넣어서 정적멤버로 바꿈
static int baseCharge2 = 1500; // 대형 기본(30분)
static int Rate1 = 500; // 소형 요율(30분)
static int Rate2 = 700; // 대형 요율(30분)
static void CalculateCharge(Car car) //요금계산하는 메서드 -> 메서드 반환을 생각해야함 void유요하지 않다, 빈손으로 돌아간다
{
Random rnd = new Random();
car.OutTime = car.InTime.AddMinutes(rnd.Next(200));
TimeSpan duration = car.OutTime - car.InTime;
car.Duration=(int)duration.TotalMinutes;
if (car.Duration <= 30)
{
if(car.IsSUV == true)
{
car.PartCharge = baseCharge2;
}
else
{
car.PartCharge = baseCharge1;
}
}
else
{
if (car.IsSUV == true)
{
car.PartCharge = baseCharge2 +
(int)Math.Ceiling((car.Duration-30)/30.0)*Rate2; //정수 나누기 정수가 안되서 30.0으로 바꿈
}
else
{
car.PartCharge = baseCharge1 +
(int)Math.Ceiling((car.Duration - 30) / 30.0) * Rate1;
}
}
}
//다음 난수를 만들어줘 //결과값 정수 정수값으로 반환으로 정의해도 됨, 자동차 설계도에 사용하지 않았던 멤버변수
static void Main(string[] args) //진입점
{
Car car1 = new Car("123가 4567", false);
CalculateCharge(car1);
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
car1.PlateNumber, car1.InTime, car1.OutTime);
Console.WriteLine("주차시간: {0}, 요금: {1}",
car1.Duration, car1.PartCharge);
}
/* static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car("123가 4567", false);
car1.OutTime = car1.InTime.AddMinutes(rnd.Next(200)); //다음 난수를 만들어줘
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
car1.PlateNumber, car1.InTime, car1.OutTime); }*/
}
}
Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ParkingManagement
{
class Car //설계도
{
public string PlateNumber; //private라는 접근제한자를 가지고 있음 -> 공개해야함 / 멤버변수 plateNumber 만듦
public bool IsSUV; //대소형차
public DateTime InTime; //몇시에 들어와서 언제 나갔나 멤버변수에 포함
public DateTime OutTime;
public int Duration; // 주차시간(분) - 정수형 변수 / 멤버변수 추가 interger형으로 멤버변수 추가
public int PartCharge; // 주차요금(원)
public Car(string plate, bool isSUV) //bool true or false //생성자
{
PlateNumber = plate; //위에서 정의하고 있는 public 멤버변수값을 결정할수 있음 / PlateNumber 매개변수값으로 겨정
IsSUV = isSUV;
InTime = DateTime.Now;
}
}
}
Program.cs
using System;
namespace ParkingManagement
{
class Park
{
static int baseCharge1 = 1000; // 소형 기본(30분) static 넣어서 정적멤버로 바꿈
static int baseCharge2 = 1500; // 대형 기본(30분)
static int Rate1 = 500; // 소형 요율(30분)
static int Rate2 = 700; // 대형 요율(30분)
static void CalculateCharge(Car car) //요금계산하는 메서드 -> 메서드 반환을 생각해야함 void유요하지 않다, 빈손으로 돌아간다
{
Random rnd = new Random();
car.OutTime = car.InTime.AddMinutes(rnd.Next(200));
TimeSpan duration = car.OutTime - car.InTime;
car.Duration=(int)duration.TotalMinutes;
if (car.Duration <= 30)
{
if(car.IsSUV == true)
{
car.PartCharge = baseCharge2;
}
else
{
car.PartCharge = baseCharge1;
}
}
else
{
if (car.IsSUV == true)
{
car.PartCharge = baseCharge2 +
(int)Math.Ceiling((car.Duration-30)/30.0)*Rate2; //정수 나누기 정수가 안되서 30.0으로 바꿈
}
else
{
car.PartCharge = baseCharge1 +
(int)Math.Ceiling((car.Duration - 30) / 30.0) * Rate1;
}
}
}
//다음 난수를 만들어줘 //결과값 정수 정수값으로 반환으로 정의해도 됨, 자동차 설계도에 사용하지 않았던 멤버변수
static void Main(string[] args) //진입점
{
Car[] cars = new Car[]
{
new Car("123가 4567", false), new Car("45678", true), new Car("2356", false) };
foreach (var item in cars) //2차원배열에서 1차원배열 잘라서 넣는다
{
CalculateCharge(item);
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
item.PlateNumber, item.InTime, item.OutTime);
Console.WriteLine("주차시간: {0}, 요금: {1}",
item.Duration, item.PartCharge);
}
}
/* static void Main(string[] args) //진입점
{
Random rnd = new Random();
Car car1 = new Car("123가 4567", false);
car1.OutTime = car1.InTime.AddMinutes(rnd.Next(200)); //다음 난수를 만들어줘
Console.WriteLine("차량번호: {0}, 입차: {1}, 출자: {2}",
car1.PlateNumber, car1.InTime, car1.OutTime); }*/
}
}
결과
'C#' 카테고리의 다른 글
C# Dictionary 예제 (0) | 2021.07.23 |
---|---|
test2 list클래스 예제 (0) | 2021.07.23 |
문자열 분리 (0) | 2021.07.22 |
continue (0) | 2021.07.22 |
foreach (0) | 2021.07.22 |